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

Fix HMR with content hashing changes #1071

Merged
merged 1 commit into from
Mar 26, 2018
Merged
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
14 changes: 14 additions & 0 deletions src/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ class Asset {
return md5(this.name) + '.' + this.type;
}

replaceBundleNames(bundleNameMap) {
for (let key in this.generated) {
let value = this.generated[key];
if (typeof value === 'string') {
// Replace temporary bundle names in the output with the final content-hashed names.
for (let [name, map] of bundleNameMap) {
value = value.split(name).join(map);
}

this.generated[key] = value;
}
}
}

generateErrorMessage(err) {
return err;
}
Expand Down
23 changes: 17 additions & 6 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,33 @@ class Bundler extends EventEmitter {
// Build the queued assets.
let loadedAssets = await this.buildQueue.run();

// Emit an HMR update for any new assets (that don't have a parent bundle yet)
// plus the asset that actually changed.
if (this.hmr && !isInitialBundle) {
this.hmr.emitUpdate([...this.findOrphanAssets(), ...loadedAssets]);
}
// The changed assets are any that don't have a parent bundle yet
// plus the ones that were in the build queue.
let changedAssets = [...this.findOrphanAssets(), ...loadedAssets];

// Invalidate bundles
for (let asset of this.loadedAssets.values()) {
asset.invalidateBundle();
}

// Create a new bundle tree and package everything up.
// Create a new bundle tree
this.mainBundle = this.createBundleTree(this.mainAsset);

// Generate the final bundle names, and replace references in the built assets.
this.bundleNameMap = this.mainBundle.getBundleNameMap(
this.options.contentHash
);

for (let asset of changedAssets) {
asset.replaceBundleNames(this.bundleNameMap);
}

// Emit an HMR update if this is not the initial bundle.
if (this.hmr && !isInitialBundle) {
this.hmr.emitUpdate(changedAssets);
}

// Package everything up
this.bundleHashes = await this.mainBundle.package(
this,
this.bundleHashes
Expand Down
6 changes: 5 additions & 1 deletion src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ class JSPackager extends Packager {
if (this.externalModules.size > 0) {
let preload = [];
for (let mod of this.externalModules) {
preload.push([mod.generateBundleName(), mod.id]);
// Find the bundle that has the module as its entry point
let bundle = Array.from(mod.bundles).find(b => b.entryAsset === mod);
if (bundle) {
preload.push([path.basename(bundle.name), mod.id]);
}
}

if (this.bundle.entryAsset) {
Expand Down
11 changes: 1 addition & 10 deletions src/packagers/Packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@ class Packager {
}

async write(string) {
await this.dest.write(this.replaceBundleNames(string));
}

replaceBundleNames(string) {
// Replace temporary bundle names in the output with the final content-hashed names.
for (let [name, map] of this.bundler.bundleNameMap) {
string = string.split(name).join(map);
}

return string;
await this.dest.write(string);
}

async start() {}
Expand Down