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

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
- private getCustomeVarible;
12
- getLessVarible(contentHash: any): Promise<{
13
- fileName: string;
14
- content: string;
15
- }[]>;
9
+ filterCssVarible(css: string): string;
16
10
  }
@@ -3,28 +3,31 @@
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');
13
10
  var microUtils = require('@tuya-sat/micro-utils');
11
+ var postcss = require('postcss');
12
+ var path = require('path');
13
+ var filterPlugin = require('./filterPlugin.js');
14
14
 
15
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
16
16
 
17
17
  var HtmlWebpackPlugin__default = /*#__PURE__*/_interopDefaultLegacy(HtmlWebpackPlugin);
18
18
  var cheerio__default = /*#__PURE__*/_interopDefaultLegacy(cheerio);
19
19
  var less__default = /*#__PURE__*/_interopDefaultLegacy(less);
20
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
21
20
  var fse__default = /*#__PURE__*/_interopDefaultLegacy(fse);
21
+ var postcss__default = /*#__PURE__*/_interopDefaultLegacy(postcss);
22
+ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
22
23
 
24
+ // const darkfile
25
+ const darkVariableFile = require.resolve("./dark.variable.less");
26
+ const darkVariableContent = fse__default["default"].readFileSync(darkVariableFile, "utf-8");
23
27
  class ThemePlugin {
24
28
  constructor(stylePath = "static/css") {
25
29
  this.stylePath = stylePath;
26
30
  this.pluginName = "ThemePlugin";
27
- this.jsFilePrefix = "static/js";
28
31
  this.lessFiles = [];
29
32
  }
30
33
  apply(compiler) {
@@ -43,22 +46,16 @@ class ThemePlugin {
43
46
  compilation.chunkGraph.getChunkModules(chunk).forEach((item) => {
44
47
  this.lessFiles.push(item.nameForCondition());
45
48
  });
49
+ compilation.updateAsset(pathname, (source) => {
50
+ return new RawSource(this.filterCssVarible(source.source().toString()));
51
+ });
46
52
  const content = yield this.runCssbundle();
47
- const lessContent = yield this.getBundledLessFile();
48
53
  const { contenthash } = compilation.assetsInfo.get(pathname);
49
54
  const darkCssFileName = `dark.${contenthash}.css`;
50
- const lessFileName = `theme.${contenthash}.less`;
51
- const lessVarible = yield this.getLessVarible(contenthash);
52
55
  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
56
  HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeAssetTagGeneration.tapAsync(this.pluginName, (data, cb) => {
58
- const { publicPath } = data.assets;
59
57
  HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeEmit.tapAsync(this.pluginName, (data, cb) => {
60
58
  const $ = cheerio__default["default"].load(data.html);
61
- const firstJs = $("script")[0];
62
59
  $("link").map(function () {
63
60
  const reg = /(?<=\/)light.([\s\S]+?).css$/;
64
61
  const href = $(this).attr("href");
@@ -66,11 +63,6 @@ class ThemePlugin {
66
63
  $(this).attr("id", "micro-default-theme-css");
67
64
  $(`<link href="${href}" rel="stylesheet/light" id="micro-light-theme-css">`).insertBefore($(this));
68
65
  $(`<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
66
  }
75
67
  });
76
68
  data.html = $.html();
@@ -83,28 +75,15 @@ class ThemePlugin {
83
75
  });
84
76
  }
85
77
  }
86
- getBundledLessFile() {
87
- return tslib.__awaiter(this, void 0, void 0, function* () {
88
- let fakefile = "";
89
- const cwd = process.cwd();
90
- this.lessFiles.forEach((file) => {
91
- const relativePath = path__default["default"].relative(cwd, file);
92
- fakefile += `@import "${relativePath}";\n`;
93
- });
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" }));
95
- return file;
96
- });
97
- }
98
78
  runCssbundle() {
99
79
  return tslib.__awaiter(this, void 0, void 0, function* () {
100
80
  const lessTasks = this.lessFiles.map((file) => {
101
81
  let bufferString = fse__default["default"].readFileSync(file, "utf-8");
82
+ bufferString = `${bufferString}`;
102
83
  return less__default["default"].render(bufferString, {
103
84
  filename: file,
104
85
  javascriptEnabled: true,
105
- modifyVars: Object.assign(Object.assign(Object.assign({}, theme.getThemeVariables({
106
- dark: true,
107
- })), microUtils.theme.getAntdOverWriteFile().dark), { "entry-name": "dark" }),
86
+ modifyVars: Object.assign(Object.assign({}, microUtils.theme.getAntdOverWriteFile().dark), { "entry-name": "dark", "root-entry-name": "variable" }),
108
87
  plugins: [createHandleImportPlugin()],
109
88
  });
110
89
  });
@@ -113,38 +92,12 @@ class ThemePlugin {
113
92
  cssContents.forEach((content) => {
114
93
  singleCssContent += content.css + "\n";
115
94
  });
116
- return singleCssContent;
95
+ return this.filterCssVarible(singleCssContent);
117
96
  });
118
97
  }
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
- ];
125
- });
126
- }
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
- });
98
+ filterCssVarible(css) {
99
+ //@ts-ignore
100
+ return postcss__default["default"]([filterPlugin.filterPlugin]).process(css).css;
148
101
  }
149
102
  }
150
103
  function createHandleImportPlugin() {
@@ -162,7 +115,17 @@ class HandleImport extends less.FileManager {
162
115
  });
163
116
  return tslib.__awaiter(this, void 0, void 0, function* () {
164
117
  if (filename.startsWith("~")) {
165
- return _super.loadFile.call(this, filename.replace("~", ""), currentDirectory, options, environment);
118
+ let filenamed = filename.replace("~", "");
119
+ let fullPath = path__default["default"].join(currentDirectory, filename);
120
+ if (fullPath.endsWith("style/themes/variable.less")) {
121
+ return {
122
+ filename: filenamed.startsWith("antd")
123
+ ? require.resolve(filenamed)
124
+ : fullPath,
125
+ contents: darkVariableContent,
126
+ };
127
+ }
128
+ return _super.loadFile.call(this, filenamed, currentDirectory, options, environment);
166
129
  }
167
130
  return _super.loadFile.call(this, filename, currentDirectory, options, environment);
168
131
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-loader",
3
- "version": "2.2.5",
3
+ "version": "2.2.9",
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.2.5",
17
+ "@tuya-sat/micro-utils": "2.2.9",
18
18
  "cheerio": "1.0.0-rc.10",
19
19
  "ejs": "3.1.6",
20
20
  "fs-extra": "10.0.1",
@@ -23,7 +23,8 @@
23
23
  "less-bundle-promise": "1.0.11",
24
24
  "loader-utils": "3.2.0",
25
25
  "path": "0.12.7",
26
- "tslib": "^2.3.1"
26
+ "postcss": "8.4.12",
27
+ "tslib": "2.3.1"
27
28
  },
28
29
  "devDependencies": {
29
30
  "@rollup/plugin-commonjs": "21.0.2",
@@ -32,7 +33,7 @@
32
33
  "@types/less": "3.0.3",
33
34
  "jest": "27.5.1",
34
35
  "rollup": "2.70.1",
35
- "rollup-plugin-copy": "3.4.0",
36
+ "rollup-copy-smartly": "1.0.2",
36
37
  "rollup-plugin-delete": "2.0.0",
37
38
  "rollup-plugin-typescript2": "0.31.2",
38
39
  "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;