This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
142 lines (117 loc) · 3.54 KB
/
webpack.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// This webpack config is used to transpile src to dist, compile externals,
// compile executables, etc
const { EnvironmentPlugin, DefinePlugin, BannerPlugin } = require('webpack');
const { verifyEnvironment } = require('./expect-env');
const nodeExternals = require('webpack-node-externals');
const debug = require('debug')(`${require('./package.json').name}:webpack-config`);
let env = {};
try {
require('fs').accessSync('.env');
env = require('dotenv').config().parsed;
debug('new env vars: %O', env);
} catch (e) {
debug(`env support disabled; reason: ${e}`);
}
verifyEnvironment();
const envPlugins = [
// ? Load our .env results as the defaults (overridden by process.env)
new EnvironmentPlugin({ ...env, ...process.env }),
// ? Create shim process.env for undefined vars (per my tastes!)
new DefinePlugin({ 'process.env': '{}' })
];
const externals = [
nodeExternals(),
({ request }, cb) =>
// ? Externalize all .json imports (required as commonjs modules)
/\.json$/.test(request) ? cb(null, `commonjs ${request}`) : cb()
];
const libConfig = {
name: 'lib',
mode: 'production',
target: 'node',
node: false,
entry: `${__dirname}/src/index.ts`,
output: {
filename: 'index.js',
path: `${__dirname}/dist`,
// ! ▼ Only required for libraries
// ! ▼ Note: ESM outputs are handled by Babel ONLY!
libraryTarget: 'commonjs2'
},
externals,
externalsPresets: { node: true },
stats: {
orphanModules: true,
providedExports: true,
usedExports: true
},
resolve: { extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'] },
module: {
rules: [{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ }]
},
optimization: { usedExports: true },
ignoreWarnings: [/critical dependency:/i],
plugins: [...envPlugins]
};
const externalsConfig = {
name: 'externals',
mode: 'production',
target: 'node',
node: false,
entry: {
dummy: `${__dirname}/external-scripts/dummy.ts`
},
output: {
filename: '[name].js',
path: `${__dirname}/external-scripts/bin`
},
externals,
externalsPresets: { node: true },
stats: {
orphanModules: true,
providedExports: true,
usedExports: true
},
resolve: { extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'] },
module: {
rules: [{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ }]
},
optimization: { usedExports: true },
ignoreWarnings: [/critical dependency:/i],
plugins: [
...envPlugins,
// * ▼ For non-bundled externals, make entry file executable w/ shebang
new BannerPlugin({ banner: '#!/usr/bin/env node', raw: true, entryOnly: true })
]
};
/*const cliConfig = {
name: 'cli',
mode: 'production',
target: 'node',
node: false,
entry: `${__dirname}/src/cli.ts`,
output: {
filename: 'cli.js',
path: `${__dirname}/dist`
},
externals,
externalsPresets: { node: true },
stats: {
orphanModules: true,
providedExports: true,
usedExports: true
},
resolve: { extensions: ['.ts', '.wasm', '.mjs', '.cjs', '.js', '.json'] },
module: {
rules: [{ test: /\.(ts|js)x?$/, loader: 'babel-loader', exclude: /node_modules/ }]
},
optimization: { usedExports: true },
ignoreWarnings: [/critical dependency:/i],
plugins: [
...envPlugins,
// * ▼ For bundled CLI applications, make entry file executable w/ shebang
new BannerPlugin({ banner: '#!/usr/bin/env node', raw: true, entryOnly: true })
]
};*/
module.exports = [libConfig, externalsConfig /*, cliConfig*/];
debug('exports: %O', module.exports);