Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inject script for hmr when there is only normal script in html #8330

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/core/integration-tests/test/html.js
Expand Up @@ -211,6 +211,31 @@ describe('html', function () {
assert(/<script src=".+?\.js"><\/script>$/.test(html));
});

it('should insert empty script tag for HMR at the end of the body when having normal inline script', async function () {
const b = await bundle(
path.join(__dirname, '/integration/html-inline-js/index.html'),
{
hmrOptions: {},
},
);

assertBundles(b, [
{type: 'js', assets: ['index.html']},
{type: 'js', assets: ['index.html']},
{type: 'js', assets: ['index.html']},
{type: 'js', assets: ['index.html']},
{type: 'js', assets: ['index.html']},
{name: 'index.html', assets: ['index.html']},
]);

const html = await outputFS.readFile(
path.join(distDir, 'index.html'),
'utf8',
);

assert(/<script src=".+?\.js"><\/script><\/body>/.test(html));
});

it('should support canonical links', async function () {
let b = await bundle(
path.join(__dirname, '/integration/html-canonical/index.html'),
Expand Down
8 changes: 4 additions & 4 deletions packages/transformers/html/src/HTMLTransformer.js
Expand Up @@ -37,9 +37,9 @@ export default (new Transformer({

asset.bundleBehavior = 'isolated';
let ast = nullthrows(await asset.getAST());
let hasScripts;
let hasModuleScripts;
try {
hasScripts = collectDependencies(asset, ast);
hasModuleScripts = collectDependencies(asset, ast);
} catch (errors) {
if (Array.isArray(errors)) {
throw new ThrowableDiagnostic({
Expand All @@ -59,14 +59,14 @@ export default (new Transformer({
throw errors;
}

const {assets: inlineAssets, hasScripts: hasInlineScripts} =
const {assets: inlineAssets, hasModuleScripts: hasInlineModuleScripts} =
extractInlineAssets(asset, ast);

const result = [asset, ...inlineAssets];

// empty <script></script> is added to make sure HMR is working even if user
// didn't add any.
if (options.hmrOptions && !(hasScripts || hasInlineScripts)) {
if (options.hmrOptions && !(hasModuleScripts || hasInlineModuleScripts)) {
const script = {
tag: 'script',
attrs: {
Expand Down
6 changes: 3 additions & 3 deletions packages/transformers/html/src/dependencies.js
Expand Up @@ -118,7 +118,7 @@ export default function collectDependencies(
ast: AST,
): boolean {
let isDirty = false;
let hasScripts = false;
let hasModuleScripts = false;
let seen = new Set();
const errors = [];
PostHTML().walk.call(ast.program, node => {
Expand Down Expand Up @@ -245,7 +245,7 @@ export default function collectDependencies(
});

asset.setAST(ast);
hasScripts = true;
if (sourceType === 'module') hasModuleScripts = true;
return copy ? [node, copy] : node;
}

Expand Down Expand Up @@ -293,5 +293,5 @@ export default function collectDependencies(
throw errors;
}

return hasScripts;
return hasModuleScripts;
}
10 changes: 5 additions & 5 deletions packages/transformers/html/src/inline.js
Expand Up @@ -16,7 +16,7 @@ const SCRIPT_TYPES = {
};

interface ExtractInlineAssetsResult {
hasScripts: boolean;
hasModuleScripts: boolean;
assets: Array<TransformerResult>;
}

Expand All @@ -29,7 +29,7 @@ export default function extractInlineAssets(

// Extract inline <script> and <style> tags for processing.
let parts: Array<TransformerResult> = [];
let hasScripts = false;
let hasModuleScripts = false;
PostHTML().walk.call(program, (node: PostHTMLNode) => {
let parcelKey = hashString(`${asset.id}:${key++}`);
if (node.tag === 'script' || node.tag === 'style') {
Expand Down Expand Up @@ -140,8 +140,8 @@ export default function extractInlineAssets(
},
});

if (type === 'js') {
hasScripts = true;
if (env && env.sourceType === 'module') {
hasModuleScripts = true;
}
}
}
Expand Down Expand Up @@ -174,6 +174,6 @@ export default function extractInlineAssets(

return {
assets: parts,
hasScripts,
hasModuleScripts,
};
}