@tuya-sat/micro-dev-loader 2.2.6 → 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
- private getCustomeVarible;
12
- getLessVarible(contentHash: any): Promise<{
13
- fileName: string;
14
- content: string;
15
- }[]>;
9
+ filterCssVarible(css: string): string;
16
10
  }
@@ -3,28 +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');
13
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');
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 path__default = /*#__PURE__*/_interopDefaultLegacy(path);
22
22
 
23
+ // const darkfile
24
+ const darkVariableFile = require.resolve("./dark.variable.less");
25
+ const darkVariableContent = fse__default["default"].readFileSync(darkVariableFile, "utf-8");
23
26
  class ThemePlugin {
24
27
  constructor(stylePath = "static/css") {
25
28
  this.stylePath = stylePath;
26
29
  this.pluginName = "ThemePlugin";
27
- this.jsFilePrefix = "static/js";
28
30
  this.lessFiles = [];
29
31
  }
30
32
  apply(compiler) {
@@ -43,22 +45,16 @@ class ThemePlugin {
43
45
  compilation.chunkGraph.getChunkModules(chunk).forEach((item) => {
44
46
  this.lessFiles.push(item.nameForCondition());
45
47
  });
48
+ compilation.updateAsset(pathname, (source) => {
49
+ return new RawSource(this.filterCssVarible(source.source().toString()));
50
+ });
46
51
  const content = yield this.runCssbundle();
47
- const lessContent = yield this.getBundledLessFile();
48
52
  const { contenthash } = compilation.assetsInfo.get(pathname);
49
53
  const darkCssFileName = `dark.${contenthash}.css`;
50
- const lessFileName = `theme.${contenthash}.less`;
51
- const lessVarible = yield this.getLessVarible(contenthash);
52
54
  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
55
  HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeAssetTagGeneration.tapAsync(this.pluginName, (data, cb) => {
58
- const { publicPath } = data.assets;
59
56
  HtmlWebpackPlugin__default["default"].getHooks(compilation).beforeEmit.tapAsync(this.pluginName, (data, cb) => {
60
57
  const $ = cheerio__default["default"].load(data.html);
61
- const firstJs = $("script")[0];
62
58
  $("link").map(function () {
63
59
  const reg = /(?<=\/)light.([\s\S]+?).css$/;
64
60
  const href = $(this).attr("href");
@@ -66,11 +62,6 @@ class ThemePlugin {
66
62
  $(this).attr("id", "micro-default-theme-css");
67
63
  $(`<link href="${href}" rel="stylesheet/light" id="micro-light-theme-css">`).insertBefore($(this));
68
64
  $(`<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
65
  }
75
66
  });
76
67
  data.html = $.html();
@@ -83,28 +74,15 @@ class ThemePlugin {
83
74
  });
84
75
  }
85
76
  }
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
77
  runCssbundle() {
99
78
  return tslib.__awaiter(this, void 0, void 0, function* () {
100
79
  const lessTasks = this.lessFiles.map((file) => {
101
80
  let bufferString = fse__default["default"].readFileSync(file, "utf-8");
81
+ bufferString = `${bufferString}`;
102
82
  return less__default["default"].render(bufferString, {
103
83
  filename: file,
104
84
  javascriptEnabled: true,
105
- modifyVars: Object.assign(Object.assign(Object.assign({}, theme.getThemeVariables({
106
- dark: true,
107
- })), microUtils.theme.getAntdOverWriteFile().dark), { "entry-name": "dark" }),
85
+ modifyVars: Object.assign(Object.assign({}, microUtils.theme.getAntdOverWriteFile().dark), { "entry-name": "dark", "root-entry-name": "variable" }),
108
86
  plugins: [createHandleImportPlugin()],
109
87
  });
110
88
  });
@@ -113,38 +91,12 @@ class ThemePlugin {
113
91
  cssContents.forEach((content) => {
114
92
  singleCssContent += content.css + "\n";
115
93
  });
116
- return singleCssContent;
94
+ return this.filterCssVarible(singleCssContent);
117
95
  });
118
96
  }
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
- });
97
+ filterCssVarible(css) {
98
+ //@ts-ignore
99
+ return postcss["default"]([filterPlugin.filterPlugin]).process(css).css;
148
100
  }
149
101
  }
150
102
  function createHandleImportPlugin() {
@@ -162,7 +114,17 @@ class HandleImport extends less.FileManager {
162
114
  });
163
115
  return tslib.__awaiter(this, void 0, void 0, function* () {
164
116
  if (filename.startsWith("~")) {
165
- 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);
166
128
  }
167
129
  return _super.loadFile.call(this, filename, currentDirectory, options, environment);
168
130
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuya-sat/micro-dev-loader",
3
- "version": "2.2.6",
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.2.6",
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;