-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (73 loc) · 2.32 KB
/
index.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
/**
* @file
* Main plugin class.
*/
const { basename } = require('path');
const { ExternalsPlugin, sources } = require('webpack');
const { parseEntries } = require('./lib/assets');
const { librariesMatcher } = require('./lib/libraries');
const { writeFile } = require('./lib/output');
/**
* Webpack externals matcher function.
*
* @param {{ request?: string }} ctx Object containing details of the file.
* @param {WebpackExternalsCallback} callback Callback function used to indicate
* how the module should be externalized.
*/
const externalsMatcher = ({ request = '' }, callback) => {
const library = librariesMatcher(request);
callback(undefined, library ? library.external : undefined);
};
module.exports = class DrupalPlugin {
constructor({
filename = 'assets.php',
processor = DrupalPlugin.maybeMinified,
extensionName = '',
libraryName = '[name]',
} = {}) {
/** @protected */
this.filename = filename;
/** @protected */
this.processor = processor;
/** @protected */
this.extensionName = extensionName;
/** @protected */
this.libraryName = libraryName;
}
/**
* Applies the plugin onto the webpack compiler.
*
* @param {import('webpack').Compiler} compiler The Webpack compiler.
*/
apply(compiler) {
// Apply Drupal libraries as externals.
new ExternalsPlugin('var', externalsMatcher).apply(compiler);
compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
const extensionName =
this.extensionName || basename(compilation.options.context || '');
compilation.hooks.additionalAssets.tap(this.constructor.name, () => {
// eslint-disable-next-line no-param-reassign
compilation.assets[this.filename] = new sources.RawSource(
writeFile(
parseEntries(compilation, {
processor: this.processor,
extensionName,
libraryName: this.libraryName,
}),
),
);
});
});
}
/**
* Adds minified property to files when minified by Webpack.
*
* @param {FileProcessorArguments} args Arguments.
* @return {Object} File properties.
*/
static maybeMinified({ filename, compilation }) {
return compilation.options.optimization.minimize && filename.endsWith('.js')
? { minified: true }
: {};
}
};