Skip to content

Commit 6fd05e2

Browse files
committed
feat: support style loading on demand
1 parent 5082969 commit 6fd05e2

File tree

4 files changed

+189
-13
lines changed

4 files changed

+189
-13
lines changed

gulpfile.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
const { src, dest, parallel } = require('gulp');
1+
const { src, dest, parallel, series } = require('gulp');
22
const through = require('through2');
33
const sass = require('gulp-sass');
44
const concat = require('gulp-concat');
5+
const del = require('del');
6+
const ts = require('gulp-typescript');
7+
const cleanCSS = require('gulp-clean-css');
8+
const rename = require('gulp-rename');
9+
// const tsProject = ts.createProject('./tsconfig.build.json');
10+
511
function outputStyleTask() {
612
return src(['src/components/**/*.scss'])
713
.pipe(
@@ -13,8 +19,11 @@ function outputStyleTask() {
1319
)
1420
.on('data', data => {
1521
convertStyles(data);
22+
jsForCss(data);
23+
jsForScss(data);
1624
});
1725
}
26+
1827
function convertStyles(data) {
1928
return src(['src/components/' + String(data) + '/*.scss'])
2029
.pipe(dest('lib/' + String(data) + '/style/'))
@@ -28,11 +37,27 @@ function globalSass() {
2837
.pipe(concat('index.scss'))
2938
.pipe(dest('lib/style'));
3039
}
31-
40+
async function clean(cb) {
41+
await del(['lib']);
42+
await cb();
43+
}
3244
function globalCss() {
33-
return src('src/components/**/*.scss')
45+
return src('lib/**/*.scss')
3446
.pipe(concat('index.css'))
3547
.pipe(sass())
48+
.on('error', sass.logError)
49+
.pipe(cleanCSS({ compatibility: 'ie11' }))
3650
.pipe(dest('lib/style'));
3751
}
38-
exports.default = parallel(outputStyleTask, globalCss, globalSass);
52+
function jsForScss(data) {
53+
return src('src/index.tsx')
54+
.pipe(ts({ declaration: true, target: 'ES5' }))
55+
.pipe(dest('lib/' + String(data) + '/style/'));
56+
}
57+
function jsForCss(data) {
58+
return src('src/index.tsx')
59+
.pipe(rename('style.tsx'))
60+
.pipe(ts({ target: 'ES5' }))
61+
.pipe(dest('lib/' + String(data) + '/style/'));
62+
}
63+
exports.default = series(clean, parallel(outputStyleTask, globalSass), globalCss);

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"increase-memory-limit": "cross-env LIMIT=8096 increase-memory-limit",
1212
"storybook": "start-storybook -p 9001 -c .storybook",
1313
"build-storybook": "export NODE_ENV='production' && build-storybook -c .storybook -o dist",
14-
"compile": "rm -rf lib && npm run build-ts && npm run build-css",
14+
"compile": "npm run build-css && npm run build-ts",
1515
"build-ts": "tsc -p tsconfig.build.json",
1616
"release": "./scripts/release.sh",
1717
"deploy-storybook": "storybook-to-ghpages",
@@ -96,6 +96,7 @@
9696
"cross-env": "^7.0.2",
9797
"css-loader": "^3.1.0",
9898
"cz-conventional-changelog": "^3.0.2",
99+
"del": "^5.1.0",
99100
"enzyme": "^3.11.0",
100101
"enzyme-adapter-react-16": "^1.15.2",
101102
"eslint": "^5.16.0",
@@ -111,9 +112,12 @@
111112
"eslint-plugin-standard": "^4.0.0",
112113
"file-loader": "^6.0.0",
113114
"gulp": "^4.0.2",
115+
"gulp-clean-css": "^4.3.0",
114116
"gulp-concat": "^2.6.1",
115117
"gulp-eslint": "^6.0.0",
118+
"gulp-rename": "^2.0.0",
116119
"gulp-sass": "^4.1.0",
120+
"gulp-typescript": "^6.0.0-alpha.1",
117121
"husky": "^1.2.0",
118122
"increase-memory-limit": "^1.0.7",
119123
"jest": "^24.9.0",

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.css'; // 此文件用于适配babel插件按需导入样式,勿改

0 commit comments

Comments
 (0)