forked from WikiEducationFoundation/WikiEduDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.webpack.js
111 lines (107 loc) · 3.46 KB
/
js.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
const path = require('path');
const config = require('./config');
const ManifestPlugin = require('webpack-manifest-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
const webpack = require('webpack');
const jsSource = `./${config.sourcePath}/${config.jsDirectory}`;
const appRoot = path.resolve('./');
const entry = {
main: [`${jsSource}/main.js`],
sentry: [`${jsSource}/sentry.js`],
styleguide: [`${jsSource}/styleguide/styleguide.jsx`],
survey: [`${jsSource}/surveys/survey.js`],
survey_admin: [`${jsSource}/surveys/survey-admin.js`],
survey_results: [`${jsSource}/surveys/survey-results.jsx`],
campaigns: [`${jsSource}/campaigns.js`],
charts: [`${jsSource}/charts.js`],
tinymce: [`${jsSource}/tinymce.js`],
embed_course_stats: [`${jsSource}/embed_course_stats.js`],
accordian: [`${jsSource}/accordian.js`]
};
module.exports = (env) => {
const doHot = env.development && !env.watch_js;
const mode = env.development ? 'development' : 'production';
const outputPath = doHot
? path.resolve(appRoot, `${config.outputPath}/${config.jsDirectory}`)
: path.resolve(`${config.outputPath}/${config.jsDirectory}`);
const devtool = env.coverage ? 'cheap-module-source-map' : 'eval';
if (env.coverage) {
// In coverage mode, every React component should
// be bundled within main.js
entry.main = [`${jsSource}/main-coverage.js`];
}
return {
mode,
entry,
output: {
path: outputPath,
filename: doHot ? '[name].js' : '[name].[chunkhash].js',
publicPath: '/assets/javascripts/',
},
resolve: {
extensions: ['.js', '.jsx'],
symlinks: false
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: [/vendor/, /node_modules(?!\/striptags)/],
use: {
loader: 'babel-loader',
query: {
cacheDirectory: true,
},
},
},
{
test: /\.jsx?$/,
exclude: [/vendor/, /node_modules(?!\/striptags)/],
loader: 'eslint-loader',
options: {
cache: true,
failOnError: !!env.production,
parser: 'babel-eslint'
},
},
],
},
externals: {
jquery: 'jQuery',
'i18n-js': 'I18n'
},
plugins: [
// manifest file
new ManifestPlugin({
fileName: 'js-manifest.json'
}),
// node environment
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(mode),
},
}),
// Creates smaller Lodash builds by replacing feature sets of modules with noop,
// identity, or simpler alternatives.
new LodashModuleReplacementPlugin(config.requiredLodashFeatures),
new MomentLocalesPlugin()
],
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]((?!(chart)).*)[\\/]/,
chunks: chunk => !/tinymce/.test(chunk.name),
name: 'vendors'
}
}
},
},
watch: env.watch_js,
// eval causes trouble with instrumenting and outputs the transformed code which is not useful with coverage data
// cheap-module-source-map outputs an almost original code at the best possible speed which helps in evaluating the coverage data
devtool: env.development ? devtool : 'source-map',
stats: env.stats ? 'normal' : 'minimal',
};
};