forked from mzohaibqc/antd-theme-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (64 loc) · 2.11 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
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"
};
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="/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();
if (!content.match(/\/color\.less/g)) {
index.source = () =>
content.replace(less, "").replace(/<body>/gi, `<body>${less}`);
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;