Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "https://github.com/parcel-bundler/parcel.git"
},
"dependencies": {
"@webcomponents/webcomponentsjs": "^1.0.20",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a dependency of parcel - it should be in the project

"babel-core": "^6.25.0",
"babel-generator": "^6.25.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
Expand Down
22 changes: 21 additions & 1 deletion src/builtins/bundle-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = exports = loadBundles;
var bundles = {};
var bundleLoaders = {
js: loadJSBundle,
css: loadCSSBundle
css: loadCSSBundle,
html: loadHTMLBundle
};

function loadBundle(bundle) {
Expand Down Expand Up @@ -78,6 +79,25 @@ function loadCSSBundle(bundle) {
});
}

function loadHTMLBundle(bundle) {
return new Promise((resolve, reject) => {
var link = document.createElement('link');
link.rel = 'import';
link.href = bundle;
link.onerror = function (e) {
link.onerror = link.onload = null;
reject(e);
};

link.onload = function () {
link.onerror = link.onload = null;
resolve();
};

document.getElementsByTagName('head')[0].appendChild(link);
});
}

function LazyPromise(executor) {
this.executor = executor;
this.promise = null;
Expand Down
21 changes: 21 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,25 @@ describe('html', function() {
let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8');
assert(html.includes('<a href="#hash_link">'));
});

it('should support dynamic imports for HTML', async function() {
let b = await bundle(__dirname + '/integration/html-import/index.html');

assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'html',
assets: ['other.html'],
childBundles: []
},
{
type: 'js',
assets: ['webcomponents-loader.js'],
childBundles: []
}
]
});
});
});
20 changes: 20 additions & 0 deletions test/integration/html-import/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML Imports</title>
<link rel="import" href="./other.html">
<script src="../../../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
</head>
<body>
<p>Testing HTML Imports</p>
<!-- Listen for the HTMLImportsLoaded event -->
<script>
window.addEventListener('HTMLImportsLoaded', () => {
var link = document.querySelector('link[rel="import"]');
var content = link.import.querySelector('#other-html');
document.body.appendChild(document.importNode(content, true));
});
</script>
</body>
</html>
8 changes: 8 additions & 0 deletions test/integration/html-import/other.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<body>
<div id="other-html">
<p>Import HTML imported other.html</p>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# yarn lockfile v1


"@webcomponents/webcomponentsjs@^1.0.20":
version "1.0.20"
resolved "https://registry.yarnpkg.com/@webcomponents/webcomponentsjs/-/webcomponentsjs-1.0.20.tgz#bac97c9709a3130241da4c2c85710e1dc8775b1c"

abbrev@1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
Expand Down