This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwebpack.common.js
142 lines (141 loc) · 3.47 KB
/
webpack.common.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
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StylelintPlugin = require('stylelint-webpack-plugin');
const embeddedSass = require('sass-embedded');
const path = require('path');
const glob = require('glob');
const RemovePlugin = require('remove-files-webpack-plugin');
const DependencyExtractionPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
module.exports = {
entry: () => {
// Grab any JS files.
const jsFiles = glob
.sync('source/**/*.js', {
ignore: ['**/_*'],
})
.reduce((entries, currentFile) => {
const updatedEntries = entries;
const filePaths = currentFile.split(path.sep);
const sourceDirIndex = filePaths.indexOf('source');
if (sourceDirIndex >= 0) {
const fileName = path.basename(currentFile, '.js');
const newFilePath = `js/${fileName}`;
// Throw an error if duplicate files detected.
if (updatedEntries[newFilePath]) {
throw new Error(
`More than one file named ${fileName}.js found.`
);
}
updatedEntries[newFilePath] = {
import: path.resolve(__dirname, currentFile),
};
}
return updatedEntries;
}, {});
// Grab any SCSS files that aren't prefixed with _.
const scssFiles = glob
.sync('source/**/*.scss', {
ignore: ['**/_*'],
})
.reduce((entries, currentFile) => {
const updatedEntries = entries;
const filePaths = currentFile.split(path.sep);
const sourceDirIndex = filePaths.indexOf('source');
if (sourceDirIndex >= 0) {
const fileName = path.basename(currentFile, '.scss');
const newFilePath = `css/${fileName}`;
// Throw an error if duplicate files detected.
if (updatedEntries[newFilePath]) {
throw new Error(
`More that one file named ${fileName}.scss found.`
);
}
updatedEntries[newFilePath] = {
import: `./${currentFile}`,
};
}
return updatedEntries;
}, {});
return {
...jsFiles,
...scssFiles,
};
},
plugins: [
new MiniCssExtractPlugin(),
new RemovePlugin({
after: {
test: [
{
folder: './build/css',
method: (absolutePath) =>
new RegExp(/\.js(\.map)?$/, 'm').test(absolutePath),
recursive: true,
},
],
},
}),
new StylelintPlugin({
exclude: ['node_modules', 'build', 'storybook'],
}),
new DependencyExtractionPlugin(),
],
module: {
rules: [
{
test: /\.scss$/i,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '/wp-content/themes/gesso/build/',
},
},
'css-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
implementation: embeddedSass,
sassOptions: {
includePaths: [
path.resolve(__dirname, 'source'),
],
},
},
},
],
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /fonts\/.*\.(woff2?|ttf|otf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/i,
exclude: ['/node_modules/'],
type: 'asset/resource',
generator: {
filename: 'fonts/[hash][ext][query]',
},
},
{
test: /\.(png|svg|jpg|gif|webp)$/i,
exclude: [
/images\/_sprite-source-files\/.*\.svg$/,
'/node_modules/',
],
type: 'asset',
generator: {
filename: 'images/[hash][ext][query]',
},
},
],
},
resolve: {
modules: [path.resolve(__dirname, 'source'), 'node_modules'],
},
output: {
path: path.resolve(__dirname, 'build'),
},
};