This repository was archived by the owner on Apr 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (44 loc) · 1.27 KB
/
index.js
File metadata and controls
50 lines (44 loc) · 1.27 KB
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
const assert = require('assert').strict;
const webpack = require('webpack');
/**
* @typedef {import('webpack').Compiler} WebpackCompiler
*/
class CnameWebpackPlugin {
/**
* @param {object} options Plugin options.
* @param {string} options.domain CNAME file content.
*/
constructor(options) {
assert(options, `${CnameWebpackPlugin.name}: missing options`);
assert(options.domain, `${CnameWebpackPlugin.name}: missing domain`);
this.domain = options.domain;
}
/**
* @param {WebpackCompiler} compiler
*
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(CnameWebpackPlugin.name, (compilation) => {
compilation.hooks.additionalAssets.tap(CnameWebpackPlugin.name, () => {
compilation.emitAsset('CNAME', new webpack.sources.RawSource(this.domain));
});
});
}
}
class CnameWebpack4Plugin extends CnameWebpackPlugin {
/**
* @param {WebpackCompiler} compiler
*
* @returns {void}
*/
apply(compiler) {
compiler.hooks.emit.tap(CnameWebpackPlugin.name, (compilation) => {
compilation.assets.CNAME = {
source: () => this.domain,
size: () => this.domain.length,
};
});
}
}
module.exports = webpack.version.startsWith('4') ? CnameWebpack4Plugin : CnameWebpackPlugin;