-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathwebpack.config.js
More file actions
101 lines (93 loc) · 3.26 KB
/
webpack.config.js
File metadata and controls
101 lines (93 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-env es6 */
'use strict';
const { PackageJsonLookup, Import } = require('@rushstack/node-core-library');
const { PreserveDynamicRequireWebpackPlugin } = require('@rushstack/webpack-preserve-dynamic-require-plugin');
const {} = require('webpack');
module.exports = ({ webpack: { BannerPlugin } }) => {
const packageJson = PackageJsonLookup.loadOwnPackageJson(__dirname);
const externalDependencyNames = new Set(Object.keys(packageJson.dependencies || {}));
// Get all export specifiers from the sidecar .exports.json file generated by DeepImportsPlugin
const rushLibFolder = Import.resolvePackage({
baseFolderPath: __dirname,
packageName: '@microsoft/rush-lib',
useNodeJSResolver: true
});
const { moduleExports: exportSpecifiers } = require(`${rushLibFolder}/lib-commonjs/index.exports.json`);
// Assign named exports after the bundle to ensure they're properly exposed for ESM imports
const footerCodeForLibShim = exportSpecifiers
.map((name) => `exports.${name} = module.exports.${name};`)
.join('\n');
// Explicitly exclude @microsoft/rush-lib
externalDependencyNames.delete('@microsoft/rush-lib');
// Resolve rush-lib deep imports to the intermediate ESM source rather than the
// DeepImportsPlugin stubs (which load from the dist/ webpack bundle). This avoids
// embedding a nested webpack runtime that expects a separate commons.js chunk.
const rushLibLibAlias = `${rushLibFolder}/lib-intermediate-esm`;
return {
context: __dirname,
mode: 'development', // So the output isn't minified
devtool: 'source-map',
entry: {
// Using CommonJS due to access of module.parent
index: `${__dirname}/lib-intermediate-commonjs/index.js`,
loader: `${__dirname}/lib-intermediate-commonjs/loader.js`
},
output: {
path: `${__dirname}/lib-shim`,
filename: '[name].js',
chunkFilename: 'chunks/[name].js',
library: {
type: 'commonjs2'
}
},
optimization: {
flagIncludedChunks: true,
concatenateModules: true,
providedExports: true,
usedExports: true,
sideEffects: true,
removeAvailableModules: true,
minimize: false,
realContentHash: true,
innerGraph: true
},
target: 'node',
plugins: [
new BannerPlugin({
raw: true,
footer: true,
include: /index\.js$/,
banner: footerCodeForLibShim
}),
new PreserveDynamicRequireWebpackPlugin()
],
resolve: {
alias: {
'@microsoft/rush-lib/lib': rushLibLibAlias
}
},
externals: [
({ request }, callback) => {
let packageName;
let firstSlashIndex = request.indexOf('/');
if (firstSlashIndex === -1) {
packageName = request;
} else if (request.startsWith('@')) {
let secondSlash = request.indexOf('/', firstSlashIndex + 1);
if (secondSlash === -1) {
packageName = request;
} else {
packageName = request.substring(0, secondSlash);
}
} else {
packageName = request.substring(0, firstSlashIndex);
}
if (externalDependencyNames.has(packageName)) {
callback(null, `commonjs ${request}`);
} else {
callback();
}
}
]
};
};