Skip to content

Commit 26452ce

Browse files
author
zhaozhiwen
committed
feat(build): 生成css.js文件
1 parent 52b01c5 commit 26452ce

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

gulpfile.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const babel = require('gulp-babel');
33
const less = require('gulp-less');
44
const autoprefixer = require('gulp-autoprefixer');
55
const cssnano = require('gulp-cssnano');
6+
const through2 = require('through2');
67

78
const paths = {
89
dest: {
@@ -14,17 +15,45 @@ const paths = {
1415
scripts: ['components/**/*.{ts,tsx}', '!components/**/demo/*.{ts,tsx}'],
1516
};
1617

18+
/**
19+
* 当前组件样式 import './index.less' => import './index.css'
20+
* 依赖的其他组件样式 import '../test-comp/style' => import '../test-comp/style/css.js'
21+
* 依赖的其他组件样式 import '../test-comp/style/index.js' => import '../test-comp/style/css.js'
22+
* @param {string} content
23+
*/
24+
function cssInjection(content) {
25+
return content
26+
.replace(/\/style\/?'/g, "/style/css'")
27+
.replace(/\/style\/?"/g, '/style/css"')
28+
.replace(/\.less/g, '.css');
29+
}
30+
1731
/**
1832
* 编译脚本文件
19-
* @param {*} babelEnv babel环境变量
20-
* @param {*} destDir 目标目录
33+
* @param {string} babelEnv babel环境变量
34+
* @param {string} destDir 目标目录
2135
*/
2236
function compileScripts(babelEnv, destDir) {
2337
const { scripts } = paths;
2438
process.env.BABEL_ENV = babelEnv;
2539
return gulp
2640
.src(scripts)
2741
.pipe(babel()) // 使用gulp-babel处理
42+
.pipe(
43+
through2.obj(function z(file, encoding, next) {
44+
this.push(file.clone());
45+
// 找到目标
46+
if (file.path.match(/(\/|\\)style(\/|\\)index\.js/)) {
47+
const content = file.contents.toString(encoding);
48+
file.contents = Buffer.from(cssInjection(content)); // 处理文件内容
49+
file.path = file.path.replace(/index\.js/, 'css.js'); // 文件重命名
50+
this.push(file); // 新增该文件
51+
next();
52+
} else {
53+
next();
54+
}
55+
}),
56+
)
2857
.pipe(gulp.dest(destDir));
2958
}
3059

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"react-simple-code-editor": "^0.10.0",
6868
"react-use": "^13.12.2",
6969
"rimraf": "^3.0.0",
70+
"through2": "^3.0.1",
7071
"typescript": "^3.7.3"
7172
},
7273
"lint-staged": {

yarn.lock

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,13 +1555,20 @@
15551555
resolved "https://registry.npm.taobao.org/@types/unist/download/@types/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e"
15561556
integrity sha1-nAiGeYdvN061mD8VDUeHqm+zLX4=
15571557

1558-
"@types/vfile-message@*", "@types/vfile-message@^2.0.0":
1558+
"@types/vfile-message@*":
15591559
version "2.0.0"
15601560
resolved "https://registry.npm.taobao.org/@types/vfile-message/download/@types/vfile-message-2.0.0.tgz#690e46af0fdfc1f9faae00cd049cc888957927d5"
15611561
integrity sha1-aQ5Grw/fwfn6rgDNBJzIiJV5J9U=
15621562
dependencies:
15631563
vfile-message "*"
15641564

1565+
"@types/vfile-message@^2.0.0":
1566+
version "2.0.0"
1567+
resolved "https://registry.npmjs.org/@types/vfile-message/-/vfile-message-2.0.0.tgz#690e46af0fdfc1f9faae00cd049cc888957927d5"
1568+
integrity sha512-GpTIuDpb9u4zIO165fUy9+fXcULdD8HFRNli04GehoMVbeNq7D6OBnqSmg3lxZnC+UvgUhEWKxdKiwYUkGltIw==
1569+
dependencies:
1570+
vfile-message "*"
1571+
15651572
"@types/vfile@^3.0.0":
15661573
version "3.0.2"
15671574
resolved "https://registry.npm.taobao.org/@types/vfile/download/@types/vfile-3.0.2.tgz#19c18cd232df11ce6fa6ad80259bc86c366b09b9"
@@ -15904,7 +15911,7 @@ through2@^2.0.0, through2@^2.0.1, through2@^2.0.2, through2@^2.0.3, through2@~2.
1590415911

1590515912
through2@^3.0.1:
1590615913
version "3.0.1"
15907-
resolved "https://registry.npm.taobao.org/through2/download/through2-3.0.1.tgz#39276e713c3302edf9e388dd9c812dd3b825bd5a"
15914+
resolved "https://registry.npm.taobao.org/through2/download/through2-3.0.1.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fthrough2%2Fdownload%2Fthrough2-3.0.1.tgz#39276e713c3302edf9e388dd9c812dd3b825bd5a"
1590815915
integrity sha1-OSducTwzAu3544jdnIEt07glvVo=
1590915916
dependencies:
1591015917
readable-stream "2 || 3"

0 commit comments

Comments
 (0)