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

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.
@@ -0,0 +1,9 @@
1
+ import { Declaration } from "postcss";
2
+ export declare const filterPlugin: {
3
+ (opts?: {}): {
4
+ postcssPlugin: string;
5
+ Rule(rule: any): void;
6
+ Declaration(decl: Declaration): void;
7
+ };
8
+ postcss: boolean;
9
+ };
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const filterPlugin = (opts = {}) => {
6
+ return {
7
+ postcssPlugin: "filter",
8
+ Rule(rule) {
9
+ if (!rule.nodes.length) {
10
+ rule.remove();
11
+ }
12
+ },
13
+ Declaration(decl) {
14
+ //@ts-ignore
15
+ if (decl.variable && decl.parent.selector === "html") {
16
+ decl.remove();
17
+ }
18
+ },
19
+ };
20
+ };
21
+ filterPlugin.postcss = true;
22
+
23
+ exports.filterPlugin = filterPlugin;
@@ -2,15 +2,9 @@ import { Compiler } from "webpack";
2
2
  export default class ThemePlugin {
3
3
  private stylePath;
4
4
  private pluginName;
5
- private jsFilePrefix;
6
5
  private lessFiles;
7
6
  constructor(stylePath?: string);
8
7
  apply(compiler: Compiler): void;
9
- getBundledLessFile(): Promise<string>;
10
8
  runCssbundle(): Promise<string>;
11
- getCustomeVarible(): {}[];
12
- getLessVarible(contentHash: any): {
13
- fileName: string;
14
- content: string;
15
- }[];
9
+ filterCssVarible(css: string): string;
16
10
  }
@@ -3,27 +3,30 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var tslib = require('tslib');
6
- var theme = require('antd/dist/theme');
7
6
  var HtmlWebpackPlugin = require('html-webpack-plugin');
8
7
  var cheerio = require('cheerio');
9
8
  var less = require('less');
10
- var path = require('path');
11
9
  var fse = require('fs-extra');
12
- var bundle = require('./bundle.js');
10
+ var microUtils = require('@tuya-sat/micro-utils');
11
+ var postcss = require('../../node_modules/postcss/lib/postcss.js');
12
+ var path = require('path');
13
+ var filterPlugin = require('./filterPlugin.js');
13
14
 
14
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
16
 
16
17
  var HtmlWebpackPlugin__default = /*#__PURE__*/_interopDefaultLegacy(HtmlWebpackPlugin);
17
18
  var cheerio__default = /*#__PURE__*/_interopDefaultLegacy(cheerio);
18
19
  var less__default = /*#__PURE__*/_interopDefaultLegacy(less);
19
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
20
20
  var fse__default = /*#__PURE__*/_interopDefaultLegacy(fse);
21
+ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
21
22
 
