From a056f1eb60c6baf5f72488d5c367d0635774a42f Mon Sep 17 00:00:00 2001 From: KS Date: Mon, 1 Aug 2022 00:20:17 +0800 Subject: [PATCH] inject script for hmr when there is only normal script in html (#8330) --- packages/core/integration-tests/test/html.js | 25 +++++++++++++++++++ .../transformers/html/src/HTMLTransformer.js | 8 +++--- .../transformers/html/src/dependencies.js | 6 ++--- packages/transformers/html/src/inline.js | 10 ++++---- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/packages/core/integration-tests/test/html.js b/packages/core/integration-tests/test/html.js index 6db1fe45d16..f4bb022b218 100644 --- a/packages/core/integration-tests/test/html.js +++ b/packages/core/integration-tests/test/html.js @@ -211,6 +211,31 @@ describe('html', function () { assert(/ 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: { diff --git a/packages/transformers/html/src/dependencies.js b/packages/transformers/html/src/dependencies.js index 0200829f346..c9c0c23581b 100644 --- a/packages/transformers/html/src/dependencies.js +++ b/packages/transformers/html/src/dependencies.js @@ -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 => { @@ -245,7 +245,7 @@ export default function collectDependencies( }); asset.setAST(ast); - hasScripts = true; + if (sourceType === 'module') hasModuleScripts = true; return copy ? [node, copy] : node; } @@ -293,5 +293,5 @@ export default function collectDependencies( throw errors; } - return hasScripts; + return hasModuleScripts; } diff --git a/packages/transformers/html/src/inline.js b/packages/transformers/html/src/inline.js index 5b7b777e9ec..9cecc7d2f75 100644 --- a/packages/transformers/html/src/inline.js +++ b/packages/transformers/html/src/inline.js @@ -16,7 +16,7 @@ const SCRIPT_TYPES = { }; interface ExtractInlineAssetsResult { - hasScripts: boolean; + hasModuleScripts: boolean; assets: Array; } @@ -29,7 +29,7 @@ export default function extractInlineAssets( // Extract inline