-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
163 lines (148 loc) · 4.69 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const path = require('path');
const fs = require('fs-extra');
const tsCheckerPlugin = new ForkTsCheckerWebpackPlugin();
module.exports = (_, argv) => {
let type = 'development';
if (argv.production) {
type = 'production';
} else if (argv.demo) {
type = 'demo';
} else if (argv.test) {
type = 'test';
}
copyAssets(type);
const base = {
mode: 'production',
module: {
rules: [
{
test: /\.(ts|js)$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: type === 'development',
configFile: 'tsconfig.json',
},
},
],
},
// For CSS modules
{
test: /\.module\.css$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName:
type === 'production'
? 'mapgl_[hash:base64]'
: '[path][name]__[local]',
},
},
},
],
},
],
},
resolve: {
symlinks: false,
extensions: ['.ts', '.js'],
},
};
const library = {
...base,
entry: './src/index.ts',
output: {
filename: 'ruler.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
libraryTarget: 'umd',
// webpack for amd uses window, which may not be in the nodejs environment.
// probably corrected in 5 webpack, then it will be possible to delete globalObject
// https://github.com/webpack/webpack/pull/8625
globalObject: "typeof self !== 'undefined' ? self : this",
},
};
const demo = {
...base,
entry: './demo/index.ts',
output: {
filename: 'demo.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
};
const test = {
...base,
mode: 'development',
entry: {
test: './test/index.ts',
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist/test'),
publicPath: '/',
},
devtool: 'eval-source-map',
plugins: [tsCheckerPlugin],
stats: 'errors-only',
};
const devConfig = {
mode: 'development',
devtool: 'eval-source-map',
plugins: [tsCheckerPlugin],
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
host: 'localhost',
port: 3000,
stats: {
modules: false,
hash: false,
version: false,
assets: false,
entrypoints: false,
builtAt: false,
// https://github.com/TypeStrong/ts-loader#transpileonly-boolean-defaultfalse
warningsFilter: /export .* was not found in /,
},
disableHostCheck: true,
clientLogLevel: 'error',
// it is used so that the script can be pulled from another domain, for example, in visual-comparator
headers: {
'Access-Control-Allow-Origin': '*',
},
},
};
switch (type) {
case 'production':
return [library];
case 'demo':
return [library, demo];
case 'test':
return test;
case 'development':
return [
{ ...library, ...devConfig },
{ ...demo, ...devConfig },
];
}
};
function copyAssets(envType) {
if (envType === 'production') {
return;
}
const root = __dirname;
const dist = path.join(root, 'dist');
if (envType === 'demo' || envType === 'development') {
fs.copySync(path.join(root, 'demo', 'index.html'), path.join(dist, 'index.html'));
}
if (envType === 'test') {
fs.copySync(path.join(root, 'test', 'index.html'), path.join(dist, 'test/index.html'));
}
console.log('+ HTML');
}