Skip to content

Commit

Permalink
feat(builders): support es2015 compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Oct 4, 2018
1 parent cd4af6a commit f4c106a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
23 changes: 23 additions & 0 deletions packages/builders/src/node/build/webpack/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ describe('getWebpackConfig', () => {
expect(result.resolve.extensions).toEqual(['.ts', '.js']);
});

it('should include module and main in mainFields', () => {
spyOn(ts, 'parseJsonConfigFileContent').and.returnValue({
options: {
target: 'es5'
}
});

const result = getWebpackConfig(input);
expect(result.resolve.mainFields).toContain('module');
expect(result.resolve.mainFields).toContain('main');
});

it('should not polyfill node apis', () => {
const result = getWebpackConfig(input);

Expand Down Expand Up @@ -128,6 +140,17 @@ describe('getWebpackConfig', () => {
'@npmScope/libraryName': '/root/libs/libraryName/src/index.ts'
});
});

it('should include es2015 in mainFields if typescript is set es2015', () => {
spyOn(ts, 'parseJsonConfigFileContent').and.returnValue({
options: {
target: 'es2015'
}
});

const result = getWebpackConfig(input);
expect(result.resolve.mainFields).toContain('es2015');
});
});

describe('the file replacements option', () => {
Expand Down
27 changes: 18 additions & 9 deletions packages/builders/src/node/build/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const OUT_FILENAME = 'main.js';
export function getWebpackConfig(
options: BuildNodeBuilderOptions
): Configuration {
const compilerOptions = getCompilerOptions(options.tsConfig);
const supportsEs2015 =
compilerOptions.target !== ts.ScriptTarget.ES3 &&
compilerOptions.target !== ts.ScriptTarget.ES5;
const webpackConfig: Configuration = {
entry: [options.main],
mode: options.optimization ? 'production' : 'development',
Expand All @@ -40,7 +44,8 @@ export function getWebpackConfig(
},
resolve: {
extensions: ['.ts', '.js'],
alias: getAliases(options)
alias: getAliases(options, compilerOptions),
mainFields: [...(supportsEs2015 ? ['es2015'] : []), 'module', 'main']
},
target: 'node',
node: false,
Expand Down Expand Up @@ -109,15 +114,9 @@ export function getWebpackConfig(
}

function getAliases(
options: BuildNodeBuilderOptions
options: BuildNodeBuilderOptions,
compilerOptions: ts.CompilerOptions
): { [key: string]: string } {
const readResult = ts.readConfigFile(options.tsConfig, ts.sys.readFile);
const tsConfig = ts.parseJsonConfigFileContent(
readResult.config,
ts.sys,
dirname(options.tsConfig)
);
const compilerOptions = tsConfig.options;
const replacements = [
...options.fileReplacements,
...(compilerOptions.paths
Expand All @@ -135,3 +134,13 @@ function getAliases(
{}
);
}

function getCompilerOptions(tsConfigPath: string) {
const readResult = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
const tsConfig = ts.parseJsonConfigFileContent(
readResult.config,
ts.sys,
dirname(tsConfigPath)
);
return tsConfig.options;
}

0 comments on commit f4c106a

Please sign in to comment.