-
Notifications
You must be signed in to change notification settings - Fork 641
/
webpack.config.js
168 lines (156 loc) · 5.18 KB
/
webpack.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const path = require('path');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const RtlCssPlugin = require('rtlcss-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const config = require('./config');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const jsSource = `./${config.sourcePath}/${config.jsDirectory}`;
const cssSource = `./${config.sourcePath}/${config.cssDirectory}`;
const outputPath = path.resolve(`${config.outputPath}`);
module.exports = (env) => {
const mode = env.development ? 'development' : 'production';
const isProductionOrCI = env.production || env.coverage;
let devtool = 'eval-cheap-source-map';
// see https://webpack.js.org/configuration/devtool/ for the detailed descriptions of these
if (env.production) {
devtool = 'source-map';
} else if (env.coverage) {
devtool = 'cheap-module-source-map';
}
const entries = {
main: [`${jsSource}/main.js`, `${cssSource}/main.styl`],
styleguide: [`${jsSource}/styleguide/styleguide.jsx`, `${cssSource}/styleguide.styl`],
sentry: [`${jsSource}/sentry.js`],
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`],
accordian: [`${jsSource}/accordian.js`],
editable: [`${jsSource}/utils/editable.js`],
i18n: [`${jsSource}/i18n.js`],
surveys: [`${cssSource}/surveys.styl`],
training: [`${cssSource}/training.styl`],
};
const output = {
mode,
output: {
path: outputPath,
filename: env.development ? 'javascripts/[name].js' : 'javascripts/[name].[chunkhash].js',
publicPath: '/assets/',
},
entry: entries,
resolve: {
extensions: ['.js', '.jsx', '.styl'],
symlinks: false,
// bug in React 17. Should be removed when we upgrade to React 18
// See https://github.com/react-dnd/react-dnd/issues/3423#issuecomment-1092621793
fallback: {
'react/jsx-runtime': 'react/jsx-runtime.js',
'react/jsx-dev-runtime': 'react/jsx-dev-runtime.js',
},
},
module: {
rules: [
{
test: /\.jsx?$/,
include: path.resolve(__dirname, 'app/assets/javascripts'),
use: {
loader: require.resolve('babel-loader'),
options: {
cacheDirectory: true,
cacheCompression: false
},
},
},
{
test: /\.styl$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
// Compiles Stylus to CSS
loader: 'stylus-native-loader',
options: {
includeCSS: true,
vendors: true
}
}
]
},
{
// this inlines resources if they are less than 8KB
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
type: 'asset'
}
],
},
plugins: [
!env.DISABLE_ESLINT && new ESLintPlugin({
files: 'app/assets/javascripts/**/*.{js,jsx}',
failOnError: isProductionOrCI,
threads: isProductionOrCI,
lintDirtyModulesOnly: !isProductionOrCI,
cache: !isProductionOrCI
}),
new MiniCssExtractPlugin({
filename: env.development ? 'stylesheets/[name].css' : 'stylesheets/[name].[contenthash].css',
}),
// generates a RTL version of the emitted CSS files
// this is done only in production/coverage
(env.production || env.coverage) && (new RtlCssPlugin({
filename: 'stylesheets/rtl-[name].[fullhash].css'
})),
new WebpackManifestPlugin({
fileName: 'javascripts/manifest.json',
map: (file) => {
if (/rtl-.*\.css$/.test(file.path)) {
file.name = `rtl-${file.name}`;
}
return file;
}
}),
(env.development && !env.coverage) && new ReactRefreshWebpackPlugin({ overlay: {
sockPort: 8080
} }),
(env.analyze && new BundleAnalyzerPlugin())
].filter(Boolean),
optimization: {
splitChunks: {
cacheGroups: {
defaultVendors: {
// all of these modules are imported dynamically so they should not be included in the vendor bundle
test: /[\\/]node_modules[\\/]((?!(chart|tinymce|jquery-ui)).*)[\\/]/,
chunks: 'all',
name: 'vendors'
},
}
},
minimizer: [
'...',
new CssMinimizerPlugin(),
],
},
externals: {
jquery: 'jQuery',
},
devtool,
stats: env.stats ? 'normal' : 'minimal',
};
if (env.development && env.memory) {
output.devServer = {
hot: true
};
} else if (env.development) {
output.devServer = {
devMiddleware: {
publicPath: path.join(__dirname, '/public'),
writeToDisk: true,
},
};
}
return output;
};