-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Import/require html from js #926
Import/require html from js #926
Conversation
Codecov Report
@@ Coverage Diff @@
## master #926 +/- ##
==========================================
+ Coverage 89.58% 89.73% +0.15%
==========================================
Files 68 68
Lines 3255 3264 +9
==========================================
+ Hits 2916 2929 +13
+ Misses 339 335 -4
Continue to review full report at Codecov.
|
…s more like a text-loader
I initially assumed some things wrong. So I just pushed a new commit. All tests are still passing. Still, I do not really know what I am doing, I essentially just randomly hack here and there. I do not know why I get this same error with my OnsenUI project. Anyways, I hope this PR can help someone :) |
How does this relate to #293? |
@davidnagli I am actually using such a custom bundle loader to implements the HTML loading part. But, in retrospect, and to be honest, I am not sure I am heading the right way with this PR. I guess, one want to What do your think ? |
I want this feature too! 👍 |
@pierredavidbelanger You want |
This will enable me to use aurelia, I was hoping something like this would be added 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! 🎉
@@ -0,0 +1,5 @@ | |||
module.exports = function loadTextBundle(bundle) { | |||
return fetch(bundle).then(function (res) { | |||
return res.text(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use an arrow function here 😀
module.exports.loadTextBundle = bundle => fetch(bundle).then(res => res.text())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linter (in the pre-commit hook) does not seems to like arrow functions:
src/builtins/loaders/text-loader.js
1:26 error Parsing error: Unexpected token >
✖ 1 problem (1 error, 0 warnings)
husky > pre-commit hook failed (add --no-verify to bypass)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to commit this:
module.exports = bundle => fetch(bundle).then(res => res.text())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ya builtins are supposed to be in ES5 👍
@@ -0,0 +1,5 @@ | |||
module.exports = function loadTextBundle(bundle) { | |||
return fetch(bundle).then(function (res) { | |||
return res.text(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ya builtins are supposed to be in ES5 👍
Is this PR supposed to be released with the next release? |
Just realized the broader implications of merging this PR... Currently HTML file imports are treated like RawAssets, in that they just return the resolved URL pointing to the file and not the actual contents of the file itself. However, by changing this behavior to allow HTML imports, we effectively break the way it previously worked. That means that anything like: import loginPage from 'login.html'
<a href=loginPage>Link to login page</a> would suddenly break, since now loginPage would be the actual contents of the login page rather than the url to the login page as it was before. Maybe I'm wrong? Does your code magically handle this somehow? Most likely we're gonna need to carefully design how this will play out and potentially introduce a breaking release when the time comes (Parcel 2.0?). Let me know if I'm wrong, but if not we should open a separate RFC issue to discuss / design everything and then come back to this once that's ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding blocking review to prevent merging for now until we work through those kinks
@davidnagli You are absolutely right. This PR breaks the way it previously worked. And my code does NOT magically handle this :) I had a bad felling a couple days ago about heading the wrong way with this PR. If was just me, I would close this one, until we know what we need and, of course, when Parcel is ready for a breaking release. |
@davidnagli just to be sure though, right now, with the master branch, your snippet is not working:
for at least 2 reasons:
I can try to have a look at this. |
@davidnagli I created the simplest app that demonstrate the problem: git clone https://github.com/pierredavidbelanger/parcel-html-test
cd parcel-html-test
yarn install
yarn run build
ls dist && tail dist/*.js 510368565fb792405acc6a86524a444c.html 510368565fb792405acc6a86524a444c.js index.html LazyPromise.prototype.then = function (onSuccess, onError) {
return this.promise || (this.promise = new Promise(this.executor).then(onSuccess, onError));
};
LazyPromise.prototype.catch = function (onError) {
return this.promise || (this.promise = new Promise(this.executor).catch(onError));
};
},{"./bundle-url":10}],0:[function(require,module,exports) {
var b=require(8);b.load([["6b4a33c18b69460d577328b29c800143.html",6],4]);
},{}]},{},[0]) Just in the little snippet here you can see both points (file name VS dep name, raw URL VS bundle-loader) I mentioned earlier. |
I am sorry, it may seems spaghetti at first, but I tried to keep the concern separated, in case you accept one PR ant not the other. I submit two PRs, for both the points I mentioned in a previous comment. I suggest you try my simple app that demonstrate the problem, then pull #1016 and #1017 into your local parcel, and try again my simple app; the problem is gone. I think that #1016 and #1017 are better fixes than this PR for the problem of |
FYI when all you need is to import a html file inside a string (e.g. like a nav menu or a logo with link) - //Import html as string
import templateString from './template.html';
//Parse string to dom node
let template = document.createElement('template');
templateString = templateString.trim(); // Never return a text node of whitespace as the result
template.innerHTML = templateString;
//Return DOM Node
console.log(template.content.firstChild);
return template.content.firstChild; You can use the babel plugin: babel-plugin-transform-html-import-to-string |
|
@adrian7 That's the perfect solution. I think that would be nice to have it by default in Parcel bundler. I have implemented a sample project with @adrian7 solution: |
I think this is a pretty crucial feature to merge in. Voicing support for this feature. |
yes, import/include (whatever you want to call it). It would be Awesome |
Simple question to ask about this PR. What if I don't want to put |
@adrian7 I owe you a big thanks. Your solution is perfect for importing AngularJS 1.6 component templates. |
Thanks @adrian7! The babel plugin worked at getting a string representation of the html. One issue it doesn't handle is resolving the dependencies/assets in the html file. For instance, if i have |
I've posted an updated PR based on this work over at #1528. |
It would be nice to have the solution @adrian7 suggested ( If you think it is valid to have it there I can do the PR. |
@jpenna this issue has been solved, and the html bundle loader will be available in the next release. |
@devongovett Attribute lowercasing in included
|
Is this supposed to be working? When I do this:
I get this:
I suppose that's what I would expect with a typical import because I'm importing an HTML file and it's expecting a module, but this ticket gives me the impression it should work with parcel. Am I misunderstanding? |
This is the first time I contribute to this project, so I am sorry if I missed something important.
I wanted to do something like what is described here #848
In short, I wanted to be able to
const template = require('./other.html');
You will see that I have no idea what I'm doing ;)
I am especially not proud of how I ended up butchering src/packagers/HTMLPackager.js
Also, all 232 tests are still passing.
Fixes #848
Closes #178