forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.js
More file actions
209 lines (197 loc) · 6.35 KB
/
webpack.js
File metadata and controls
209 lines (197 loc) · 6.35 KB
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
var _ = require('lodash');
var webpack = require('webpack');
var path = require('path');
var LiveReloadPlugin = require('webpack-livereload-plugin');
var envConstants = require('./envConstants');
var UnminifiedWebpackPlugin = require('unminified-webpack-plugin');
// Our base config, on which other configs are derived
var baseConfig = {
resolve: {
extensions: ["", ".js", ".jsx"],
alias: {
'@cdo/locale': path.resolve(__dirname, 'src', '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/apps': path.resolve(__dirname, 'src'),
repl: path.resolve(__dirname, 'src/noop'),
}
},
module: {
loaders: [
{test: /\.json$/, loader: 'json'},
{test: /\.ejs$/, loader: 'ejs-compiled'},
{test: /\.css$/, loader: 'style-loader!css-loader'},
],
preLoaders: [
{
test: /\.jsx?$/,
include: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'test'),
path.resolve(__dirname, 'node_modules', '@cdo')
],
exclude: [
path.resolve(__dirname, 'src', 'lodash.js'),
],
loader: "babel",
query: {
cacheDirectory: path.resolve(__dirname, '.babel-cache'),
compact: false,
}
},
],
},
node: {
fs: 'empty',
},
};
// modify baseConfig's preLoaders if looking for code coverage info
if (envConstants.COVERAGE) {
baseConfig.module.preLoaders = [
{
test: /\.jsx?$/,
include: [
path.resolve(__dirname, 'test'),
path.resolve(__dirname, 'node_modules', '@cdo'),
],
loader: "babel",
query: {
cacheDirectory: true,
}
}, {
test: /\.jsx?$/,
loader: 'babel-istanbul',
include: path.resolve(__dirname, 'src'),
exclude: [
path.resolve(__dirname, 'src', 'lodash.js'),
],
query: {
cacheDirectory: true,
}
},
];
}
var storybookConfig = _.extend({}, baseConfig, {
devtool: 'inline-source-map',
externals: {
"johnny-five": "var JohnnyFive",
"playground-io": "var PlaygroundIO",
"chrome-serialport": "var ChromeSerialport",
"blockly": "this Blockly",
},
plugins: [
new webpack.ProvidePlugin({React: 'react'}),
new webpack.DefinePlugin({
IN_UNIT_TEST: JSON.stringify(false),
'process.env.mocha_entry': JSON.stringify(process.env.mocha_entry),
'process.env.NODE_ENV': JSON.stringify(envConstants.NODE_ENV || 'development'),
BUILD_STYLEGUIDE: JSON.stringify(true),
PISKEL_DEVELOPMENT_MODE: JSON.stringify(false),
}),
]
});
// config for our test runner
var karmaConfig = _.extend({}, baseConfig, {
devtool: 'cheap-module-source-map',
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'),
'firebase': path.resolve(__dirname, 'test', 'util', 'MockFirebase.js'),
}),
}),
externals: {
"johnny-five": "var JohnnyFive",
"playground-io": "var PlaygroundIO",
"chrome-serialport": "var ChromeSerialport",
"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
},
plugins: [
new webpack.ProvidePlugin({React: 'react'}),
new webpack.DefinePlugin({
IN_UNIT_TEST: JSON.stringify(true),
'process.env.mocha_entry': JSON.stringify(process.env.mocha_entry),
'process.env.NODE_ENV': JSON.stringify(envConstants.NODE_ENV || 'development'),
BUILD_STYLEGUIDE: JSON.stringify(false),
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 {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 piskelDevMode = options.piskelDevMode;
var plugins = options.plugins;
var externals = options.externals;
var config = _.extend({}, baseConfig, {
output: {
path: outputDir,
filename: "[name]." + (minify ? "min." : "") + "js",
},
devtool: !process.env.CI && options.minify ? 'source-map' : 'inline-source-map',
entry: entries,
externals: externals,
plugins: [
new webpack.DefinePlugin({
IN_UNIT_TEST: JSON.stringify(false),
'process.env.NODE_ENV': JSON.stringify(envConstants.NODE_ENV || 'development'),
BUILD_STYLEGUIDE: JSON.stringify(false),
PISKEL_DEVELOPMENT_MODE: JSON.stringify(piskelDevMode),
}),
new webpack.IgnorePlugin(/^serialport$/),
new webpack.optimize.OccurrenceOrderPlugin(true)
].concat(plugins),
watch: watch,
keepalive: watch,
failOnError: !watch
});
if (minify) {
config.plugins = config.plugins.concat(
[
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
}),
new UnminifiedWebpackPlugin(),
]
);
}
if (watch) {
config.plugins = config.plugins.concat(
new LiveReloadPlugin({
appendScriptTag: envConstants.AUTO_RELOAD
})
);
}
return config;
}
module.exports = {
config: baseConfig,
karmaConfig: karmaConfig,
storybookConfig: storybookConfig,
create: create
};