Repository navigation
Expand file tree
/
Copy pathwebpack.dist.config.js
More file actions
123 lines (114 loc) · 3.74 KB
/
Copy pathwebpack.dist.config.js
File metadata and controls
123 lines (114 loc) · 3.74 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require("fs");
const path = require("path");
const nodeExternals = require("webpack-node-externals");
const isSingleModule =
fs.existsSync('./src/index.ts') ||
fs.existsSync('./src/index.tsx');
const package_ = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const loaders = require('./webpack.loaders.js');
const plugins = require('./webpack.plugins.js');
/**
* Exclude src/? folders when not in single mode
* @type {string[]}
*/
const EXCLUDE_SRC_FOLDERS = [
"@types",
// Other folders that won't by built by Webpack might be listed here
]
const getModuleNames =
root =>
fs.readdirSync(root, {withFileTypes: true})
.filter(dirent => dirent.isDirectory())
.filter(dirent => !EXCLUDE_SRC_FOLDERS.includes(dirent.name))
.map(dirent => dirent.name);
const moduleNames = getModuleNames('./src');
process.traceDeprecation = true;
const entry =
isSingleModule
? (
// Classic export of the /src/index.tsx
[
path.resolve(__dirname, 'src/index.tsx')
]
)
: (
// Multiple module exports of the /src/<Module name>/index.tsx
moduleNames
.reduce((acc, entry) => {
acc[entry] = `./src/${entry}`;
return acc;
}, {})
);
/**
* The package is distributed in two formats, built by two webpack configs:
* - `umd`: dist/index.js — loadable via `require()` (CommonJS), AMD or a <script> tag
* - `esm`: dist/index.mjs — native `import`, tree-shakeable by consumer bundlers
* The `exports` map of the package.json routes the consumers to the right file.
*/
const createConfig = format => ({
mode: "development", // distribute it without minification
// Note: Not "web", to allow the components to be used from the server package!
target:
format === 'esm'
? ['node', 'es2022'] // es2022 environment is needed for module output
: "node",
entry,
externals: nodeExternals(
format === 'esm'
? {importType: 'module'} // reference externals with `import` instead of `require`
: undefined,
),
experiments: {
outputModule: format === 'esm',
},
optimization: {
// help: https://webpack.js.org/guides/tree-shaking/
usedExports: true, // true to remove the dead code,
},
devtool: "source-map", // help: https://webpack.js.org/configuration/devtool/
// Every folder of ./src is a standalone exported module
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
filename:
(isSingleModule
? '' // Classic export of the /src/index.tsx
: '[name]/') // Multiple module exports of the /src/<Module name>/index.tsx
+ (format === 'esm' ? 'index.mjs' : 'index.js'),
library:
format === 'esm'
? {type: 'module'}
: {
name: package_.name,
type: 'umd',
umdNamedDefine: true,
},
// No `clean` here: both configs emit into ./dist in parallel and would
// delete each other's output; the build scripts rimraf ./dist instead.
},
resolve: {
alias: {},
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js", ".jsx"],
fallback: {
// Remove Node.js modules, use browser alternatives or undefined
"fs": false,
"path": false,
"buffer": false, // Remove - conflicts in browser
"stream": false,
"crypto": false,
"util": false,
// Add if needed:
// "buffer": require.resolve("buffer/"),
}
},
module: {
rules: loaders.module.rules,
},
// The d.ts build and the circular-dependency check need to run only once,
// so the plugins are applied to the UMD config only
plugins: format === 'umd' ? plugins.plugins : [],
});
module.exports = [
createConfig('umd'),
createConfig('esm'),
];