forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.js
373 lines (355 loc) · 11 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
var _ = require('lodash');
var webpack = require('webpack');
var path = require('path');
var LiveReloadPlugin = require('webpack-livereload-plugin');
var envConstants = require('./envConstants');
var WebpackNotifierPlugin = require('webpack-notifier');
// Certain packages ship in ES6 and need to be transpiled for our purposes -
// especially for tests, which run on PhantomJS with _zero_ ES6 support.
var toTranspileWithinNodeModules = [
// All of our @cdo-aliased files should get transpiled as they are our own
// source files.
path.resolve(__dirname, 'node_modules', '@cdo'),
// playground-io ships in ES6 as of 0.3.0
path.resolve(__dirname, 'node_modules', 'playground-io'),
path.resolve(__dirname, 'node_modules', 'chai-as-promised'),
path.resolve(__dirname, 'node_modules', 'enzyme-wait'),
path.resolve(__dirname, 'node_modules', 'json-parse-better-errors'),
path.resolve(__dirname, 'node_modules', '@code-dot-org', 'snack-sdk'),
// parse5 ships in ES6: https://github.com/inikulin/parse5/issues/263#issuecomment-410745073
path.resolve(__dirname, 'node_modules', 'parse5')
];
const scssIncludePath = path.resolve(__dirname, '..', 'shared', 'css');
// Our base config, on which other configs are derived
var baseConfig = {
resolve: {
extensions: ['.js', '.jsx'],
alias: {
'@cdo/locale': path.resolve(
__dirname,
'src',
'util',
'locale-do-not-import.js'
),
'@cdo/netsim/locale': path.resolve(
__dirname,
'src',
'netsim',
'locale-do-not-import.js'
),
'@cdo/applab/locale': path.resolve(
__dirname,
'src',
'applab',
'locale-do-not-import.js'
),
'@cdo/gamelab/locale': path.resolve(
__dirname,
'src',
'gamelab',
'locale-do-not-import.js'
),
'@cdo/weblab/locale': path.resolve(
__dirname,
'src',
'weblab',
'locale-do-not-import.js'
),
'@cdo/tutorialExplorer/locale': path.resolve(
__dirname,
'src',
'tutorialExplorer',
'locale-do-not-import.js'
),
'@cdo/regionalPartnerSearch/locale': path.resolve(
__dirname,
'src',
'regionalPartnerSearch',
'locale-do-not-import.js'
),
'@cdo/regionalPartnerMiniContact/locale': path.resolve(
__dirname,
'src',
'regionalPartnerMiniContact',
'locale-do-not-import.js'
),
'@cdo/apps': path.resolve(__dirname, 'src'),
'@cdo/static': path.resolve(__dirname, 'static'),
repl: path.resolve(__dirname, 'src/noop')
}
},
module: {
rules: [
{test: /\.exported_json$/, loader: 'raw-loader'},
{test: /\.ejs$/, loader: 'ejs-webpack-loader'},
{test: /\.css$/, loader: 'style-loader!css-loader'},
{
test: /\.scss$/,
loader: `style-loader!css-loader!sass-loader?includePaths=${scssIncludePath}`
},
{test: /\.interpreted.js$/, loader: 'raw-loader'},
{test: /\.exported_js$/, loader: 'raw-loader'},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
include: [
path.resolve(__dirname, 'static'),
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'test'),
path.resolve(`${__dirname}/../dashboard/app/assets/`, 'images')
],
// note that in the name template given below, a dash prefixing
// the hash is explicitly avoided. If rails tries to serve
// this file when asset digests are turned off, it will return a
// 404 because it thinks the hash is a digest and it won't
// be able to find the file without the hash. :( :(
loader: 'url-loader?limit=1024&name=[name]wp[hash].[ext]'
},
{
test: /\.jsx?$/,
enforce: 'pre',
include: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'test')
].concat(toTranspileWithinNodeModules),
exclude: [path.resolve(__dirname, 'src', 'lodash.js')],
loader: 'babel-loader',
query: {
cacheDirectory: path.resolve(__dirname, '.babel-cache'),
compact: false
}
}
],
noParse: [/html2canvas/]
}
};
if (envConstants.HOT) {
baseConfig.module.loaders.push({
test: /\.jsx?$/,
loader: 'react-hot-loader',
include: [path.resolve(__dirname, 'src')]
});
}
// modify baseConfig's preLoaders if looking for code coverage info
if (envConstants.COVERAGE) {
baseConfig.module.rules.splice(
-1,
1,
{
test: /\.jsx?$/,
enforce: 'pre',
include: [path.resolve(__dirname, 'test')].concat(
toTranspileWithinNodeModules
),
loader: 'babel-loader',
query: {
cacheDirectory: true,
compact: false
}
},
{
test: /\.jsx?$/,
enforce: 'pre',
loader: 'babel-istanbul-loader',
include: path.resolve(__dirname, 'src'),
exclude: [
path.resolve(__dirname, 'src', 'lodash.js'),
// we need to turn off coverage for this file
// because we have tests that actually make assertions
// about the contents of the compiled version of this file :(
path.resolve(__dirname, 'src', 'flappy', 'levels.js')
],
query: {
cacheDirectory: true,
compact: false
}
}
);
}
var devtool = process.env.DEV ? 'cheap-inline-source-map' : 'inline-source-map';
var storybookConfig = _.extend({}, baseConfig, {
devtool: devtool,
resolve: _.extend({}, baseConfig.resolve, {
alias: _.extend({}, baseConfig.resolve.alias, {
'@cdo/apps/lib/util/firehose': path.resolve(__dirname, 'test', 'util')
})
}),
externals: {
blockly: 'this Blockly'
},
plugins: [
new webpack.ProvidePlugin({React: 'react'}),
new webpack.DefinePlugin({
IN_UNIT_TEST: JSON.stringify(false),
IN_STORYBOOK: JSON.stringify(true),
'process.env.mocha_entry': JSON.stringify(process.env.mocha_entry),
'process.env.NODE_ENV': JSON.stringify(
envConstants.NODE_ENV || 'development'
),
PISKEL_DEVELOPMENT_MODE: JSON.stringify(false)
}),
new webpack.IgnorePlugin(/^serialport$/)
]
});
// config for our test runner
var karmaConfig = _.extend({}, baseConfig, {
devtool: devtool,
resolve: _.extend({}, baseConfig.resolve, {
alias: _.extend({}, baseConfig.resolve.alias, {
'@cdo/locale': path.resolve(
__dirname,
'test',
'util',
'locale-do-not-import.js'
),
'@cdo/netsim/locale': path.resolve(
__dirname,
'test',
'util',
'netsim',
'locale-do-not-import.js'
),
'@cdo/applab/locale': path.resolve(
__dirname,
'test',
'util',
'applab',
'locale-do-not-import.js'
),
'@cdo/gamelab/locale': path.resolve(
__dirname,
'test',
'util',
'gamelab',
'locale-do-not-import.js'
),
'@cdo/weblab/locale': path.resolve(
__dirname,
'test',
'util',
'weblab',
'locale-do-not-import.js'
),
'@cdo/tutorialExplorer/locale': path.resolve(
__dirname,
'test',
'util',
'tutorialExplorer',
'locale-do-not-import.js'
),
firebase: path.resolve(__dirname, 'test', 'util', 'MockFirebase.js'),
// Use mock-firmata to unit test playground-io maker components
firmata: 'mock-firmata/mock-firmata',
'chrome-serialport': path.resolve(
__dirname,
'test',
'unit',
'lib',
'kits',
'maker',
'StubChromeSerialPort.js'
)
})
}),
externals: {
blockly: 'this Blockly',
// The below are necessary for enzyme to work.
// See https://github.com/airbnb/enzyme/blob/master/docs/guides/webpack.md
cheerio: 'window',
'react/addons': true,
'react/lib/ExecutionEnvironment': true,
'react/lib/ReactContext': true,
// The below are necessary for serialport import to not choke during webpack-ing.
fs: '{}',
child_process: true,
bindings: true
},
plugins: [
new webpack.ProvidePlugin({React: 'react'}),
new webpack.DefinePlugin({
IN_UNIT_TEST: JSON.stringify(true),
IN_STORYBOOK: JSON.stringify(false),
'process.env.mocha_entry': JSON.stringify(process.env.mocha_entry),
'process.env.NODE_ENV': JSON.stringify(
envConstants.NODE_ENV || 'development'
),
LEVEL_TYPE: JSON.stringify(envConstants.LEVEL_TYPE),
PISKEL_DEVELOPMENT_MODE: JSON.stringify(false)
})
]
});
/**
* Generate the appropriate webpack config based off of our base config and
* some input options
* @param {object} options
* @param {string} options.output
* @param {string[]} options.entries - list of input source files
* @param {bool} options.minify
* @param {bool} options.watch
* @param {bool} options.watchNotify
* @param {string} options.piskelDevMode
* @param {Array} options.plugins - list of additional plugins to use
* @param {Array} options.externals - list of webpack externals
*/
function create(options) {
var outputDir = options.output;
var entries = options.entries;
var minify = options.minify;
var watch = options.watch;
var debugMinify = envConstants.DEBUG_MINIFIED;
var watchNotify = options.watchNotify;
var piskelDevMode = options.piskelDevMode;
var plugins = options.plugins;
var externals = options.externals;
var optimization = options.optimization;
var mode = options.mode;
var config = _.extend({}, baseConfig, {
output: {
path: outputDir,
publicPath: '/assets/js/',
// When debugging minified code, use the .js suffix (rather than .min.js)
// to allow the application to load minified js locally without running it
// through the rails asset pipeline. This is much simpler than hacking the
// application to load .min.js locally.
filename: '[name].' + (minify && !debugMinify ? 'min.' : '') + 'js'
},
devtool: !process.env.CI && options.minify ? 'source-map' : devtool,
entry: entries,
externals: externals,
optimization: optimization,
mode: mode,
plugins: [
new webpack.DefinePlugin({
IN_UNIT_TEST: JSON.stringify(false),
IN_STORYBOOK: JSON.stringify(false),
'process.env.NODE_ENV': JSON.stringify(
envConstants.NODE_ENV || 'development'
),
PISKEL_DEVELOPMENT_MODE: JSON.stringify(piskelDevMode)
}),
new webpack.IgnorePlugin(/^serialport$/),
new webpack.optimize.OccurrenceOrderPlugin(true)
].concat(plugins),
watch: watch,
keepalive: watch,
failOnError: !watch
});
if (watch) {
config.plugins = config.plugins.concat(
new LiveReloadPlugin({
appendScriptTag: envConstants.AUTO_RELOAD
})
);
if (watchNotify) {
config.plugins = config.plugins.concat(
new WebpackNotifierPlugin({alwaysNotify: true})
);
}
}
return config;
}
module.exports = {
config: baseConfig,
karmaConfig: karmaConfig,
storybookConfig: storybookConfig,
create: create
};