Skip to content

Commit bdbb319

Browse files
committed
build: fix Load missing styles on demand
fix Load missing styles on demand
1 parent 7e9e8a4 commit bdbb319

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

gulpfile.js

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { src, dest, parallel, series } = require('gulp');
1+
const { src, dest, parallel, series, task } = require('gulp');
22
const fs = require('fs');
33
const through = require('through2');
44
const sass = require('gulp-sass');
@@ -8,7 +8,87 @@ const ts = require('gulp-typescript');
88
const cleanCSS = require('gulp-clean-css');
99
const rename = require('gulp-rename');
1010
const babel = require('gulp-babel');
11+
const mergeStream = require('merge-stream');
1112

13+
function difference(array, arrCompare) {
14+
return array.concat(arrCompare).filter(function (v, i, arr) {
15+
return arr.indexOf(v) === arr.lastIndexOf(v);
16+
});
17+
}
18+
function unique (arr) {
19+
return Array.from(new Set(arr));
20+
}
21+
/**
22+
* existStyleCatalogName ---- The name of the component catalog where the style exists
23+
* componentCatalogName ---- All component catalog names
24+
*/
25+
let existStyleCatalogName = [];
26+
let componentCatalogName = [];
27+
28+
function writeStyleFile(catalogArr) {
29+
for (let catalogName of catalogArr) {
30+
fs.writeFile(`src/components/${catalogName}/style.scss`, "", { 'flag': 'wx' }, (err) => {
31+
if (err) {
32+
throw err;
33+
}
34+
})
35+
}
36+
}
37+
38+
function delStyleFile(catalogArr) {
39+
for (let catalogName of catalogArr) {
40+
del([`src/components/${catalogName}/style.scss`])
41+
}
42+
}
43+
/**
44+
* traverse files
45+
* @param isDelete Distinguish between write and delete style file
46+
*/
47+
function traverseExistStyleFile(isDelete) {
48+
return src(['src/components/*/*.scss'])
49+
.pipe(through.obj(function (file, enc, callback) {
50+
isExist = Boolean(file.contents.toString())
51+
if (isExist) {
52+
existStyleCatalogName.push(file.relative.split('/')[0]);
53+
}
54+
callback();
55+
})).on('end', function () {
56+
const noStyleComp = difference(unique(componentCatalogName), unique(existStyleCatalogName));
57+
if (isDelete) {
58+
delStyleFile(noStyleComp)
59+
console.log('delStyleFile exec end')
60+
} else {
61+
writeStyleFile(noStyleComp)
62+
console.log('writeStyleFile exec end')
63+
}
64+
})
65+
}
66+
function traverseComponent() {
67+
console.warn('Do not edit or modify the source file when the project is compiled !!!')
68+
return src(['src/components/*/', '!src/components/utils/']) // exclude utils/
69+
.pipe(through.obj(function (file, enc, callback) {
70+
componentCatalogName.push(file.relative.split('/')[0]);
71+
callback();
72+
})).on('end', function () {
73+
console.log('traverseComponent exec end ~')
74+
})
75+
}
76+
77+
function mergeTraverseStream (type) {
78+
return mergeStream(traverseComponent(), traverseExistStyleFile(type))
79+
}
80+
81+
function generateStyleFile () {
82+
return mergeTraverseStream(false)
83+
}
84+
85+
function cleanUselessStyleFile() {
86+
return mergeTraverseStream(true)
87+
}
88+
/**
89+
* TODO optimize outputStyleTask
90+
* outputStyleTask Function Time-consuming to execute..
91+
*/
1292
function outputStyleTask() {
1393
fs.writeFile('src/index.tsx',"import './style.scss';",{'flag':'a'},(err)=>{
1494
if(err){
@@ -44,7 +124,8 @@ function globalSass() {
44124
.pipe(dest('lib/style'));
45125
}
46126
async function clean(cb) {
47-
await del(['lib','src/index.tsx']);
127+
await cleanUselessStyleFile()
128+
await del(['lib', 'src/index.tsx']);
48129
await cb();
49130
}
50131
function globalCss() {
@@ -67,4 +148,4 @@ function jsForCss(data) {
67148
.pipe(babel())
68149
.pipe(dest('lib/' + String(data) + '/style/'));
69150
}
70-
exports.default = series(clean, parallel(outputStyleTask, globalSass), globalCss);
151+
exports.default = series(clean, generateStyleFile, parallel(outputStyleTask, globalSass), globalCss, cleanUselessStyleFile);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"storybook": "start-storybook -p 9001 -c .storybook",
1313
"build-storybook": "export NODE_ENV='production' && build-storybook -c .storybook -o dist",
1414
"compile": "npm run build-css && npm run build-ts",
15-
"build-ts": "tsc -p tsconfig.build.json",
15+
"build-ts": "tsc -p tsconcfig.build.json",
1616
"release": "./scripts/release.sh",
1717
"deploy-storybook": "storybook-to-ghpages",
1818
"deploy": "./scripts/deploy.sh",
@@ -131,6 +131,7 @@
131131
"less": "^3.9.0",
132132
"less-loader": "^5.0.0",
133133
"lint-md": "^0.1.1",
134+
"merge-stream": "^2.0.0",
134135
"node-sass": "^4.12.0",
135136
"react-docgen-typescript-loader": "^3.1.1",
136137
"react-icons": "^3.10.0",

0 commit comments

Comments
 (0)