Skip to content

Commit a6eaabd

Browse files
committed
update webpack config
1 parent 45d65ac commit a6eaabd

File tree

1 file changed

+127
-77
lines changed

1 file changed

+127
-77
lines changed

config/webpack.config.js

+127-77
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
/**
22
* External dependencies
33
*/
4-
const { sync: readPkgUp } = require("read-pkg-up");
5-
const LiveReloadPlugin = require("webpack-livereload-plugin");
6-
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
7-
const path = require("path");
8-
const { existsSync, lstatSync, readdirSync, realpathSync } = require("fs");
4+
const { existsSync, lstatSync, readdirSync, realpathSync } = require('fs');
5+
const { sync: readPkgUp } = require('read-pkg-up');
6+
const globImporter = require('node-sass-glob-importer');
7+
const LiveReloadPlugin = require('webpack-livereload-plugin');
8+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
9+
const path = require('path');
910

1011
/**
1112
* WordPress dependencies
1213
*/
13-
const DependencyExtractionWebpackPlugin = require("@wordpress/dependency-extraction-webpack-plugin");
14+
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
1415

1516
/**
1617
* Internal dependencies
1718
*/
18-
const RemoveSuprefluousAssetsPlugin = require("../plugins/remove-superfluous-assets");
19+
const RemoveSuprefluousAssetsPlugin = require('../plugins/remove-superfluous-assets');
1920
const {
2021
getArg,
2122
getScriptsConfig,
2223
hasArg,
2324
hasBabelConfig,
2425
hasFileArg,
25-
hasPostCSSConfig
26-
} = require("../utils");
26+
hasPostCSSConfig,
27+
} = require('../utils');
2728

2829
const { path: pkgPath } = readPkgUp({
29-
cwd: realpathSync(process.cwd())
30+
cwd: realpathSync(process.cwd()),
3031
});
3132

3233
const config = getScriptsConfig();
3334

3435
const paths = Object.assign(
3536
{
36-
src: "src/assets",
37-
output: "dist",
38-
scripts: "js",
39-
styles: "scss",
40-
images: "images"
37+
src: 'src/assets',
38+
output: 'dist',
39+
scripts: 'js',
40+
styles: 'scss',
41+
images: 'images',
42+
fonts: 'fonts',
4143
},
4244
config && config.paths ? config.paths : {}
4345
);
4446

