Skip to content

Commit 1901397

Browse files
author
zaizhou.sym
committed
feat(pri): component构建添加less编译
1 parent 5ef7134 commit 1901397

File tree

4 files changed

+217
-43
lines changed

4 files changed

+217
-43
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@babel/plugin-transform-runtime": "7.7.5",
4444
"@babel/preset-env": "7.7.5",
4545
"@babel/preset-react": "7.7.4",
46-
"@babel/preset-typescript": "7.7.4",
46+
"@babel/preset-typescript": "7.9.0",
4747
"@babel/runtime": "7.7.5",
4848
"@commitlint/cli": "^8.2.0",
4949
"@commitlint/lint": "^8.2.0",
@@ -54,6 +54,7 @@
5454
"@types/enzyme": "^3.10.5",
5555
"@types/eslint": "^6.1.3",
5656
"@types/fs-extra": "8.0.1",
57+
"@types/gulp-less": "^0.0.31",
5758
"@types/highlight.js": "9.12.3",
5859
"@types/jest": "24.0.23",
5960
"@types/koa": "2.11.0",

src/utils/babel-options.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export function getBabelOptions(options?: Partial<DefaultOptions>) {
3232
return {
3333
babelrc: false,
3434
comments: globalState.isDevelopment,
35-
presets: [[babelPresetEnv, { modules: mergedOptions.modules }], [babelPresetReact], [babelPresetTypescript]],
35+
presets: [
36+
[babelPresetEnv, { modules: mergedOptions.modules }],
37+
[babelPresetReact],
38+
[babelPresetTypescript, { onlyRemoveTypeImports: true }],
39+
],
3640
plugins: [
3741
[transformRuntime],
3842
...(globalState.isDevelopment ? [[babelPluginReactHotLoader]] : []),

src/utils/ts-plus-babel.ts

+27-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as gulp from 'gulp';
22
import * as fs from 'fs-extra';
33
import * as gulpBabel from 'gulp-babel';
44
import * as gulpSass from 'gulp-sass';
5+
import * as gulpLess from 'gulp-less';
56
import * as gulpWatch from 'gulp-watch';
67
import * as gulpStripCssComments from 'gulp-strip-css-comments';
78
import * as gulpConcatCss from 'gulp-concat-css';
@@ -20,9 +21,17 @@ function getGulpByWatch(watch: boolean, filesPath: string) {
2021
if (watch) {
2122
return gulpWatch(filesPath);
2223
}
24+
/** 文件匹配 */
2325
return gulp.src(filesPath);
2426
}
2527

28+
function getCssByWatch(watch: boolean, filesPath: Array<any>) {
29+
if (watch) {
30+
return gulpWatch(filesPath);
31+
}
32+
/** 文件匹配 */
33+
return gulp.src(filesPath);
34+
}
2635
const buildTs = (watch: boolean, outdir: string, babelOptions: any, wholeProject: boolean, sourcePath: string) => {
2736
const targetPath = wholeProject
2837
? path.join(pri.projectRootPath, '{src,packages}/**/*.{ts,tsx}')
@@ -47,19 +56,28 @@ const buildTs = (watch: boolean, outdir: string, babelOptions: any, wholeProject
4756
});
4857
};
4958

50-
const buildSass = (watch: boolean, outdir: string, wholeProject: boolean, sourcePath: string) => {
51-
const targetPath =
52-
wholeProject || (pri.selectedSourceType === 'root' && pri.sourceConfig.cssExtract)
53-
? path.join(pri.projectRootPath, '{src,packages}/**/*.scss')
54-
: path.join(sourcePath || pri.sourceRoot, srcPath.dir, '**/*.scss');
55-
const a = 1;
59+
/** 获取样式文件路径 */
60+
function getStyleFilePath(suffix: string, wholeProject: boolean, sourcePath: string) {
61+
return wholeProject || (pri.selectedSourceType === 'root' && pri.sourceConfig.cssExtract)
62+
? path.join(pri.projectRootPath, `{src,packages}/**/*.${suffix}`)
63+
: path.join(sourcePath || pri.sourceRoot, srcPath.dir, `**/*.${suffix}`);
64+
}
65+
66+
const buildSassAndLess = (watch: boolean, outdir: string, wholeProject: boolean, sourcePath: string) => {
67+
const targetScssPath = getStyleFilePath('scss', wholeProject, sourcePath);
68+
const targetLessPath = getStyleFilePath('less', wholeProject, sourcePath);
5669
return new Promise((resolve, reject) => {
57-
getGulpByWatch(watch, targetPath)
70+
getCssByWatch(watch, [targetScssPath, targetLessPath])
5871
.pipe(
5972
gulpSass({
6073
includePaths: path.join(pri.projectRootPath, 'node_modules'),
6174
}),
6275
)
76+
.pipe(
77+
gulpLess({
78+
paths: [path.join(pri.projectRootPath, 'node_modules', 'includes')],
79+
}),
80+
)
6381
.pipe(gulpIf(pri.sourceConfig.cssExtract, gulpConcatCss(pri.sourceConfig.outCssFileName)))
6482
.pipe(gulpStripCssComments())
6583
.on('error', reject)
@@ -171,8 +189,8 @@ export const tsPlusBabel = async (watch = false, wholeProject = false, packageIn
171189
}
172190

173191
return Promise.all([
174-
pri.sourceConfig.componentEntries ? null : buildSass(watch, mainDistPath, wholeProject, sourcePath),
175-
pri.sourceConfig.componentEntries ? null : buildSass(watch, moduleDistPath, wholeProject, sourcePath),
192+
pri.sourceConfig.componentEntries ? null : buildSassAndLess(watch, mainDistPath, wholeProject, sourcePath),
193+
pri.sourceConfig.componentEntries ? null : buildSassAndLess(watch, moduleDistPath, wholeProject, sourcePath),
176194

177195
buildTs(
178196
watch,

0 commit comments

Comments
 (0)