Skip to content

Commit

Permalink
feat: compile with lib (#265)
Browse files Browse the repository at this point in the history
* feat: compile with lib experimental

* refactor: remove experimental

* refactor: remove transform less-
  • Loading branch information
chunsch committed Nov 22, 2022
1 parent e2d0c15 commit caf0c84
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 160 deletions.
30 changes: 0 additions & 30 deletions lib/getWebpackConfig.js
Expand Up @@ -148,36 +148,6 @@ function getWebpackConfig(modules) {
},
],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: resolve('css-loader'),
options: {
sourceMap: true,
},
},
{
loader: resolve('postcss-loader'),
options: {
postcssOptions: {
plugins: ['autoprefixer'],
},
sourceMap: true,
},
},
{
loader: resolve('less-loader'),
options: {
lessOptions: {
javascriptEnabled: true,
},
sourceMap: true,
},
},
],
},

// Images
{
Expand Down
99 changes: 7 additions & 92 deletions lib/gulpfile.js
Expand Up @@ -21,11 +21,9 @@ const stripCode = require('gulp-strip-code');
const install = require('./install');
const runCmd = require('./runCmd');
const getBabelCommonConfig = require('./getBabelCommonConfig');
const transformLess = require('./transformLess');
const getNpm = require('./getNpm');
const selfPackage = require('../package.json');
const getNpmArgs = require('./utils/get-npm-args');
const { cssInjection } = require('./utils/styleUtil');
const tsConfig = require('./getTSCommonConfig')();
const replaceLib = require('./replaceLib');
const checkDeps = require('./lint/checkDeps');
Expand Down Expand Up @@ -187,86 +185,20 @@ gulp.task(
})
);

function babelify(js, modules, processLess = true) {
function babelify(js, modules) {
const babelConfig = getBabelCommonConfig(modules);
delete babelConfig.cacheDirectory;
if (modules === false) {
babelConfig.plugins.push(replaceLib);
}
let stream = js.pipe(babel(babelConfig));
if (processLess) {
stream = stream.pipe(
through2.obj(function z(file, encoding, next) {
this.push(file.clone());
if (file.path.match(/(\/|\\)style(\/|\\)index\.js/)) {
const content = file.contents.toString(encoding);
if (content.indexOf("'react-native'") !== -1) {
// actually in antd-mobile@2.0, this case will never run,
// since we both split style/index.mative.js style/index.js
// but let us keep this check at here
// in case some of our developer made a file name mistake ==
next();
return;
}

file.contents = Buffer.from(cssInjection(content));
file.path = file.path.replace(/index\.js/, 'css.js');
this.push(file);
next();
} else {
next();
}
})
);
}
const stream = js.pipe(babel(babelConfig));
return stream.pipe(gulp.dest(modules === false ? esDir : libDir));
}

function compile(modules, processLess = true, processLocale = true) {
const { compile: { transformTSFile, transformFile, includeLessFile = [] } = {} } = getConfig();
function compile(modules, processLocale = true) {
const { compile: { transformTSFile, transformFile } = {} } = getConfig();
rimraf.sync(modules !== false ? libDir : esDir);

// =============================== LESS ===============================
let less;
if (processLess) {
less = gulp
.src(['components/**/*.less'])
.pipe(
through2.obj(function (file, encoding, next) {
// Replace content
const cloneFile = file.clone();
const content = file.contents.toString().replace(/^\uFEFF/, '');

cloneFile.contents = Buffer.from(content);

// Clone for css here since `this.push` will modify file.path
const cloneCssFile = cloneFile.clone();

this.push(cloneFile);

// Transform less file
if (
file.path.match(/(\/|\\)style(\/|\\)index\.less$/) ||
file.path.match(/(\/|\\)style(\/|\\)v2-compatible-reset\.less$/) ||
includeLessFile.some(regex => file.path.match(regex))
) {
transformLess(cloneCssFile.contents.toString(), cloneCssFile.path)
.then(css => {
cloneCssFile.contents = Buffer.from(css);
cloneCssFile.path = cloneCssFile.path.replace(/\.less$/, '.css');
this.push(cloneCssFile);
next();
})
.catch(e => {
console.error(e);
});
} else {
next();
}
})
)
.pipe(gulp.dest(modules === false ? esDir : libDir));
}
const assets = gulp
.src(['components/**/*.@(png|svg)'])
.pipe(gulp.dest(modules === false ? esDir : libDir));
Expand Down Expand Up @@ -349,9 +281,9 @@ function compile(modules, processLess = true, processLocale = true) {

tsResult.on('finish', check);
tsResult.on('end', check);
const tsFilesStream = babelify(tsResult.js, modules, processLess);
const tsFilesStream = babelify(tsResult.js, modules);
const tsd = tsResult.dts.pipe(gulp.dest(modules === false ? esDir : libDir));
return merge2([less, tsFilesStream, tsd, assets, transformFileStream].filter(s => s));
return merge2([tsFilesStream, tsd, assets, transformFileStream].filter(s => s));
}

function compileLocale(done) {
Expand Down Expand Up @@ -458,11 +390,6 @@ gulp.task('compile-with-es', done => {
compile(false).on('finish', done);
});

gulp.task('compile-with-es-experimental', done => {
console.log('[Parallel] Compile to es...');
compile(false, false).on('finish', done);
});

gulp.task('compile-with-locale', done => {
console.log('[Parallel] Compile locale files to js...');
compileLocale(done);
Expand All @@ -485,13 +412,8 @@ gulp.task('compile-finalize', done => {

gulp.task(
'compile',
gulp.series(gulp.parallel('compile-with-es', 'compile-with-lib'), 'compile-finalize')
);

gulp.task(
'compile-experimental',
gulp.series(
gulp.parallel('compile-with-es-experimental', 'compile-with-locale'),
gulp.parallel('compile-with-es', 'compile-with-lib', 'compile-with-locale'),
'compile-finalize'
)
);
Expand All @@ -510,13 +432,6 @@ gulp.task(
})
);

gulp.task(
'pub-experimental',
gulp.series('check-git', 'compile-experimental', 'dist', 'package-diff', done => {
pub(done);
})
);

gulp.task(
'update-self',
gulp.series(done => {
Expand Down
27 changes: 0 additions & 27 deletions lib/transformLess.js

This file was deleted.

11 changes: 0 additions & 11 deletions lib/utils/styleUtil.js

This file was deleted.

0 comments on commit caf0c84

Please sign in to comment.