@tuya-sat/micro-dev-loader 2.0.0 → 2.1.2
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.
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -2
- package/dist/plugins/theme/bundle.d.ts +1 -0
- package/dist/plugins/theme/bundle.js +116 -0
- package/dist/plugins/theme/index.d.ts +26 -0
- package/dist/plugins/theme/index.js +156 -0
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,16 +3,17 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var loaderUtils = require('loader-utils');
|
|
6
|
-
var index$
|
|
6
|
+
var index$2 = require('./codeMaker/index.js');
|
|
7
7
|
var microUtils = require('@tuya-sat/micro-utils');
|
|
8
8
|
var index = require('./plugins/layoutMock/index.js');
|
|
9
|
+
var index$1 = require('./plugins/theme/index.js');
|
|
9
10
|
|
|
10
11
|
const addjsLoader = function (sourceCode) {
|
|
11
12
|
var _a;
|
|
12
13
|
const { authedCode = [], microFramework } = loaderUtils.getOptions(this);
|
|
13
14
|
const manifest = microUtils.parseManifest();
|
|
14
15
|
const isMobile = ((_a = manifest.supportedPlatform) === null || _a === void 0 ? void 0 : _a[0]) === "MOBILE";
|
|
15
|
-
const codeMaker = new (index$
|
|
16
|
+
const codeMaker = new (index$2.createCodeMaker(isMobile))({
|
|
16
17
|
sourceCode,
|
|
17
18
|
microFramework,
|
|
18
19
|
});
|
|
@@ -24,4 +25,5 @@ const addjsLoader = function (sourceCode) {
|
|
|
24
25
|
};
|
|
25
26
|
|
|
26
27
|
exports.LayoutMockPlugin = index["default"];
|
|
28
|
+
exports.ThemePlugin = index$1["default"];
|
|
27
29
|
exports["default"] = addjsLoader;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function compress(data: string, path: string): Promise<string>;
|
|
@@ -0,0 +1,116 @@
|
|
|
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) {
|
|
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
|
+
if (!(lessFileRegex.test(imported) || cssFileRegex.test(imported))) {
|
|
39
|
+
imported += ".less";
|
|
40
|
+
}
|
|
41
|
+
if (imported.match(/^~/)) {
|
|
42
|
+
hashPath = imported.replace("~", path__default["default"].join(__dirname, "../../"));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
hashPath = path__default["default"].resolve(filePath, "..", imported);
|
|
46
|
+
}
|
|
47
|
+
if (typeof imports[hashPath] === "undefined") {
|
|
48
|
+
imports[hashPath] = true;
|
|
49
|
+
file = fs__default["default"].readFileSync(hashPath, "utf8");
|
|
50
|
+
splitLines = file.split(/\r\n|\n/);
|
|
51
|
+
splitLines[0] = splitLines[0].trim();
|
|
52
|
+
buildContents(splitLines, hashPath);
|
|
53
|
+
}
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
currentLines.push(lines[index]);
|
|
57
|
+
}
|
|
58
|
+
// Push all remaining lines
|
|
59
|
+
writers.push(new Writer(currentLines));
|
|
60
|
+
return index;
|
|
61
|
+
}
|
|
62
|
+
function compress(data, path) {
|
|
63
|
+
return new Promise(function (resolve, reject) {
|
|
64
|
+
var splitLines = data.split(/\r\n|\n/);
|
|
65
|
+
splitLines[0] = splitLines[0].trim();
|
|
66
|
+
buildContents(splitLines, path);
|
|
67
|
+
let previousLine = "";
|
|
68
|
+
writers.forEach((writer) => {
|
|
69
|
+
previousLine = writer.write(output, previousLine);
|
|
70
|
+
});
|
|
71
|
+
while (!output[output.length - 1]) {
|
|
72
|
+
output.pop();
|
|
73
|
+
}
|
|
74
|
+
// Make sure the file ends in a new line.
|
|
75
|
+
if (!!output[output.length - 1].trim()) {
|
|
76
|
+
output.push("");
|
|
77
|
+
}
|
|
78
|
+
resolve(output.join("\n"));
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
class Writer {
|
|
82
|
+
/**
|
|
83
|
+
* Creates a new Writer.
|
|
84
|
+
*
|
|
85
|
+
* @param operator The array of lines to associate with this writer.
|
|
86
|
+
*/
|
|
87
|
+
constructor(operator) {
|
|
88
|
+
if (Array.isArray(operator)) {
|
|
89
|
+
this.__lines = operator;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Writes the lines for this writer into the output array.
|
|
95
|
+
*
|
|
96
|
+
* @param output The output lines to which the Writer's lines should be appended.
|
|
97
|
+
* @param previousLine The previous line that was added to output.
|
|
98
|
+
*/
|
|
99
|
+
write(output, previousLine) {
|
|
100
|
+
// If this writer has a module associated with it, we want to first add the
|
|
101
|
+
// lines for the module before adding the lines for this writer.
|
|
102
|
+
var trim;
|
|
103
|
+
// Add every line to the output.
|
|
104
|
+
this.__lines.forEach((line) => {
|
|
105
|
+
trim = line.trim();
|
|
106
|
+
if (!previousLine && !trim) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
output.push(line);
|
|
110
|
+
previousLine = trim;
|
|
111
|
+
});
|
|
112
|
+
return previousLine;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
exports["default"] = compress;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Compiler } from "webpack";
|
|
2
|
+
interface StringObject {
|
|
3
|
+
[key: string]: string;
|
|
4
|
+
}
|
|
5
|
+
interface CustomLessVarible {
|
|
6
|
+
light: StringObject;
|
|
7
|
+
dark: StringObject;
|
|
8
|
+
}
|
|
9
|
+
export default class ThemePlugin {
|
|
10
|
+
private filePath;
|
|
11
|
+
private customLessVarible;
|
|
12
|
+
private pluginName;
|
|
13
|
+
private hash;
|
|
14
|
+
private outputPath;
|
|
15
|
+
private lessFiles;
|
|
16
|
+
constructor(filePath?: string, customLessVarible?: CustomLessVarible);
|
|
17
|
+
apply(compiler: Compiler): void;
|
|
18
|
+
getBundledLessFile(): Promise<string>;
|
|
19
|
+
runCssbundle(): Promise<string>;
|
|
20
|
+
getLessScript(): any[];
|
|
21
|
+
getLessVarible(): {
|
|
22
|
+
key: string;
|
|
23
|
+
content: string;
|
|
24
|
+
}[];
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var tslib = require('tslib');
|
|
6
|
+
var theme = require('../../node_modules/antd/dist/theme.js');
|
|
7
|
+
var HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
8
|
+
var cheerio = require('cheerio');
|
|
9
|
+
var less = require('less');
|
|
10
|
+
var path = require('path');
|
|
11
|
+
var fse = require('fs-extra');
|
|
12
|
+
var bundle = require('./bundle.js');
|
|
13
|
+
|
|
14
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
|
+
|
|
16
|
+
var HtmlWebpackPlugin__default = /*#__PURE__*/_interopDefaultLegacy(HtmlWebpackPlugin);
|
|
17
|
+
var cheerio__default = /*#__PURE__*/_interopDefaultLegacy(cheerio);
|
|
18
|
+
var less__default = /*#__PURE__*/_interopDefaultLegacy(less);
|
|
19
|
+
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
20
|
+
var fse__default = /*#__PURE__*/_interopDefaultLegacy(fse);
|
|
21
|
+
|
|
22
|
+
class ThemePlugin {
|
|
23
|
+
constructor(filePath = "static/css", customLessVarible = { dark: {}, light: {} }) {
|
|
24
|
+
this.filePath = filePath;
|
|
25
|
+
this.customLessVarible = customLessVarible;
|
|
26
|
+
this.pluginName = "ThemePlugin";
|
|
27
|
+
this.lessFiles = [];
|
|
28
|
+
}
|
|
29
|
+
apply(compiler) {
|
|
30
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
31
|
+
if (isProduction) {
|
|
32
|
+
const { RawSource } = compiler.webpack.sources;
|
|
33
|
+
compiler.hooks.thisCompilation.tap(this.pluginName, (compilation) => {
|
|
34
|
+
compilation.hooks.processAssets.tapAsync({
|
|
35
|
+
name: this.pluginName,
|
|
36
|
+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
|
|
37
|
+
}, (assets, callback) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const [pathname] = Object.entries(assets).find(([pathname]) => {
|
|
39
|
+
return /\/light\.[\s\S]+?\.css$/.test(pathname);
|
|
40
|
+
});
|
|
41
|
+
const chunk = compilation.namedChunks.get("light");
|
|
42
|
+
compilation.chunkGraph.getChunkModules(chunk).forEach((item) => {
|
|
43
|
+
this.lessFiles.push(item.nameForCondition());
|
|
44
|
+
});
|
|
45
|
+
const content = yield this.runCssbundle();
|
|
46
|
+
const lessContent = yield this.getBundledLessFile();
|
|
47
|
+
const { contenthash } = compilation.assetsInfo.get(pathname);
|
|
48
|
+
const darkCssFileName = `dark.${contenthash}.css`;
|
|
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
|
+
}
|
|
64
|
+
});
|
|
65
|
+
data.html = $.html();
|
|
66
|
+
cb(null, data);
|
|
67
|
+
});
|
|
68
|
+
callback();
|
|
69
|
+
}));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
getBundledLessFile() {
|
|
74
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
let fakefile = "";
|
|
76
|
+
const cwd = process.cwd();
|
|
77
|
+
this.lessFiles.forEach((file) => {
|
|
78
|
+
const relativePath = path__default["default"].relative(cwd, file);
|
|
79
|
+
fakefile += `@import "${relativePath}";\n`;
|
|
80
|
+
});
|
|
81
|
+
const file = yield bundle["default"](fakefile, path__default["default"].resolve(cwd, "fake.less"));
|
|
82
|
+
return file;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
runCssbundle() {
|
|
86
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
const lessTasks = this.lessFiles.map((file) => {
|
|
88
|
+
let bufferString = fse__default["default"].readFileSync(file, "utf-8");
|
|
89
|
+
return less__default["default"].render(bufferString, {
|
|
90
|
+
filename: file,
|
|
91
|
+
javascriptEnabled: true,
|
|
92
|
+
modifyVars: theme["default"].getThemeVariables({
|
|
93
|
+
dark: true,
|
|
94
|
+
}),
|
|
95
|
+
plugins: [createHandleImportPlugin()],
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
const cssContents = yield Promise.all(lessTasks);
|
|
99
|
+
let singleCssContent = "";
|
|
100
|
+
cssContents.forEach((content) => {
|
|
101
|
+
singleCssContent += content.css + "\n";
|
|
102
|
+
});
|
|
103
|
+
return singleCssContent;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
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;
|
|
113
|
+
}
|
|
114
|
+
getLessVarible() {
|
|
115
|
+
let antdLightVarible = theme["default"].getThemeVariables();
|
|
116
|
+
let antdDarkVarible = theme["default"].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
|
+
];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function createHandleImportPlugin() {
|
|
135
|
+
return {
|
|
136
|
+
install(lessInstance, pluginManager) {
|
|
137
|
+
pluginManager.addFileManager(new HandleImport());
|
|
138
|
+
},
|
|
139
|
+
minVersion: [3, 0, 0],
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
class HandleImport extends less.FileManager {
|
|
143
|
+
loadFile(filename, currentDirectory, options, environment) {
|
|
144
|
+
const _super = Object.create(null, {
|
|
145
|
+
loadFile: { get: () => super.loadFile }
|
|
146
|
+
});
|
|
147
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
if (filename.startsWith("~")) {
|
|
149
|
+
return _super.loadFile.call(this, filename.replace("~", ""), currentDirectory, options, environment);
|
|
150
|
+
}
|
|
151
|
+
return _super.loadFile.call(this, filename, currentDirectory, options, environment);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
exports["default"] = ThemePlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tuya-sat/micro-dev-loader",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
@@ -20,13 +20,16 @@
|
|
|
20
20
|
"fs-extra": "10.0.1",
|
|
21
21
|
"html-webpack-plugin": "5.5.0",
|
|
22
22
|
"less": "4.1.2",
|
|
23
|
+
"less-bundle-promise": "1.0.11",
|
|
23
24
|
"loader-utils": "3.2.0",
|
|
24
|
-
"path": "0.12.7"
|
|
25
|
+
"path": "0.12.7",
|
|
26
|
+
"tslib": "^2.3.1"
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
29
|
"@rollup/plugin-commonjs": "21.0.2",
|
|
28
30
|
"@rollup/plugin-node-resolve": "13.1.3",
|
|
29
31
|
"@types/jest": "27.4.1",
|
|
32
|
+
"@types/less": "3.0.3",
|
|
30
33
|
"jest": "27.5.1",
|
|
31
34
|
"rollup": "2.70.1",
|
|
32
35
|
"rollup-plugin-copy": "3.4.0",
|