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

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
+ private getCustomeVarible;
12
+ getLessVarible(contentHash: any): Promise<{
13
+ fileName: string;
23
14
  content: string;
24
- }[];
15
+ }[]>;
25
16
  }
26
- export {};
@@ -10,6 +10,7 @@ var less = require('less');
10
10
  var path = require('path');
11
11
  var fse = require('fs-extra');
12
12
  var bundle = require('./bundle.js');
13
+ var microUtils = require('@tuya-sat/micro-utils');
13
14
 
14
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
16
 
@@ -20,10 +21,10 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
20
21
  var fse__default = /*#__PURE__*/_interopDefaultLegacy(fse);
21
22
 
22
23
  class ThemePlugin {
23
- constructor(filePath = "static/css", customLessVarible = { dark: {}, light: {} }) {
24
- this.filePath = filePath;
25
- this.customLessVarible = customLessVarible;
24
+ constructor(stylePath = "static/css") {
25
+ this.stylePath = stylePath;
26
26
  this.pluginName = "ThemePlugin";
27
+ this.jsFilePrefix = "static/js";
27
28
  this.lessFiles = [];
28
29
  }
29
30
  apply(compiler) {
@@ -47,22 +48,34 @@ class ThemePlugin {
47
48
  const { contenthash } = compilation.assetsInfo.get(pathname);
48
49
  const darkCssFileName = `dark.${contenthash}.css`;
49
50
  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
- }
51
+ const lessVarible = yield this.getLessVarible(contenthash);
52
+ compilation.emitAsset(`${this.stylePath}/${darkCssFileName}`, new RawSource(content));
53
+ compilation.emitAsset(`${this.stylePath}/${lessFileName}`, new RawSource(lessContent));
54
+ lessVarible.forEach(({ fileName, content }) => {
55
+ compilation.emitAsset(fileName, new RawSource(content));
56
+ });
57
+ HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeAssetTagGeneration.tapAsync(this.pluginName, (data, cb) => {
58
+ const { publicPath } = data.assets;
59
+ HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeEmit.tapAsync(this.pluginName, (data, cb) => {
60
+ const $ = cheerio__default["default"].load(data.html);
61
+ const firstJs = $("script")[0];
62
+ $("link").map(function () {
63
+ const reg = /(?<=\/)light.([\s\S]+?).css$/;
64
+ const href = $(this).attr("href");
65
+ if (reg.test(href)) {
66
+ $(this).attr("id", "micro-default-theme-css");
67
+ $(`<link href="${href}" rel="stylesheet/light" id="micro-light-theme-css">`).insertBefore($(this));
68
+ $(`<link href="${href.replace(reg, darkCssFileName)}" rel="stylesheet/dark" id="micro-dark-theme-css">`).insertBefore($(this));
69
+ lessVarible.forEach(({ fileName }) => {
70
+ const src = path__default["default"].join(publicPath, fileName);
71
+ $(`<script src="${src}" type="javascript/less" />`).insertBefore(firstJs);
72
+ });
73
+ $(`<link href="${href.replace(reg, lessFileName)}" rel="stylesheet/less" id="micro-theme-less">`).insertBefore(firstJs);
74
+ }
75
+ });
76
+ data.html = $.html();
77
+ cb(null, data);
64
78
  });
65
- data.html = $.html();
66
79
  cb(null, data);
67
80
  });
68
81
  callback();
@@ -78,7 +91,7 @@ class ThemePlugin {
78
91
  const relativePath = path__default["default"].relative(cwd, file);
79
92
  fakefile += `@import "${relativePath}";\n`;
80
93
  });
81
- const file = yield bundle["default"](fakefile, path__default["default"].resolve(cwd, "fake.less"));
94
+ const file = yield bundle["default"](fakefile, path__default["default"].resolve(cwd, "fake.less"), Object.assign(Object.assign({}, theme.getThemeVariables({ dark: true })), { "entry-name": "light" }));
82
95
  return file;
83
96
  });
84
97
  }
@@ -89,9 +102,9 @@ class ThemePlugin {
89
102
  return less__default["default"].render(bufferString, {
90
103
  filename: file,
91
104
  javascriptEnabled: true,
92
- modifyVars: theme.getThemeVariables({
105
+ modifyVars: Object.assign(Object.assign(Object.assign({}, theme.getThemeVariables({
93
106
  dark: true,
94
- }),
107
+ })), microUtils.theme.getAntdOverWriteFile().dark), { "entry-name": "dark" }),
95
108
  plugins: [createHandleImportPlugin()],
96
109
  });
97
110
  });
@@ -103,32 +116,35 @@ class ThemePlugin {
103
116
  return singleCssContent;
104
117
  });
105
118
  }
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));
119
+ getCustomeVarible() {
120
+ return tslib.__awaiter(this, void 0, void 0, function* () {
121
+ return [
122
+ yield microUtils.theme.getCustomLightVaribleFile(),
123
+ yield microUtils.theme.getCustomDarkVaribleFile(),
124
+ ];
111
125
  });
112
- return jsSrc;
113
126
  }
114
- getLessVarible() {
115
- let antdLightVarible = theme.getThemeVariables();
116
- let antdDarkVarible = theme.getThemeVariables({ dark: true });
117
- delete antdDarkVarible.hack;
118
- 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);
122
- return [
123
- {
124
- key: `dark.${this.hash}.less.js`,
125
- content: `var dark = ${JSON.stringify(darkLessVarible)}`,
126
- },
127
- {
128
- key: `light.${this.hash}.less.js`,
129
- content: `var light = ${JSON.stringify(lightLessVarible)}`,
130
- },
131
- ];
127
+ getLessVarible(contentHash) {
128
+ return tslib.__awaiter(this, void 0, void 0, function* () {
129
+ let antdLightVarible = theme.getThemeVariables();
130
+ let antdDarkVarible = theme.getThemeVariables({ dark: true });
131
+ const antdOverWriteVarible = microUtils.theme.getAntdOverWriteFile();
132
+ delete antdDarkVarible.hack;
133
+ delete antdLightVarible.hack;
134
+ const [customLightVarible, customDarkVarible] = yield this.getCustomeVarible();
135
+ let lightLessVarible = Object.assign(Object.assign(Object.assign({}, antdLightVarible), antdOverWriteVarible.light), customLightVarible);
136
+ let darkLessVarible = Object.assign(Object.assign(Object.assign({}, antdDarkVarible), antdOverWriteVarible.dark), customDarkVarible);
137
+ return [
138
+ {
139
+ fileName: `${this.jsFilePrefix}/dark.${contentHash}.less.js`,
140
+ content: `window.darkLessVarible = ${JSON.stringify(darkLessVarible)}`,
141
+ },
142
+ {
143
+ fileName: `${this.jsFilePrefix}/light.${contentHash}.less.js`,
144
+ content: `window.lightLessVarible = ${JSON.stringify(lightLessVarible)}`,
145
+ },
146
+ ];
147
+ });
132
148
  }
133
149
  }
134
150
  function createHandleImportPlugin() {
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.5",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -14,7 +14,7 @@
14
14
  "@babel/generator": "7.17.7",
15
15
  "@babel/plugin-syntax-typescript": "7.16.7",
16
16
  "@babel/template": "7.16.7",
17
- "@tuya-sat/micro-utils": "2.0.0",
17
+ "@tuya-sat/micro-utils": "2.2.5",
18
18
  "cheerio": "1.0.0-rc.10",
19
19
  "ejs": "3.1.6",
20
20
  "fs-extra": "10.0.1",