@tuya-sat/micro-dev-loader 2.1.3 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,16 @@
1
1
  import { Compiler } from "webpack";
2
- interface StringObject {
3
- [key: string]: string;
4
- }
5
- interface CustomLessVarible {
6
- light: StringObject;
7
- dark: StringObject;
8
- }
9
2
  export default class ThemePlugin {
10
- private filePath;
11
- private customLessVarible;
3
+ private stylePath;
12
4
  private pluginName;
13
- private hash;
14
- private outputPath;
5
+ private jsFilePrefix;
15
6
  private lessFiles;
16
- constructor(filePath?: string, customLessVarible?: CustomLessVarible);
7
+ constructor(stylePath?: string);
17
8
  apply(compiler: Compiler): void;
18
9
  getBundledLessFile(): Promise<string>;
19
10
  runCssbundle(): Promise<string>;
20
- getLessScript(): any[];
21
- getLessVarible(): {
22
- key: string;
11
+ getCustomeVarible(): {}[];
12
+ getLessVarible(contentHash: any): {
13
+ fileName: string;
23
14
  content: string;
24
15
  }[];
25
16
  }
26
- export {};
@@ -20,10 +20,10 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
20
20
  var fse__default = /*#__PURE__*/_interopDefaultLegacy(fse);
21
21
 
22
22
  class ThemePlugin {
23
- constructor(filePath = "static/css", customLessVarible = { dark: {}, light: {} }) {
24
- this.filePath = filePath;
25
- this.customLessVarible = customLessVarible;
23
+ constructor(stylePath = "static/css") {
24
+ this.stylePath = stylePath;
26
25
  this.pluginName = "ThemePlugin";
26
+ this.jsFilePrefix = "static/js";
27
27
  this.lessFiles = [];
28
28
  }
29
29
  apply(compiler) {
@@ -47,22 +47,34 @@ class ThemePlugin {
47
47
  const { contenthash } = compilation.assetsInfo.get(pathname);
48
48
  const darkCssFileName = `dark.${contenthash}.css`;
49
49
  const lessFileName = `theme.${contenthash}.less`;
50
- compilation.emitAsset(`${this.filePath}/${darkCssFileName}`, new RawSource(content));
51
- compilation.emitAsset(`${this.filePath}/${lessFileName}`, new RawSource(lessContent));
52
- HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeEmit.tapAsync("LayoutMockPlugin", (data, cb) => {
53
- const $ = cheerio__default["default"].load(data.html);
54
- const firstJs = $("script")[0];
55
- $("link").map(function () {
56
- const reg = /(?<=\/)light.([\s\S]+?).css$/;
57
- const href = $(this).attr("href");
58
- if (reg.test(href)) {
59
- $(this).attr("id", "micro-default-theme-css");
60
- $(`<link href="${href}" rel="stylesheet/light" id="micro-light-theme-css">`).insertBefore($(this));
61
- $(`<link href="${href.replace(reg, darkCssFileName)}" rel="stylesheet/dark" id="micro-dark-theme-css">`).insertBefore($(this));
62
- $(`<link href="${href.replace(reg, lessFileName)}" rel="stylesheet/less" id="micro-theme-less">`).insertBefore(firstJs);
63
- }
50
+ const lessVarible = this.getLessVarible(contenthash);
51
+ compilation.emitAsset(`${this.stylePath}/${darkCssFileName}`, new RawSource(content));
52
+ compilation.emitAsset(`${this.stylePath}/${lessFileName}`, new RawSource(lessContent));
53
+ lessVarible.forEach(({ fileName, content }) => {
54
+ compilation.emitAsset(fileName, new RawSource(content));
55
+ });
56
+ HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeAssetTagGeneration.tapAsync(this.pluginName, (data, cb) => {
57
+ const { publicPath } = data.assets;
58
+ HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeEmit.tapAsync(this.pluginName, (data, cb) => {
59
+ const $ = cheerio__default["default"].load(data.html);
60
+ const firstJs = $("script")[0];
61
+ $("link").map(function () {
62
+ const reg = /(?<=\/)light.([\s\S]+?).css$/;
63
+ const href = $(this).attr("href");
64
+ if (reg.test(href)) {
65
+ $(this).attr("id", "micro-default-theme-css");
66
+ $(`<link href="${href}" rel="stylesheet/light" id="micro-light-theme-css">`).insertBefore($(this));
67
+ $(`<link href="${href.replace(reg, darkCssFileName)}" rel="stylesheet/dark" id="micro-dark-theme-css">`).insertBefore($(this));
68
+ lessVarible.forEach(({ fileName }) => {
69
+ const src = path__default["default"].join(publicPath, fileName);
70
+ $(`<script src="${src}" type="javascript/less" />`).insertBefore(firstJs);
71
+ });
72
+ $(`<link href="${href.replace(reg, lessFileName)}" rel="stylesheet/less" id="micro-theme-less">`).insertBefore(firstJs);
73
+ }
74
+ });
75
+ data.html = $.html();
76
+ cb(null, data);
64
77
  });
65
- data.html = $.html();
66
78
  cb(null, data);
67
79
  });
68
80
  callback();
@@ -89,9 +101,9 @@ class ThemePlugin {
89
101
  return less__default["default"].render(bufferString, {
90
102
  filename: file,
91
103
  javascriptEnabled: true,
92
- modifyVars: theme.getThemeVariables({
104
+ modifyVars: Object.assign(Object.assign({}, theme.getThemeVariables({
93
105
  dark: true,
94
- }),
106
+ })), { "entry-name": "dark" }),
95
107
  plugins: [createHandleImportPlugin()],
96
108
  });
97
109
  });
@@ -103,30 +115,43 @@ class ThemePlugin {
103
115
  return singleCssContent;
104
116
  });
105
117
  }
106
- getLessScript() {
107
- let jsSrc = [];
108
- this.getLessVarible().forEach(({ key, content }) => {
109
- fse__default["default"].writeFileSync(path__default["default"].join(this.outputPath, "static/js", key), content);
110
- jsSrc.push(path__default["default"].join("static/js", key));
111
- });
112
- return jsSrc;
118
+ getCustomeVarible() {
119
+ const lightFile = this.lessFiles.find((filePath) => /\/src\/styles\/light.less/.test(filePath));
120
+ const darkFile = this.lessFiles.find((filePath) => /\/src\/styles\/dark.less/.test(filePath));
121
+ return [
122
+ convertLessFileToVarible(lightFile),
123
+ convertLessFileToVarible(darkFile),
124
+ ];
125
+ function convertLessFileToVarible(filePath) {
126
+ if (!filePath) {
127
+ return {};
128
+ }
129
+ let content = fse__default["default"].readFileSync(filePath, "utf-8");
130
+ return content
131
+ .split(";")
132
+ .map((item) => item.split(":"))
133
+ .reduce((prev, [key, value]) => {
134
+ prev[key] = value;
135
+ return prev;
136
+ }, {});
137
+ }
113
138
  }
114
- getLessVarible() {
139
+ getLessVarible(contentHash) {
115
140
  let antdLightVarible = theme.getThemeVariables();
116
141
  let antdDarkVarible = theme.getThemeVariables({ dark: true });
117
142
  delete antdDarkVarible.hack;
118
143
  delete antdLightVarible.hack;
119
- const { dark, light } = this.customLessVarible;
120
- let lightLessVarible = Object.assign(Object.assign({}, antdLightVarible), light);
121
- let darkLessVarible = Object.assign(Object.assign({}, antdDarkVarible), dark);
144
+ const [customLightVarible, customDarkVarible] = this.getCustomeVarible();
145
+ let lightLessVarible = Object.assign(Object.assign({}, antdLightVarible), customLightVarible);
146
+ let darkLessVarible = Object.assign(Object.assign({}, antdDarkVarible), customDarkVarible);
122
147
  return [
123
148
  {
124
- key: `dark.${this.hash}.less.js`,
125
- content: `var dark = ${JSON.stringify(darkLessVarible)}`,
149
+ fileName: `${this.jsFilePrefix}/dark.${contentHash}.less.js`,
150
+ content: `window.darkLessVarible = ${JSON.stringify(darkLessVarible)}`,
126
151
  },
127
152
  {
128
- key: `light.${this.hash}.less.js`,
129
- content: `var light = ${JSON.stringify(lightLessVarible)}`,
153
+ fileName: `${this.jsFilePrefix}/light.${contentHash}.less.js`,
154
+ content: `window.lightLessVarible = ${JSON.stringify(lightLessVarible)}`,
130
155
  },
131
156
  ];
132
157
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-loader",
3
- "version": "2.1.3",
3
+ "version": "2.2.3",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {