@tuya-sat/micro-dev-loader 2.1.2 → 2.2.4

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 +1,3 @@
1
- export default function compress(data: string, path: string): Promise<string>;
1
+ export default function compress(data: string, path: string, lessVarible: {
2
+ [key: string]: any;
3
+ }): Promise<string>;
@@ -16,7 +16,7 @@ const cssFileRegex = /.css$/;
16
16
  const imports = [];
17
17
  const writers = [];
18
18
  const output = [];
19
- function buildContents(lines, filePath) {
19
+ function buildContents(lines, filePath, lessVarible) {
20
20
  let currentLines = [];
21
21
  let line;
22
22
  let hashPath;
@@ -35,11 +35,18 @@ function buildContents(lines, filePath) {
35
35
  currentLines = [];
36
36
  }
37
37
  imported = line.replace(stringLiteralRegex, "$1");
38
+ //处理变量路径(目前,处理不了文件内的变量)
39
+ imported = imported.replace(/\@\{([^}]+)\}/, (match, p1) => {
40
+ if (lessVarible[p1] !== undefined) {
41
+ return lessVarible[p1];
42
+ }
43
+ return p1;
44
+ });
38
45
  if (!(lessFileRegex.test(imported) || cssFileRegex.test(imported))) {
39
46
  imported += ".less";
40
47
  }
41
48
  if (imported.match(/^~/)) {
42
- hashPath = imported.replace("~", path__default["default"].join(__dirname, "../../"));
49
+ hashPath = require.resolve(imported.replace("~", ""));
43
50
  }
44
51
  else {
45
52
  hashPath = path__default["default"].resolve(filePath, "..", imported);
@@ -49,7 +56,7 @@ function buildContents(lines, filePath) {
49
56
  file = fs__default["default"].readFileSync(hashPath, "utf8");
50
57
  splitLines = file.split(/\r\n|\n/);
51
58
  splitLines[0] = splitLines[0].trim();
52
- buildContents(splitLines, hashPath);
59
+ buildContents(splitLines, hashPath, lessVarible);
53
60
  }
54
61
  continue;
55
62
  }
@@ -59,11 +66,11 @@ function buildContents(lines, filePath) {
59
66
  writers.push(new Writer(currentLines));
60
67
  return index;
61
68
  }
62
- function compress(data, path) {
69
+ function compress(data, path, lessVarible) {
63
70
  return new Promise(function (resolve, reject) {
64
71
  var splitLines = data.split(/\r\n|\n/);
65
72
  splitLines[0] = splitLines[0].trim();
66
- buildContents(splitLines, path);
73
+ buildContents(splitLines, path, lessVarible);
67
74
  let previousLine = "";
68
75
  writers.forEach((writer) => {
69
76
  previousLine = writer.write(output, previousLine);
@@ -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 {};
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var tslib = require('tslib');
6
- var theme = require('../../node_modules/antd/dist/theme.js');
6
+ var theme = require('antd/dist/theme');
7
7
  var HtmlWebpackPlugin = require('html-webpack-plugin');
8
8
  var cheerio = require('cheerio');
9
9
  var less = require('less');
@@ -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();
@@ -78,7 +90,7 @@ class ThemePlugin {
78
90
  const relativePath = path__default["default"].relative(cwd, file);
79
91
  fakefile += `@import "${relativePath}";\n`;
80
92
  });
81
- const file = yield bundle["default"](fakefile, path__default["default"].resolve(cwd, "fake.less"));
93
+ const file = yield bundle["default"](fakefile, path__default["default"].resolve(cwd, "fake.less"), Object.assign(Object.assign({}, theme.getThemeVariables({ dark: true })), { "entry-name": "dark" }));
82
94
  return file;
83
95
  });
84
96
  }
@@ -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["default"].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() {
115
- let antdLightVarible = theme["default"].getThemeVariables();
116
- let antdDarkVarible = theme["default"].getThemeVariables({ dark: true });
139
+ getLessVarible(contentHash) {
140
+ let antdLightVarible = theme.getThemeVariables();
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.2",
3
+ "version": "2.2.4",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {