Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
237 lines (228 loc) · 6.64 KB
/
Copy pathwebpack.config.js
File metadata and controls
237 lines (228 loc) · 6.64 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* Webpack Configuration
*/
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const TerserPlugin = require( 'terser-webpack-plugin' );
const fs = require( 'fs' );
// Generate web component SCSS entries: every .scss file directly inside each web component folder.
const webComponentsDir = './js/src/web-components';
const webComponentScssFiles = fs.readdirSync( webComponentsDir, { withFileTypes: true } )
.filter( dir => dir.isDirectory() )
.reduce( ( scssList, dir ) => {
fs.readdirSync( `${ webComponentsDir }/${ dir.name }` )
.filter( file => file.endsWith( '.scss' ) )
.forEach( file => {
scssList[`../js/src/web-components/${ dir.name }/${ file.slice( 0, -5 ) }`] = `${ webComponentsDir }/${ dir.name }/${ file }`;
} );
return scssList;
}, {});
/**
* Environment configuration
*/
const isDevelopment = process.env.NODE_ENV !== 'production';
/**
* Path configurations
*/
const paths = {
js: path.resolve(__dirname, 'js'),
jsSource: path.resolve(__dirname, 'js/src'),
css: path.resolve(__dirname, 'css'),
scss: path.resolve(__dirname, 'resources/scss')
};
/**
* Entry points configuration
*/
const entries = {
// JavaScript entries
js: {
formidable_blocks: './js/src/blocks.js',
formidable_overlay: './js/src/overlay.js',
'form-templates': './js/src/form-templates/index.js',
formidable_dashboard: './js/src/dashboard.js',
'onboarding-wizard': './js/src/onboarding-wizard/index.js',
'addons-page': './js/src/addons-page/index.js',
formidable_styles: './js/src/admin/styles.js',
'formidable-color-picker': './js/src/color-picker/index.js',
'formidable-form-settings': './js/src/admin/form-settings.js',
'formidable-global-settings': './js/src/admin/global-settings.js',
formidable_builder: './js/src/admin/builder.js',
'formidable-import-export': './js/src/admin/import-export.js',
formidable_admin: './js/src/admin/admin.js',
frm_testing_mode: './js/src/frm_testing_mode.js',
'formidable-settings-components': './js/src/settings-components/index.js',
'formidable-web-components': './js/src/web-components/index.js',
'welcome-tour': './js/src/welcome-tour',
},
// SCSS entries
scss: {
frm_admin: './resources/scss/admin/frm_admin.scss',
'admin/frm-settings-components': './resources/scss/admin/frm-settings-components.scss',
'admin/frm-legacy-paypal': './resources/scss/admin/frm-legacy-paypal.scss',
font_icons: './resources/scss/font_icons.scss',
// Dynamically generated web component SCSS files
frm_testing_mode: './resources/scss/test-mode/frm_testing_mode.scss',
'admin/welcome-tour': './resources/scss/admin/welcome-tour.scss',
...webComponentScssFiles
}
};
/**
* Shared configuration used in both configs
*/
const sharedConfig = {
mode: isDevelopment ? 'development' : 'production',
devtool: isDevelopment ? 'source-map' : undefined,
// Keep build results on disk so a rebuild only reprocesses the files that changed.
// Editing this file, .babelrc, .browserslistrc or package.json throws the cache away. Pass --no-cache for a clean build.
cache: {
type: 'filesystem',
buildDependencies: {
config: [__filename, path.resolve(__dirname, '.babelrc'), path.resolve(__dirname, '.browserslistrc'), path.resolve(__dirname, 'package.json')]
}
},
resolve: {
extensions: ['.json', '.js', '.jsx', '.scss', '.css'],
modules: [paths.jsSource, 'node_modules'],
alias: {
core: path.resolve(paths.jsSource, 'core'),
css: paths.css
}
},
externals: {
jquery: 'jQuery',
$: 'jQuery'
},
optimization: {
// Scope hoisting renames the `__` locals of concatenated modules, which hides strings from make-pot.
concatenateModules: false,
minimizer: [
// Keep i18n function names and translator comments so translate.wordpress.org can extract strings from the built files.
new TerserPlugin( {
terserOptions: {
compress: {
// Prevents folding `a ? __( x ) : __( y )` into `__( a ? x : y )`, which make-pot cannot read.
conditionals: false
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ]
},
format: {
comments: /translators:/i
}
}
} )
]
}
};
/**
* Sass loader configuration with shared options
*/
const sassLoaderOptions = {
// Reuse one sass-embedded compiler process for every file.
api: 'modern-compiler',
sassOptions: {
// The SCSS still uses @import. Hide its deprecation notice until it moves to @use.
silenceDeprecations: [ 'import' ],
includePaths: [
paths.scss,
paths.css,
__dirname // Root directory for absolute paths
]
}
};
/**
* JavaScript specific config
*/
const jsConfig = {
...sharedConfig,
name: 'js',
entry: entries.js,
output: {
filename: '[name].js',
path: paths.js,
chunkFormat: false,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
include: /js/,
use: [{ loader: 'babel-loader' }]
},
{
test: /\.svg$/,
use: ['@svgr/webpack']
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
exclude: /-component\.css$/
},
{
test: /-component\.css$/i,
use: ['raw-loader']
},
{
test: /\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: { url: false }
},
{
loader: 'sass-loader',
options: sassLoaderOptions
}
]
}
]
}
};
/**
* SCSS/CSS specific config
*/
const cssConfig = {
...sharedConfig,
name: 'css',
entry: entries.scss,
plugins: [
// Prevents JS files from being generated for SCSS entries
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: '../css/[name].css'
})
],
module: {
rules: [
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false,
sourceMap: isDevelopment
}
},
'css-unicode-loader', // Handle unicode escape sequences in CSS
{
loader: 'sass-loader',
options: sassLoaderOptions
}
]
}
]
},
output: {
path: paths.css,
filename: '[name].js' // This won't be generated due to RemoveEmptyScriptsPlugin
}
};
/**
* Export both configurations
*/
module.exports = [jsConfig, cssConfig];