Skip to content

Commit 75b3d53

Browse files
author
chengyu.chengyulia
committed
feat: 合并scss和less编译
1 parent 37da1be commit 75b3d53

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

declare/npm.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ declare module 'gulp-sass';
2525
declare module 'gulp-concat';
2626
declare module 'gulp-typescript';
2727
declare module 'gulp-watch';
28+
declare module 'merge-stream';
2829
declare module 'express';
2930
declare module 'react-markdown';
3031
declare module 'gulp-sourcemaps';

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
"koa-route": "3.2.0",
121121
"koa-static": "5.0.0",
122122
"lodash": "^4.17.15",
123+
"merge-stream": "^2.0.0",
123124
"mini-css-extract-plugin": "0.9.0",
124125
"node-forge": "0.9.1",
125126
"normalize-path": "3.0.0",

src/utils/ts-plus-babel.ts

+29-30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as gulpStripCssComments from 'gulp-strip-css-comments';
88
import * as gulpConcatCss from 'gulp-concat-css';
99
import * as gulpIf from 'gulp-if';
1010
import * as gulpSourcemaps from 'gulp-sourcemaps';
11+
import * as mergeStream from 'merge-stream';
1112
import * as path from 'path';
1213
import { pri, srcPath } from '../node';
1314
import { plugin } from './plugins';
@@ -56,43 +57,41 @@ function getStyleFilePath(suffix: string, wholeProject: boolean, sourcePath: str
5657
: path.join(sourcePath || pri.sourceRoot, srcPath.dir, `**/*.${suffix}`);
5758
}
5859

59-
const buildSass = (watch: boolean, outdir: string, wholeProject: boolean, sourcePath: string) => {
60+
const buildSass = (wholeProject: boolean, sourcePath: string) => {
6061
const targetScssPath = getStyleFilePath('scss', wholeProject, sourcePath);
61-
return new Promise((resolve, reject) => {
62-
getGulpByWatch(watch, targetScssPath)
63-
.pipe(
64-
gulpSass({
65-
includePaths: path.join(pri.projectRootPath, 'node_modules'),
66-
}),
67-
)
68-
.pipe(gulpIf(pri.sourceConfig.cssExtract, gulpConcatCss(pri.sourceConfig.outCssFileName)))
69-
.pipe(gulpStripCssComments())
70-
.on('error', reject)
71-
.pipe(gulp.dest(outdir))
72-
.on('end', resolve);
73-
});
62+
return gulp.src(targetScssPath).pipe(
63+
gulpSass({
64+
includePaths: path.join(pri.projectRootPath, 'node_modules'),
65+
}),
66+
);
7467
};
7568

76-
const buildLess = (watch: boolean, outdir: string, wholeProject: boolean, sourcePath: string) => {
69+
const buildLess = (wholeProject: boolean, sourcePath: string) => {
7770
const targetLessPath = getStyleFilePath('less', wholeProject, sourcePath);
78-
return new Promise((resolve, reject) => {
79-
getGulpByWatch(watch, targetLessPath)
80-
.pipe(
81-
gulpLess({
82-
paths: [path.join(pri.projectRootPath, 'node_modules', 'includes')],
83-
}),
84-
)
71+
return gulp.src(targetLessPath).pipe(
72+
gulpLess({
73+
paths: [path.join(pri.projectRootPath, 'node_modules', 'includes')],
74+
}),
75+
);
76+
};
77+
78+
const buildSassAndLess = (watch: boolean, outdir: string, wholeProject: boolean, sourcePath: string) => {
79+
const targetPath = getStyleFilePath('{scss,less}', wholeProject, sourcePath);
80+
const mergeStyle = (resolve: (value?: any) => void, reject: (value?: any) => void) =>
81+
mergeStream(buildLess(wholeProject, sourcePath), buildSass(wholeProject, sourcePath))
8582
.pipe(gulpIf(pri.sourceConfig.cssExtract, gulpConcatCss(pri.sourceConfig.outCssFileName)))
8683
.pipe(gulpStripCssComments())
8784
.on('error', reject)
8885
.pipe(gulp.dest(outdir))
8986
.on('end', resolve);
90-
});
91-
};
92-
93-
const buildSassOrLess = (watch: boolean, outdir: string, wholeProject: boolean, sourcePath: string) => {
94-
buildSass(watch, outdir, wholeProject, sourcePath);
95-
buildLess(watch, outdir, wholeProject, sourcePath);
87+
if (watch) {
88+
return new Promise((resolve, reject) => {
89+
gulpWatch(targetPath, () => {
90+
mergeStyle(resolve, reject);
91+
});
92+
});
93+
}
94+
return new Promise(mergeStyle);
9695
};
9796

9897
const buildCssWithWebpack = (outDir: string, copyDir: string) => {
@@ -198,8 +197,8 @@ export const tsPlusBabel = async (watch = false, wholeProject = false, packageIn
198197
}
199198

200199
return Promise.all([
201-
pri.sourceConfig.componentEntries ? null : buildSassOrLess(watch, mainDistPath, wholeProject, sourcePath),
202-
pri.sourceConfig.componentEntries ? null : buildSassOrLess(watch, moduleDistPath, wholeProject, sourcePath),
200+
pri.sourceConfig.componentEntries ? null : buildSassAndLess(watch, mainDistPath, wholeProject, sourcePath),
201+
pri.sourceConfig.componentEntries ? null : buildSassAndLess(watch, moduleDistPath, wholeProject, sourcePath),
203202

204203
buildTs(
205204
watch,

0 commit comments

Comments
 (0)