-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
137 lines (129 loc) · 3.3 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
const path = require( 'path' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const CopyPlugin = require( 'copy-webpack-plugin' );
const SVGSpritemapPlugin = require( 'svg-spritemap-webpack-plugin' );
const { CleanWebpackPlugin } = require( 'clean-webpack-plugin' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const ESLintPlugin = require( 'eslint-webpack-plugin' );
const StylelintPlugin = require( 'stylelint-webpack-plugin' );
const { glob } = require( 'glob' );
// See: https://stackoverflow.com/a/63604863 to convert `glob` output into an object with keys as entry names.
const customBlockEntryPaths = glob
.sync( './src/js/blocks/custom/*.js', {
dotRelative: true,
} )
.reduce( ( acc, filePath ) => {
const entry = filePath.replace( /^.*[\\\/]/, '' ).replace( '.js', '' );
acc[ entry ] = filePath;
return acc;
}, {} );
/**
* Webpack config (Development mode)
*
* @see https://developer.wordpress.org/block-editor/packages/packages-scripts/#provide-your-own-webpack-config
*/
module.exports = {
...defaultConfig,
entry: {
index: './src/index.js',
...customBlockEntryPaths,
},
module: {
rules: [
{
test: /\.(webp|png|jpe?g|gif)$/,
type: 'asset/resource',
generator: {
filename: 'images/[name][ext]',
},
},
{
test: /\.(sa|sc|c)ss$/,
exclude: '/node_modules',
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'svg-transform-loader/encode-query',
'sass-loader',
],
},
{
test: /\.svg$/,
type: 'asset/inline',
use: 'svg-transform-loader',
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext]',
},
},
],
},
plugins: [
...defaultConfig.plugins,
new MiniCssExtractPlugin(),
/**
* Copy source files/directories to a build directory.
*
* @see https://www.npmjs.com/package/copy-webpack-plugin
*/
new CopyPlugin( {
patterns: [
{
from: '**/*.{jpg,jpeg,png,gif,svg}',
to: 'images/[path][name][ext]',
context: path.resolve( process.cwd(), 'src/images' ),
noErrorOnMissing: true,
},
{
from: '*.svg',
to: 'images/icons/[name][ext]',
context: path.resolve( process.cwd(), 'src/images/icons' ),
noErrorOnMissing: true,
},
{
from: '**/*.{woff,woff2,eot,ttf,otf}',
to: 'fonts/[path][name][ext]',
context: path.resolve( process.cwd(), 'src/fonts' ),
noErrorOnMissing: true,
},
],
} ),
/**
* Generate an SVG sprite.
*
* @see https://github.com/cascornelissen/svg-spritemap-webpack-plugin
*/
new SVGSpritemapPlugin( 'src/images/icons/*.svg', {
output: {
filename: 'images/icons/sprite.svg',
},
sprite: {
prefix: false,
},
} ),
/**
* Clean build directory.
*
* @see https://www.npmjs.com/package/clean-webpack-plugin
*/
new CleanWebpackPlugin( {
cleanAfterEveryBuildPatterns: [ '!fonts/**', '!*.woff2' ],
} ),
/**
* Report JS warnings and errors to the command line.
*
* @see https://www.npmjs.com/package/eslint-webpack-plugin
*/
new ESLintPlugin(),
/**
* Report css warnings and errors to the command line.
*
* @see https://www.npmjs.com/package/stylelint-webpack-plugin
*/
new StylelintPlugin(),
],
};