Skip to content

Commit

Permalink
Fixed issue where external module script tag content not inlined when…
Browse files Browse the repository at this point in the history
… it wasn't recognized as being an es6 module due to lack of import/export statements. (#3326)
  • Loading branch information
usergenic authored Jan 10, 2019
1 parent 545bed1 commit b3633e0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/bundler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

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

Expand Down
5 changes: 2 additions & 3 deletions packages/bundler/src/deps-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* http://polymer.github.io/PATENTS.txt
*/
import {Analyzer, Document, Import, ResolvedUrl} from 'polymer-analyzer';
import {JavaScriptDocument} from 'polymer-analyzer/lib/javascript/javascript-document';
import {ScriptTagImport} from 'polymer-analyzer/lib/html/html-script-tag';

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

Expand Down Expand Up @@ -165,8 +165,7 @@ function getDependencies(
[...document.getFeatures({kind: 'html-script', ...getFeaturesOptions})]
.filter(
(i) => i.document !== undefined &&
(i.document.parsedDocument as JavaScriptDocument)
.parsedAsSourceType === 'module');
i instanceof ScriptTagImport && i.isModule);
for (const htmlScript of htmlScripts) {
const relativeUrl =
analyzer.urlResolver.relative(document.url, htmlScript.document!.url);
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler/src/html-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export class HtmlBundler {
if (!this.assignedBundle.bundle.files.has(resolvedImportUrl)) {
return;
}
const scriptContent = `import ${JSON.stringify(scriptHref)};`;
const scriptContent = `import ${JSON.stringify(resolvedImportUrl)};`;
dom5.removeAttribute(scriptTag, 'src');
dom5.setTextContent(scriptTag, encodeString(scriptContent, true));
}
Expand Down
22 changes: 22 additions & 0 deletions packages/bundler/src/test/html-bundler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ const stripSpace = (html: string): string =>
html.replace(/>\s+/g, '>').replace(/>/g, '>\n').trim();

suite('HtmlBundler', () => {
test('external script tag inlines without imports/exports', async () => {
// This is a regression test added to ensure coverage for
// https://github.com/Polymer/tools/issues/3323
const analyzer = inMemoryAnalyzer({
'entrypoint.html': heredoc`
<script src="module.js" type="module"></script>
`,
'module.js': heredoc`
console.log('import/export-free code');
`
});
const bundler = new Bundler({analyzer});
const entrypointUrl = analyzer.resolveUrl('entrypoint.html')!;
const {documents} =
await bundler.bundle(await bundler.generateManifest([entrypointUrl]));
const entrypointDoc = documents.getHtmlDoc(entrypointUrl)!;
assert.deepEqual(entrypointDoc.content, heredoc`
<script type="module">
console.log('import/export-free code');
</script>
`);
});
test('external script tag inlines an es6 module', async () => {
const root = 'test/html/inline-es6-modules';
const analyzer = new Analyzer({
Expand Down

0 comments on commit b3633e0

Please sign in to comment.