-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.mjs
223 lines (179 loc) · 6.21 KB
/
webpack.config.mjs
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import Encore from '@symfony/webpack-encore';
import { glob } from 'glob';
import * as path from 'node:path';
import { default as vendorize } from '@consensus.enterprises/pnp-vendorize';
// The remaining modules are CommonJS only. Because of this, they must be
// import()ed and destructured like so to behave similarly to ESM imports.
const { default: autoprefixer } = await import('autoprefixer');
const { default: baseThemeImporter } = await import(
'drupal-ambientimpact-base/baseThemeImporter',
);
const { default: componentPaths } = await import(
'drupal-ambientimpact-core/componentPaths',
);
const { default: easingGradients } = await import(
'@neurocracy/postcss-easing-gradients',
);
const { default: FaviconsWebpackPlugin } = await import(
'favicons-webpack-plugin',
);
const { default: RemoveEmptyScriptsPlugin } = await import(
'webpack-remove-empty-scripts',
);
const distPath = '.webpack-dist';
/**
* Whether to output to the paths where the source files are found.
*
* If this is true, compiled Sass files, source maps, etc. will be placed
* alongside their source files. If this is false, built files will be placed in
* the dist directory defined by distPath.
*
* @type {Boolean}
*/
const outputToSourcePaths = true;
/**
* Get globbed entry points.
*
* This uses the 'glob' package to automagically build the array of entry
* points, as there are a lot of them spread out over many components.
*
* @return {Object.<string, string>}
*
* @see https://www.npmjs.com/package/glob
*/
function getGlobbedEntries() {
/**
* Entries to be returned.
*
* @type {Object.<string, string>}
*
* @see Encore#addEntries()
* Explains expected format.
*/
let entries = {};
const results = glob.sync(
`./!(${distPath}|${vendorize.getVendorDirName()})/**/!(_)*.scss`,
);
for (const result of results) {
const parsed = path.parse(result);
// Note the leading './'
entries[`./${parsed.dir}/${parsed.name}`] = `./${result}`;
}
return entries;
};
// @see https://symfony.com/doc/current/frontend/encore/installation.html#creating-the-webpack-config-js-file
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore.setOutputPath(path.resolve(
path.dirname(new URL(import.meta.url).pathname),
(outputToSourcePaths ? '.' : distPath)
))
// Encore will complain if the public path doesn't start with a slash.
// Unfortunately, it doesn't seem Webpack's automatic public path works here.
//
// @see https://webpack.js.org/guides/public-path/#automatic-publicpath
.setPublicPath('/')
.setManifestKeyPrefix('')
// We output multiple files.
.disableSingleRuntimeChunk()
.configureFilenames({
// Since Webpack started out primarily for building JavaScript applications,
// it always outputs a JS files, even if empty. We place these in a temporary
// directory by default. Note that the 'webpack-remove-empty-scripts' plug-in
// should prevent these being output, but if there's an error while running
// Webpack, you'll get a nice 'temp' directory you can just delete.
js: 'temp/[name].js',
// Assets are left at their original locations and not hashed. The [query]
// must be left in to ensure any query string specified in the CSS is
// preserved.
//
// @see https://stackoverflow.com/questions/68737296/disable-asset-bundling-in-webpack-5#68768905
//
// @see https://github.com/webpack-contrib/css-loader/issues/889#issuecomment-1298907914
assets: '[file][query]',
})
.addEntries(getGlobbedEntries())
// Clean out any previously built files in case of source files being removed or
// renamed. We need to exclude the vendor directory or CSS bundled with
// libraries will get deleted.
//
// @see https://github.com/johnagan/clean-webpack-plugin
.cleanupOutputBeforeBuild([
'**/*.css', '**/*.css.map', `!${vendorize.getVendorDirName()}/**`
])
.enableSourceMaps(!Encore.isProduction())
// We don't use Babel so we can probably just remove all presets to speed it up.
//
// @see https://github.com/symfony/webpack-encore/issues/154#issuecomment-361277968
.configureBabel(function(config) {
config.presets = [];
})
// Remove any empty scripts Webpack would generate as we aren't a primarily
// JavaScript-based app and only output CSS and assets.
.addPlugin(new RemoveEmptyScriptsPlugin())
// @see https://www.npmjs.com/package/favicons-webpack-plugin
//
// @todo Switch to using the generated manifest.webmanifest and
// browserconfig.xml? The paths don't seem easily customizable.
.addPlugin(new FaviconsWebpackPlugin({
logo: './images/icons/icon.png',
logoMaskable: './images/icons/icon_maskable.png',
outputPath: './images/icons/generated',
favicons: {
appName: 'Omnipedia',
appShortName: 'Omnipedia',
start_url: '/',
// background: '#ffffff',
// theme_color: '#c07300',
display: 'standalone',
lang: 'en-GB',
// @todo This doesn't seem to add a version query string?
version: '1',
icons: {
// We provide our own rather than have them generated.
windows: false,
yandex: false,
},
},
}))
.enableSassLoader(function(options) {
options.sassOptions = {
importer: [baseThemeImporter],
includePaths: componentPaths().all,
};
})
.enablePostCssLoader(function(options) {
options.postcssOptions = {
plugins: [
easingGradients(),
autoprefixer(),
],
};
})
// Re-enable automatic public path for paths referenced in CSS.
//
// @see https://github.com/symfony/webpack-encore/issues/915#issuecomment-1189319882
.configureMiniCssExtractPlugin(function(config) {
config.publicPath = 'auto';
})
// Disable the Encore image rule because we provide our own loader config.
.configureImageRule({enabled: false})
// This disables asset bundling/copying for certain asset types.
//
// @see https://stackoverflow.com/questions/68737296/disable-asset-bundling-in-webpack-5#68768905
.addLoader({
test: /\.(png|jpe?g|gif|svg)$/i,
type: 'asset/resource',
generator: {
emit: false,
},
})
// Rewrite referenced vendor font paths to vendor directory.
.configureFontRule({
type: 'asset/resource',
filename: function(pathData) {
return vendorize.assetFileName(pathData);
},
});
export default Encore.getWebpackConfig();