Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a html bundleloader #178

Closed
devlux opened this issue Dec 9, 2017 · 12 comments
Closed

Add a html bundleloader #178

devlux opened this issue Dec 9, 2017 · 12 comments

Comments

@devlux
Copy link

devlux commented Dec 9, 2017

🙋 feature request

Add a html bundleloader to allow html fragments to be loaded dynamically, like already possible with js or css content.

🤔 Expected Behavior

Add a html bundleloader to allow html fragments to be loaded dynamically, like already possible with js or css content.

import('view.html')

😯 Current Behavior

Currently the following exception is thrown as no html bundleloader is implemented yet:

bundleLoaders[type] is not a function

💁 Possible Solution

Add a new html bundleloader to the existing bundleloaders:

var bundleLoaders = {

🔦 Context

I'm migrating a small spa from requirejs to parcel-bundler.

@abiduzz420
Copy link

abiduzz420 commented Dec 9, 2017

This issue looks interesting to me, although I don't know much about it. I shall read about it.
I would like to take it up @davidnagli

@davidnagli
Copy link
Contributor

@abiduzz420 Sounds good!

@abiduzz420
Copy link

@davidnagli @devlux I am going to add support for "div", "button" . Please suggest me other html fragments which should be included?

@abiduzz420
Copy link

abiduzz420 commented Dec 10, 2017

@davidnagli @devongovett I have done some reading and got a bit of understanding. Want to share the code on the lines I'm thinking. Would like to know if I'm doing it properly:

function loadHTMLBundle(bundle) {
	return new Promise(function (resolve,reject) {
		var nodeElement = document.createElement('nodeName'); // nodeName could be 'div','button'
		/* add html element properties : Refer https://www.w3schools.com/jsref/dom_obj_all.asp
		/*
		/*
		/*
		/*
		*/
		nodeElement.onerror = function (e) {
			nodeElement.onerror = nodeElement.onload = null;
			reject(e);
		};

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

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

Also mention if there is any certain detail I've not taken care of.
Thanks!

@devlux
Copy link
Author

devlux commented Dec 10, 2017

@abiduzz420 thanks for taking over this story! :-) I've read into the code myself today and I think the loadHTMLBundle method receives an URL to load the HTML fragment from. The purpose of this method is to load the content from that URL, e.g. by using HTML import as follows:

function loadHTMLBundle(bundle) {
  return new Promise(function (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);
  });
}

The content may then be accessed by calling:

var content = document.querySelector('link[rel="import"]').import;

Unfortunately HTML import it is not yet supported by all browsers, but there is a polyfill:

https://caniuse.com/#search=html%20import

I'm still trying to understand how the bundle model is created initially. And in the end the content needs to be resolved by the require promise. Therefore the above snippet seems to be only one part of the solution... ;-)

@abiduzz420
Copy link

@devlux Thanks for sharing the update.

@davidnagli davidnagli added this to In Progress in Features Dec 11, 2017
@abiduzz420
Copy link

abiduzz420 commented Dec 11, 2017

@devlux Have you looked at Webpack, browserify source code on how they solve this problem ? Perhaps we can take inspiration from there. I will look into how this problem is solved by webpack. Do let me know if you've found something exciting 😄

https://github.com/webpack-contrib/html-loader

@abiduzz420
Copy link

abiduzz420 commented Dec 12, 2017

I'm reading about Polyfills-Web components, looking into Polymer as well. Will update my progress soon @davidnagli
I request to let me work on this issue, even if it takes few more extra days :)

@davidnagli
Copy link
Contributor

Sounds good to me

@abiduzz420
Copy link

I wrote the test for the below, not able to figure why it throws TypeError: name undefined.

1) html should support dynamic imports for HTML:
     TypeError: Cannot read property 'name' of undefined
      at assertBundleTree (test\utils.js:93:39)
      at Context.<anonymous> (test\html.js:141:5)
      at <anonymous>

@DeMoorJasper
Copy link
Member

@abiduzz420 hmu on slack would love to help you out with implementing this

@logicalphase
Copy link

logicalphase commented Mar 28, 2018

Just wanted to make sure everyone knows Polymer 3 abandons HTML Imports in favor of ES modules NPM. You can glean an example of Polymer 3 pre.12, PSK (Polymer Starter Kit) with Webpack at: https://goo.gl/9KSRr7 Would love to see Parcel for Polymer 3. 🤗

Features automation moved this from PR to Done Jul 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Features
  
Done
Development

No branches or pull requests

5 participants