-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
webpack.config.js
143 lines (129 loc) · 4.19 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
143
const { resolve } = require('path');
const webpack = require('webpack');
const { getIfUtils, removeEmpty } = require('webpack-config-utils');
const packageJSON = require('./package.json');
const packageName = normalizePackageName(packageJSON.name);
const LIB_NAME = pascalCase(packageName);
const PATHS = {
entryPoint: resolve(__dirname, 'index.ts'),
umd: resolve(__dirname, 'dist/umd'),
fesm: resolve(__dirname, 'dist/lib-fesm'),
};
// https://webpack.js.org/configuration/configuration-types/#exporting-a-function-to-use-env
// this is equal to 'webpack --env=dev'
const DEFAULT_ENV = 'dev';
const EXTERNALS = ['jest', 'jest-diff', 'jest-matcher-utils', /^expect\/.*$/, /^rxjs.*$/];
const RULES = {
ts: {
test: /\.tsx?$/,
include: /./,
use: [
{
loader: 'ts-loader',
options: {
// we don't want any declaration file in the bundles
// folder since it wouldn't be of any use ans the source
// map already include everything for debugging
// This cannot be set because -> Option 'declarationDir' cannot be specified without specifying option 'declaration'.
// declaration: false,
},
},
],
},
tsNext: {
test: /\.tsx?$/,
include: /./,
use: [
{
loader: 'ts-loader',
options: {
compilerOptions: {
target: 'es2020',
},
},
},
],
},
};
const config = (env = DEFAULT_ENV) => {
const { ifProd } = getIfUtils(env);
const PLUGINS = removeEmpty([
// enable scope hoisting
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.LoaderOptionsPlugin({
debug: false,
minimize: true,
}),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: ifProd('"production"', '"development"') },
}),
]);
const UMDConfig = {
mode: ifProd('production', 'development'),
// These are the entry point of our library. We tell webpack to use
// the name we assign later, when creating the bundle. We also use
// the name to filter the second entry point for applying code
// minification via UglifyJS
entry: {
[ifProd(`${packageName}.min`, packageName)]: [PATHS.entryPoint],
},
// The output defines how and where we want the bundles. The special
// value `[name]` in `filename` tell Webpack to use the name we defined above.
// We target a UMD and name it MyLib. When including the bundle in the browser
// it will be accessible at `window.MyLib`
output: {
path: PATHS.umd,
filename: '[name].js',
libraryTarget: 'umd',
library: LIB_NAME,
globalObject: `(typeof self !== 'undefined' ? self : this)`,
// libraryExport: LIB_NAME,
// will name the AMD module of the UMD build. Otherwise an anonymous define is used.
umdNamedDefine: true,
},
// Add resolve for `tsx` and `ts` files, otherwise Webpack would
// only look for common JavaScript file extension (.js)
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
// add here all 3rd party libraries that you will use as peerDependncies
// https://webpack.js.org/guides/author-libraries/#add-externals
externals: EXTERNALS,
// Activate source maps for the bundles in order to preserve the original
// source when the user debugs the application
devtool: 'source-map',
plugins: PLUGINS,
module: {
rules: [RULES.ts],
},
};
const FESMconfig = Object.assign({}, UMDConfig, {
mode: ifProd('production', 'development'),
entry: {
[ifProd('index.min', 'index')]: [PATHS.entryPoint],
},
output: {
path: PATHS.fesm,
filename: UMDConfig.output.filename,
},
module: {
rules: [RULES.tsNext],
},
});
return [UMDConfig, FESMconfig];
};
module.exports = config;
// helpers
function dashToCamelCase(myStr) {
return myStr.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
}
function toUpperCase(myStr) {
return `${myStr.charAt(0).toUpperCase()}${myStr.substr(1)}`;
}
function pascalCase(myStr) {
return toUpperCase(dashToCamelCase(myStr));
}
function normalizePackageName(rawPackageName) {
const scopeEnd = rawPackageName.indexOf('/') + 1;
return rawPackageName.substring(scopeEnd);
}