From a345bd25ceae7ade3c975d0078363ea1d596af25 Mon Sep 17 00:00:00 2001 From: abiduzz420 Date: Wed, 13 Dec 2017 18:28:01 +0530 Subject: [PATCH 1/3] Added support for dynamic HTML imports. (#178) --- package.json | 1 + src/builtins/bundle-loader.js | 22 +++++++++++++++++++++- test/html.js | 8 ++++++++ test/integration/html-import/index.html | 20 ++++++++++++++++++++ test/integration/html-import/other.html | 8 ++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/integration/html-import/index.html create mode 100644 test/integration/html-import/other.html diff --git a/package.json b/package.json index e6b8c3b2e6d..6aaf3e4532d 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "url": "https://github.com/parcel-bundler/parcel.git" }, "dependencies": { + "@webcomponents/webcomponentsjs": "^1.0.20", "babel-core": "^6.25.0", "babel-generator": "^6.25.0", "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", diff --git a/src/builtins/bundle-loader.js b/src/builtins/bundle-loader.js index 16323dff036..780854cce61 100644 --- a/src/builtins/bundle-loader.js +++ b/src/builtins/bundle-loader.js @@ -23,7 +23,8 @@ module.exports = exports = loadBundles; var bundles = {}; var bundleLoaders = { js: loadJSBundle, - css: loadCSSBundle + css: loadCSSBundle, + html: loadHTMLBundle }; function loadBundle(bundle) { @@ -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; diff --git a/test/html.js b/test/html.js index e7e7430000a..b320472e5db 100644 --- a/test/html.js +++ b/test/html.js @@ -134,4 +134,12 @@ describe('html', function() { let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8'); assert(html.includes('')); }); + + it('should support dynamic imports for HTML', async function() { + let b = await bundle(_dirname + '/integration/html-import/index.html'); + + let html = fs.readFileSync(__dirname + '/dist/index.html','utf-8'); + assert(html.includes('')); + }) }); + diff --git a/test/integration/html-import/index.html b/test/integration/html-import/index.html new file mode 100644 index 00000000000..b7e554c7791 --- /dev/null +++ b/test/integration/html-import/index.html @@ -0,0 +1,20 @@ + + + + + HTML Imports + + + +

Testing HTML Imports

+ + + + + \ No newline at end of file diff --git a/test/integration/html-import/other.html b/test/integration/html-import/other.html new file mode 100644 index 00000000000..39b61f8bc47 --- /dev/null +++ b/test/integration/html-import/other.html @@ -0,0 +1,8 @@ + + + +
+

Import HTML imported other.html

+
+ + \ No newline at end of file From cdc371c59335d1279b6a137f652066821b00e7f0 Mon Sep 17 00:00:00 2001 From: abiduzz420 Date: Wed, 13 Dec 2017 19:45:38 +0530 Subject: [PATCH 2/3] WIP:HTML import test fails. (#178) --- test/html.js | 25 ++++++++++++++++++++----- test/integration/html-import/index.html | 2 +- yarn.lock | 4 ++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/test/html.js b/test/html.js index b320472e5db..9922953e236 100644 --- a/test/html.js +++ b/test/html.js @@ -136,10 +136,25 @@ describe('html', function() { }); it('should support dynamic imports for HTML', async function() { - let b = await bundle(_dirname + '/integration/html-import/index.html'); + let b = await bundle(__dirname + '/integration/html-import/index.html'); - let html = fs.readFileSync(__dirname + '/dist/index.html','utf-8'); - assert(html.includes('')); - }) -}); + assertBundleTree(b, { + name: 'index.html', + assets: ['index.html'], + childBundles: [ + { + type: 'html', + assets: ['other.html'], + childBundles: [] + } + ] + }); + let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf-8'); + assert( + html.includes( + '' + ) + ); + }); +}); diff --git a/test/integration/html-import/index.html b/test/integration/html-import/index.html index b7e554c7791..f32503015dd 100644 --- a/test/integration/html-import/index.html +++ b/test/integration/html-import/index.html @@ -4,10 +4,10 @@ HTML Imports +

Testing HTML Imports

- ' - ) - ); }); }); diff --git a/test/integration/html-import/index.html b/test/integration/html-import/index.html index f32503015dd..f8a2e9bce06 100644 --- a/test/integration/html-import/index.html +++ b/test/integration/html-import/index.html @@ -3,8 +3,8 @@ HTML Imports - - + +

Testing HTML Imports