piral-cli-webpack5 1.12.5 → 1.13.0-beta.c05add8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/enhancers/pilet-webpack-config-enhancer/helpers.d.ts +1 -0
- package/lib/enhancers/pilet-webpack-config-enhancer/helpers.js +17 -1
- package/lib/enhancers/pilet-webpack-config-enhancer/helpers.js.map +1 -1
- package/lib/enhancers/pilet-webpack-config-enhancer/index.js +2 -0
- package/lib/enhancers/pilet-webpack-config-enhancer/index.js.map +1 -1
- package/lib/plugins/SharedChunks.d.ts +40 -0
- package/lib/plugins/SharedChunks.js +295 -0
- package/lib/plugins/SharedChunks.js.map +1 -0
- package/lib/plugins/SharedChunksLoader.d.ts +16 -0
- package/lib/plugins/SharedChunksLoader.js +62 -0
- package/lib/plugins/SharedChunksLoader.js.map +1 -0
- package/package.json +2 -2
- package/src/enhancers/pilet-webpack-config-enhancer/helpers.ts +18 -1
- package/src/enhancers/pilet-webpack-config-enhancer/index.ts +7 -0
- package/src/plugins/SharedChunks.ts +409 -0
- package/src/plugins/SharedChunksLoader.ts +85 -0
|
@@ -5,5 +5,6 @@ export declare function setEnvironment(variables: Record<string, string>): void;
|
|
|
5
5
|
export declare function getDefineVariables(variables: Record<string, string>): {};
|
|
6
6
|
export declare function getShared(importmap: Array<SharedDependency>, externals: Array<string>): {};
|
|
7
7
|
export declare function getDependencies(importmap: Array<SharedDependency>, compilerOptions: Configuration): {};
|
|
8
|
+
export declare function withSharedChunks(compilerOptions: Configuration, importmap: Array<SharedDependency>): void;
|
|
8
9
|
export declare function withSetPath(compilerOptions: Configuration): void;
|
|
9
10
|
export declare function withExternals(compilerOptions: Configuration, externals: Array<string>): void;
|
|
@@ -5,9 +5,11 @@ exports.setEnvironment = setEnvironment;
|
|
|
5
5
|
exports.getDefineVariables = getDefineVariables;
|
|
6
6
|
exports.getShared = getShared;
|
|
7
7
|
exports.getDependencies = getDependencies;
|
|
8
|
+
exports.withSharedChunks = withSharedChunks;
|
|
8
9
|
exports.withSetPath = withSetPath;
|
|
9
10
|
exports.withExternals = withExternals;
|
|
10
11
|
const path_1 = require("path");
|
|
12
|
+
const SharedChunks_1 = require("../../plugins/SharedChunks");
|
|
11
13
|
function getVariables(name, version, env) {
|
|
12
14
|
return {
|
|
13
15
|
NODE_ENV: env,
|
|
@@ -50,7 +52,7 @@ function getShared(importmap, externals) {
|
|
|
50
52
|
};
|
|
51
53
|
// This shares non-root exports of a package (e.g., `@scope/package/nested-export`).
|
|
52
54
|
// Ref: https://webpack.js.org/plugins/module-federation-plugin/#sharing-libraries
|
|
53
|
-
shared[dep.name +
|
|
55
|
+
shared[dep.name + '/'] = {
|
|
54
56
|
eager: false,
|
|
55
57
|
requiredVersion,
|
|
56
58
|
version,
|
|
@@ -74,6 +76,20 @@ function getDependencies(importmap, compilerOptions) {
|
|
|
74
76
|
}
|
|
75
77
|
return dependencies;
|
|
76
78
|
}
|
|
79
|
+
function withSharedChunks(compilerOptions, importmap) {
|
|
80
|
+
const groups = (0, SharedChunks_1.getSharedChunkGroups)(importmap);
|
|
81
|
+
if (groups.length > 0) {
|
|
82
|
+
compilerOptions.module = compilerOptions.module || {};
|
|
83
|
+
compilerOptions.module.rules = compilerOptions.module.rules || [];
|
|
84
|
+
compilerOptions.module.rules.push({
|
|
85
|
+
enforce: 'pre',
|
|
86
|
+
test: /\.m?js$/i,
|
|
87
|
+
include: groups.map((g) => g.root),
|
|
88
|
+
loader: require.resolve('../../plugins/SharedChunksLoader'),
|
|
89
|
+
options: { groups },
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
77
93
|
function withSetPath(compilerOptions) {
|
|
78
94
|
if (typeof compilerOptions.entry === 'object' && compilerOptions.entry) {
|
|
79
95
|
const setPath = (0, path_1.join)(__dirname, '..', '..', 'set-path');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/enhancers/pilet-webpack-config-enhancer/helpers.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/enhancers/pilet-webpack-config-enhancer/helpers.ts"],"names":[],"mappings":";;AAMA,oCAQC;AAED,wCAEC;AAED,gDAKC;AAED,8BAsCC;AAED,0CAgBC;AAED,4CAaC;AAED,kCAgBC;AAED,sCAoBC;AA1ID,+BAA4B;AAI5B,6DAAkE;AAElE,SAAgB,YAAY,CAAC,IAAY,EAAE,OAAe,EAAE,GAAW;IACrE,OAAO;QACL,QAAQ,EAAE,GAAG;QACb,UAAU,EAAE,IAAI,IAAI,EAAE,CAAC,YAAY,EAAE;QACrC,eAAe,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACzC,kBAAkB,EAAE,OAAO;QAC3B,eAAe,EAAE,IAAI;KACtB,CAAC;AACJ,CAAC;AAED,SAAgB,cAAc,CAAC,SAAiC;IAC9D,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AAC/E,CAAC;AAED,SAAgB,kBAAkB,CAAC,SAAiC;IAClE,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;QAC7D,GAAG,CAAC,eAAe,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACnD,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC;AAED,SAAgB,SAAS,CAAC,SAAkC,EAAE,SAAwB;IACpF,MAAM,MAAM,GAAG,EAAE,CAAC;IAElB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,M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C;YACD,OAAO,KAAK,CAAC;QACf,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;IAEH,eAAe,CAAC,SAAS,GAAG,CAAC,cAAc,EAAE,GAAG,YAAY,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -64,6 +64,7 @@ function piletV2WebpackConfigEnhancer(options, compiler) {
|
|
|
64
64
|
(0, helpers_1.withExternals)(compiler, externals);
|
|
65
65
|
(0, helpers_1.setEnvironment)(variables);
|
|
66
66
|
const dependencies = (0, helpers_1.getDependencies)(importmap, compiler);
|
|
67
|
+
(0, helpers_1.withSharedChunks)(compiler, importmap);
|
|
67
68
|
const banner = `//@pilet v:2(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`;
|
|
68
69
|
plugins.push(new SheetPlugin_1.default(piletCss, name, entry), new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables)), new webpack_1.BannerPlugin({
|
|
69
70
|
banner,
|
|
@@ -84,6 +85,7 @@ function piletV3WebpackConfigEnhancer(options, compiler) {
|
|
|
84
85
|
(0, helpers_1.withExternals)(compiler, externals);
|
|
85
86
|
(0, helpers_1.setEnvironment)(variables);
|
|
86
87
|
const dependencies = (0, helpers_1.getDependencies)(importmap, compiler);
|
|
88
|
+
(0, helpers_1.withSharedChunks)(compiler, importmap);
|
|
87
89
|
const banner = `//@pilet v:3(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`;
|
|
88
90
|
plugins.push(new StylesPlugin_1.default(piletCss, entry), new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(variables)), new webpack_1.BannerPlugin({
|
|
89
91
|
banner,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/enhancers/pilet-webpack-config-enhancer/index.ts"],"names":[],"mappings":";;;AAAA,4GAA4G;AAE5G,qCAA+E;AAC/E,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/enhancers/pilet-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AAE,CAAC,CAAC,QAAuB,EAAE,EAAE;IACpH,MAAM,EAAE,SAAS,GAAG,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IACtD,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC;IAC1D,MAAM,OAAO,GAA0B;QACrC,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,SAAS;QACT,IAAI,EAAE,OAAO,CAAC,QAAQ;QACtB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,SAAS;QACT,SAAS,EAAE;YACT,GAAG,IAAA,sBAAY,EAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,WAAW,CAAC;YAC3D,GAAG,OAAO,CAAC,SAAS;SACrB;KACF,CAAC;IAEF,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,IAAI;YACP,OAAO,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACzD,KAAK,IAAI;YACP,OAAO,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACzD,KAAK,IAAI;YACP,OAAO,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACzD,KAAK,IAAI;YACP,OAAO,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACzD,KAAK,IAAI;YACP,OAAO,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACzD,KAAK,MAAM,CAAC;QACZ;YACE,OAAO,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAC3D,CAAC;AACH,CAAC,CAAC;AA9BW,QAAA,0BAA0B,8BA8BrC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { SharedDependency } from 'piral-cli';
|
|
2
|
+
import type { ExportAllDeclaration, ExportNamedDeclaration, ImportDeclaration } from 'acorn';
|
|
3
|
+
interface SharedChunkEntry {
|
|
4
|
+
name: string;
|
|
5
|
+
file: string;
|
|
6
|
+
}
|
|
7
|
+
interface SharedChunkGroup {
|
|
8
|
+
root: string;
|
|
9
|
+
packageName: string;
|
|
10
|
+
entries: Array<SharedChunkEntry>;
|
|
11
|
+
}
|
|
12
|
+
type ModuleDependencyNode = ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration;
|
|
13
|
+
interface ModuleDependency {
|
|
14
|
+
node: ModuleDependencyNode;
|
|
15
|
+
target: string;
|
|
16
|
+
names: Array<string>;
|
|
17
|
+
}
|
|
18
|
+
interface SharedChunkRewrite {
|
|
19
|
+
dep: ModuleDependency;
|
|
20
|
+
owner: string;
|
|
21
|
+
mapped: Array<string | undefined>;
|
|
22
|
+
}
|
|
23
|
+
interface SharedChunkRewrites {
|
|
24
|
+
rewrites: Array<SharedChunkRewrite>;
|
|
25
|
+
problems: Array<string>;
|
|
26
|
+
}
|
|
27
|
+
interface SharedChunkAnalysis {
|
|
28
|
+
group: SharedChunkGroup;
|
|
29
|
+
enabled: boolean;
|
|
30
|
+
reason?: string;
|
|
31
|
+
owners: Map<string, string>;
|
|
32
|
+
getRewrites(file: string): SharedChunkRewrites;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Groups the local (i.e., bundled by this pilet) importmap entries by their package; only
|
|
36
|
+
* packages exposing more than one shared entry point (or used via a subpath) are relevant.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getSharedChunkGroups(importmap: Array<SharedDependency>): Array<SharedChunkGroup>;
|
|
39
|
+
export declare function getSharedChunkAnalysis(group: SharedChunkGroup): SharedChunkAnalysis;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSharedChunkGroups = getSharedChunkGroups;
|
|
4
|
+
exports.getSharedChunkAnalysis = getSharedChunkAnalysis;
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const acorn = require(require.resolve('acorn', { paths: [(0, path_1.dirname)(require.resolve('webpack/package.json'))] }));
|
|
8
|
+
const analysisCache = new Map();
|
|
9
|
+
function getPackageName(id) {
|
|
10
|
+
const parts = id.split('/');
|
|
11
|
+
return id.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
12
|
+
}
|
|
13
|
+
function findPackageRoot(file, packageName) {
|
|
14
|
+
let dir = (0, path_1.dirname)(file);
|
|
15
|
+
while (dir !== (0, path_1.dirname)(dir)) {
|
|
16
|
+
const pj = (0, path_1.join)(dir, 'package.json');
|
|
17
|
+
if ((0, fs_1.existsSync)(pj)) {
|
|
18
|
+
try {
|
|
19
|
+
const { name } = JSON.parse((0, fs_1.readFileSync)(pj, 'utf8'));
|
|
20
|
+
if (name === packageName) {
|
|
21
|
+
return dir;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// ignore invalid package.json files
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
dir = (0, path_1.dirname)(dir);
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
function safeRealpath(file) {
|
|
33
|
+
try {
|
|
34
|
+
return (0, fs_1.realpathSync)(file);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return file;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Groups the local (i.e., bundled by this pilet) importmap entries by their package; only
|
|
42
|
+
* packages exposing more than one shared entry point (or used via a subpath) are relevant.
|
|
43
|
+
*/
|
|
44
|
+
function getSharedChunkGroups(importmap) {
|
|
45
|
+
const groups = new Map();
|
|
46
|
+
for (const dep of importmap) {
|
|
47
|
+
if (dep.type !== 'local' || typeof dep.entry !== 'string' || !/\.m?js$/i.test(dep.entry)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const packageName = getPackageName(dep.name);
|
|
51
|
+
const file = safeRealpath(dep.entry);
|
|
52
|
+
const root = findPackageRoot(file, packageName);
|
|
53
|
+
if (root) {
|
|
54
|
+
const group = groups.get(root) || { root, packageName, entries: [] };
|
|
55
|
+
group.entries.push({ name: dep.name, file });
|
|
56
|
+
groups.set(root, group);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return [...groups.values()];
|
|
60
|
+
}
|
|
61
|
+
function getName(node) {
|
|
62
|
+
return node.type === 'Identifier' ? node.name : String(node.value);
|
|
63
|
+
}
|
|
64
|
+
function isRelative(request) {
|
|
65
|
+
return request.startsWith('./') || request.startsWith('../');
|
|
66
|
+
}
|
|
67
|
+
function getModuleSource(node) {
|
|
68
|
+
if (node.type === 'ImportDeclaration' || node.type === 'ExportAllDeclaration') {
|
|
69
|
+
return typeof node.source.value === 'string' ? node.source.value : undefined;
|
|
70
|
+
}
|
|
71
|
+
if (node.type === 'ExportNamedDeclaration' && typeof node.source?.value === 'string') {
|
|
72
|
+
return node.source.value;
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
function createAnalysis(group) {
|
|
77
|
+
const { root, entries } = group;
|
|
78
|
+
const modules = new Map();
|
|
79
|
+
const inPackage = (file) => file.startsWith(root + path_1.sep);
|
|
80
|
+
const resolveRelative = (from, request) => {
|
|
81
|
+
const base = (0, path_1.resolve)((0, path_1.dirname)(from), request);
|
|
82
|
+
const candidates = [
|
|
83
|
+
base,
|
|
84
|
+
...['.mjs', '.js', '.cjs'].map((ext) => base + ext),
|
|
85
|
+
...['index.mjs', 'index.js'].map((idx) => (0, path_1.join)(base, idx)),
|
|
86
|
+
];
|
|
87
|
+
const found = candidates.find((c) => (0, fs_1.existsSync)(c) && (0, fs_1.statSync)(c).isFile());
|
|
88
|
+
return found ? safeRealpath(found) : base;
|
|
89
|
+
};
|
|
90
|
+
const parseModule = (file) => {
|
|
91
|
+
const cached = modules.get(file);
|
|
92
|
+
if (cached) {
|
|
93
|
+
return cached;
|
|
94
|
+
}
|
|
95
|
+
const info = { file, ok: false, deps: [], exports: {}, stars: [] };
|
|
96
|
+
modules.set(file, info);
|
|
97
|
+
try {
|
|
98
|
+
const ast = acorn.parse((0, fs_1.readFileSync)(file, 'utf8'), {
|
|
99
|
+
ecmaVersion: 'latest',
|
|
100
|
+
sourceType: 'module',
|
|
101
|
+
});
|
|
102
|
+
const locals = {};
|
|
103
|
+
const pending = [];
|
|
104
|
+
for (const node of ast.body) {
|
|
105
|
+
const source = getModuleSource(node);
|
|
106
|
+
const target = source && isRelative(source) ? resolveRelative(file, source) : undefined;
|
|
107
|
+
if (node.type === 'ImportDeclaration') {
|
|
108
|
+
const names = node.specifiers.map((s) => s.type === 'ImportSpecifier' ? getName(s.imported) : s.type === 'ImportDefaultSpecifier' ? 'default' : '*');
|
|
109
|
+
node.specifiers.forEach((s, i) => (locals[s.local.name] = { file: target || source, name: names[i] }));
|
|
110
|
+
target && info.deps.push({ node, target, names });
|
|
111
|
+
}
|
|
112
|
+
else if (node.type === 'ExportNamedDeclaration') {
|
|
113
|
+
if (source) {
|
|
114
|
+
const names = node.specifiers.map((s) => getName(s.local));
|
|
115
|
+
node.specifiers.forEach((s, i) => (info.exports[getName(s.exported)] = { file: target || source, name: names[i] }));
|
|
116
|
+
target && info.deps.push({ node, target, names });
|
|
117
|
+
}
|
|
118
|
+
else if (node.declaration) {
|
|
119
|
+
const decl = node.declaration;
|
|
120
|
+
const ids = decl.type === 'VariableDeclaration'
|
|
121
|
+
? decl.declarations.map((d) => (d.id.type === 'Identifier' ? d.id.name : 'undefined'))
|
|
122
|
+
: [decl.id?.name ?? 'undefined'];
|
|
123
|
+
ids.forEach((id) => (info.exports[id] = { file, name: id }));
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
node.specifiers.forEach((s) => pending.push([getName(s.exported), getName(s.local)]));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else if (node.type === 'ExportDefaultDeclaration') {
|
|
130
|
+
info.exports.default = { file, name: 'default' };
|
|
131
|
+
}
|
|
132
|
+
else if (node.type === 'ExportAllDeclaration') {
|
|
133
|
+
if (node.exported) {
|
|
134
|
+
info.exports[getName(node.exported)] = { file: target || source, name: '*' };
|
|
135
|
+
}
|
|
136
|
+
else if (target) {
|
|
137
|
+
info.stars.push(target);
|
|
138
|
+
}
|
|
139
|
+
target && info.deps.push({ node, target, names: ['*'] });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
pending.forEach(([exported, local]) => (info.exports[exported] = locals[local] || { file, name: local }));
|
|
143
|
+
info.ok = true;
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
// not an ES module we can analyze - leave it alone
|
|
147
|
+
}
|
|
148
|
+
info.deps.filter((d) => inPackage(d.target)).forEach((d) => parseModule(d.target));
|
|
149
|
+
info.stars.filter(inPackage).forEach(parseModule);
|
|
150
|
+
return info;
|
|
151
|
+
};
|
|
152
|
+
const getExportNames = (file, seen = new Set()) => {
|
|
153
|
+
const info = parseModule(file);
|
|
154
|
+
if (seen.has(file)) {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
seen.add(file);
|
|
158
|
+
const names = new Set(Object.keys(info.exports));
|
|
159
|
+
info.stars.forEach((s) => getExportNames(s, seen).forEach((n) => n !== 'default' && names.add(n)));
|
|
160
|
+
return [...names];
|
|
161
|
+
};
|
|
162
|
+
// resolves the module and binding name that originally declares the given export
|
|
163
|
+
const getOrigin = (file, name) => {
|
|
164
|
+
for (let i = 0; i < 100; i++) {
|
|
165
|
+
if (!inPackage(file)) {
|
|
166
|
+
return `${file}#${name}`;
|
|
167
|
+
}
|
|
168
|
+
const info = parseModule(file);
|
|
169
|
+
let next = info.exports[name];
|
|
170
|
+
if (!next && name !== 'default') {
|
|
171
|
+
const star = info.stars.find((s) => getExportNames(s).includes(name));
|
|
172
|
+
next = star && { file: star, name };
|
|
173
|
+
}
|
|
174
|
+
if (!next || (next.file === file && next.name === name) || next.name === '*') {
|
|
175
|
+
return `${file}#${name}`;
|
|
176
|
+
}
|
|
177
|
+
({ file, name } = next);
|
|
178
|
+
}
|
|
179
|
+
return `${file}#${name}`;
|
|
180
|
+
};
|
|
181
|
+
const getClosure = (file, acc = new Set()) => {
|
|
182
|
+
if (!acc.has(file) && inPackage(file)) {
|
|
183
|
+
acc.add(file);
|
|
184
|
+
parseModule(file).deps.forEach((d) => getClosure(d.target, acc));
|
|
185
|
+
}
|
|
186
|
+
return acc;
|
|
187
|
+
};
|
|
188
|
+
// the public names under which each original binding is reachable per shared entry
|
|
189
|
+
const exportIndex = new Map();
|
|
190
|
+
const closures = new Map();
|
|
191
|
+
for (const entry of entries) {
|
|
192
|
+
closures.set(entry.name, getClosure(entry.file));
|
|
193
|
+
for (const name of getExportNames(entry.file)) {
|
|
194
|
+
const origin = getOrigin(entry.file, name);
|
|
195
|
+
const byEntry = exportIndex.get(origin) || new Map();
|
|
196
|
+
byEntry.has(entry.name) || byEntry.set(entry.name, []);
|
|
197
|
+
byEntry.get(entry.name).push(name);
|
|
198
|
+
exportIndex.set(origin, byEntry);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const owners = new Map();
|
|
202
|
+
for (const entry of entries) {
|
|
203
|
+
owners.has(entry.file) || owners.set(entry.file, entry.name);
|
|
204
|
+
}
|
|
205
|
+
const allModules = new Set([...closures.values()].flatMap((c) => [...c]));
|
|
206
|
+
for (const file of allModules) {
|
|
207
|
+
if (owners.has(file)) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
const prefix = `${file}#`;
|
|
211
|
+
const score = (entryName) => {
|
|
212
|
+
let count = 0;
|
|
213
|
+
exportIndex.forEach((byEntry, origin) => origin.startsWith(prefix) && byEntry.has(entryName) && count++);
|
|
214
|
+
return count;
|
|
215
|
+
};
|
|
216
|
+
// prefer the entry that publicly exposes most of the module's bindings, then the smallest one
|
|
217
|
+
const candidates = entries
|
|
218
|
+
.filter((e) => closures.get(e.name).has(file))
|
|
219
|
+
.map((e) => ({ name: e.name, score: score(e.name), size: closures.get(e.name).size }))
|
|
220
|
+
.sort((a, b) => b.score - a.score || a.size - b.size || a.name.localeCompare(b.name));
|
|
221
|
+
owners.set(file, candidates[0].name);
|
|
222
|
+
}
|
|
223
|
+
const getPublicName = (target, name, owner) => {
|
|
224
|
+
const byEntry = exportIndex.get(getOrigin(target, name));
|
|
225
|
+
const names = byEntry && byEntry.get(owner);
|
|
226
|
+
return names && (names.includes(name) ? name : names[0]);
|
|
227
|
+
};
|
|
228
|
+
// computes the rewrites for the module; a rewrite is only possible if all names are publicly available
|
|
229
|
+
const getRewrites = (file) => {
|
|
230
|
+
const info = parseModule(file);
|
|
231
|
+
const self = owners.get(file);
|
|
232
|
+
const rewrites = [];
|
|
233
|
+
const problems = [];
|
|
234
|
+
if (!info.ok) {
|
|
235
|
+
return { rewrites, problems };
|
|
236
|
+
}
|
|
237
|
+
for (const dep of info.deps) {
|
|
238
|
+
const owner = owners.get(dep.target);
|
|
239
|
+
if (!owner || owner === self) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const mapped = dep.names.map((n) => n === '*' ? undefined : getPublicName(dep.target, n, owner));
|
|
243
|
+
const missing = dep.names.filter((_, i) => !mapped[i]);
|
|
244
|
+
if (missing.length) {
|
|
245
|
+
problems.push(`"${(0, path_1.relative)(root, file)}" requires ${missing.join(', ')} from "${(0, path_1.relative)(root, dep.target)}", which is not exposed by "${owner}"`);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
rewrites.push({ dep, owner, mapped });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return { rewrites, problems };
|
|
252
|
+
};
|
|
253
|
+
// the rewrites must not introduce cycles between the shared entries (SystemJS modules)
|
|
254
|
+
const edges = new Map();
|
|
255
|
+
for (const file of allModules) {
|
|
256
|
+
const from = owners.get(file);
|
|
257
|
+
for (const { owner } of getRewrites(file).rewrites) {
|
|
258
|
+
edges.has(from) || edges.set(from, new Set());
|
|
259
|
+
edges.get(from).add(owner);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const hasCycle = (() => {
|
|
263
|
+
const state = new Map();
|
|
264
|
+
const visit = (n) => {
|
|
265
|
+
if (state.get(n) === 1) {
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
if (state.get(n) === 2) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
state.set(n, 1);
|
|
272
|
+
const cyclic = [...(edges.get(n) || [])].some(visit);
|
|
273
|
+
state.set(n, 2);
|
|
274
|
+
return cyclic;
|
|
275
|
+
};
|
|
276
|
+
return [...edges.keys()].some(visit);
|
|
277
|
+
})();
|
|
278
|
+
return {
|
|
279
|
+
group,
|
|
280
|
+
enabled: !hasCycle,
|
|
281
|
+
reason: hasCycle
|
|
282
|
+
? `Sharing the private modules of "${group.packageName}" would introduce cyclic shared dependencies.`
|
|
283
|
+
: undefined,
|
|
284
|
+
owners,
|
|
285
|
+
getRewrites,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function getSharedChunkAnalysis(group) {
|
|
289
|
+
const key = `${group.root}|${group.entries.map((e) => `${e.name}=${e.file}`).join(',')}`;
|
|
290
|
+
if (!analysisCache.has(key)) {
|
|
291
|
+
analysisCache.set(key, createAnalysis(group));
|
|
292
|
+
}
|
|
293
|
+
return analysisCache.get(key);
|
|
294
|
+
}
|
|
295
|
+
//# sourceMappingURL=SharedChunks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SharedChunks.js","sourceRoot":"","sources":["../../src/plugins/SharedChunks.ts"],"names":[],"mappings":";;AAwGA,oDAoBC;AAoRD,wDAQC;AAxZD,2BAAsE;AACtE,+BAA6D;AA8D7D,MAAM,KAAK,GAA2B,OAAO,CAC3C,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,CAAC,EAA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K,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAChB,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;QACF,OAAO,CAAC,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO;QACL,KAAK;QACL,OAAO,EAAE,CAAC,QAAQ;QAClB,MAAM,EAAE,QAAQ;YACd,CAAC,CAAC,mCAAmC,KAAK,CAAC,WAAW,+CAA+C;YACrG,CAAC,CAAC,SAAS;QACb,MAAM;QACN,WAAW;KACZ,CAAC;AACJ,CAAC;AAED,SAAgB,sBAAsB,CAAC,KAAuB;IAC5D,MAAM,GAAG,GAAG,GAAG,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;IAEzF,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5B,aAAa,CAAC,GAAG,CAAC,GAAG,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,aAAa,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC;AACjC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type getSharedChunkGroups } from './SharedChunks';
|
|
2
|
+
type SharedChunkGroup = ReturnType<typeof getSharedChunkGroups>[number];
|
|
3
|
+
interface SharedChunksLoaderContext {
|
|
4
|
+
getOptions(): {
|
|
5
|
+
groups?: Array<SharedChunkGroup>;
|
|
6
|
+
} | undefined;
|
|
7
|
+
resourcePath: string;
|
|
8
|
+
emitWarning(warning: Error): void;
|
|
9
|
+
callback(error: Error | null, content: string, sourceMap?: unknown): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Rewrites relative imports of private modules (chunks) that are owned by another
|
|
13
|
+
* shared entry point of the same package to go through that entry point instead.
|
|
14
|
+
*/
|
|
15
|
+
export default function SharedChunksLoader(this: SharedChunksLoaderContext, source: string, map?: unknown): void;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = SharedChunksLoader;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const SharedChunks_1 = require("./SharedChunks");
|
|
6
|
+
const warned = new Set();
|
|
7
|
+
function binding(publicName, localName) {
|
|
8
|
+
return publicName === localName ? publicName : `${publicName} as ${localName}`;
|
|
9
|
+
}
|
|
10
|
+
function getLocalName(specifier) {
|
|
11
|
+
return specifier.type === 'Identifier' ? specifier.name : JSON.stringify(specifier.value);
|
|
12
|
+
}
|
|
13
|
+
function render({ dep, owner, mapped }) {
|
|
14
|
+
const { node } = dep;
|
|
15
|
+
const request = JSON.stringify(owner);
|
|
16
|
+
if (node.type === 'ImportDeclaration') {
|
|
17
|
+
if (node.specifiers.length === 0) {
|
|
18
|
+
return `import ${request};`;
|
|
19
|
+
}
|
|
20
|
+
const specs = node.specifiers.map((s, i) => binding(mapped[i], s.local.name));
|
|
21
|
+
return `import { ${specs.join(', ')} } from ${request};`;
|
|
22
|
+
}
|
|
23
|
+
if (node.type === 'ExportAllDeclaration') {
|
|
24
|
+
return `export * from ${request};`;
|
|
25
|
+
}
|
|
26
|
+
const specs = node.specifiers.map((s, i) => binding(mapped[i], getLocalName(s.exported)));
|
|
27
|
+
return `export { ${specs.join(', ')} } from ${request};`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Rewrites relative imports of private modules (chunks) that are owned by another
|
|
31
|
+
* shared entry point of the same package to go through that entry point instead.
|
|
32
|
+
*/
|
|
33
|
+
function SharedChunksLoader(source, map) {
|
|
34
|
+
const { groups = [] } = this.getOptions() || {};
|
|
35
|
+
const file = this.resourcePath;
|
|
36
|
+
const group = groups.find((g) => file.startsWith(g.root));
|
|
37
|
+
if (group) {
|
|
38
|
+
const analysis = (0, SharedChunks_1.getSharedChunkAnalysis)(group);
|
|
39
|
+
if (analysis.enabled) {
|
|
40
|
+
const { rewrites, problems } = analysis.getRewrites(file);
|
|
41
|
+
problems.forEach((p) => this.emitWarning(new Error(`Cannot share private module: ${p}. It will be duplicated.`)));
|
|
42
|
+
if (rewrites.length > 0) {
|
|
43
|
+
// the analysis parsed the same file from disk; make sure we see the same content
|
|
44
|
+
const original = (0, fs_1.readFileSync)(file, 'utf8');
|
|
45
|
+
if (original === source) {
|
|
46
|
+
let result = source;
|
|
47
|
+
for (const rewrite of [...rewrites].sort((a, b) => b.dep.node.start - a.dep.node.start)) {
|
|
48
|
+
const { start, end } = rewrite.dep.node;
|
|
49
|
+
result = result.slice(0, start) + render(rewrite) + result.slice(end);
|
|
50
|
+
}
|
|
51
|
+
return this.callback(null, result, map);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (!warned.has(analysis.reason)) {
|
|
56
|
+
warned.add(analysis.reason);
|
|
57
|
+
this.emitWarning(new Error(analysis.reason));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
this.callback(null, source, map);
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=SharedChunksLoader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SharedChunksLoader.js","sourceRoot":"","sources":["../../src/plugins/SharedChunksLoader.ts"],"names":[],"mappings":";;AAkDA,qCAkCC;AApFD,2BAAkC;AAGlC,iDAAmF;AAYnF,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;AAEjC,SAAS,OAAO,CAAC,UAA8B,EAAE,SAA6B;IAC5E,OAAO,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CA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MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;wBACxC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;oBACxE,CAAC;oBAED,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;gBAC1C,CAAC;YACH,CAAC;QACH,CAAC;aAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YACxC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YAC5B,IAAI,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;IAED,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;AACnC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-cli-webpack5",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0-beta.c05add8",
|
|
4
4
|
"description": "Provides debug and build capabilities for pilets and Piral instances using Webpack v5.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral-cli",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"@types/node": "^18",
|
|
49
49
|
"@types/terser-webpack-plugin": "^5.2.0",
|
|
50
50
|
"@types/yargs": "^15",
|
|
51
|
-
"piral-cli": "^1.
|
|
51
|
+
"piral-cli": "^1.13.0-beta.c05add8",
|
|
52
52
|
"strip-ansi": "^6"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
@@ -2,6 +2,8 @@ import { join } from 'path';
|
|
|
2
2
|
import type { SharedDependency } from 'piral-cli';
|
|
3
3
|
import type { Configuration } from 'webpack';
|
|
4
4
|
|
|
5
|
+
import { getSharedChunkGroups } from '../../plugins/SharedChunks';
|
|
6
|
+
|
|
5
7
|
export function getVariables(name: string, version: string, env: string): Record<string, string> {
|
|
6
8
|
return {
|
|
7
9
|
NODE_ENV: env,
|
|
@@ -51,7 +53,7 @@ export function getShared(importmap: Array<SharedDependency>, externals: Array<s
|
|
|
51
53
|
|
|
52
54
|
// This shares non-root exports of a package (e.g., `@scope/package/nested-export`).
|
|
53
55
|
// Ref: https://webpack.js.org/plugins/module-federation-plugin/#sharing-libraries
|
|
54
|
-
shared[dep.name +
|
|
56
|
+
shared[dep.name + '/'] = {
|
|
55
57
|
eager: false,
|
|
56
58
|
requiredVersion,
|
|
57
59
|
version,
|
|
@@ -81,6 +83,21 @@ export function getDependencies(importmap: Array<SharedDependency>, compilerOpti
|
|
|
81
83
|
return dependencies;
|
|
82
84
|
}
|
|
83
85
|
|
|
86
|
+
export function withSharedChunks(compilerOptions: Configuration, importmap: Array<SharedDependency>) {
|
|
87
|
+
const groups = getSharedChunkGroups(importmap);
|
|
88
|
+
if (groups.length > 0) {
|
|
89
|
+
compilerOptions.module = compilerOptions.module || {};
|
|
90
|
+
compilerOptions.module.rules = compilerOptions.module.rules || [];
|
|
91
|
+
compilerOptions.module.rules.push({
|
|
92
|
+
enforce: 'pre',
|
|
93
|
+
test: /\.m?js$/i,
|
|
94
|
+
include: groups.map((g) => g.root),
|
|
95
|
+
loader: require.resolve('../../plugins/SharedChunksLoader'),
|
|
96
|
+
options: { groups },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
84
101
|
export function withSetPath(compilerOptions: Configuration) {
|
|
85
102
|
if (typeof compilerOptions.entry === 'object' && compilerOptions.entry) {
|
|
86
103
|
const setPath = join(__dirname, '..', '..', 'set-path');
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
withExternals,
|
|
10
10
|
getDependencies,
|
|
11
11
|
getShared,
|
|
12
|
+
withSharedChunks,
|
|
12
13
|
} from './helpers';
|
|
13
14
|
import StylesPlugin from '../../plugins/StylesPlugin';
|
|
14
15
|
import SheetPlugin from '../../plugins/SheetPlugin';
|
|
@@ -143,6 +144,9 @@ function piletV2WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler:
|
|
|
143
144
|
setEnvironment(variables);
|
|
144
145
|
|
|
145
146
|
const dependencies = getDependencies(importmap, compiler);
|
|
147
|
+
|
|
148
|
+
withSharedChunks(compiler, importmap);
|
|
149
|
+
|
|
146
150
|
const banner = `//@pilet v:2(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`;
|
|
147
151
|
|
|
148
152
|
plugins.push(
|
|
@@ -174,6 +178,9 @@ function piletV3WebpackConfigEnhancer(options: SchemaEnhancerOptions, compiler:
|
|
|
174
178
|
setEnvironment(variables);
|
|
175
179
|
|
|
176
180
|
const dependencies = getDependencies(importmap, compiler);
|
|
181
|
+
|
|
182
|
+
withSharedChunks(compiler, importmap);
|
|
183
|
+
|
|
177
184
|
const banner = `//@pilet v:3(webpackChunk${jsonpFunction},${JSON.stringify(dependencies)})`;
|
|
178
185
|
|
|
179
186
|
plugins.push(
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'fs';
|
|
2
|
+
import { dirname, join, relative, resolve, sep } from 'path';
|
|
3
|
+
import type { SharedDependency } from 'piral-cli';
|
|
4
|
+
import type {
|
|
5
|
+
ExportAllDeclaration,
|
|
6
|
+
ExportNamedDeclaration,
|
|
7
|
+
Identifier,
|
|
8
|
+
ImportDeclaration,
|
|
9
|
+
Literal,
|
|
10
|
+
Program,
|
|
11
|
+
} from 'acorn';
|
|
12
|
+
|
|
13
|
+
interface SharedChunkEntry {
|
|
14
|
+
name: string;
|
|
15
|
+
file: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface SharedChunkGroup {
|
|
19
|
+
root: string;
|
|
20
|
+
packageName: string;
|
|
21
|
+
entries: Array<SharedChunkEntry>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface BindingReference {
|
|
25
|
+
file: string;
|
|
26
|
+
name: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type ModuleDependencyNode = ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration;
|
|
30
|
+
|
|
31
|
+
interface ModuleDependency {
|
|
32
|
+
node: ModuleDependencyNode;
|
|
33
|
+
target: string;
|
|
34
|
+
names: Array<string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface ModuleInfo {
|
|
38
|
+
file: string;
|
|
39
|
+
ok: boolean;
|
|
40
|
+
deps: Array<ModuleDependency>;
|
|
41
|
+
exports: Record<string, BindingReference>;
|
|
42
|
+
stars: Array<string>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface SharedChunkRewrite {
|
|
46
|
+
dep: ModuleDependency;
|
|
47
|
+
owner: string;
|
|
48
|
+
mapped: Array<string | undefined>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface SharedChunkRewrites {
|
|
52
|
+
rewrites: Array<SharedChunkRewrite>;
|
|
53
|
+
problems: Array<string>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface SharedChunkAnalysis {
|
|
57
|
+
group: SharedChunkGroup;
|
|
58
|
+
enabled: boolean;
|
|
59
|
+
reason?: string;
|
|
60
|
+
owners: Map<string, string>;
|
|
61
|
+
getRewrites(file: string): SharedChunkRewrites;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const acorn: typeof import('acorn') = require(
|
|
65
|
+
require.resolve('acorn', { paths: [dirname(require.resolve('webpack/package.json'))] }),
|
|
66
|
+
);
|
|
67
|
+
const analysisCache = new Map<string, SharedChunkAnalysis>();
|
|
68
|
+
|
|
69
|
+
function getPackageName(id: string): string {
|
|
70
|
+
const parts = id.split('/');
|
|
71
|
+
return id.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function findPackageRoot(file: string, packageName: string): string | undefined {
|
|
75
|
+
let dir = dirname(file);
|
|
76
|
+
while (dir !== dirname(dir)) {
|
|
77
|
+
const pj = join(dir, 'package.json');
|
|
78
|
+
if (existsSync(pj)) {
|
|
79
|
+
try {
|
|
80
|
+
const { name } = JSON.parse(readFileSync(pj, 'utf8')) as { name?: string };
|
|
81
|
+
|
|
82
|
+
if (name === packageName) {
|
|
83
|
+
return dir;
|
|
84
|
+
}
|
|
85
|
+
} catch {
|
|
86
|
+
// ignore invalid package.json files
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
dir = dirname(dir);
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
function safeRealpath(file: string): string {
|
|
94
|
+
try {
|
|
95
|
+
return realpathSync(file);
|
|
96
|
+
} catch {
|
|
97
|
+
return file;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Groups the local (i.e., bundled by this pilet) importmap entries by their package; only
|
|
103
|
+
* packages exposing more than one shared entry point (or used via a subpath) are relevant.
|
|
104
|
+
*/
|
|
105
|
+
export function getSharedChunkGroups(importmap: Array<SharedDependency>): Array<SharedChunkGroup> {
|
|
106
|
+
const groups = new Map<string, SharedChunkGroup>();
|
|
107
|
+
|
|
108
|
+
for (const dep of importmap) {
|
|
109
|
+
if (dep.type !== 'local' || typeof dep.entry !== 'string' || !/\.m?js$/i.test(dep.entry)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const packageName = getPackageName(dep.name);
|
|
114
|
+
const file = safeRealpath(dep.entry);
|
|
115
|
+
const root = findPackageRoot(file, packageName);
|
|
116
|
+
|
|
117
|
+
if (root) {
|
|
118
|
+
const group = groups.get(root) || { root, packageName, entries: [] };
|
|
119
|
+
group.entries.push({ name: dep.name, file });
|
|
120
|
+
groups.set(root, group);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return [...groups.values()];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function getName(node: Identifier | Literal): string {
|
|
128
|
+
return node.type === 'Identifier' ? node.name : String(node.value);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function isRelative(request: string): boolean {
|
|
132
|
+
return request.startsWith('./') || request.startsWith('../');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function getModuleSource(node: Program['body'][number]): string | undefined {
|
|
136
|
+
if (node.type === 'ImportDeclaration' || node.type === 'ExportAllDeclaration') {
|
|
137
|
+
return typeof node.source.value === 'string' ? node.source.value : undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (node.type === 'ExportNamedDeclaration' && typeof node.source?.value === 'string') {
|
|
141
|
+
return node.source.value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function createAnalysis(group: SharedChunkGroup): SharedChunkAnalysis {
|
|
148
|
+
const { root, entries } = group;
|
|
149
|
+
const modules = new Map<string, ModuleInfo>();
|
|
150
|
+
const inPackage = (file: string): boolean => file.startsWith(root + sep);
|
|
151
|
+
const resolveRelative = (from: string, request: string): string => {
|
|
152
|
+
const base = resolve(dirname(from), request);
|
|
153
|
+
const candidates = [
|
|
154
|
+
base,
|
|
155
|
+
...['.mjs', '.js', '.cjs'].map((ext) => base + ext),
|
|
156
|
+
...['index.mjs', 'index.js'].map((idx) => join(base, idx)),
|
|
157
|
+
];
|
|
158
|
+
const found = candidates.find((c) => existsSync(c) && statSync(c).isFile());
|
|
159
|
+
return found ? safeRealpath(found) : base;
|
|
160
|
+
};
|
|
161
|
+
const parseModule = (file: string): ModuleInfo => {
|
|
162
|
+
const cached = modules.get(file);
|
|
163
|
+
|
|
164
|
+
if (cached) {
|
|
165
|
+
return cached;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const info: ModuleInfo = { file, ok: false, deps: [], exports: {}, stars: [] };
|
|
169
|
+
modules.set(file, info);
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
const ast: Program = acorn.parse(readFileSync(file, 'utf8'), {
|
|
173
|
+
ecmaVersion: 'latest',
|
|
174
|
+
sourceType: 'module',
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const locals: Record<string, BindingReference> = {};
|
|
178
|
+
const pending: Array<[string, string]> = [];
|
|
179
|
+
|
|
180
|
+
for (const node of ast.body) {
|
|
181
|
+
const source = getModuleSource(node);
|
|
182
|
+
const target = source && isRelative(source) ? resolveRelative(file, source) : undefined;
|
|
183
|
+
|
|
184
|
+
if (node.type === 'ImportDeclaration') {
|
|
185
|
+
const names = node.specifiers.map((s) =>
|
|
186
|
+
s.type === 'ImportSpecifier' ? getName(s.imported) : s.type === 'ImportDefaultSpecifier' ? 'default' : '*',
|
|
187
|
+
);
|
|
188
|
+
node.specifiers.forEach((s, i) => (locals[s.local.name] = { file: target || source, name: names[i] }));
|
|
189
|
+
target && info.deps.push({ node, target, names });
|
|
190
|
+
} else if (node.type === 'ExportNamedDeclaration') {
|
|
191
|
+
if (source) {
|
|
192
|
+
const names = node.specifiers.map((s) => getName(s.local));
|
|
193
|
+
node.specifiers.forEach(
|
|
194
|
+
(s, i) => (info.exports[getName(s.exported)] = { file: target || source, name: names[i] }),
|
|
195
|
+
);
|
|
196
|
+
target && info.deps.push({ node, target, names });
|
|
197
|
+
} else if (node.declaration) {
|
|
198
|
+
const decl = node.declaration;
|
|
199
|
+
const ids =
|
|
200
|
+
decl.type === 'VariableDeclaration'
|
|
201
|
+
? decl.declarations.map((d) => (d.id.type === 'Identifier' ? d.id.name : 'undefined'))
|
|
202
|
+
: [decl.id?.name ?? 'undefined'];
|
|
203
|
+
ids.forEach((id) => (info.exports[id] = { file, name: id }));
|
|
204
|
+
} else {
|
|
205
|
+
node.specifiers.forEach((s) => pending.push([getName(s.exported), getName(s.local)]));
|
|
206
|
+
}
|
|
207
|
+
} else if (node.type === 'ExportDefaultDeclaration') {
|
|
208
|
+
info.exports.default = { file, name: 'default' };
|
|
209
|
+
} else if (node.type === 'ExportAllDeclaration') {
|
|
210
|
+
if (node.exported) {
|
|
211
|
+
info.exports[getName(node.exported)] = { file: target || source, name: '*' };
|
|
212
|
+
} else if (target) {
|
|
213
|
+
info.stars.push(target);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
target && info.deps.push({ node, target, names: ['*'] });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
pending.forEach(([exported, local]) => (info.exports[exported] = locals[local] || { file, name: local }));
|
|
221
|
+
info.ok = true;
|
|
222
|
+
} catch {
|
|
223
|
+
// not an ES module we can analyze - leave it alone
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
info.deps.filter((d) => inPackage(d.target)).forEach((d) => parseModule(d.target));
|
|
227
|
+
info.stars.filter(inPackage).forEach(parseModule);
|
|
228
|
+
return info;
|
|
229
|
+
};
|
|
230
|
+
const getExportNames = (file: string, seen = new Set<string>()): Array<string> => {
|
|
231
|
+
const info = parseModule(file);
|
|
232
|
+
|
|
233
|
+
if (seen.has(file)) {
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
seen.add(file);
|
|
238
|
+
const names = new Set(Object.keys(info.exports));
|
|
239
|
+
info.stars.forEach((s) => getExportNames(s, seen).forEach((n) => n !== 'default' && names.add(n)));
|
|
240
|
+
return [...names];
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// resolves the module and binding name that originally declares the given export
|
|
244
|
+
const getOrigin = (file: string, name: string): string => {
|
|
245
|
+
for (let i = 0; i < 100; i++) {
|
|
246
|
+
if (!inPackage(file)) {
|
|
247
|
+
return `${file}#${name}`;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const info = parseModule(file);
|
|
251
|
+
let next = info.exports[name];
|
|
252
|
+
|
|
253
|
+
if (!next && name !== 'default') {
|
|
254
|
+
const star = info.stars.find((s) => getExportNames(s).includes(name));
|
|
255
|
+
next = star && { file: star, name };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (!next || (next.file === file && next.name === name) || next.name === '*') {
|
|
259
|
+
return `${file}#${name}`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
({ file, name } = next);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return `${file}#${name}`;
|
|
266
|
+
};
|
|
267
|
+
const getClosure = (file: string, acc = new Set<string>()): Set<string> => {
|
|
268
|
+
if (!acc.has(file) && inPackage(file)) {
|
|
269
|
+
acc.add(file);
|
|
270
|
+
parseModule(file).deps.forEach((d) => getClosure(d.target, acc));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return acc;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// the public names under which each original binding is reachable per shared entry
|
|
277
|
+
const exportIndex = new Map<string, Map<string, Array<string>>>();
|
|
278
|
+
const closures = new Map<string, Set<string>>();
|
|
279
|
+
|
|
280
|
+
for (const entry of entries) {
|
|
281
|
+
closures.set(entry.name, getClosure(entry.file));
|
|
282
|
+
for (const name of getExportNames(entry.file)) {
|
|
283
|
+
const origin = getOrigin(entry.file, name);
|
|
284
|
+
const byEntry = exportIndex.get(origin) || new Map<string, Array<string>>();
|
|
285
|
+
byEntry.has(entry.name) || byEntry.set(entry.name, []);
|
|
286
|
+
byEntry.get(entry.name).push(name);
|
|
287
|
+
exportIndex.set(origin, byEntry);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const owners = new Map<string, string>();
|
|
292
|
+
|
|
293
|
+
for (const entry of entries) {
|
|
294
|
+
owners.has(entry.file) || owners.set(entry.file, entry.name);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const allModules = new Set([...closures.values()].flatMap((c) => [...c]));
|
|
298
|
+
|
|
299
|
+
for (const file of allModules) {
|
|
300
|
+
if (owners.has(file)) {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const prefix = `${file}#`;
|
|
305
|
+
const score = (entryName: string): number => {
|
|
306
|
+
let count = 0;
|
|
307
|
+
exportIndex.forEach((byEntry, origin) => origin.startsWith(prefix) && byEntry.has(entryName) && count++);
|
|
308
|
+
return count;
|
|
309
|
+
};
|
|
310
|
+
// prefer the entry that publicly exposes most of the module's bindings, then the smallest one
|
|
311
|
+
const candidates = entries
|
|
312
|
+
.filter((e) => closures.get(e.name).has(file))
|
|
313
|
+
.map((e) => ({ name: e.name, score: score(e.name), size: closures.get(e.name).size }))
|
|
314
|
+
.sort((a, b) => b.score - a.score || a.size - b.size || a.name.localeCompare(b.name));
|
|
315
|
+
owners.set(file, candidates[0].name);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const getPublicName = (target: string, name: string, owner: string): string | undefined => {
|
|
319
|
+
const byEntry = exportIndex.get(getOrigin(target, name));
|
|
320
|
+
const names = byEntry && byEntry.get(owner);
|
|
321
|
+
return names && (names.includes(name) ? name : names[0]);
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
// computes the rewrites for the module; a rewrite is only possible if all names are publicly available
|
|
325
|
+
const getRewrites = (file: string): SharedChunkRewrites => {
|
|
326
|
+
const info = parseModule(file);
|
|
327
|
+
const self = owners.get(file);
|
|
328
|
+
const rewrites: Array<SharedChunkRewrite> = [];
|
|
329
|
+
const problems: Array<string> = [];
|
|
330
|
+
|
|
331
|
+
if (!info.ok) {
|
|
332
|
+
return { rewrites, problems };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
for (const dep of info.deps) {
|
|
336
|
+
const owner = owners.get(dep.target);
|
|
337
|
+
|
|
338
|
+
if (!owner || owner === self) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const mapped: Array<string | undefined> = dep.names.map((n) =>
|
|
343
|
+
n === '*' ? undefined : getPublicName(dep.target, n, owner),
|
|
344
|
+
);
|
|
345
|
+
const missing = dep.names.filter((_, i) => !mapped[i]);
|
|
346
|
+
|
|
347
|
+
if (missing.length) {
|
|
348
|
+
problems.push(
|
|
349
|
+
`"${relative(root, file)}" requires ${missing.join(', ')} from "${relative(root, dep.target)}", which is not exposed by "${owner}"`,
|
|
350
|
+
);
|
|
351
|
+
} else {
|
|
352
|
+
rewrites.push({ dep, owner, mapped });
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return { rewrites, problems };
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// the rewrites must not introduce cycles between the shared entries (SystemJS modules)
|
|
360
|
+
const edges = new Map<string, Set<string>>();
|
|
361
|
+
|
|
362
|
+
for (const file of allModules) {
|
|
363
|
+
const from = owners.get(file);
|
|
364
|
+
|
|
365
|
+
for (const { owner } of getRewrites(file).rewrites) {
|
|
366
|
+
edges.has(from) || edges.set(from, new Set());
|
|
367
|
+
edges.get(from).add(owner);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const hasCycle = (() => {
|
|
372
|
+
const state = new Map<string, number>();
|
|
373
|
+
const visit = (n: string): boolean => {
|
|
374
|
+
if (state.get(n) === 1) {
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (state.get(n) === 2) {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
state.set(n, 1);
|
|
383
|
+
const cyclic = [...(edges.get(n) || [])].some(visit);
|
|
384
|
+
state.set(n, 2);
|
|
385
|
+
return cyclic;
|
|
386
|
+
};
|
|
387
|
+
return [...edges.keys()].some(visit);
|
|
388
|
+
})();
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
group,
|
|
392
|
+
enabled: !hasCycle,
|
|
393
|
+
reason: hasCycle
|
|
394
|
+
? `Sharing the private modules of "${group.packageName}" would introduce cyclic shared dependencies.`
|
|
395
|
+
: undefined,
|
|
396
|
+
owners,
|
|
397
|
+
getRewrites,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function getSharedChunkAnalysis(group: SharedChunkGroup): SharedChunkAnalysis {
|
|
402
|
+
const key = `${group.root}|${group.entries.map((e) => `${e.name}=${e.file}`).join(',')}`;
|
|
403
|
+
|
|
404
|
+
if (!analysisCache.has(key)) {
|
|
405
|
+
analysisCache.set(key, createAnalysis(group));
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return analysisCache.get(key)!;
|
|
409
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import type { Identifier, Literal } from 'acorn';
|
|
3
|
+
|
|
4
|
+
import { getSharedChunkAnalysis, type getSharedChunkGroups } from './SharedChunks';
|
|
5
|
+
|
|
6
|
+
type SharedChunkGroup = ReturnType<typeof getSharedChunkGroups>[number];
|
|
7
|
+
type SharedChunkRewrite = ReturnType<ReturnType<typeof getSharedChunkAnalysis>['getRewrites']>['rewrites'][number];
|
|
8
|
+
|
|
9
|
+
interface SharedChunksLoaderContext {
|
|
10
|
+
getOptions(): { groups?: Array<SharedChunkGroup> } | undefined;
|
|
11
|
+
resourcePath: string;
|
|
12
|
+
emitWarning(warning: Error): void;
|
|
13
|
+
callback(error: Error | null, content: string, sourceMap?: unknown): void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const warned = new Set<string>();
|
|
17
|
+
|
|
18
|
+
function binding(publicName: string | undefined, localName: string | undefined): string | undefined {
|
|
19
|
+
return publicName === localName ? publicName : `${publicName} as ${localName}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getLocalName(specifier: Identifier | Literal): string | undefined {
|
|
23
|
+
return specifier.type === 'Identifier' ? specifier.name : JSON.stringify(specifier.value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function render({ dep, owner, mapped }: SharedChunkRewrite): string {
|
|
27
|
+
const { node } = dep;
|
|
28
|
+
const request = JSON.stringify(owner);
|
|
29
|
+
|
|
30
|
+
if (node.type === 'ImportDeclaration') {
|
|
31
|
+
if (node.specifiers.length === 0) {
|
|
32
|
+
return `import ${request};`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const specs = node.specifiers.map((s, i) => binding(mapped[i], s.local.name));
|
|
36
|
+
return `import { ${specs.join(', ')} } from ${request};`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (node.type === 'ExportAllDeclaration') {
|
|
40
|
+
return `export * from ${request};`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const specs = node.specifiers.map((s, i) => binding(mapped[i], getLocalName(s.exported)));
|
|
44
|
+
return `export { ${specs.join(', ')} } from ${request};`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Rewrites relative imports of private modules (chunks) that are owned by another
|
|
49
|
+
* shared entry point of the same package to go through that entry point instead.
|
|
50
|
+
*/
|
|
51
|
+
export default function SharedChunksLoader(this: SharedChunksLoaderContext, source: string, map?: unknown): void {
|
|
52
|
+
const { groups = [] } = this.getOptions() || {};
|
|
53
|
+
const file = this.resourcePath;
|
|
54
|
+
const group = groups.find((g) => file.startsWith(g.root));
|
|
55
|
+
|
|
56
|
+
if (group) {
|
|
57
|
+
const analysis = getSharedChunkAnalysis(group);
|
|
58
|
+
|
|
59
|
+
if (analysis.enabled) {
|
|
60
|
+
const { rewrites, problems } = analysis.getRewrites(file);
|
|
61
|
+
problems.forEach((p) => this.emitWarning(new Error(`Cannot share private module: ${p}. It will be duplicated.`)));
|
|
62
|
+
|
|
63
|
+
if (rewrites.length > 0) {
|
|
64
|
+
// the analysis parsed the same file from disk; make sure we see the same content
|
|
65
|
+
const original = readFileSync(file, 'utf8');
|
|
66
|
+
|
|
67
|
+
if (original === source) {
|
|
68
|
+
let result = source;
|
|
69
|
+
|
|
70
|
+
for (const rewrite of [...rewrites].sort((a, b) => b.dep.node.start - a.dep.node.start)) {
|
|
71
|
+
const { start, end } = rewrite.dep.node;
|
|
72
|
+
result = result.slice(0, start) + render(rewrite) + result.slice(end);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return this.callback(null, result, map);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
} else if (!warned.has(analysis.reason)) {
|
|
79
|
+
warned.add(analysis.reason);
|
|
80
|
+
this.emitWarning(new Error(analysis.reason));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.callback(null, source, map);
|
|
85
|
+
}
|