-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack-plugins.js
76 lines (73 loc) · 2.27 KB
/
webpack-plugins.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
const { generateTheme } = require("antd-theme-generator");
const path = require("path");
class AntDesignThemePlugin {
constructor(options) {
const defaulOptions = {
varFile: path.join(__dirname, "../../src/styles/variables.less"),
mainLessFile: path.join(__dirname, "../../src/styles/index.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(defaulOptions, options);
this.generated = false;
}
apply(compiler) {
const options = this.options;
compiler.plugin("emit", function(compilation, callback) {
const less = `
<link rel="stylesheet/less" type="text/css" href="${
options.publicPath
}/color.less" />
<script>
window.less = {
async: false,
env: 'production'
};
</script>
<script type="text/javascript" src="${options.lessUrl}"></script>
`;
if (
options.indexFileName &&
options.indexFileName in compilation.assets
) {
const index = compilation.assets[options.indexFileName];
let content = index.source();
const replaceContent = `<body>${less}`;
if (!content.match(/\/color\.less/g)) {
index.source = () =>
content.replace(less, "").replace(/<body>/gi, replaceContent);
content = index.source();
index.size = () => content.length;
}
}
if (options.generateOnce && this.colors) {
compilation.assets["color.less"] = {
source: () => this.colors,
size: () => this.colors.length
};
return callback();
}
generateTheme(options)
.then(css => {
if (options.generateOnce) {
this.colors = css;
}
compilation.assets["color.less"] = {
source: () => css,
size: () => css.length
};
callback();
})
.catch(err => {
callback(err);
});
});
}
}
module.exports = AntDesignThemePlugin;