Skip to content

Commit 2d9ce36

Browse files
committed
fix: typescript compile react mode
1 parent 150d925 commit 2d9ce36

5 files changed

Lines changed: 86 additions & 2 deletions

File tree

config/loader.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ exports.typescript = {
2929
exclude: /node_modules/,
3030
use() {
3131
const loaders = [];
32-
const createTsLoader = options =>{
32+
const createTsLoader = options => {
3333
return { loader: 'ts-loader', options };
3434
};
3535
const compile = this.config.compile;
@@ -42,6 +42,10 @@ exports.typescript = {
4242
if (compile.cache) {
4343
loaders.unshift(this.createCacheLoader(compile.cache));
4444
}
45+
// react typescript need to dynamic import
46+
if (this.typescript && this.framework === 'react') {
47+
loaders.unshift(this.createBabelLoader());
48+
}
4549
return loaders;
4650
}
4751
};

lib/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class Config {
8787
return false;
8888
}
8989

90+
get framework() {
91+
return this.config.framework;
92+
}
93+
9094
get buildPath() {
9195
return utils.normalizeBuildPath(this.config.buildPath, this.baseDir);
9296
}

test/helper.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const WebpackTool = require('webpack-tool');
4+
const merge = WebpackTool.merge;
5+
const WebpackClientBuilder = require('../lib/client');
6+
const path = require('path').posix;
7+
// http://chaijs.com/api/bdd/
8+
9+
exports.createClientBuilder = config => {
10+
const builder = new WebpackClientBuilder(merge({
11+
entry: {
12+
include: path.join(__dirname)
13+
}
14+
}, config));
15+
if (config && config.type) {
16+
builder.type = config.type;
17+
}
18+
builder.setBuildPath(path.join(__dirname, 'dist/client'));
19+
builder.setPublicPath('/public');
20+
return builder;
21+
};
22+
23+
exports.getLoaderByName = (name, rules, test) => {
24+
const loaderName = `${name}-loader`;
25+
return rules.find(rule => {
26+
return rule.use.some(loader => {
27+
const hasLoader = loaderName === loader || (typeof loader === 'object' && loader.loader === loaderName);
28+
if (test && rule.test && typeof loader === 'object') {
29+
return rule.test.toString().indexOf(test) > -1 && hasLoader;
30+
}
31+
return hasLoader;
32+
});
33+
});
34+
};
35+
36+
exports.getPluginByLabel = (label, plugins) => {
37+
return plugins.find(plugin => {
38+
return plugin.__lable__ === label || plugin.__plugin__ === label;
39+
});
40+
};

test/typescript.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const expect = require('chai').expect;
3+
const helper = require('./helper');
4+
// http://chaijs.com/api/bdd/
5+
6+
describe('typescript.test.js', () => {
7+
before(() => {
8+
});
9+
10+
after(() => {
11+
});
12+
13+
beforeEach(() => {
14+
});
15+
16+
afterEach(() => {
17+
});
18+
19+
describe('#webpack typescript test', () => {
20+
21+
it('should webpack typescript extendsions test', () => {
22+
const builder = helper.createClientBuilder({ loaders: { typescript: true }});
23+
const webpackConfig = builder.create();
24+
expect(webpackConfig.resolve.extensions).to.include.members(['.ts']);
25+
});
26+
27+
it('should webpack typescript babel test', () => {
28+
const builder = helper.createClientBuilder({ framework: 'react', loaders: { typescript: true }});
29+
const webpackConfig = builder.create();
30+
const loaders = helper.getLoaderByName('ts', webpackConfig.module.rules);
31+
expect(webpackConfig.resolve.extensions).to.include.members(['.ts']);
32+
expect(loaders.use.length).to.equal(4);
33+
expect(loaders.use[0].loader).to.equal('babel-loader');
34+
});
35+
});
36+
});

utils/adapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = class WebpackAdapter {
1414
const configLoaders = config.loaders;
1515
if (utils.isObject(configLoaders)) {
1616
// 默认 typescript 开启, tslint 开启,eslint 禁用
17-
if (configLoaders.typescript) {
17+
if (this.builder.typescript) {
1818
if (utils.isObject(loaders.eslint) && configLoaders.eslint === undefined) {
1919
this.builder.mergeLoader({ eslint: false });
2020
}

0 commit comments

Comments
 (0)