-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwebpack.common.js
195 lines (190 loc) · 5.39 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
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
import path, { dirname } from 'node:path';
import { Glob } from 'glob';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import RemovePlugin from 'remove-files-webpack-plugin';
import StylelintPlugin from 'stylelint-webpack-plugin';
import SpriteLoaderPlugin from 'svg-sprite-loader/plugin.js';
import * as embeddedSass from 'sass-embedded';
import { fileURLToPath } from 'node:url';
const __dirname =
import.meta.dirname ?? dirname(fileURLToPath(import.meta.url));
async function gatherProjectFiles() {
const jsFiles = {};
const scssFiles = {};
const jsGlob = new Glob('source/**/!(*.stories).{cjs,js,ts}', {
ignore: ['**/_*', 'source/@types/**', 'source/07-react/**'],
});
const scssGlob = new Glob('source/**/*.scss', jsGlob);
for await (const currentFile of jsGlob.iterate()) {
const filePaths = currentFile.split(path.sep);
const sourceDirIndex = filePaths.indexOf('source');
if (sourceDirIndex >= 0) {
const fileName = path.basename(currentFile).replace(/\.c?[jt]s$/, '');
const newFilePath = `js/${fileName}`;
// Throw an error if duplicate files detected.
if (jsFiles[newFilePath]) {
throw new Error(`More than one file named ${fileName}.[jt]s found.`);
}
jsFiles[newFilePath] = {
import: path.resolve(__dirname, currentFile),
};
}
}
for await (const currentFile of scssGlob.iterate()) {
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 (scssFiles[newFilePath]) {
throw new Error(`More that one file named ${fileName}.scss found.`);
}
scssFiles[newFilePath] = {
import: `./${currentFile}`,
};
}
}
return {
...jsFiles,
...scssFiles,
};
}
const commonConfig = {
entry: () => gatherProjectFiles(),
plugins: [
new MiniCssExtractPlugin(),
new RemovePlugin({
after: {
test: [
{
folder: './dist/css',
method: absolutePath => /\.js(\.map)?$/m.test(absolutePath),
recursive: true,
},
],
log: false,
logError: true,
logWarning: false,
},
}),
new StylelintPlugin({
exclude: ['node_modules', 'dist', 'storybook'],
}),
new SpriteLoaderPlugin(),
new ForkTsCheckerWebpackPlugin(),
],
context: __dirname,
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
// We will check types in fork plugin
transpileOnly: true,
},
resolve: {
fullySpecified: false,
},
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['swc-loader'],
resolve: {
fullySpecified: false,
},
},
{
test: /\.scss$/i,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
},
},
{
loader: 'css-loader',
options: {
esModule: false,
// Ignore /core/ URLs
url: {
filter: url => !url.includes('/core/'),
},
},
},
'postcss-loader',
{
loader: 'sass-loader',
options: {
implementation: embeddedSass,
webpackImporter: false,
sassOptions: {
loadPaths: [path.resolve(__dirname, 'source')],
// Hiding mixed declaration warnings for now.
// https://sass-lang.com/documentation/breaking-changes/mixed-decls/
silenceDeprecations: ['mixed-decls'],
},
},
},
],
},
{
test: /images\/_sprite-source-files\/.*\.svg$/,
exclude: /node_modules/,
use: [
{
loader: 'svg-sprite-loader',
options: {
extract: true,
spriteFilename: 'sprite.artifact.svg',
outputPath: 'images/',
},
},
'svg-transform-loader',
'svgo-loader',
],
},
{
test: /fonts\/.*\.(woff2?|ttf|otf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/i,
exclude: ['/node_modules/'],
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext][query]',
},
},
{
test: /\.(png|svg|jpg|gif|webp)$/i,
exclude: [/images\/_sprite-source-files\/.*\.svg$/, '/node_modules/'],
type: 'asset',
generator: {
filename: 'images/backgrounds/[hash][ext][query]',
},
},
],
},
externals: {
drupal: 'Drupal',
drupalSettings: 'drupalSettings',
once: 'once',
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
extensionAlias: {
'.es6': ['.es6.ts', '.es6.js'],
},
modules: [path.resolve(__dirname, 'source'), 'node_modules'],
enforceExtension: false,
},
output: {
path: path.resolve(__dirname, 'dist'),
clean: false,
},
stats: 'minimal',
};
export default commonConfig;