4547
for (const pathKey in paths) {
4648
let pathValue = getArg(`--${pathKey}-path`, paths[pathKey]);
4749

48-
if (["src", "output"].includes(pathKey) && "/" !== pathValue.charAt(0)) {
50+
if (['src', 'output'].includes(pathKey) && '/' !== pathValue.charAt(0)) {
4951
pathValue = path.join(path.dirname(pkgPath), pathValue);
5052
}
5153

@@ -58,7 +60,7 @@ const entry = {};
5860
if (!hasFileArg()) {
5961
if (!existsSync(paths.src)) {
6062
/* eslint-disable-next-line no-console */
61-
console.log("Source directory does not exist.");
63+
console.log('Source directory does not exist.');
6264
process.exit(1);
6365
}
6466

@@ -68,9 +70,9 @@ if (!hasFileArg()) {
6870
const ext = path.extname(file);
6971
const name = path.basename(file, ext);
7072

71-
if ("_" !== name.charAt(0)) {
73+
if ('_' !== name.charAt(0)) {
7274
const filePath = path.join(srcPath, file);
73-
outputPath = outputPath.replace("scss", "css");
75+
outputPath = outputPath.replace('scss', 'css');
7476

7577
if (lstatSync(filePath).isFile()) {
7678
entry[`${outputPath}/${name}`] = filePath;
@@ -81,7 +83,7 @@ if (!hasFileArg()) {
8183
const directoryErrors = [];
8284
let hasDirectory = false;
8385

84-
for (const assetType of ["scripts", "styles"]) {
86+
for (const assetType of ['scripts', 'styles']) {
8587
if (false === paths[assetType]) {
8688
continue;
8789
}
@@ -100,7 +102,7 @@ if (!hasFileArg()) {
100102

101103
hasDirectory = true;
102104

103-
readdirSync(srcPath).forEach(file =>
105+
readdirSync(srcPath).forEach((file) =>
104106
addEntry(file, srcPath, paths[assetType])
105107
);
106108
}
@@ -118,114 +120,162 @@ if (!hasFileArg()) {
118120

119121
if (!entry) {
120122
/* eslint-disable-next-line no-console */
121-
console.log("No entry files available.");
123+
console.log('No entry files available.');
122124
process.exit(1);
123125
}
124126
}
125127

126128
const mode = getArg(
127-
"--mode",
128-
process.env.NODE_ENV === "production" ? "production" : "development"
129+
'--mode',
130+
process.env.NODE_ENV === 'production' ? 'production' : 'development'
129131
);
130-
const isProduction = "production" === mode;
132+
const isProduction = 'production' === mode;
133+
134+
const imageWebpackLoaderConfig = {
135+
loader: 'image-webpack-loader',
136+
options: {
137+
disable: false === config.imagemin,
138+
...('object' === typeof config.imagemin && config.imagemin),
139+
},
140+
};
131141

132142
module.exports = {
133143
mode,
134144
context,
135145
entry,
136146
output: {
137147
path: paths.output,
138-
filename: "[name].js",
139-
publicPath: "../"
148+
filename: '[name].js',
149+
publicPath: '../',
140150
},
141-
devtool: isProduction ? false : "source-map",
151+
devtool: isProduction ? false : 'source-map',
142152
module: {
143153
rules: [
144154
{
145155
test: /\.js$/,
146156
exclude: /node_modules/,
147157
use: [
148-
require.resolve("thread-loader"),
158+
require.resolve('thread-loader'),
149159
{
150-
loader: require.resolve("babel-loader"),
160+
loader: require.resolve('babel-loader'),
151161
options: {
152162
// Babel uses a directory within local node_modules
153163
// by default. Use the environment variable option
154164
// to enable more persistent caching.
155-
cacheDirectory: process.env.BABEL_CACHE_DIRECTORY || true,
165+
cacheDirectory:
166+
process.env.BABEL_CACHE_DIRECTORY || true,
156167
...(!hasBabelConfig() && {
157168
babelrc: false,
158169
configFile: false,
159-
presets: [require.resolve("@wordpress/babel-preset-default")]
160-
})
161-
}
162-
}
163-
]
170+
presets: [
171+
require.resolve(
172+
'@wordpress/babel-preset-default'
173+
),
174+
],
175+
}),
176+
},
177+
},
178+
],
164179
},
165180
{
166181
test: /\.scss$/,
167182
use: [
168183
MiniCssExtractPlugin.loader,
169184
{
170-
loader: "css-loader",
185+
loader: 'css-loader',
171186
options: {
172187
importLoaders: 2,
173-
sourceMap: true
174-
}
188+
sourceMap: true,
189+
},
175190
},
176191
{
177-
loader: "postcss-loader",
192+
loader: 'postcss-loader',
178193
options: {
179194
sourceMap: true,
180195
...(!hasPostCSSConfig() && {
181-
plugins: () => [require("autoprefixer")]
182-
})
183-
}
196+
postcssOptions: {
197+
plugins: () => [require('autoprefixer')],
198+
},
199+
}),
200+
},
184201
},
185202
{
186-
loader: "sass-loader",
203+
loader: 'sass-loader',
187204
options: {
188-
sourceMap: true
189-
}
190-
}
191-
]
192-
},
193-
{
194-
test: /\.svg$/,
195-
issuer: {
196-
test: /\.jsx?$/
197-
},
198-
use: ["@svgr/webpack"]
205+
sassOptions: {
206+
importer: [globImporter()],
207+
},
208+
sourceMap: true,
209+
},
210+
},
211+
],
199212
},
200213
{
201-
test: /\.(png|svg|jpg|gif)$/,
202-
use: [
214+
test: /\.(png|svg|jpe?g|gif)$/,
215+
oneOf: [
203216
{
204-
loader: "url-loader",
205-
options: {
206-
limit: config.urlLoader ? config.urlLoader : false,
207-
name: "[name].[ext]",
208-
outputPath: paths.images
209-
}
217+
issuer: /\.(ts|tsx|js|jsx)$/,
218+
resourceQuery: { not: /inline/ },
219+
test: /\.svg$/,
220+
type: 'javascript/auto',
221+
use: [
222+
'@svgr/webpack',
223+
{
224+
loader: 'url-loader',
225+
options: {
226+
limit: config.inlineAssets
227+
? config.inlineAssets
228+
: false,
229+
name: '[hash].[ext]',
230+
outputPath: paths.images,
231+
},
232+
},
233+
imageWebpackLoaderConfig,
234+
],
210235
},
211236
{
212-
loader: "image-webpack-loader",
213-
options: {
214-
disable: false === config.imagemin,
215-
...("object" === typeof config.imagemin && config.imagemin)
216-
}
217-
}
218-
]
219-
}
220-
]
237+
resourceQuery: { not: /inline/ },
238+
generator: {
239+
filename: `${paths.images}/[hash][ext][query]`,
240+
},
241+
type:
242+
config.inlineAssets === false
243+
? 'asset'
244+
: 'asset/resource',
245+
use: [imageWebpackLoaderConfig],
246+
...(Number.isInteger(config.inlineAssets) && {
247+
parser: {
248+
dataUrlCondition: {
249+
maxSize: config.inlineAssets,
250+
},
251+
},
252+
}),
253+
},
254+
{
255+
resourceQuery: /inline/,
256+
type: 'asset/inline',
257+
use: [imageWebpackLoaderConfig],
258+
},
259+
],
260+
},
261+
{
262+
test: /\.(woff|woff2|eot|ttf|otf)$/i,
263+
type: 'asset/resource',
264+
generator: {
265+
filename: `${paths.fonts}/[hash][ext][query]`,
266+
},
267+
},
268+
],
221269
},
222270
plugins: [
223271
new MiniCssExtractPlugin({
224-
filename: "[name].css"
272+
filename: '[name].css',
225273
}),
226-
!hasArg("--no-deps") &&
227-
new DependencyExtractionWebpackPlugin({ injectPolyfill: true }),
274+
!hasArg('--no-deps') &&
275+
new DependencyExtractionWebpackPlugin({
276+
injectPolyfill: true,
277+
}),
228278
new RemoveSuprefluousAssetsPlugin(),
229-
!isProduction && new LiveReloadPlugin()
230-
].filter(Boolean)
279+
!isProduction && new LiveReloadPlugin(),
280+
].filter(Boolean),
231281
};

0 commit comments

Comments
 (0)