Skip to content

Commit

Permalink
fixes the HTMLPackager and also adds the code to be able to `require(…
Browse files Browse the repository at this point in the history
…'./other.html')`
  • Loading branch information
pierredavidbelanger committed Mar 1, 2018
1 parent e7ea515 commit dedb0ee
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Bundler extends EventEmitter {
'css',
require.resolve('./builtins/loaders/css-loader')
);
this.addBundleLoader(
'html',
require.resolve('./builtins/loaders/raw-loader')
);
this.addBundleLoader('js', require.resolve('./builtins/loaders/js-loader'));

this.pending = false;
Expand Down
5 changes: 5 additions & 0 deletions src/builtins/loaders/raw-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function loadHtmlBundle(bundle) {
return fetch(bundle).then(function (res) {
return res.text();
});
};
21 changes: 20 additions & 1 deletion src/packagers/HTMLPackager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Packager = require('./Packager');
const posthtml = require('posthtml');
const path = require('path');
const fs = require('../utils/fs');
const url = require('url');
const urlJoin = require('../utils/urlJoin');

// https://www.w3.org/TR/html5/dom.html#metadata-content-2
Expand All @@ -16,6 +18,8 @@ const metadataContent = new Set([
]);

class HTMLPackager extends Packager {
setup() {}

async addAsset(asset) {
let html = asset.generated.html || '';

Expand All @@ -31,9 +35,24 @@ class HTMLPackager extends Packager {
).process(html, {sync: true}).html;
}

await this.dest.write(html);
let name = this.bundle.name;
if (asset !== this.bundle.entryAsset) {
name = url.resolve(
path.join(path.dirname(this.bundle.name), asset.generateBundleName()),
''
);
}

this.size = html.length;
await fs.writeFile(name, html);
}

getSize() {
return this.size || 0;
}

end() {}

addBundlesToTree(bundles, tree) {
const head = find(tree, 'head');
if (head) {
Expand Down
2 changes: 1 addition & 1 deletion src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class JSPackager extends Packager {
}

async addAsset(asset) {
if (this.dedupe.has(asset.generated.js)) {
if (!asset.generated || this.dedupe.has(asset.generated.js)) {
return;
}

Expand Down
9 changes: 7 additions & 2 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('html', function() {
},
{
type: 'js',
assets: ['index.js'],
assets: ['bundle-loader.js', 'raw-loader.js', 'index.js'],
childBundles: [
{
type: 'map'
Expand All @@ -45,7 +45,12 @@ describe('html', function() {
},
{
type: 'js',
assets: ['index.js'],
assets: [
'bundle-loader.js',
'bundle-url.js',
'raw-loader.js',
'index.js'
],
childBundles: [
{
type: 'map'
Expand Down
2 changes: 2 additions & 0 deletions test/integration/html/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
alert('Hi');

require('./other.html');

0 comments on commit dedb0ee

Please sign in to comment.