-
Notifications
You must be signed in to change notification settings - Fork 6
/
webpack.config.js
187 lines (178 loc) · 5.65 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const RollbarSourceMapPlugin = require('rollbar-sourcemap-webpack-plugin');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
// Taken from https://webpack.js.org/loaders/style-loader/#insert
function insertAtTop(element) {
var parent = document.querySelector('head');
// eslint-disable-next-line no-underscore-dangle
var lastInsertedElement = window._lastElementInsertedByStyleLoader;
if (!lastInsertedElement) {
parent.insertBefore(element, parent.firstChild);
} else if (lastInsertedElement.nextSibling) {
parent.insertBefore(element, lastInsertedElement.nextSibling);
} else {
parent.appendChild(element);
}
// eslint-disable-next-line no-underscore-dangle
window._lastElementInsertedByStyleLoader = element;
}
const ASSET_PATH =
process.env.ASSET_PATH || (process.env.NODE_ENV === 'production' ? '/packs/' : 'https://localhost:3135/packs/');
const config = {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
devtool: process.env.NODE_ENV === 'production' ? 'source-map' : 'eval-source-map',
entry: {
/// !!!!!! IMPORTANT !!!!!!
/// When adding new production entry points, make sure to add them to production.rb's list of assets to exclude
/// from caching! Otherwise, subsequent deploys will still have old versions of entry points.
/// !!!!!! IMPORTANT !!!!!!
application: './app/javascript/packs/applicationEntry.ts',
'application-styles': './app/javascript/packs/applicationStyles.ts',
...(process.env.NODE_ENV === 'production'
? {}
: {
'dev-mode-graphiql': './app/javascript/DevModeGraphiql.tsx',
}),
},
output: {
filename: '[name].js',
chunkFilename: '[name]-[chunkhash].chunk.js',
hotUpdateChunkFilename: '[name]-[fullhash].hot-update.js',
path: path.resolve('public/packs'),
publicPath: ASSET_PATH,
environment: {
arrowFunction: false,
},
},
optimization: {
minimizer: [
new TerserPlugin({
minify: TerserPlugin.swcMinify,
// `terserOptions` options will be passed to `swc` (`@swc/core`)
// Link to options - https://swc.rs/docs/config-js-minify
terserOptions: {},
}),
],
},
devServer: {
port: 3135,
historyApiFallback: {
disableDotRule: true,
},
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization',
},
host: '0.0.0.0',
server: {
type: 'https',
options: {
key: './dev_certificate.key',
cert: './dev_certificate.crt',
ca: './dev_ca.crt',
},
},
webSocketServer: 'ws',
allowedHosts: 'all',
hot: false,
watchFiles: {
options: {
ignored: [path.resolve(__dirname, 'public/product_images'), path.resolve(__dirname, 'public/uploads')],
},
},
},
cache: {
type: 'filesystem',
cacheDirectory: path.resolve(__dirname, 'build-cache/webpack-web'),
},
module: {
rules: [
{
test: /\.(jpg|jpeg|png|gif|tiff|ico|svg|eot|otf|ttf|woff|woff2)$/i,
type: 'asset/resource',
},
{
test: /\.s[ac]ss$/i,
use: [
{
loader: require.resolve('style-loader'),
options: { insert: insertAtTop },
},
require.resolve('css-loader'),
require.resolve('postcss-loader'),
{ loader: require.resolve('sass-loader'), options: { sassOptions: { quietDeps: true } } },
],
},
{
test: /\.css$/i,
use: [
{
loader: require.resolve('style-loader'),
options: { insert: insertAtTop },
},
require.resolve('css-loader'),
require.resolve('postcss-loader'),
],
},
{
test: /\.(jsx)$/,
include: [/node_modules\/cadmus-navbar-admin/],
type: 'javascript/auto',
use: [
{
loader: require.resolve('swc-loader'),
},
],
},
{
test: /\.(mjs|js|jsx|ts|tsx)?(\.erb)?$/,
exclude: [/node_modules/, /\.yalc/],
use: [
{
loader: require.resolve('swc-loader'),
},
],
},
],
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.png', '.svg', '.gif', '.jpeg', '.jpg'],
alias: {
'lodash.isequal': 'lodash-es/isEqual',
},
fallback: {
stream: require.resolve('stream-browserify'),
crypto: require.resolve('crypto-browserify'),
events: require.resolve('events'),
buffer: require.resolve('buffer'),
punycode: require.resolve('punycode'),
path: require.resolve('path-browserify'),
},
},
plugins: [
new webpack.DefinePlugin({
__DEV__: JSON.stringify(process.env.NODE_ENV !== 'production'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV ?? 'development'),
'process.env.ASSET_PATH': JSON.stringify(ASSET_PATH),
}),
new CaseSensitivePathsPlugin(),
],
};
if (process.env.ANALYZE_BUNDLE_SIZE) {
config.plugins.push(new BundleAnalyzerPlugin());
}
if (process.env.ROLLBAR_ACCESS_TOKEN) {
config.plugins.push(
new RollbarSourceMapPlugin({
accessToken: process.env.ROLLBAR_ACCESS_TOKEN,
version: process.env.SOURCE_VERSION,
publicPath: process.env.ROLLBAR_PUBLIC_PATH,
}),
);
}
module.exports = config;