This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
webpack.common.config.js
145 lines (137 loc) · 7.28 KB
/
webpack.common.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
// Bundles static assets for loading in development environments.
// Optimizes for fast re-build time at the expense of a large bundle size.
const path = require('path');
const webpack = require('webpack');
const BundleTracker = require('webpack-bundle-tracker');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
module.exports = {
context: __dirname,
entry: {
// Application entry points
'application-main': './analytics_dashboard/static/js/application-main',
'engagement-content-main': './analytics_dashboard/static/js/engagement-content-main',
'engagement-video-content-main': './analytics_dashboard/static/js/engagement-video-content-main',
'engagement-videos-main': './analytics_dashboard/static/js/engagement-videos-main',
'engagement-video-timeline-main': './analytics_dashboard/static/js/engagement-video-timeline-main',
'enrollment-activity-main': './analytics_dashboard/static/js/enrollment-activity-main',
'enrollment-geography-main': './analytics_dashboard/static/js/enrollment-geography-main',
'enrollment-demographics-age-main': './analytics_dashboard/static/js/enrollment-demographics-age-main',
'enrollment-demographics-education-main': './analytics_dashboard/static/js/enrollment-demographics-education-main',
'enrollment-demographics-gender-main': './analytics_dashboard/static/js/enrollment-demographics-gender-main',
'performance-content-main': './analytics_dashboard/static/js/performance-content-main',
'performance-problems-main': './analytics_dashboard/static/js/performance-problems-main',
'performance-answer-distribution-main': './analytics_dashboard/static/js/performance-answer-distribution-main',
'learners-main': './analytics_dashboard/static/apps/learners/app/learners-main',
'performance-learning-outcomes-content-main': './analytics_dashboard/static/js/performance-learning-outcomes-content-main',
'performance-learning-outcomes-section-main': './analytics_dashboard/static/js/performance-learning-outcomes-section-main',
'course-list-main': './analytics_dashboard/static/apps/course-list/app/course-list-main',
// Entry point for bundling large amount of cldr-data json files separately from other vendor code
globalization: './analytics_dashboard/static/js/utils/globalization',
},
resolve: {
// For legacy reasons (when we used require.js), we need to add extra module resolving directories for some
// `requires` in our javascript that use relative paths.
modules: [
'node_modules',
'analytics_dashboard/static',
'analytics_dashboard/static/js',
'analytics_dashboard/static/apps',
],
alias: {
// Marionette in bower was called 'marionette'. In npm it's 'backbone.marionette'.
marionette: 'backbone.marionette',
// Internal Globalize.js code seems to expect 'cldr' to refer to this file in cldrjs.
cldr: 'cldrjs/dist/cldr',
// Aliases used in tests
uitk: 'edx-ui-toolkit/src/js',
URI: 'urijs/src/URI',
// Dedupe copies of modules in bundles by forcing all dependencies to use our copy of the module they need:
moment: path.resolve('./node_modules/moment'),
jquery: path.resolve('./node_modules/jquery'),
backbone: path.resolve('./node_modules/backbone'),
},
},
output: {
// Output bundles directly to the Django static assets directory
path: path.resolve('./analytics_dashboard/static/bundles/'),
// Tells clients to load bundles from the static route (uses CDN in production)
publicPath: '/static/bundles/',
// Bundle names will not change between builds if the content stays identical
filename: '[name]-[chunkhash].js',
},
module: {
// Specify file-by-file rules to Webpack. Some file-types need a particular kind of loader.
rules: [
// raw-loader just inlines files as a string in the javascript bundles
{
test: /\.underscore$/,
use: 'raw-loader',
include: path.join(__dirname, 'analytics_dashboard/static'),
exclude: /node_modules/,
},
// Webpack, by default, uses the url-loader for images and fonts that are required/included by files it
// processes, which just base64 encodes them and inlines them in the javascript bundles. This makes the
// javascript bundles ginormous and defeats caching so we will use the file-loader instead to copy the files
// directly to the output directory (the Django assets folder).
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
use: 'file-loader?name=fonts/[name].[ext]',
include: [path.join(__dirname, 'analytics_dashboard/static'), path.join(__dirname, 'node_modules')],
},
],
// This option can increase build speed slightly by forcing Webpack to skip processing for some large
// dependencies. Only add dependencies here that do not contain any `require`s or `include`s.
noParse: [/cldr-data|underscore/],
},
// Specify additional processing or side-effects done on the Webpack output bundles as a whole.
plugins: [
// Logs all bundle information to a JSON file which is needed by Django as a reference for finding the actual
// filenames on disk, which it doesn't know at runtime, from the bundle names, which it does know.
new BundleTracker({
filename: './webpack-stats.json',
}),
// We reference jquery as a global variable a lot. This plugin inlines a reference to the module in place of the
// free variable.
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
// This defines the theme that the SCSS should be building with
new webpack.DefinePlugin({
'process.env': {
THEME_SCSS: JSON.stringify(process.env.THEME_SCSS || 'sass/themes/open-edx.scss'),
},
}),
// AggressiveMergingPlugin in conjunction with these CommonChunkPlugins turns many GBs worth of individual
// chunks into one or two large chunks that entry chunks reference. It reduces output bundle size a lot.
new webpack.optimize.AggressiveMergingPlugin({ minSizeReduce: 1.1 }),
new webpack.optimize.CommonsChunkPlugin({
// Extracts code and json files for globalize.js into a separate bundle for efficient caching.
names: 'globalization',
minChunks: Infinity,
}),
new webpack.optimize.CommonsChunkPlugin({
// Extracts every 3rd-party module common among all bundles into one chunk (excluding the modules in the
// globalization bundle)
name: 'manifest',
minChunks(module, count) {
return (
module.context
&& module.context.indexOf('node_modules') !== -1
&& module.context.indexOf('cldr') === -1
&& module.context.indexOf('globalize') === -1
);
},
}),
new webpack.optimize.CommonsChunkPlugin({
// This chunk should only include the webpack runtime code. The runtime code changes on every webpack
// compile. We extract this so that the hash on the above vendor chunk does not change on every webpack
// compile (we don't want its hash to change without vendor lib changes, because that would bust the cache).
name: 'manifest',
}),
// Enable this plugin to see a pretty tree map of modules in each bundle and how much size they take up.
// new BundleAnalyzerPlugin({
// analyzerMode: 'static'
// })
],
};