Skip to content

Commit

Permalink
Prioritize manual chunk name over dynamic entry id (#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 13, 2021
1 parent 345ae5d commit 3078c61
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Chunk.ts
Expand Up @@ -187,6 +187,7 @@ export default class Chunk {
private dependencies = new Set<ExternalModule | Chunk>();
private dynamicDependencies = new Set<ExternalModule | Chunk>();
private dynamicEntryModules: Module[] = [];
private dynamicName: string | null = null;
private exportNamesByVariable = new Map<Variable, string[]>();
private exports = new Set<Variable>();
private exportsByName: Record<string, Variable> = Object.create(null);
Expand Down Expand Up @@ -384,7 +385,7 @@ export default class Chunk {
this.facadeModule = module;
this.facadeChunkByModule.set(module, this);
this.strictFacade = true;
this.assignFacadeName({}, module);
this.dynamicName = getChunkNameFromModule(module);
} else if (
this.facadeModule === module &&
!this.strictFacade &&
Expand Down Expand Up @@ -815,9 +816,7 @@ export default class Chunk {
if (fileName) {
this.fileName = fileName;
} else {
this.name = sanitizeFileName(
name || facadedModule.chunkName || getAliasName(facadedModule.id)
);
this.name = sanitizeFileName(name || getChunkNameFromModule(facadedModule));
}
}

Expand Down Expand Up @@ -1080,6 +1079,9 @@ export default class Chunk {
if (this.manualChunkAlias) {
return this.manualChunkAlias;
}
if (this.dynamicName) {
return this.dynamicName;
}
if (this.fileName) {
return getAliasName(this.fileName);
}
Expand Down Expand Up @@ -1370,3 +1372,7 @@ export default class Chunk {
}
}
}

function getChunkNameFromModule(module: Module): string {
return module.chunkName || getAliasName(module.id);
}
12 changes: 12 additions & 0 deletions test/chunking-form/samples/single-file-manual-chunk/_config.js
@@ -0,0 +1,12 @@
module.exports = {
description: 'chunk aliasing with extensions',
options: {
output: {
manualChunks(id) {
if (id.endsWith('main.js')) return;
if (id.endsWith('a.js')) return 'first';
return 'second';
}
}
}
};
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('a');

});
@@ -0,0 +1,18 @@
define(['exports'], function (exports) { 'use strict';

console.log('b');

var b = /*#__PURE__*/Object.freeze({
__proto__: null
});

console.log('c');

var c = /*#__PURE__*/Object.freeze({
__proto__: null
});

exports.b = b;
exports.c = c;

});
@@ -0,0 +1,7 @@
define(['require'], function (require) { 'use strict';

new Promise(function (resolve, reject) { require(['./generated-first'], resolve, reject) });
new Promise(function (resolve, reject) { require(['./generated-second'], resolve, reject) }).then(function (n) { return n.b; });
new Promise(function (resolve, reject) { require(['./generated-second'], resolve, reject) }).then(function (n) { return n.c; });

});
@@ -0,0 +1,3 @@
'use strict';

console.log('a');
@@ -0,0 +1,16 @@
'use strict';

console.log('b');

var b = /*#__PURE__*/Object.freeze({
__proto__: null
});

console.log('c');

var c = /*#__PURE__*/Object.freeze({
__proto__: null
});

exports.b = b;
exports.c = c;
@@ -0,0 +1,5 @@
'use strict';

Promise.resolve().then(function () { return require('./generated-first.js'); });
Promise.resolve().then(function () { return require('./generated-second.js'); }).then(function (n) { return n.b; });
Promise.resolve().then(function () { return require('./generated-second.js'); }).then(function (n) { return n.c; });
@@ -0,0 +1 @@
console.log('a');
@@ -0,0 +1,13 @@
console.log('b');

var b = /*#__PURE__*/Object.freeze({
__proto__: null
});

console.log('c');

var c = /*#__PURE__*/Object.freeze({
__proto__: null
});

export { b, c };
@@ -0,0 +1,3 @@
import('./generated-first.js');
import('./generated-second.js').then(function (n) { return n.b; });
import('./generated-second.js').then(function (n) { return n.c; });
@@ -0,0 +1,10 @@
System.register([], function () {
'use strict';
return {
execute: function () {

console.log('a');

}
};
});
@@ -0,0 +1,22 @@
System.register([], function (exports) {
'use strict';
return {
execute: function () {

console.log('b');

var b = /*#__PURE__*/Object.freeze({
__proto__: null
});
exports('b', b);

console.log('c');

var c = /*#__PURE__*/Object.freeze({
__proto__: null
});
exports('c', c);

}
};
});
@@ -0,0 +1,12 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

module.import('./generated-first.js');
module.import('./generated-second.js').then(function (n) { return n.b; });
module.import('./generated-second.js').then(function (n) { return n.c; });

}
};
});
1 change: 1 addition & 0 deletions test/chunking-form/samples/single-file-manual-chunk/a.js
@@ -0,0 +1 @@
console.log('a');
1 change: 1 addition & 0 deletions test/chunking-form/samples/single-file-manual-chunk/b.js
@@ -0,0 +1 @@
console.log('b');
1 change: 1 addition & 0 deletions test/chunking-form/samples/single-file-manual-chunk/c.js
@@ -0,0 +1 @@
console.log('c');
3 changes: 3 additions & 0 deletions test/chunking-form/samples/single-file-manual-chunk/main.js
@@ -0,0 +1,3 @@
import('./a.js');
import('./b.js');
import('./c.js');

0 comments on commit 3078c61

Please sign in to comment.