Add HMR port script when needed (#375)

* Always add HMR port script when HMR is enabled

* Add it only if there are client side components

* Fix the test

* Add a test where HMR port not set

* Upgrade snowpack version

* Fix snowpack semver
This commit is contained in:
Matthew Phillips 2021-06-10 14:10:15 -04:00 committed by GitHub
parent a660e49f80
commit afa09ee9dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 10 deletions

View file

@ -87,7 +87,7 @@
"sass": "^1.32.13", "sass": "^1.32.13",
"shorthash": "^0.0.2", "shorthash": "^0.0.2",
"slash": "^4.0.0", "slash": "^4.0.0",
"snowpack": "^3.5.6", "snowpack": "^3.5.7",
"source-map-support": "^0.5.19", "source-map-support": "^0.5.19",
"string-width": "^5.0.0", "string-width": "^5.0.0",
"tiny-glob": "^0.2.8", "tiny-glob": "^0.2.8",

View file

@ -11,7 +11,7 @@ export default function (opts: TransformOptions): Transformer {
visitors: { visitors: {
html: { html: {
InlineComponent: { InlineComponent: {
enter(node, parent) { enter(node) {
const [name, kind] = node.name.split(':'); const [name, kind] = node.name.split(':');
if (kind && !hasComponents) { if (kind && !hasComponents) {
hasComponents = true; hasComponents = true;
@ -20,7 +20,6 @@ export default function (opts: TransformOptions): Transformer {
}, },
Element: { Element: {
enter(node) { enter(node) {
if (!hasComponents) return;
switch (node.name) { switch (node.name) {
case 'head': { case 'head': {
head = node; head = node;
@ -56,7 +55,7 @@ export default function (opts: TransformOptions): Transformer {
}); });
} }
if (isHmrEnabled) { if (isHmrEnabled && hasComponents) {
const { hmrPort } = opts.compileOptions; const { hmrPort } = opts.compileOptions;
children.push( children.push(
{ {

View file

@ -5,7 +5,11 @@ import { setup } from './helpers.js';
const Basics = suite('Basic test'); const Basics = suite('Basic test');
setup(Basics, './fixtures/astro-basic'); setup(Basics, './fixtures/astro-basic', {
runtimeOptions: {
mode: 'development'
}
});
Basics('Can load page', async ({ runtime }) => { Basics('Can load page', async ({ runtime }) => {
const result = await runtime.load('/'); const result = await runtime.load('/');
@ -16,4 +20,16 @@ Basics('Can load page', async ({ runtime }) => {
assert.equal($('h1').text(), 'Hello world!'); assert.equal($('h1').text(), 'Hello world!');
}); });
Basics('Sets the HMR port when dynamic components used', async ({ runtime }) => {
const result = await runtime.load('/client');
const html = result.contents;
assert.ok(/HMR_WEBSOCKET_URL/.test(html), 'Sets the websocket port');
});
Basics('Does not set the HMR port when no dynamic component used', async ({ runtime }) => {
const result = await runtime.load('/');
const html = result.contents;
assert.ok(!/HMR_WEBSOCKET_URL/.test(html), 'Does not set the websocket port');
});
Basics.run(); Basics.run();

View file

@ -0,0 +1,5 @@
import { h } from 'preact';
export default function() {
return <div>Testing</div>
}

View file

@ -0,0 +1,12 @@
---
import Tour from '../components/Tour.jsx';
---
<html>
<head>
<title>Stuff</title>
</head>
<body>
<Tour:load />
</body>
</html>

View file

@ -11,7 +11,7 @@ const MAX_TEST_TIME = 10000; // max time an individual test may take
const MAX_SHUTDOWN_TIME = 3000; // max time shutdown() may take const MAX_SHUTDOWN_TIME = 3000; // max time shutdown() may take
/** setup fixtures for tests */ /** setup fixtures for tests */
export function setup(Suite, fixturePath) { export function setup(Suite, fixturePath, { runtimeOptions = {} } = {}) {
let runtime; let runtime;
const timers = {}; const timers = {};
@ -24,6 +24,7 @@ export function setup(Suite, fixturePath) {
runtime = await createRuntime(astroConfig, { runtime = await createRuntime(astroConfig, {
logging: { level: 'error', dest: process.stderr }, logging: { level: 'error', dest: process.stderr },
...runtimeOptions
}); });
context.runtime = runtime; context.runtime = runtime;

View file

@ -8938,10 +8938,10 @@ smartwrap@^1.2.3:
wcwidth "^1.0.1" wcwidth "^1.0.1"
yargs "^15.1.0" yargs "^15.1.0"
snowpack@^3.5.6: snowpack@^3.5.7:
version "3.5.6" version "3.5.7"
resolved "https://registry.yarnpkg.com/snowpack/-/snowpack-3.5.6.tgz#79f8699e4248e19f67ec50433e50072bb2e69e89" resolved "https://registry.yarnpkg.com/snowpack/-/snowpack-3.5.7.tgz#1d56a273c97e6f8fe23800f4d3c641eefb65f356"
integrity sha512-R9uqattPhS2j1zaOFgwl7i54rFTtxxk4kmKmqJBRwD+9YggUulGm7O7YRXaMMjtmi69czK7jxbtZs1Up/xA+1A== integrity sha512-mMdbG9vSs7JL69/Zk2VtpduBnbfzCCCXvhJeX4GzPg4aYyKtfs0s6MA5VoQbN9HL6lZpKRy/knaigKcC4E29GA==
dependencies: dependencies:
cli-spinners "^2.5.0" cli-spinners "^2.5.0"
default-browser-id "^2.0.0" default-browser-id "^2.0.0"