Skip to content

Commit

Permalink
inject script for hmr when there is only normal script in html (#8330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Magi-KS committed Jul 31, 2022
1 parent 4d2de22 commit a056f1e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
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,
};
}

0 comments on commit a056f1e

Please sign in to comment.