-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.dist.config.js
93 lines (86 loc) · 2.54 KB
/
webpack.dist.config.js
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
const fs = require('fs');
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const buffer = require.resolve("buffer");
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 be 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;
module.exports = {
mode: "development", // distribute it without minification
target: "node",
entry:
isSingleModule
? (
// Classic export of the /src/index.ts
[
path.resolve(__dirname, 'src/index.ts')
]
)
: (
// Multiple module exports of the /src/<Module name>/index.ts
moduleNames
.reduce((acc, entry) => {
acc[entry] = `./src/${entry}`;
return acc;
}, {})
),
externals: nodeExternals(),
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:
isSingleModule
? {
// Classic export of the /src/index.ts
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
filename: 'index.js',
library: package_.name,
libraryTarget: 'umd',
umdNamedDefine: true,
clean: true,
}
: {
// Multiple module exports of the /src/<Module name>/index.ts
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
filename: '[name]/index.js',
library: package_.name,
libraryTarget: 'umd',
umdNamedDefine: true,
clean: true,
},
resolve: {
alias: {},
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js", ".jsx"],
fallback: {
stream: buffer,
}
},
module: {
rules: loaders.module.rules,
},
plugins: plugins.plugins,
};