Skip to content

Commit b3633e0

Browse files
authored
Fixed issue where external module script tag content not inlined when it wasn't recognized as being an es6 module due to lack of import/export statements. (#3326)
1 parent 545bed1 commit b3633e0

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

packages/bundler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99
* Fixed issue where excluded URLs were not resolved by the analyzer when bundler was initialized, resulting in some imports not being treated as excluded.
10+
* Fixed issue where `<script type="module" src="x.js">` tags would not inline if the referenced script contained no `import` or `export` statements.
1011
* Removed non-essential files from published package, such as tests.
1112
<!-- Add new, unreleased changes here. -->
1213

packages/bundler/src/deps-index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* http://polymer.github.io/PATENTS.txt
1313
*/
1414
import {Analyzer, Document, Import, ResolvedUrl} from 'polymer-analyzer';
15-
import {JavaScriptDocument} from 'polymer-analyzer/lib/javascript/javascript-document';
15+
import {ScriptTagImport} from 'polymer-analyzer/lib/html/html-script-tag';
1616

1717
import {getAnalysisDocument} from './analyzer-utils';
1818

@@ -165,8 +165,7 @@ function getDependencies(
165165
[...document.getFeatures({kind: 'html-script', ...getFeaturesOptions})]
166166
.filter(
167167
(i) => i.document !== undefined &&
168-
(i.document.parsedDocument as JavaScriptDocument)
169-
.parsedAsSourceType === 'module');
168+
i instanceof ScriptTagImport && i.isModule);
170169
for (const htmlScript of htmlScripts) {
171170
const relativeUrl =
172171
analyzer.urlResolver.relative(document.url, htmlScript.document!.url);

packages/bundler/src/html-bundler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ export class HtmlBundler {
511511
if (!this.assignedBundle.bundle.files.has(resolvedImportUrl)) {
512512
return;
513513
}
514-
const scriptContent = `import ${JSON.stringify(scriptHref)};`;
514+
const scriptContent = `import ${JSON.stringify(resolvedImportUrl)};`;
515515
dom5.removeAttribute(scriptTag, 'src');
516516
dom5.setTextContent(scriptTag, encodeString(scriptContent, true));
517517
}

packages/bundler/src/test/html-bundler_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ const stripSpace = (html: string): string =>
3030
html.replace(/>\s+/g, '>').replace(/>/g, '>\n').trim();
3131

3232
suite('HtmlBundler', () => {
33+
test('external script tag inlines without imports/exports', async () => {
34+
// This is a regression test added to ensure coverage for
35+
// https://github.com/Polymer/tools/issues/3323
36+
const analyzer = inMemoryAnalyzer({
37+
'entrypoint.html': heredoc`
38+
<script src="module.js" type="module"></script>
39+
`,
40+
'module.js': heredoc`
41+
console.log('import/export-free code');
42+
`
43+
});
44+
const bundler = new Bundler({analyzer});
45+
const entrypointUrl = analyzer.resolveUrl('entrypoint.html')!;
46+
const {documents} =
47+
await bundler.bundle(await bundler.generateManifest([entrypointUrl]));
48+
const entrypointDoc = documents.getHtmlDoc(entrypointUrl)!;
49+
assert.deepEqual(entrypointDoc.content, heredoc`
50+
<script type="module">
51+
console.log('import/export-free code');
52+
</script>
53+
`);
54+
});
3355
test('external script tag inlines an es6 module', async () => {
3456
const root = 'test/html/inline-es6-modules';
3557
const analyzer = new Analyzer({

0 commit comments

Comments
 (0)