Skip to content

Commit 150d925

Browse files
committed
fix: ts cache and auto check egg
1 parent e6a3d33 commit 150d925

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

config/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ exports.typescript = {
4848

4949
exports.tslint = {
5050
enable: false,
51-
test: /\.ts$/,
51+
test: /\.tsx?$/,
5252
exclude: /node_modules/,
5353
enforce: 'pre',
5454
use: ['tslint-loader'],

lib/base.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -592,28 +592,30 @@ class WebpackBaseBuilder extends Config {
592592
return { loader, options };
593593
}
594594

595-
createBabelLoader() {
596-
const loaderName = 'babel-loader';
597-
const babelOptions = {};
595+
createBabelLoader(babel = {}) {
596+
babel = this.merge({ loader: 'babel-loader', options: {}}, babel);
598597
const babelEnv = process.env.BABEL_ENV;
599598
if (babelEnv) {
600599
if (this.isBabel7) { // @babel/core use envName key
601-
babelOptions.envName = babelEnv;
600+
babel.options.envName = babelEnv;
602601
} else { // babel-core use forceEnv key
603-
babelOptions.forceEnv = babelEnv;
602+
babel.options.forceEnv = babelEnv;
604603
}
605604
}
606605
const compile = this.config.compile || {};
607-
const configCacheOptions = this.utils.isObject(compile.cache) ? compile.cache : {};
608-
const cacheOptions = this.merge(babelOptions, configCacheOptions);
609-
const config = compile.cache ? this.createCacheLoader(cacheOptions, loaderName) : { loader: loaderName, babelOptions };
606+
// use babel cache
607+
if (compile.cache) {
608+
const cacheOptions = this.utils.isBoolean(compile.cache) ? {} : compile.cache;
609+
const cacheDirectory = this.utils.getCacheLoaderInfoPath(babel.loader, this.env, this.type);
610+
babel.options = this.merge(babel.options, { cacheDirectory }, cacheOptions);
611+
}
610612
// use project .babelrc
611613
if (fs.existsSync(this.projectBabelrc)) {
612-
return config;
614+
return babel;
613615
}
614616
// use default babelrc
615-
const options = this.merge(babelOptions, this.babelConfig);
616-
return this.merge(config, { options });
617+
babel.options = this.merge(babel.options, this.babelConfig);
618+
return babel;
617619
}
618620

619621
createCacheLoader(loaderOptions, name) {

lib/config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ class Config {
7373
return this._isEgg;
7474
}
7575

76+
get typescript() {
77+
const configLoaders = this.config.loaders;
78+
if (this.utils.isObject(configLoaders)) {
79+
if (configLoaders.typescript) {
80+
return true;
81+
}
82+
} else if (Array.isArray(configLoaders)) {
83+
return configLoaders.some(loader => {
84+
return !!loader.typescript;
85+
});
86+
}
87+
return false;
88+
}
89+
7690
get buildPath() {
7791
return utils.normalizeBuildPath(this.config.buildPath, this.baseDir);
7892
}

utils/adapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = class WebpackAdapter {
2323
}
2424
// egg project, auto set client typescript tsconfig.json config
2525
const tsConfigFile = g(config, 'typescript.options.configFile');
26-
if (config.egg && !tsConfigFile) {
26+
if (this.builder.egg && !tsConfigFile) {
2727
const configFile = path.join(this.baseDir, './app/web/tsconfig.json');
2828
if (fs.existsSync(configFile)) {
2929
this.builder.mergeConfig({

0 commit comments

Comments
 (0)