-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebpack.config.js
151 lines (144 loc) · 4.05 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
const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const SRC_DIR = path.join(__dirname, 'src');
// At _least_ these tokens need to be preserved in order for chevrotain to
// work correctly after minification. See here: https://sap.github.io/chevrotain/docs/FAQ.html#MINIFIED.
// The ones that are commented out may be needed at some point, so may as
// well leave them there for people who look here in the future.
const reserved = [
'BaseSparqlParser',
'W3SpecSparqlParser',
'StardogSparqlParser',
'SrsParser',
'SmsParser',
'TurtleParser',
'TrigParser',
'BaseGraphQlParser',
'StandardGraphQlParser',
'StardogGraphQlParser',
'Parser',
// 'srsTokenTypes',
// 'srsTokenMap',
// 'multiModeLexerDefinition',
// 'turtleTokenTypes',
// 'turtleTokenMap',
// 'smsTokenTypes',
// 'smsTokenMap',
// 'sparqlTokenTypes',
// 'pathTokens',
// 'baseTokens',
// 'sparqlTokenMap',
// 'terminals',
// 'keywords',
// 'sparqlKeywords',
// 'sparqlTerminals',
];
const individualEntryData = [
'graphql',
'shacl',
'sms',
'sparql',
'srs',
'turtle',
'trig',
].reduce(
(entries, languageId) => ({
...entries,
[languageId]: path.join(SRC_DIR, languageId, 'index.ts'),
}),
{}
);
const coreWebpackConfig = {
mode: 'production',
module: {
rules: [
{
test: /\.ts$/,
enforce: 'pre',
loader: 'tslint-loader',
exclude: [/node_modules/],
},
{
test: /\.ts$/,
use: [
{
loader: 'ts-loader',
options: {
// Use ts-loader only for transpilation; type checking is handled
// by ForkTsCheckerWebpackPlugin
transpileOnly: true,
},
},
],
exclude: [/node_modules/],
},
],
},
resolve: {
modules: [SRC_DIR, 'node_modules'],
extensions: ['.ts', '.js'],
},
plugins: [
new ForkTsCheckerWebpackPlugin({
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
watch: SRC_DIR,
}),
],
devtool: 'source-map',
optimization: {
minimizer: [
new TerserPlugin({
sourceMap: true,
terserOptions: {
// Chevrotain does not cooperate with webpack mangling (see here: https://sap.github.io/chevrotain/docs/FAQ.html#MINIFIED).
mangle: {
reserved,
},
},
}),
],
},
};
const nonBrowserWebpackConfig = {
...coreWebpackConfig,
entry: {
// full bundle:
millan: path.join(SRC_DIR, 'index.ts'),
// individual bundles for importing when desired:
...individualEntryData,
},
output: {
path: path.join(__dirname, 'dist'),
filename: (chunkData) =>
chunkData.chunk.name === 'millan'
? 'millan.js'
: path.join('standalone', 'millan.[name].js'),
library: 'millan',
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: "(typeof self !== 'undefined' ? self : this)", // https://github.com/webpack/webpack/issues/6525
},
};
const browserWebpackConfig = {
...coreWebpackConfig,
entry: individualEntryData, // main bundle generated by non-browser build is aleady setup for browser since it's UMD -- no need to generate twice
output: {
path: path.join(__dirname, 'dist', 'browser'), // output everything to `browser` directory
filename: (chunkData) =>
chunkData.chunk.name === 'millan' ? 'millan.js' : 'millan.[name].js',
chunkFilename: 'millan.[name].js',
// Array `library` ensures that separate bundles are propertly namespaced; see example: https://github.com/webpack/webpack/tree/master/examples/multi-part-library
library: ['millan', '[name]'],
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: "(typeof self !== 'undefined' ? self : this)", // https://github.com/webpack/webpack/issues/6525
},
optimization: {
...coreWebpackConfig.optimization,
splitChunks: {
chunks: 'all',
},
},
};
module.exports = [nonBrowserWebpackConfig, browserWebpackConfig];