Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit 12c708c

Browse files
committed
feat(*): add babel options, write some tests on babel
1 parent 0d9e687 commit 12c708c

File tree

12 files changed

+93
-14
lines changed

12 files changed

+93
-14
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,22 @@ Less logging from the checker.
128128

129129
### useBabel *(boolean) (default=false)*
130130

131-
Invoke Babel to transpile files. Useful with ES6 target.
131+
Invoke Babel to transpile files. Useful with ES6 target. Please see `useCache` option
132+
which can improve warm-up time.
132133

133-
### usePrecompiledFiles *(boolean) (default=false)*
134+
### babelOptions *(object) (default=null)*
134135

135-
Use pre-compiled files if any. Files must be named as `{filename}.js` and `{filename}.map`.
136+
Use this option to pass some options to Babel (e.g. presets). Please note that
137+
[`.babelrc` file](https://babeljs.io/docs/usage/babelrc/) is more universal way to do this.
136138

137139
### useCache *(boolean) (default=false)*
138140

139141
Use internal file cache. This is useful with Babel, when processing takes a long time to complete. Improves warm-up time.
140142

143+
### usePrecompiledFiles *(boolean) (default=false)*
144+
145+
Use pre-compiled files if any. Files must be named as `{filename}.js` and `{filename}.map`.
146+
141147
### cacheDirectory *(string) (default='.awcache')*
142148

143149
Directory when cache is stored.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
},
4545
"devDependencies": {
4646
"babel-cli": "^6.3.17",
47+
"babel-core": "^6.7.4",
4748
"babel-preset-es2015": "^6.1.2",
4849
"babel-preset-es2015-node4": "^1.0.0",
4950
"babel-preset-stage-2": "^6.1.2",

src/host.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface ICompilerOptions extends ts.CompilerOptions {
4444
forkChecker?: boolean;
4545
forkCheckerSilent?: boolean;
4646
useBabel?: boolean;
47+
babelOptions?: any;
4748
usePrecompiledFiles?: boolean;
4849
useCache?: boolean;
4950
cacheDirectory?: string;

src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ async function compiler(webpack: IWebPack, text: string): Promise<void> {
3636
let instanceName = options.instanceName || 'default';
3737

3838
let instance = ensureInstance(webpack, options, instanceName);
39-
4039
let state = instance.tsState;
4140

4241
let callback = webpack.async();
@@ -140,7 +139,9 @@ async function compiler(webpack: IWebPack, text: string): Promise<void> {
140139
sourceMap: true
141140
};
142141

143-
let babelResult = instance.babelImpl.transform(resultText, defaultOptions);
142+
let babelOptions = Object.assign({}, defaultOptions, options.babelOptions);
143+
let babelResult = instance.babelImpl.transform(resultText, babelOptions);
144+
144145
resultText = babelResult.code;
145146
resultSourceMap = babelResult.map;
146147
}

src/test/babel.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
cleanAndCompile, expect, readOutputFile,
3+
fixturePath, readFixture, expectSource, createConfig
4+
} from './utils';
5+
6+
describe('main test', function() {
7+
8+
it('should transpile file with babel', async function() {
9+
// babel need some time to init
10+
this.timeout(10000);
11+
12+
let config = {
13+
entry: fixturePath(['babel', 'babel.ts'])
14+
};
15+
16+
let loaderQuery = {
17+
useBabel: true,
18+
babelOptions: {
19+
"presets": ["es2015"]
20+
}
21+
};
22+
23+
let stats = await cleanAndCompile(createConfig(config, { loaderQuery }));
24+
expect(stats.compilation.errors.length).eq(0);
25+
26+
let result = await readOutputFile();
27+
let expectation = await readFixture(['babel', 'babel.js']);
28+
29+
expectSource(result, expectation);
30+
});
31+
32+
it('should use options from query', async function() {
33+
// babel need some time to init
34+
this.timeout(10000);
35+
36+
let config = {
37+
entry: fixturePath(['babel', 'babel.ts'])
38+
};
39+
40+
let loaderQuery = {
41+
useBabel: true,
42+
babelOptions: {
43+
"presets": ["unknown-preset"]
44+
}
45+
};
46+
47+
let throws = false;
48+
try {
49+
let stats = await cleanAndCompile(createConfig(config, { loaderQuery }));
50+
expect(stats.compilation.errors.length).eq(0);
51+
} catch (e) {
52+
throws = true;
53+
}
54+
55+
expect(throws).to.true;
56+
});
57+
});

src/test/checker.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ describe('checker test', function() {
4141
{
4242
watch: true,
4343
forkChecker: true,
44-
loaderParams: '&+forkChecker'
44+
loaderQuery: {
45+
forkChecker: true
46+
}
4547
}
4648
);
4749

src/test/fixtures/babel/babel.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var HiThere = function HiThere(a, b) {
2+
_classCallCheck(this, HiThere);
3+
var t = a + b;
4+
}

src/test/fixtures/babel/babel.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class HiThere {
2+
constructor(a: number, b: string) {
3+
let t = a + b;
4+
}
5+
}

src/test/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe('main test', function() {
2929
});
3030

3131
it('should load tsx files and use tsconfig', async function() {
32-
let tsConfig = fixturePath(['tsx', 'tsconfig.json']);
32+
let tsconfig = fixturePath(['tsx', 'tsconfig.json']);
3333
let config = {
3434
entry: fixturePath(['tsx', 'basic.tsx'])
3535
};
3636

37-
let loaderParams = `&tsconfig=${tsConfig}`;
37+
let loaderQuery = { tsconfig };
3838

39-
let stats = await cleanAndCompile(createConfig(config, { loaderParams }));
39+
let stats = await cleanAndCompile(createConfig(config, { loaderQuery }));
4040
expect(stats.compilation.errors.length).eq(1);
4141

4242
let result = await readOutputFile();

src/test/salsa.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ describe('salsa test', function() {
1010
};
1111

1212
let tsconfig = fixturePath(['salsa', 'tsconfig.json']);
13-
let loaderParams = `&tsconfig=${tsconfig}`;
13+
let loaderQuery = { tsconfig };
1414
let exclude = [ /exclude/ ];
1515

16-
let stats = await cleanAndCompile(createConfig(config, { loaderParams, exclude }));
16+
let stats = await cleanAndCompile(createConfig(config, { loaderQuery, exclude }));
1717

1818
console.log(stats.compilation.errors);
1919

src/test/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ export const defaultOutputDir = path.join(process.cwd(), 'src', 'test', 'output'
2222
export const defaultFixturesDir = path.join(process.cwd(), 'src', 'test', 'fixtures');
2323

2424
export interface ConfigOptions {
25-
loaderParams?: string;
25+
loaderQuery?: any;
2626
watch?: boolean;
2727
forkChecker?: boolean;
2828
include?: (string | RegExp)[];
2929
exclude?: (string | RegExp)[];
3030
}
3131

3232
let defaultOptions: ConfigOptions = {
33-
loaderParams: '',
3433
watch: false,
3534
forkChecker: false,
3635
};
@@ -50,7 +49,8 @@ export function createConfig(conf, _options: ConfigOptions = defaultOptions) {
5049
loaders: [
5150
{
5251
test: /\.(tsx?|jsx?)/,
53-
loader: loaderDir + '?target=es6' + options.loaderParams,
52+
loader: loaderDir,
53+
query: Object.assign({ target: 'es6' }, options.loaderQuery)
5454
},
5555
],
5656
},

src/tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"./index.ts",
2626
"./instance.ts",
2727
"./resolver.ts",
28+
"./test/babel.ts",
2829
"./test/checker.ts",
30+
"./test/fixtures/babel/babel.ts",
2931
"./test/fixtures/basic/basic.ts",
3032
"./test/fixtures/checker/to-check.ts",
3133
"./test/fixtures/errors/with-type-errors.ts",

0 commit comments

Comments
 (0)