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

added html importing from js file #1528

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Bundler.js
Expand Up @@ -52,6 +52,10 @@ class Bundler extends EventEmitter {
browser: require.resolve('./builtins/loaders/browser/js-loader'),
node: require.resolve('./builtins/loaders/node/js-loader')
});
this.addBundleLoader('html', {
browser: require.resolve('./builtins/loaders/browser/html-loader'),
node: require.resolve('./builtins/loaders/node/html-loader')
})

this.pending = false;
this.loadedAssets = new Map();
Expand Down
5 changes: 5 additions & 0 deletions src/builtins/loaders/browser/html-loader.js
@@ -0,0 +1,5 @@
module.exports = function loadHTMLBundle(bundle) {
return fetch(bundle).then(function (res) {
return res.text();
});
};
20 changes: 20 additions & 0 deletions src/builtins/loaders/node/html-loader.js
@@ -0,0 +1,20 @@
var fs = require('fs');

module.exports = function loadHTMLBundle(bundle) {
return new Promise(function(resolve, reject) {
fs.readFile(__dirname + bundle, 'utf8', function(err, data) {
if (err) {
reject(err);
} else {
// wait for the next event loop iteration, so we are sure
// the current module is fully loaded
setImmediate(function() {
resolve(data);
});
}
});
})
.then(function(code) {
new Function('', code)();
});
};
16 changes: 14 additions & 2 deletions src/packagers/JSPackager.js
Expand Up @@ -69,8 +69,20 @@ class JSPackager extends Packager {
!this.bundle.assets.has(mod) &&
(!this.bundle.parentBundle || this.bundle.parentBundle.type !== 'js')
) {
this.externalModules.add(mod);
this.bundleLoaders.add(mod.type);
if(mod.type === "html") {
this.externalModules.add(mod);
this.bundleLoaders.add(mod.type);

// if your JS file imports an html dependency, the html
// needs to be included in the generated javascript file
await this.writeModule(
mod.id,
`module.exports=${JSON.stringify(mod.generated.html)}`
Copy link
Member

Choose a reason for hiding this comment

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

The problem with inlining like this is that we do additional processing on the HTML output in the HTML packager, which wouldn't be accounted for here. For example, we insert additional script/style tags when you import css from js etc. This problem is related to #1456, which is about doing the exact opposite of this and inlining JS/CSS into HTML. We should be able to solve both issues with the same architecture.

);
} else {
this.externalModules.add(mod);
this.bundleLoaders.add(mod.type);
}
}
}
}
Expand Down
Binary file added test/integration/html-css-js-html/100x100.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/integration/html-css-js-html/icons.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/integration/html-css-js-html/index.css
@@ -0,0 +1,3 @@
body {
background: red;
}
18 changes: 18 additions & 0 deletions test/integration/html-css-js-html/index.html
@@ -0,0 +1,18 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<h1>Hello world</h1>
<p>Linking to <a href="other.html">another page</a></p>
<a href="#hash_link">Hash link</a>
<a href="mailto:someone@acme.com">Mailto link</a>
<a href="tel:+33636757575">Tel link</a>
<script src="index.js"></script>
<script src="https://unpkg.com/parcel-bundler"></script>
<i>hello</i> <i>world</i>
<svg><use href="icons.svg#icon-repo-pull"></use></svg>
<script src="index.js"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions test/integration/html-css-js-html/index.js
@@ -0,0 +1,3 @@
alert('Hi');

import Page from './other.html';
10 changes: 10 additions & 0 deletions test/integration/html-css-js-html/other.html
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<h1>Other page</h1>
<img src="100x100.png" />
</body>
</html>