23
+ // const darkfile
24
+ const darkVariableFile = require.resolve("./dark.variable.less");
25
+ const darkVariableContent = fse__default["default"].readFileSync(darkVariableFile, "utf-8");
22
26
  class ThemePlugin {
23
27
  constructor(stylePath = "static/css") {
24
28
  this.stylePath = stylePath;
25
29
  this.pluginName = "ThemePlugin";
26
- this.jsFilePrefix = "static/js";
27
30
  this.lessFiles = [];
28
31
  }
29
32
  apply(compiler) {
@@ -42,22 +45,16 @@ class ThemePlugin {
42
45
  compilation.chunkGraph.getChunkModules(chunk).forEach((item) => {
43
46
  this.lessFiles.push(item.nameForCondition());
44
47
  });
48
+ compilation.updateAsset(pathname, (source) => {
49
+ return new RawSource(this.filterCssVarible(source.source().toString()));
50
+ });
45
51
  const content = yield this.runCssbundle();
46
- const lessContent = yield this.getBundledLessFile();
47
52
  const { contenthash } = compilation.assetsInfo.get(pathname);
48
53
  const darkCssFileName = `dark.${contenthash}.css`;
49
- const lessFileName = `theme.${contenthash}.less`;
50
- const lessVarible = this.getLessVarible(contenthash);
51
54
  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
55
  HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeAssetTagGeneration.tapAsync(this.pluginName, (data, cb) => {
57
- const { publicPath } = data.assets;
58
56
  HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeEmit.tapAsync(this.pluginName, (data, cb) => {
59
57
  const $ = cheerio__default["default"].load(data.html);
60
- const firstJs = $("script")[0];
61
58
  $("link").map(function () {
62
59
  const reg = /(?<=\/)light.([\s\S]+?).css$/;
63
60
  const href = $(this).attr("href");
@@ -65,11 +62,6 @@ class ThemePlugin {
65
62
  $(this).attr("id", "micro-default-theme-css");
66
63
  $(`<link href="${href}" rel="stylesheet/light" id="micro-light-theme-css">`).insertBefore($(this));
67
64
  $(`<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
65
  }
74
66
  });
75
67
  data.html = $.html();
@@ -82,28 +74,15 @@ class ThemePlugin {
82
74
  });
83
75
  }
84
76
  }
85
- getBundledLessFile() {
86
- return tslib.__awaiter(this, void 0, void 0, function* () {
87
- let fakefile = "";
88
- const cwd = process.cwd();
89
- this.lessFiles.forEach((file) => {
90
- const relativePath = path__default["default"].relative(cwd, file);
91
- fakefile += `@import "${relativePath}";\n`;
92
- });
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" }));
94
- return file;
95
- });
96
- }
97
77
  runCssbundle() {
98
78
  return tslib.__awaiter(this, void 0, void 0, function* () {
99
79
  const lessTasks = this.lessFiles.map((file) => {
100
80
  let bufferString = fse__default["default"].readFileSync(file, "utf-8");
81
+ bufferString = `${bufferString}`;
101
82
  return less__default["default"].render(bufferString, {
102
83
  filename: file,
103
84
  javascriptEnabled: true,
104
- modifyVars: Object.assign(Object.assign({}, theme.getThemeVariables({
105
- dark: true,
106
- })), { "entry-name": "dark" }),
85
+ modifyVars: Object.assign(Object.assign({}, microUtils.theme.getAntdOverWriteFile().dark), { "entry-name": "dark", "root-entry-name": "variable" }),
107
86
  plugins: [createHandleImportPlugin()],
108
87
  });
109
88
  });
@@ -112,48 +91,12 @@ class ThemePlugin {
112
91
  cssContents.forEach((content) => {
113
92
  singleCssContent += content.css + "\n";
114
93
  });
115
- return singleCssContent;
94
+ return this.filterCssVarible(singleCssContent);
116
95
  });
117
96
  }
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
- }
138
- }
139
- getLessVarible(contentHash) {
140
- let antdLightVarible = theme.getThemeVariables();
141
- let antdDarkVarible = theme.getThemeVariables({ dark: true });
142
- delete antdDarkVarible.hack;
143
- delete antdLightVarible.hack;
144
- const [customLightVarible, customDarkVarible] = this.getCustomeVarible();
145
- let lightLessVarible = Object.assign(Object.assign({}, antdLightVarible), customLightVarible);
146
- let darkLessVarible = Object.assign(Object.assign({}, antdDarkVarible), customDarkVarible);
147
- return [
148
- {
149
- fileName: `${this.jsFilePrefix}/dark.${contentHash}.less.js`,
150
- content: `window.darkLessVarible = ${JSON.stringify(darkLessVarible)}`,
151
- },
152
- {
153
- fileName: `${this.jsFilePrefix}/light.${contentHash}.less.js`,
154
- content: `window.lightLessVarible = ${JSON.stringify(lightLessVarible)}`,
155
- },
156
- ];
97
+ filterCssVarible(css) {
98
+ //@ts-ignore
99
+ return postcss["default"]([filterPlugin.filterPlugin]).process(css).css;
157
100
  }
158
101
  }
159
102
  function createHandleImportPlugin() {
@@ -171,7 +114,17 @@ class HandleImport extends less.FileManager {
171
114
  });
172
115
  return tslib.__awaiter(this, void 0, void 0, function* () {
173
116
  if (filename.startsWith("~")) {
174
- return _super.loadFile.call(this, filename.replace("~", ""), currentDirectory, options, environment);
117
+ let filenamed = filename.replace("~", "");
118
+ let fullPath = path__default["default"].join(currentDirectory, filename);
119
+ if (fullPath.endsWith("style/themes/variable.less")) {
120
+ return {
121
+ filename: filenamed.startsWith("antd")
122
+ ? require.resolve(filenamed)
123
+ : fullPath,
124
+ contents: darkVariableContent,
125
+ };
126
+ }
127
+ return _super.loadFile.call(this, filenamed, currentDirectory, options, environment);
175
128
  }
176
129
  return _super.loadFile.call(this, filename, currentDirectory, options, environment);
177
130
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-loader",
3
- "version": "2.2.4",
3
+ "version": "2.2.8",
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.8",
18
18
  "cheerio": "1.0.0-rc.10",
19
19
  "ejs": "3.1.6",
20
20
  "fs-extra": "10.0.1",
@@ -32,7 +32,7 @@
32
32
  "@types/less": "3.0.3",
33
33
  "jest": "27.5.1",
34
34
  "rollup": "2.70.1",
35
- "rollup-plugin-copy": "3.4.0",
35
+ "rollup-copy-smartly": "^1.0.2",
36
36
  "rollup-plugin-delete": "2.0.0",
37
37
  "rollup-plugin-typescript2": "0.31.2",
38
38
  "ts-jest": "27.1.3",
@@ -1,123 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var fs = require('fs');
6
- var path = require('path');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
11
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
12
-
13
- const stringLiteralRegex = /.*(?:'|")(.*)(?:'|").*/;
14
- const lessFileRegex = /.less$/;
15
- const cssFileRegex = /.css$/;
16
- const imports = [];
17
- const writers = [];
18
- const output = [];
19
- function buildContents(lines, filePath, lessVarible) {
20
- let currentLines = [];
21
- let line;
22
- let hashPath;
23
- let imported;
24
- let file;
25
- let splitLines;
26
- if (typeof imports[filePath] === "undefined") {
27
- imports[filePath] = true;
28
- }
29
- for (var index = 0; index < lines.length; ++index) {
30
- line = lines[index].trim();
31
- if (line.indexOf("@import ") === 0) {
32
- // We found an import statement
33
- if (currentLines.length > 0) {
34
- writers.push(new Writer(currentLines));
35
- currentLines = [];
36
- }
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
- });
45
- if (!(lessFileRegex.test(imported) || cssFileRegex.test(imported))) {
46
- imported += ".less";
47
- }
48
- if (imported.match(/^~/)) {
49
- hashPath = require.resolve(imported.replace("~", ""));
50
- }
51
- else {
52
- hashPath = path__default["default"].resolve(filePath, "..", imported);
53
- }
54
- if (typeof imports[hashPath] === "undefined") {
55
- imports[hashPath] = true;
56
- file = fs__default["default"].readFileSync(hashPath, "utf8");
57
- splitLines = file.split(/\r\n|\n/);
58
- splitLines[0] = splitLines[0].trim();
59
- buildContents(splitLines, hashPath, lessVarible);
60
- }
61
- continue;
62
- }
63
- currentLines.push(lines[index]);
64
- }
65
- // Push all remaining lines
66
- writers.push(new Writer(currentLines));
67
- return index;
68
- }
69
- function compress(data, path, lessVarible) {
70
- return new Promise(function (resolve, reject) {
71
- var splitLines = data.split(/\r\n|\n/);
72
- splitLines[0] = splitLines[0].trim();
73
- buildContents(splitLines, path, lessVarible);
74
- let previousLine = "";
75
- writers.forEach((writer) => {
76
- previousLine = writer.write(output, previousLine);
77
- });
78
- while (!output[output.length - 1]) {
79
- output.pop();
80
- }
81
- // Make sure the file ends in a new line.
82
- if (!!output[output.length - 1].trim()) {
83
- output.push("");
84
- }
85
- resolve(output.join("\n"));
86
- });
87
- }
88
- class Writer {
89
- /**
90
- * Creates a new Writer.
91
- *
92
- * @param operator The array of lines to associate with this writer.
93
- */
94
- constructor(operator) {
95
- if (Array.isArray(operator)) {
96
- this.__lines = operator;
97
- return;
98
- }
99
- }
100
- /**
101
- * Writes the lines for this writer into the output array.
102
- *
103
- * @param output The output lines to which the Writer's lines should be appended.
104
- * @param previousLine The previous line that was added to output.
105
- */
106
- write(output, previousLine) {
107
- // If this writer has a module associated with it, we want to first add the
108
- // lines for the module before adding the lines for this writer.
109
- var trim;
110
- // Add every line to the output.
111
- this.__lines.forEach((line) => {
112
- trim = line.trim();
113
- if (!previousLine && !trim) {
114
- return;
115
- }
116
- output.push(line);
117
- previousLine = trim;
118
- });
119
- return previousLine;
120
- }
121
- }
122
-
123
- exports["default"] = compress;