-
Notifications
You must be signed in to change notification settings - Fork 30
/
webpack.config.js
193 lines (179 loc) · 4.88 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var StatsPlugin = require("stats-webpack-plugin");
var StringReplacePlugin = require("string-replace-webpack-plugin");
var CopyWebpackPlugin = require("copy-webpack-plugin");
var webpack = require("webpack");
const version = require("./package.json").version;
const date = new Date().toISOString().replace(/:\d+\.\d+Z$/, "Z");
const copyright = `/*!
* CLDR JavaScript Library v@VERSION @DATE MIT license © Rafael Xavier
* http://git.io/h4lmVg
*/`
.replace(/@VERSION/g, version)
.replace(/@DATE/g, date);
const banner = {
banner: copyright,
raw: true
};
// If you enable this, webpack will create a
// profile_*.json file for each bundle.
// This file can then be uploaded on
// https://webpack.github.io/analyse/ for analysis.
const enable_profile = false;
const optimization = {
// You can enable this to debug webpack's output
//minimize: false
};
// This is used in each bundle that isn't
// cldr's core to tell it how to include it
const externalsConfig = {
commonjs2: "../cldr",
commonjs: "../cldr",
amd: "../cldr",
root: "Cldr"
};
const externals = {
"../../../core": externalsConfig,
"../../core": externalsConfig,
"../core": externalsConfig,
"./core": externalsConfig
};
const outputDefaults = {
library: "Cldr",
libraryTarget: "umd",
libraryExport: "default",
globalObject: "typeof self !== 'undefined' ? self : this"
};
function stats(file) {
if (!enable_profile) {
return false;
}
return new StatsPlugin(file, {
chunkModules: true
});
}
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function createReplacements(substitutions) {
const replacements = Object.keys(substitutions).map(key => {
return {
pattern: new RegExp(`import (.*) from "${escapeRegExp(key)}";`, "gi"),
replacement: (match, p1) => `import Cldr_${p1} from "${substitutions[key]}"; const ${p1} = Cldr_${p1}._${p1};`
};
});
return StringReplacePlugin.replace({ replacements });
}
module.exports = [
// Cldr's Core file
{
entry: "./src/core.js",
output: {
...outputDefaults,
filename: "cldr.js"
},
plugins: [
new webpack.BannerPlugin(banner),
new CopyWebpackPlugin([
{
from: "src/build/node_main.js",
to: "node_main.js",
transform(content) {
return banner.banner + "\n" + content;
}
}
]),
new StringReplacePlugin(),
stats("profile_core.json")
].filter(Boolean),
optimization
},
// Cldr's Event extension
// Excluding Cldr core
{
entry: "./src/event.js",
output: {
...outputDefaults,
filename: "cldr/event.js"
},
module: {
rules: [
{
test: /\.js$/,
// Build optimization hack to avoid duplicating functions across modules.
loader: createReplacements({
"./common/validate/presence": "./core",
"./common/validate/type": "./core",
"../type": "../../core",
"./path/normalize": "./core"
})
}
]
},
plugins: [
new webpack.BannerPlugin(banner),
new StringReplacePlugin(),
stats("profile_event.json")
].filter(Boolean),
optimization,
externals
},
// Cldr's Supplemental extension
// Excluding Cldr core
{
entry: "./src/supplemental.js",
output: {
...outputDefaults,
filename: "cldr/supplemental.js"
},
module: {
rules: [
{
test: /\.js$/,
// Build optimization hack to avoid duplicating functions across modules.
loader: createReplacements({
"../util/always_array": "../core"
})
}
]
},
plugins: [
new webpack.BannerPlugin(banner),
new StringReplacePlugin(),
stats("profile_supplemental.json")
].filter(Boolean),
optimization,
externals
},
// Cldr's Unresolved extension
// Excluding Cldr core
{
entry: "./src/unresolved.js",
output: {
...outputDefaults,
filename: "cldr/unresolved.js"
},
module: {
rules: [
{
test: /\.js$/,
// Build optimization hack to avoid duplicating functions across modules.
loader: createReplacements({
"../path/normalize": "../core",
"../util/json/merge": "../core",
"../resource/get": "../core",
"./core/load": "./core",
"../common/validate/presence": "../core",
"./common/validate/type/path": "./core"
})
}
]
},
plugins: [
new webpack.BannerPlugin(banner),
new StringReplacePlugin(),
stats("profile_unresolved.json")
].filter(Boolean),
optimization,
externals
}
];