@oipage/cli 0.2.1 → 0.3.0-alpha.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/.github/FUNDING.yml +11 -11
- package/AUTHORS.txt +6 -6
- package/CHANGELOG +40 -21
- package/LICENSE +20 -20
- package/README.md +132 -132
- package/bin/WebSocket/decodeWsFrame.js +43 -43
- package/bin/WebSocket/encodeWsFrame.js +28 -28
- package/bin/WebSocket/headersToJSON.js +26 -26
- package/bin/WebSocket/index.js +106 -83
- package/bin/data/mineTypes.json +104 -104
- package/bin/disk.js +42 -42
- package/bin/help.js +53 -50
- package/bin/intercept.js +15 -15
- package/bin/network.js +21 -21
- package/bin/notebook/index.js +9 -0
- package/bin/proxy.js +82 -75
- package/bin/run +142 -134
- package/bin/serve.js +245 -195
- package/bin/template/404.html +184 -184
- package/bin/tools/deleteEmptyFolder.js +33 -33
- package/bin/tools/format.js +75 -75
- package/bin/tools/network.js +42 -42
- package/bin/tools/openBrowser.js +34 -25
- package/bin/tools/resolve404.js +98 -83
- package/bin/tools/resolveImport.js +89 -88
- package/bin/website-htmls/components/ui-select-file/index.html +8 -8
- package/bin/website-htmls/components/ui-select-file/index.js +42 -42
- package/bin/website-htmls/components/ui-select-file/index.scss +44 -44
- package/bin/website-htmls/dialogs/imageSize/index.html +54 -54
- package/bin/website-htmls/dialogs/imageSize/index.js +53 -53
- package/bin/website-htmls/dialogs/imageSize/index.scss +140 -140
- package/bin/website-htmls/dialogs/index.js +52 -52
- package/bin/website-htmls/images/addFile.svg +1 -1
- package/bin/website-htmls/images/img-to-pdf.svg +1 -1
- package/bin/website-htmls/index.html +17 -17
- package/bin/website-htmls/main.js +13 -13
- package/bin/website-htmls/pages/App/index.html +6 -6
- package/bin/website-htmls/pages/App/index.js +22 -22
- package/bin/website-htmls/pages/App/index.scss +43 -43
- package/bin/website-htmls/pages/appStore/index.html +7 -7
- package/bin/website-htmls/pages/appStore/index.js +19 -19
- package/bin/website-htmls/pages/appStore/index.scss +54 -54
- package/bin/website-htmls/pages/chart/index.html +8 -8
- package/bin/website-htmls/pages/chart/index.js +72 -72
- package/bin/website-htmls/pages/chart/index.scss +71 -71
- package/bin/website-htmls/pages/home/index.html +19 -19
- package/bin/website-htmls/pages/home/index.js +19 -19
- package/bin/website-htmls/pages/home/index.scss +87 -87
- package/bin/website-htmls/pages/image-editor/index.html +27 -27
- package/bin/website-htmls/pages/image-editor/index.js +106 -106
- package/bin/website-htmls/pages/image-editor/index.scss +69 -69
- package/bin/website-htmls/pages/img-to-pdf/index.html +3 -3
- package/bin/website-htmls/pages/img-to-pdf/index.js +44 -44
- package/bin/website-htmls/pages/img-to-pdf/index.scss +9 -9
- package/bin/website-htmls/pages/recorder-screen/index.html +15 -15
- package/bin/website-htmls/pages/recorder-screen/index.js +227 -227
- package/bin/website-htmls/pages/recorder-screen/index.scss +52 -52
- package/bin/website-htmls/router.config.js +29 -29
- package/bin/website-htmls/styles/common.css +44 -44
- package/bin/website-htmls/styles/normalize.css +94 -94
- package/bin/website-plugins/intercept/chart.js +33 -33
- package/bin/website-plugins/intercept/getPackage.js +17 -17
- package/bin/website-plugins/intercept/head.js +8 -8
- package/bin/website-plugins/intercept/index.js +8 -8
- package/bin/website-plugins/intercept/oipage-vislite-intercept.js +55 -55
- package/bin/website-plugins/intercept/oipage-zipaper-intercept.js +36 -36
- package/bin/website-plugins/loader/index.js +11 -11
- package/bin/website-plugins/loader/oipage-html-loader.js +7 -7
- package/bin/website-plugins/loader/oipage-scss-loader.js +149 -149
- package/docs/config.md +84 -84
- package/package.json +42 -42
- package/src/index.js +28 -28
- package/types/serve.d.ts +76 -76
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
const headFactory = require("./head.js");
|
|
2
|
-
const getPackage = require("./getPackage.js");
|
|
3
|
-
|
|
4
|
-
const head = headFactory();
|
|
5
|
-
|
|
6
|
-
// VISLite库
|
|
7
|
-
module.exports = {
|
|
8
|
-
test: /^vislite$/,
|
|
9
|
-
handler(request, response) {
|
|
10
|
-
head["Content-Type"] = "application/javascript;charset=utf-8";
|
|
11
|
-
head["ETag"] = "VISLite@v" + require("vislite/package.json").version;
|
|
12
|
-
|
|
13
|
-
if (request.headers["if-none-match"] === head["ETag"]) {
|
|
14
|
-
response.writeHead('304', head);
|
|
15
|
-
response.end();
|
|
16
|
-
console.log("<i> \x1b[1m\x1b[32m[OIPage-http-server] Cache File: VISLite\x1b[0m " + new Date().toLocaleString() + "\x1b[33m\x1b[1m 304\x1b[0m");;
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
response.writeHead(200, head);
|
|
21
|
-
|
|
22
|
-
let source =getPackage("vislite/lib/index.umd.min.js");
|
|
23
|
-
|
|
24
|
-
response.write(`let module = { exports: {} };
|
|
25
|
-
let exports = module.exports;
|
|
26
|
-
${source}
|
|
27
|
-
export let Cardinal = module.exports.Cardinal;
|
|
28
|
-
export let Hermite = module.exports.Hermite;
|
|
29
|
-
export let Matrix4 = module.exports.Matrix4;
|
|
30
|
-
export let rotate = module.exports.rotate;
|
|
31
|
-
export let move = module.exports.move;
|
|
32
|
-
export let scale = module.exports.scale;
|
|
33
|
-
export let getLoopColors = module.exports.getLoopColors;
|
|
34
|
-
export let animation = module.exports.animation;
|
|
35
|
-
export let ruler = module.exports.ruler;
|
|
36
|
-
export let SVG = module.exports.SVG;
|
|
37
|
-
export let Canvas = module.exports.Canvas;
|
|
38
|
-
export let RawCanvas = module.exports.RawCanvas;
|
|
39
|
-
export let getWebGLContext = module.exports.getWebGLContext;
|
|
40
|
-
export let Shader = module.exports.Shader;
|
|
41
|
-
export let Texture = module.exports.Texture;
|
|
42
|
-
export let Buffer = module.exports.Buffer;
|
|
43
|
-
export let Eoap = module.exports.Eoap;
|
|
44
|
-
export let Mercator = module.exports.Mercator;
|
|
45
|
-
export let throttle = module.exports.throttle;
|
|
46
|
-
export let assemble = module.exports.assemble;
|
|
47
|
-
export let MapCoordinate = module.exports.MapCoordinate;
|
|
48
|
-
export let TreeLayout = module.exports.TreeLayout;
|
|
49
|
-
export let PieLayout = module.exports.PieLayout;
|
|
50
|
-
export let BarLayout = module.exports.BarLayout;
|
|
51
|
-
export let initOption = module.exports.initOption;
|
|
52
|
-
export let mergeOption = module.exports.mergeOption;
|
|
53
|
-
`);
|
|
54
|
-
response.end();
|
|
55
|
-
}
|
|
1
|
+
const headFactory = require("./head.js");
|
|
2
|
+
const getPackage = require("./getPackage.js");
|
|
3
|
+
|
|
4
|
+
const head = headFactory();
|
|
5
|
+
|
|
6
|
+
// VISLite库
|
|
7
|
+
module.exports = {
|
|
8
|
+
test: /^vislite$/,
|
|
9
|
+
handler(request, response) {
|
|
10
|
+
head["Content-Type"] = "application/javascript;charset=utf-8";
|
|
11
|
+
head["ETag"] = "VISLite@v" + require("vislite/package.json").version;
|
|
12
|
+
|
|
13
|
+
if (request.headers["if-none-match"] === head["ETag"]) {
|
|
14
|
+
response.writeHead('304', head);
|
|
15
|
+
response.end();
|
|
16
|
+
console.log("<i> \x1b[1m\x1b[32m[OIPage-http-server] Cache File: VISLite\x1b[0m " + new Date().toLocaleString() + "\x1b[33m\x1b[1m 304\x1b[0m");;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
response.writeHead(200, head);
|
|
21
|
+
|
|
22
|
+
let source =getPackage("vislite/lib/index.umd.min.js");
|
|
23
|
+
|
|
24
|
+
response.write(`let module = { exports: {} };
|
|
25
|
+
let exports = module.exports;
|
|
26
|
+
${source}
|
|
27
|
+
export let Cardinal = module.exports.Cardinal;
|
|
28
|
+
export let Hermite = module.exports.Hermite;
|
|
29
|
+
export let Matrix4 = module.exports.Matrix4;
|
|
30
|
+
export let rotate = module.exports.rotate;
|
|
31
|
+
export let move = module.exports.move;
|
|
32
|
+
export let scale = module.exports.scale;
|
|
33
|
+
export let getLoopColors = module.exports.getLoopColors;
|
|
34
|
+
export let animation = module.exports.animation;
|
|
35
|
+
export let ruler = module.exports.ruler;
|
|
36
|
+
export let SVG = module.exports.SVG;
|
|
37
|
+
export let Canvas = module.exports.Canvas;
|
|
38
|
+
export let RawCanvas = module.exports.RawCanvas;
|
|
39
|
+
export let getWebGLContext = module.exports.getWebGLContext;
|
|
40
|
+
export let Shader = module.exports.Shader;
|
|
41
|
+
export let Texture = module.exports.Texture;
|
|
42
|
+
export let Buffer = module.exports.Buffer;
|
|
43
|
+
export let Eoap = module.exports.Eoap;
|
|
44
|
+
export let Mercator = module.exports.Mercator;
|
|
45
|
+
export let throttle = module.exports.throttle;
|
|
46
|
+
export let assemble = module.exports.assemble;
|
|
47
|
+
export let MapCoordinate = module.exports.MapCoordinate;
|
|
48
|
+
export let TreeLayout = module.exports.TreeLayout;
|
|
49
|
+
export let PieLayout = module.exports.PieLayout;
|
|
50
|
+
export let BarLayout = module.exports.BarLayout;
|
|
51
|
+
export let initOption = module.exports.initOption;
|
|
52
|
+
export let mergeOption = module.exports.mergeOption;
|
|
53
|
+
`);
|
|
54
|
+
response.end();
|
|
55
|
+
}
|
|
56
56
|
};
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
const headFactory = require("./head.js");
|
|
2
|
-
const getPackage = require("./getPackage.js");
|
|
3
|
-
|
|
4
|
-
const head = headFactory();
|
|
5
|
-
|
|
6
|
-
// Zipaper框架
|
|
7
|
-
module.exports = {
|
|
8
|
-
test: /^zipaper$/,
|
|
9
|
-
handler(request, response) {
|
|
10
|
-
head["Content-Type"] = "application/javascript;charset=utf-8";
|
|
11
|
-
head["ETag"] = "Zipaper@v" + require("zipaper/package.json").version;
|
|
12
|
-
|
|
13
|
-
if (request.headers["if-none-match"] === head["ETag"]) {
|
|
14
|
-
response.writeHead('304', head);
|
|
15
|
-
response.end();
|
|
16
|
-
console.log("<i> \x1b[1m\x1b[32m[OIPage-http-server] Cache File: Zipaper\x1b[0m " + new Date().toLocaleString() + "\x1b[33m\x1b[1m 304\x1b[0m");;
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
response.writeHead(200, head);
|
|
21
|
-
|
|
22
|
-
let source = getPackage("zipaper/dist/Zipaper.min.js");
|
|
23
|
-
|
|
24
|
-
response.write(`let module = { exports: {} };
|
|
25
|
-
let exports = module.exports;
|
|
26
|
-
${source}
|
|
27
|
-
export let createApp = exports.createApp;
|
|
28
|
-
export let defineDirective = exports.defineDirective;
|
|
29
|
-
export let defineElement = exports.defineElement;
|
|
30
|
-
export let defineRouter = exports.defineRouter;
|
|
31
|
-
export let ref = exports.ref;
|
|
32
|
-
export let reactive = exports.reactive;
|
|
33
|
-
export let watcher = exports.watcher;
|
|
34
|
-
`);
|
|
35
|
-
response.end();
|
|
36
|
-
}
|
|
1
|
+
const headFactory = require("./head.js");
|
|
2
|
+
const getPackage = require("./getPackage.js");
|
|
3
|
+
|
|
4
|
+
const head = headFactory();
|
|
5
|
+
|
|
6
|
+
// Zipaper框架
|
|
7
|
+
module.exports = {
|
|
8
|
+
test: /^zipaper$/,
|
|
9
|
+
handler(request, response) {
|
|
10
|
+
head["Content-Type"] = "application/javascript;charset=utf-8";
|
|
11
|
+
head["ETag"] = "Zipaper@v" + require("zipaper/package.json").version;
|
|
12
|
+
|
|
13
|
+
if (request.headers["if-none-match"] === head["ETag"]) {
|
|
14
|
+
response.writeHead('304', head);
|
|
15
|
+
response.end();
|
|
16
|
+
console.log("<i> \x1b[1m\x1b[32m[OIPage-http-server] Cache File: Zipaper\x1b[0m " + new Date().toLocaleString() + "\x1b[33m\x1b[1m 304\x1b[0m");;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
response.writeHead(200, head);
|
|
21
|
+
|
|
22
|
+
let source = getPackage("zipaper/dist/Zipaper.min.js");
|
|
23
|
+
|
|
24
|
+
response.write(`let module = { exports: {} };
|
|
25
|
+
let exports = module.exports;
|
|
26
|
+
${source}
|
|
27
|
+
export let createApp = exports.createApp;
|
|
28
|
+
export let defineDirective = exports.defineDirective;
|
|
29
|
+
export let defineElement = exports.defineElement;
|
|
30
|
+
export let defineRouter = exports.defineRouter;
|
|
31
|
+
export let ref = exports.ref;
|
|
32
|
+
export let reactive = exports.reactive;
|
|
33
|
+
export let watcher = exports.watcher;
|
|
34
|
+
`);
|
|
35
|
+
response.end();
|
|
36
|
+
}
|
|
37
37
|
};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
const OIPageScssLoader = require("./oipage-scss-loader.js");
|
|
2
|
-
const OIPageHtmlLoader = require("./oipage-html-loader.js");
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
rules: [{
|
|
6
|
-
test: /\.scss$/,
|
|
7
|
-
use: OIPageScssLoader
|
|
8
|
-
}, {
|
|
9
|
-
test: /\.html$/,
|
|
10
|
-
use: OIPageHtmlLoader
|
|
11
|
-
}]
|
|
1
|
+
const OIPageScssLoader = require("./oipage-scss-loader.js");
|
|
2
|
+
const OIPageHtmlLoader = require("./oipage-html-loader.js");
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
rules: [{
|
|
6
|
+
test: /\.scss$/,
|
|
7
|
+
use: OIPageScssLoader
|
|
8
|
+
}, {
|
|
9
|
+
test: /\.html$/,
|
|
10
|
+
use: OIPageHtmlLoader
|
|
11
|
+
}]
|
|
12
12
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const { parseTemplate } = require("xhtml-to-json");
|
|
2
|
-
|
|
3
|
-
module.exports = function (source) {
|
|
4
|
-
if (this.entry) return source;
|
|
5
|
-
|
|
6
|
-
this.setFileType("application/javascript");
|
|
7
|
-
return `export default ${JSON.stringify(parseTemplate(source).toJson())}`;
|
|
1
|
+
const { parseTemplate } = require("xhtml-to-json");
|
|
2
|
+
|
|
3
|
+
module.exports = function (source) {
|
|
4
|
+
if (this.entry) return source;
|
|
5
|
+
|
|
6
|
+
this.setFileType("application/javascript");
|
|
7
|
+
return `export default ${JSON.stringify(parseTemplate(source).toJson())}`;
|
|
8
8
|
};
|
|
@@ -1,150 +1,150 @@
|
|
|
1
|
-
// 参考:https://github.com/zxl20070701/zxl20070701.github.io/blob/master/bin/loader/scss.js
|
|
2
|
-
let toSelector = function (preSelectorArray, deep) {
|
|
3
|
-
let selectors = preSelectorArray[0], i, j, k;
|
|
4
|
-
for (i = 1; i < deep; i++) {
|
|
5
|
-
let temp = [];
|
|
6
|
-
for (j = 0; j < selectors.length; j++) {
|
|
7
|
-
for (k = 0; k < preSelectorArray[i].length; k++) {
|
|
8
|
-
temp.push(selectors[j] + preSelectorArray[i][k]);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
selectors = temp;
|
|
12
|
-
}
|
|
13
|
-
return "\n" + (selectors.join(',')) + "{\n";
|
|
14
|
-
};
|
|
15
|
-
let analyseBlock = function (source) {
|
|
16
|
-
let i = -1, currentChar = null;
|
|
17
|
-
let next = function () {
|
|
18
|
-
currentChar = i++ < source.length - 1 ? source[i] : null;
|
|
19
|
-
return currentChar;
|
|
20
|
-
}
|
|
21
|
-
let nextNValue = function (n) {
|
|
22
|
-
return source.substring(i, n + i > source.length ? source.length : n + i);
|
|
23
|
-
}
|
|
24
|
-
let blocks = [];
|
|
25
|
-
let currentBlock = "";
|
|
26
|
-
next();
|
|
27
|
-
while (true) {
|
|
28
|
-
while (new RegExp("[\\x20\\t\\r\\n\\f]").test(currentChar)) {
|
|
29
|
-
next();
|
|
30
|
-
}
|
|
31
|
-
if (currentChar == null) break;
|
|
32
|
-
if (nextNValue(2) == '/*') {
|
|
33
|
-
|
|
34
|
-
next();
|
|
35
|
-
next();
|
|
36
|
-
currentBlock = "/*";
|
|
37
|
-
|
|
38
|
-
while (nextNValue(2) != '*/' && currentChar != null) {
|
|
39
|
-
currentBlock += currentChar;
|
|
40
|
-
next();
|
|
41
|
-
}
|
|
42
|
-
if (currentChar == null) {
|
|
43
|
-
throw new Error('The comment is not closed.');
|
|
44
|
-
}
|
|
45
|
-
currentBlock += "*/";
|
|
46
|
-
next();
|
|
47
|
-
next();
|
|
48
|
-
blocks.push({
|
|
49
|
-
value: currentBlock,
|
|
50
|
-
type: "comment-double"
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
else if (nextNValue(2) == '//') {
|
|
54
|
-
currentBlock = '';
|
|
55
|
-
|
|
56
|
-
while (currentChar != '\n' && currentChar != null) {
|
|
57
|
-
currentBlock += currentChar;
|
|
58
|
-
next();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
blocks.push({
|
|
62
|
-
value: currentBlock,
|
|
63
|
-
type: "comment-single"
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
else if (currentChar == '}') {
|
|
68
|
-
|
|
69
|
-
blocks.push({
|
|
70
|
-
value: "}",
|
|
71
|
-
type: "end"
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
next();
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
|
|
79
|
-
currentBlock = '';
|
|
80
|
-
while (currentChar != '{' && currentChar != ';' && currentChar != null) {
|
|
81
|
-
currentBlock += currentChar;
|
|
82
|
-
next();
|
|
83
|
-
}
|
|
84
|
-
if (currentChar == null) {
|
|
85
|
-
throw new Error('Statement or code block missing closure.');
|
|
86
|
-
}
|
|
87
|
-
blocks.push({
|
|
88
|
-
value: currentBlock + currentChar,
|
|
89
|
-
type: {
|
|
90
|
-
'{': "begin",
|
|
91
|
-
';': 'statement'
|
|
92
|
-
}[currentChar]
|
|
93
|
-
});
|
|
94
|
-
next();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return blocks;
|
|
98
|
-
};
|
|
99
|
-
module.exports = function (source) {
|
|
100
|
-
this.setFileType("application/javascript");
|
|
101
|
-
let blocks = analyseBlock(source + "");
|
|
102
|
-
let i, j, cssCode = "", preSelectorArray = [], deep = 0;
|
|
103
|
-
for (i = 0; i < blocks.length; i++) {
|
|
104
|
-
if (blocks[i].type == 'comment-double') {
|
|
105
|
-
cssCode += blocks[i].value;
|
|
106
|
-
}
|
|
107
|
-
else if (blocks[i].type == 'comment-single') {
|
|
108
|
-
cssCode += "\n/* " + blocks[i].value + " */\n";
|
|
109
|
-
}
|
|
110
|
-
else if (blocks[i].type == 'begin') {
|
|
111
|
-
let preSplit = blocks[i].value.split(',');
|
|
112
|
-
let preSelect = [];
|
|
113
|
-
for (j = 0; j < preSplit.length; j++) {
|
|
114
|
-
preSelect[j] = preSplit[j].replace(/\{$/, '').trim();
|
|
115
|
-
if (/^&/.test(preSelect[j])) {
|
|
116
|
-
preSelect[j] = preSelect[j].replace(/^&/, '');
|
|
117
|
-
} else {
|
|
118
|
-
preSelect[j] = " " + preSelect[j];
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
preSelectorArray[deep] = preSelect;
|
|
122
|
-
deep += 1;
|
|
123
|
-
}
|
|
124
|
-
else if (blocks[i].type == 'end') {
|
|
125
|
-
deep -= 1;
|
|
126
|
-
}
|
|
127
|
-
else if (blocks[i].type == 'statement') {
|
|
128
|
-
j = 1;
|
|
129
|
-
let preType = blocks[i - j].type;
|
|
130
|
-
while (['comment-double', 'comment-single'].indexOf(preType) > -1) {
|
|
131
|
-
j += 1;
|
|
132
|
-
preType = blocks[i - j].type;
|
|
133
|
-
}
|
|
134
|
-
if (['end', 'begin'].indexOf(preType) > -1) {
|
|
135
|
-
cssCode += toSelector(preSelectorArray, deep);
|
|
136
|
-
}
|
|
137
|
-
cssCode += "\n" + blocks[i].value + "\n";
|
|
138
|
-
j = 1;
|
|
139
|
-
let nextType = blocks[i + j].type;
|
|
140
|
-
while (['comment-double', 'comment-single'].indexOf(nextType) > -1) {
|
|
141
|
-
j += 1;
|
|
142
|
-
nextType = blocks[i + j].type;
|
|
143
|
-
}
|
|
144
|
-
if (['end', 'begin'].indexOf(nextType) > -1) {
|
|
145
|
-
cssCode += "\n}\n";
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return `export default ${JSON.stringify(cssCode)}`;
|
|
1
|
+
// 参考:https://github.com/zxl20070701/zxl20070701.github.io/blob/master/bin/loader/scss.js
|
|
2
|
+
let toSelector = function (preSelectorArray, deep) {
|
|
3
|
+
let selectors = preSelectorArray[0], i, j, k;
|
|
4
|
+
for (i = 1; i < deep; i++) {
|
|
5
|
+
let temp = [];
|
|
6
|
+
for (j = 0; j < selectors.length; j++) {
|
|
7
|
+
for (k = 0; k < preSelectorArray[i].length; k++) {
|
|
8
|
+
temp.push(selectors[j] + preSelectorArray[i][k]);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
selectors = temp;
|
|
12
|
+
}
|
|
13
|
+
return "\n" + (selectors.join(',')) + "{\n";
|
|
14
|
+
};
|
|
15
|
+
let analyseBlock = function (source) {
|
|
16
|
+
let i = -1, currentChar = null;
|
|
17
|
+
let next = function () {
|
|
18
|
+
currentChar = i++ < source.length - 1 ? source[i] : null;
|
|
19
|
+
return currentChar;
|
|
20
|
+
}
|
|
21
|
+
let nextNValue = function (n) {
|
|
22
|
+
return source.substring(i, n + i > source.length ? source.length : n + i);
|
|
23
|
+
}
|
|
24
|
+
let blocks = [];
|
|
25
|
+
let currentBlock = "";
|
|
26
|
+
next();
|
|
27
|
+
while (true) {
|
|
28
|
+
while (new RegExp("[\\x20\\t\\r\\n\\f]").test(currentChar)) {
|
|
29
|
+
next();
|
|
30
|
+
}
|
|
31
|
+
if (currentChar == null) break;
|
|
32
|
+
if (nextNValue(2) == '/*') {
|
|
33
|
+
|
|
34
|
+
next();
|
|
35
|
+
next();
|
|
36
|
+
currentBlock = "/*";
|
|
37
|
+
|
|
38
|
+
while (nextNValue(2) != '*/' && currentChar != null) {
|
|
39
|
+
currentBlock += currentChar;
|
|
40
|
+
next();
|
|
41
|
+
}
|
|
42
|
+
if (currentChar == null) {
|
|
43
|
+
throw new Error('The comment is not closed.');
|
|
44
|
+
}
|
|
45
|
+
currentBlock += "*/";
|
|
46
|
+
next();
|
|
47
|
+
next();
|
|
48
|
+
blocks.push({
|
|
49
|
+
value: currentBlock,
|
|
50
|
+
type: "comment-double"
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
else if (nextNValue(2) == '//') {
|
|
54
|
+
currentBlock = '';
|
|
55
|
+
|
|
56
|
+
while (currentChar != '\n' && currentChar != null) {
|
|
57
|
+
currentBlock += currentChar;
|
|
58
|
+
next();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
blocks.push({
|
|
62
|
+
value: currentBlock,
|
|
63
|
+
type: "comment-single"
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
}
|
|
67
|
+
else if (currentChar == '}') {
|
|
68
|
+
|
|
69
|
+
blocks.push({
|
|
70
|
+
value: "}",
|
|
71
|
+
type: "end"
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
next();
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
|
|
79
|
+
currentBlock = '';
|
|
80
|
+
while (currentChar != '{' && currentChar != ';' && currentChar != null) {
|
|
81
|
+
currentBlock += currentChar;
|
|
82
|
+
next();
|
|
83
|
+
}
|
|
84
|
+
if (currentChar == null) {
|
|
85
|
+
throw new Error('Statement or code block missing closure.');
|
|
86
|
+
}
|
|
87
|
+
blocks.push({
|
|
88
|
+
value: currentBlock + currentChar,
|
|
89
|
+
type: {
|
|
90
|
+
'{': "begin",
|
|
91
|
+
';': 'statement'
|
|
92
|
+
}[currentChar]
|
|
93
|
+
});
|
|
94
|
+
next();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return blocks;
|
|
98
|
+
};
|
|
99
|
+
module.exports = function (source) {
|
|
100
|
+
this.setFileType("application/javascript");
|
|
101
|
+
let blocks = analyseBlock(source + "");
|
|
102
|
+
let i, j, cssCode = "", preSelectorArray = [], deep = 0;
|
|
103
|
+
for (i = 0; i < blocks.length; i++) {
|
|
104
|
+
if (blocks[i].type == 'comment-double') {
|
|
105
|
+
cssCode += blocks[i].value;
|
|
106
|
+
}
|
|
107
|
+
else if (blocks[i].type == 'comment-single') {
|
|
108
|
+
cssCode += "\n/* " + blocks[i].value + " */\n";
|
|
109
|
+
}
|
|
110
|
+
else if (blocks[i].type == 'begin') {
|
|
111
|
+
let preSplit = blocks[i].value.split(',');
|
|
112
|
+
let preSelect = [];
|
|
113
|
+
for (j = 0; j < preSplit.length; j++) {
|
|
114
|
+
preSelect[j] = preSplit[j].replace(/\{$/, '').trim();
|
|
115
|
+
if (/^&/.test(preSelect[j])) {
|
|
116
|
+
preSelect[j] = preSelect[j].replace(/^&/, '');
|
|
117
|
+
} else {
|
|
118
|
+
preSelect[j] = " " + preSelect[j];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
preSelectorArray[deep] = preSelect;
|
|
122
|
+
deep += 1;
|
|
123
|
+
}
|
|
124
|
+
else if (blocks[i].type == 'end') {
|
|
125
|
+
deep -= 1;
|
|
126
|
+
}
|
|
127
|
+
else if (blocks[i].type == 'statement') {
|
|
128
|
+
j = 1;
|
|
129
|
+
let preType = blocks[i - j].type;
|
|
130
|
+
while (['comment-double', 'comment-single'].indexOf(preType) > -1) {
|
|
131
|
+
j += 1;
|
|
132
|
+
preType = blocks[i - j].type;
|
|
133
|
+
}
|
|
134
|
+
if (['end', 'begin'].indexOf(preType) > -1) {
|
|
135
|
+
cssCode += toSelector(preSelectorArray, deep);
|
|
136
|
+
}
|
|
137
|
+
cssCode += "\n" + blocks[i].value + "\n";
|
|
138
|
+
j = 1;
|
|
139
|
+
let nextType = blocks[i + j].type;
|
|
140
|
+
while (['comment-double', 'comment-single'].indexOf(nextType) > -1) {
|
|
141
|
+
j += 1;
|
|
142
|
+
nextType = blocks[i + j].type;
|
|
143
|
+
}
|
|
144
|
+
if (['end', 'begin'].indexOf(nextType) > -1) {
|
|
145
|
+
cssCode += "\n}\n";
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return `export default ${JSON.stringify(cssCode)}`;
|
|
150
150
|
};
|