Skip to content
This repository was archived by the owner on Jan 22, 2023. It is now read-only.

Commit 11eed33

Browse files
authored
Merge pull request #85 from LarsDenBakker/feature/es-modules
Also run module scripts in HTML imports
2 parents 1ec515b + 53ba93d commit 11eed33

11 files changed

+91
-12
lines changed

html-imports.min.js

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

html-imports.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/html-imports.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
const disabledLinkSelector = `link[rel=stylesheet][href][type=${importDisableType}]`;
205205

206206
const scriptsSelector = `script:not([type]),script[type="application/javascript"],` +
207-
`script[type="text/javascript"]`;
207+
`script[type="text/javascript"],script[type="module"]`;
208208

209209
const importDependenciesSelector = `${importSelector},${disabledLinkSelector},` +
210210
`style:not([type]),link[rel=stylesheet][href]:not([type]),` + scriptsSelector;
@@ -347,6 +347,9 @@
347347
n.setAttribute(importDependencyAttr, '');
348348
// Generate source map hints for inline scripts.
349349
if (n.localName === 'script' && !n.src && n.textContent) {
350+
if(n.type === 'module') {
351+
throw new Error('Inline module scripts are not supported in HTML Imports.');
352+
}
350353
const num = inlineScriptIndex ? `-${inlineScriptIndex}` : '';
351354
const content = n.textContent + `\n//# sourceURL=${url}${num}.js\n`;
352355
// We use the src attribute so it triggers load/error events, and it's
@@ -725,7 +728,7 @@
725728
// itself is used as the `import` document.
726729
/** @type {Object|undefined} */
727730
const native_baseURI = Object.getOwnPropertyDescriptor(Node.prototype, 'baseURI');
728-
// NOTE: if not configurable (e.g. safari9), set it on the Element prototype.
731+
// NOTE: if not configurable (e.g. safari9), set it on the Element prototype.
729732
const klass = !native_baseURI || native_baseURI.configurable ? Node : Element;
730733
Object.defineProperty(klass.prototype, 'baseURI', {
731734
get() {

tests/html/es-module.html

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
5+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8+
Code distributed by Google as part of the polymer project is also
9+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
-->
11+
<html>
12+
13+
<head>
14+
<title>HTML Imports es module</title>
15+
<script src="../../src/html-imports.js"></script>
16+
<script>
17+
WCT = {
18+
waitFor: function (callback) {
19+
HTMLImports.whenReady(callback);
20+
}
21+
};
22+
window.loadedModules = [];
23+
</script>
24+
<script src="../../../web-component-tester/browser.js"></script>
25+
<link rel="import" href="./imports/es-module-import-1.html">
26+
</head>
27+
28+
<body>
29+
30+
<script>
31+
// Polymer build transpiles modules to AMD on browsers without es module and import.meta.url
32+
// support. We should not execute this test in that case.
33+
test('es modules', () => {
34+
if (!window.define) {
35+
assert.deepEqual(window.loadedModules, ['es-module-2', 'es-module-1', 'es-module-3']);
36+
}
37+
});
38+
39+
test('inline module', (done) => {
40+
// This test should be executed only on browsers that support native modules but not HTML Imports
41+
if (!window.define && !HTMLImports.useNative) {
42+
const link = document.createElement('link');
43+
link.setAttribute('rel', 'import');
44+
link.setAttribute('href', './imports/es-module-inline.html');
45+
46+
// Catch error thrown by polyfill
47+
window.onerror = () => {
48+
done();
49+
}
50+
51+
document.head.appendChild(link);
52+
} else {
53+
done();
54+
}
55+
});
56+
</script>
57+
</body>
58+
59+
</html>

tests/html/imports/es-module-1.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {message} from './es-module-2.js';
2+
3+
window.loadedModules.push('es-module-1');

tests/html/imports/es-module-2.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const message = 'foo';
2+
3+
window.loadedModules.push('es-module-2');

tests/html/imports/es-module-3.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './es-module-1.js';
2+
3+
window.loadedModules.push('es-module-3');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<link rel="import" href="./es-module-import-2.html">
2+
3+
<script type="module" src="./es-module-1.js"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script type="module" src="./es-module-3.js"></script>
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script type="module">
2+
// Inline module should throw an error
3+
</script>

tests/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@
5151
'html/ready-state.html',
5252
'html/resolve-path.html',
5353
'html/load-imports.html',
54+
'html/es-module.html'
5455
]);
5556
</script>

0 commit comments

Comments
 (0)