-
Notifications
You must be signed in to change notification settings - Fork 81
/
index.js
113 lines (97 loc) · 3.3 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
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
const {generateTheme} = require("antd-theme-generator");
const webpack = require('webpack');
const {RawSource} = webpack.sources || require('webpack-sources');
const path = require("path");
class AntDesignThemePlugin {
constructor(options) {
const defaultOptions = {
varFile: path.join(__dirname, "../../src/styles/variables.less"),
antDir: path.join(__dirname, "../../node_modules/antd"),
stylesDir: path.join(__dirname, "../../src/styles/antd"),
themeVariables: ["@primary-color"],
indexFileName: "index.html",
generateOnce: false,
lessUrl:
"https://cdnjs.cloudflare.com/ajax/libs/less.js/2.7.2/less.min.js",
publicPath: ""
};
this.options = Object.assign(defaultOptions, options);
this.generated = false;
this.version = webpack.version;
}
apply(compiler) {
const pluginName = 'AntDesignThemePlugin';
if (this.version.startsWith('5.')) {
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
compilation.hooks.processAssets.tapAsync(
{
name: pluginName,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
},
(assets, callback) => this.addAssets(compilation, assets, callback)
);
});
}
else {
compiler.hooks.emit.tapAsync(pluginName, (compilation, callback) =>
this.addAssets(compilation, compilation.assets, callback));
}
}
addAssets(compilation, assets, callback) {
this.generateIndexContent(assets, compilation);
if (this.options.generateOnce && this.colors) {
this.generateColorStylesheet(compilation, this.colors);
return callback();
}
generateTheme(this.options)
.then(css => {
if (this.options.generateOnce) {
this.colors = css;
}
this.generateColorStylesheet(compilation, css);
callback();
})
.catch(err => {
callback(err);
});
};
generateIndexContent(assets, compilation) {
if (
this.options.indexFileName &&
this.options.indexFileName in assets
) {
const index = assets[this.options.indexFileName];
let content = index.source();
if (!content.match(/\/color\.less/g)) {
const less = `
<link rel="stylesheet/less" type="text/css" href="${this.options.publicPath}/color.less" />
<script>
window.less = {
async: false,
env: 'production'
};
</script>
<script type="text/javascript" src="${this.options.lessUrl}"></script>
`;
const updatedContent = content.replace(less, "").replace(/<body>/gi, `<body>${less}`);
if (this.version.startsWith('5.')) {
compilation.updateAsset(this.options.indexFileName, new RawSource(updatedContent), { size: updatedContent.length });
return;
}
index.source = () => updatedContent;
index.size = () => updatedContent.length;
}
}
};
generateColorStylesheet(compilation, source) {
if (this.version.startsWith('5.')) {
compilation.emitAsset('color.less', new RawSource(source), { size: source.length });
return;
}
compilation.assets['color.less'] = {
source: () => source,
size: () => source.length
};
};
}
module.exports = AntDesignThemePlugin;