-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebpack.js
216 lines (197 loc) · 6.29 KB
/
webpack.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const path = require('path'),
webpack = require('webpack'),
DIST_PATH = path.resolve(__dirname, 'dist'),
UglifyJSPlugin = require('uglifyjs-webpack-plugin'),
HtmlWebpackPlugin = require('html-webpack-plugin'),
WorkerRunnerPlugin = require('./WorkerRunnerPlugin')
require('dotenv').config({ path: '.env' });
let FRAME_URL = process.env.FRAME_URL || 'http://localhost:9090'
const NODE_ENV = process.env.NODE_ENV
function resolvePath(dir) {
return path.resolve.apply(path, [__dirname].concat(dir.split('/')));
}
function replaceDependency(regex, replacement) {
return new webpack.NormalModuleReplacementPlugin(regex, resolvePath(replacement));
}
function stubDependency(regex) {
return replaceDependency(regex, 'stubs/stub.js')
}
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS,
RPC_URL = process.env.RPC_URL;
function webpackConfig (entry, hash = true) {
const config = {
entry: entry,
devtool: 'source-map',
output: {
filename: (NODE_ENV === 'production' || NODE_ENV === 'staging') && hash ? '[name].[hash].js' : '[name].js',
path: DIST_PATH
},
plugins: [
new webpack.DefinePlugin({
'window.RPC_URL': JSON.stringify(RPC_URL),
'self.CONTRACT_ADDRESS': JSON.stringify(CONTRACT_ADDRESS),
'process.env': {
'NODE_ENV': JSON.stringify(NODE_ENV || 'development'), // This has effect on the react lib size,
'DEBUG': NODE_ENV !== 'production',
'FRAME_URL': JSON.stringify(FRAME_URL)
}
})
],
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json']
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: [/node_modules/],
loaders: ['ts-loader']
},
{
enforce: 'pre',
test: /\.js$/,
exclude: [/node_modules/],
loader: 'source-map-loader'
},
{
test: /\.s?css$/i,
exclude: [/node_modules/],
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 1,
modules: true,
camelCase: true,
localIdentName: '[name]_[local]_[hash:base64:5]',
minimize: false
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => ([
require('postcss-import')(),
// Following CSS Nesting Module Level 3: http://tabatkins.github.io/specs/css-nesting/
require('postcss-nesting')(),
//https://github.com/ai/browserslist
require('autoprefixer')({
browsers: ['last 2 versions', 'ie >= 9']
})
])
}
}
]
},
{
test: /\.css$/i,
exclude: [resolvePath('vynos'), resolvePath('harness')],
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 1,
minimize: true
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => ([
require('postcss-import')({
//If you are using postcss-import v8.2.0 & postcss-loader v1.0.0 or later, this is unnecessary.
//addDependencyTo: webpack // Must be first item in list
}),
require('postcss-nesting')(), // Following CSS Nesting Module Level 3: http://tabatkins.github.io/specs/css-nesting/
require('autoprefixer')({
browsers: ['last 2 versions', 'ie >= 9'] //https://github.com/ai/browserslist
})
])
}
}
]
},
{
test: /\.(eot|woff|woff2|svg|ttf|png|otf)([\?]?.*)$/,
loader: 'file-loader'
}
]
},
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
module: 'empty'
}
};
if (NODE_ENV === 'production') {
config.plugins.push(new UglifyJSPlugin({
parallel: true,
uglifyOptions: {
output: {
comments: false,
beautify: false
}
}
}))
}
return config
}
const WORKER_FILE_REGEX = /^worker(\.)?([\w\d]+)?\.js$/i;
const COMMONS_FILE_REGEX = /^commons(\.)?([\w\d]+)?\.js$/i;
const FRAME_FILE_REGEX = /^frame(\.)?([\w\d]+)?\.js$/i;
function vynosConfig(entry) {
const config = webpackConfig(entry);
config.plugins = config.plugins.concat([
new webpack.IgnorePlugin(/^(pg|mongodb)$/), // ignore pg and mongo since we're using nedb
stubDependency(/wordlists\/((chinese_(.*))|french|italian|japanese|korean|spanish)\.json$/),
stubDependency(/^(request|xhr)$/),
replaceDependency(/^unorm$/, 'stubs/unorm.js'),
replaceDependency(/Unidirectional\.json$/, 'vendor/@machinomy/contracts/dist/build/contracts/Unidirectional.json'),
new webpack.HashedModuleIdsPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'commons',
filename: NODE_ENV === 'production' || NODE_ENV === 'staging' ? 'commons.[hash].js' : 'commons.js'
}),
new HtmlWebpackPlugin({
template: resolvePath('vynos/frame.html'),
filename: 'frame.html',
excludeChunks: ['worker']
}),
new WorkerRunnerPlugin({
contentScripts: [
COMMONS_FILE_REGEX,
WORKER_FILE_REGEX
],
filename: NODE_ENV === 'production' || NODE_ENV === 'staging' ? 'workerRunner.[hash].js' : 'workerRunner.js',
replaceFilename: 'workerRunner.js',
replacer: (filename) => filename.match(FRAME_FILE_REGEX)
})
]);
return config
}
const VYNOS_BACKGROUND = vynosConfig({
frame: [
resolvePath('vynos/frame.ts')
],
worker: [
resolvePath('vynos/worker.ts')
]
});
const VYNOS_SDK = webpackConfig({
vynos: resolvePath('vynos/vynos.ts')
}, false);
const HARNESS = webpackConfig({
harness: resolvePath('harness/harness.ts')
}, false);
module.exports.HARNESS = HARNESS;
module.exports.VYNOS_BACKGROUND = VYNOS_BACKGROUND;
module.exports.VYNOS_SDK = VYNOS_SDK;