-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.mjs
146 lines (120 loc) · 4.26 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
import Encore from '@symfony/webpack-encore';
import { glob } from 'glob';
import * as path from 'node:path';
// 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: componentPaths } = await import(
'drupal-ambientimpact-core/componentPaths'
);
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})/**/!(_)*.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.
//
// @see https://github.com/johnagan/clean-webpack-plugin
.cleanupOutputBeforeBuild(['**/*.css', '**/*.css.map'])
.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())
.enableSassLoader(function(options) {
options.sassOptions = {
includePaths: componentPaths().all,
};
})
.enablePostCssLoader(function(options) {
options.postcssOptions = {
plugins: [
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';
});
export default Encore.getWebpackConfig();