-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
185 lines (177 loc) · 4.33 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
const wordpressConfig = require('@wordpress/scripts/config/webpack.config');
const { getWebpackEntryPoints } = require('@wordpress/scripts/utils/config');
const { getWordPressSrcDirectory } = require('@wordpress/scripts/utils');
const { mergeWithRules } = require('webpack-merge');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MergeJsonWebpackPlugin = require('merge-jsons-webpack-plugin');
const { sync: glob } = require('fast-glob');
const { basename } = require('path');
/**
* Converts a legacy path to the entry pair supported by webpack, e.g.:
* `./entry-one.js` -> `[ 'entry-one', './entry-one.js] ]`
* `entry-two.js` -> `[ 'entry-two', './entry-two.js' ]`
*
* @param {string} path The path provided.
*
* @return {string[]} The entry pair of its name and the file path.
*/
const stylesheetPathToEntry = (path) => {
const entryName = basename(path, '.scss');
if (!path.startsWith('./')) {
path = './' + path;
}
return [entryName, path];
};
/**
* Get all of the block specific stylesheets to use for entry points.
*
* @return {Object} An object of entry keys and values.
*/
function getBlockStylesEntryPoints() {
const styles = glob('./src/css/blocks/*.scss');
const entry = {};
styles.forEach((fileArg) => {
const [entryName, path] = fileArg.includes('=')
? fileArg.split('=')
: stylesheetPathToEntry(fileArg);
entry['css/blocks/' + entryName] = path;
});
process.env.WP_ENTRY = JSON.stringify(entry);
return entry;
}
/**
* Pulsars default config.
*/
const pulsarConfig = {
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: require.resolve('css-loader'),
options: {
url: false,
},
},
{
loader: require.resolve('resolve-url-loader'),
options: {
root: __dirname,
},
},
],
},
{
test: /\.(sc|sa)ss$/,
use: [
{
loader: require.resolve('css-loader'),
options: {
url: false,
},
},
{
loader: require.resolve('resolve-url-loader'),
options: {
root: __dirname,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true, // Required for resolve-url-loader
sassOptions: {
includePaths: [__dirname + '/src/css'],
},
},
},
],
},
{
test: /\.svg$/,
issuer: /\.(j|t)sx?$/,
use: ['@svgr/webpack'],
type: 'javascript/auto',
},
],
},
stats: 'minimal',
entry: {
...getWebpackEntryPoints(),
...getBlockStylesEntryPoints(),
'css/app': ['./src/css/app.scss'],
'css/editor': ['./src/css/editor.scss'],
'js/app': ['./src/js/app.js'],
'js/editor': ['./src/js/editor.js'],
},
output: {
path: __dirname + '/dist',
publicPath: './',
},
devServer: {
hot: true,
static: __dirname + '/dist/',
allowedHosts: 'all',
host: 'localhost',
port: 1873,
proxy: {
'/dist': {
pathRewrite: {
'^/dist': '',
},
},
},
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: '**/*.{svg,jpg,png}',
context: getWordPressSrcDirectory(),
noErrorOnMissing: true,
},
],
}),
new RemoveEmptyScriptsPlugin({
stage: RemoveEmptyScriptsPlugin.STAGE_AFTER_PROCESS_PLUGINS,
}),
new MergeJsonWebpackPlugin({
space: '\t',
files: [
'./config/theme-json/base.json',
'./config/theme-json/settings.general.json',
'./config/theme-json/settings.background.json',
'./config/theme-json/settings.blocks.json',
'./config/theme-json/settings.border.json',
'./config/theme-json/settings.color.json',
'./config/theme-json/settings.custom.json',
'./config/theme-json/settings.dimensions.json',
'./config/theme-json/settings.layout.json',
'./config/theme-json/settings.lightbox.json',
'./config/theme-json/settings.position.json',
'./config/theme-json/settings.shadow.json',
'./config/theme-json/settings.spacing.json',
'./config/theme-json/settings.typography.json',
'./config/theme-json/styles.json',
'./config/theme-json/customTemplates.json',
'./config/theme-json/templateParts.json',
],
output: {
fileName: '../theme.json',
},
}),
],
};
module.exports = mergeWithRules({
module: {
rules: {
test: 'match',
use: {
loader: 'match',
options: 'replace',
},
},
},
})(wordpressConfig, pulsarConfig);