Skip to content

Commit f4c106a

Browse files
FrozenPandazvsavkin
authored andcommittedOct 4, 2018
feat(builders): support es2015 compilation
1 parent cd4af6a commit f4c106a

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed
 

‎packages/builders/src/node/build/webpack/config.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ describe('getWebpackConfig', () => {
6969
expect(result.resolve.extensions).toEqual(['.ts', '.js']);
7070
});
7171

72+
it('should include module and main in mainFields', () => {
73+
spyOn(ts, 'parseJsonConfigFileContent').and.returnValue({
74+
options: {
75+
target: 'es5'
76+
}
77+
});
78+
79+
const result = getWebpackConfig(input);
80+
expect(result.resolve.mainFields).toContain('module');
81+
expect(result.resolve.mainFields).toContain('main');
82+
});
83+
7284
it('should not polyfill node apis', () => {
7385
const result = getWebpackConfig(input);
7486

@@ -128,6 +140,17 @@ describe('getWebpackConfig', () => {
128140
'@npmScope/libraryName': '/root/libs/libraryName/src/index.ts'
129141
});
130142
});
143+
144+
it('should include es2015 in mainFields if typescript is set es2015', () => {
145+
spyOn(ts, 'parseJsonConfigFileContent').and.returnValue({
146+
options: {
147+
target: 'es2015'
148+
}
149+
});
150+
151+
const result = getWebpackConfig(input);
152+
expect(result.resolve.mainFields).toContain('es2015');
153+
});
131154
});
132155

133156
describe('the file replacements option', () => {

‎packages/builders/src/node/build/webpack/config.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export const OUT_FILENAME = 'main.js';
1616
export function getWebpackConfig(
1717
options: BuildNodeBuilderOptions
1818
): Configuration {
19+
const compilerOptions = getCompilerOptions(options.tsConfig);
20+
const supportsEs2015 =
21+
compilerOptions.target !== ts.ScriptTarget.ES3 &&
22+
compilerOptions.target !== ts.ScriptTarget.ES5;
1923
const webpackConfig: Configuration = {
2024
entry: [options.main],
2125
mode: options.optimization ? 'production' : 'development',
@@ -40,7 +44,8 @@ export function getWebpackConfig(
4044
},
4145
resolve: {
4246
extensions: ['.ts', '.js'],
43-
alias: getAliases(options)
47+
alias: getAliases(options, compilerOptions),
48+
mainFields: [...(supportsEs2015 ? ['es2015'] : []), 'module', 'main']
4449
},
4550
target: 'node',
4651
node: false,
@@ -109,15 +114,9 @@ export function getWebpackConfig(
109114
}
110115

111116
function getAliases(
112-
options: BuildNodeBuilderOptions
117+
options: BuildNodeBuilderOptions,
118+
compilerOptions: ts.CompilerOptions
113119
): { [key: string]: string } {
114-
const readResult = ts.readConfigFile(options.tsConfig, ts.sys.readFile);
115-
const tsConfig = ts.parseJsonConfigFileContent(
116-
readResult.config,
117-
ts.sys,
118-
dirname(options.tsConfig)
119-
);
120-
const compilerOptions = tsConfig.options;
121120
const replacements = [
122121
...options.fileReplacements,
123122
...(compilerOptions.paths
@@ -135,3 +134,13 @@ function getAliases(
135134
{}
136135
);
137136
}
137+
138+
function getCompilerOptions(tsConfigPath: string) {
139+
const readResult = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
140+
const tsConfig = ts.parseJsonConfigFileContent(
141+
readResult.config,
142+
ts.sys,
143+
dirname(tsConfigPath)
144+
);
145+
return tsConfig.options;
146+
}

0 commit comments

Comments
 (0)
Please sign in to comment.