-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable tsconfig customization thru the programmatic API (#517)
Closes #256
- Loading branch information
Showing
7 changed files
with
199 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,64 @@ | ||
import { InjectionToken, FactoryProvider } from 'injection-js'; | ||
import * as path from 'path'; | ||
import { CliArguments } from '../commands/build.command'; | ||
import { NgArtefacts } from '../ng-package-format/artefacts'; | ||
import { NgPackage } from '../ng-package-format/package'; | ||
import { copyFiles } from '../util/copy'; | ||
import * as log from '../util/log'; | ||
import { rimraf } from '../util/rimraf'; | ||
import { BuildStep } from '../deprecations'; | ||
import { discoverPackages } from './init'; | ||
import { transformSources } from './entry-point-transforms'; | ||
import { ENTRY_POINT_TRANSFORMS_TOKEN } from './entry-point-transforms'; | ||
|
||
// XX: should eventually become a BuildStep | ||
export async function buildNgPackage(opts: CliArguments): Promise<void> { | ||
log.info(`Building Angular Package`); | ||
|
||
let ngPackage: NgPackage; | ||
try { | ||
// READ `NgPackage` from either 'package.json', 'ng-package.json', or 'ng-package.js' | ||
ngPackage = await discoverPackages(opts); | ||
|
||
// clean the primary dest folder (should clean all secondary module directories as well) | ||
await rimraf(ngPackage.dest); | ||
|
||
const artefacts = new NgArtefacts(ngPackage.primary, ngPackage); | ||
await transformSources({ artefacts, entryPoint: ngPackage.primary, pkg: ngPackage }); | ||
for (const secondary of ngPackage.secondaries) { | ||
const artefacts = new NgArtefacts(secondary, ngPackage); | ||
await transformSources({ artefacts, entryPoint: secondary, pkg: ngPackage }); | ||
} | ||
export function buildNgPackageFactory(entryPointTransforms: BuildStep) { | ||
|
||
await copyFiles(`${ngPackage.src}/README.md`, ngPackage.dest); | ||
await copyFiles(`${ngPackage.src}/LICENSE`, ngPackage.dest); | ||
|
||
// clean the working directory for a successful build only | ||
await rimraf(ngPackage.workingDirectory); | ||
log.success(`Built Angular Package! | ||
- from: ${ngPackage.src} | ||
- to: ${ngPackage.dest} | ||
`); | ||
} catch (error) { | ||
// Report error messages and throw the error further up | ||
log.error(error); | ||
if (ngPackage) { | ||
log.info(`Build failed. The working directory was not pruned. Files are stored at ${ngPackage.workingDirectory}.`); | ||
} | ||
return async function buildNgPackage(opts: CliArguments): Promise<void> { | ||
log.info(`Building Angular Package`); | ||
|
||
let ngPackage: NgPackage; | ||
try { | ||
// READ `NgPackage` from either 'package.json', 'ng-package.json', or 'ng-package.js' | ||
ngPackage = await discoverPackages(opts); | ||
|
||
// clean the primary dest folder (should clean all secondary module directories as well) | ||
await rimraf(ngPackage.dest); | ||
|
||
// Sequentially build entry points | ||
const entryPoints = [ ngPackage.primary, ...ngPackage.secondaries ]; | ||
for (const entryPoint of entryPoints) { | ||
// Prepare artefacts. Will be populated by the entry point transformations | ||
const artefacts = new NgArtefacts(ngPackage.primary, ngPackage); | ||
await entryPointTransforms({ artefacts, entryPoint, pkg: ngPackage }); | ||
} | ||
|
||
await copyFiles(`${ngPackage.src}/README.md`, ngPackage.dest); | ||
await copyFiles(`${ngPackage.src}/LICENSE`, ngPackage.dest); | ||
|
||
throw error; | ||
} | ||
// clean the working directory for a successful build only | ||
await rimraf(ngPackage.workingDirectory); | ||
log.success(`Built Angular Package! | ||
- from: ${ngPackage.src} | ||
- to: ${ngPackage.dest} | ||
`); | ||
} catch (error) { | ||
// Report error messages and throw the error further up | ||
log.error(error); | ||
if (ngPackage) { | ||
log.info(`Build failed. The working directory was not pruned. Files are stored at ${ngPackage.workingDirectory}.`); | ||
} | ||
|
||
throw error; | ||
} | ||
}; | ||
} | ||
|
||
export type BuildCallSignature = (opts: CliArguments) => Promise<void>; | ||
|
||
export const BUILD_NG_PACKAGE_TOKEN = new InjectionToken<BuildCallSignature>('ng.v5.buildNgPackage'); | ||
|
||
export const BUILD_NG_PACKAGE_PROVIDER: FactoryProvider = { | ||
provide: BUILD_NG_PACKAGE_TOKEN, | ||
useFactory: buildNgPackageFactory, | ||
deps: [ ENTRY_POINT_TRANSFORMS_TOKEN ] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as ng from '@angular/compiler-cli'; | ||
// XX: has or is using name 'ParsedConfiguration' ... but cannot be named | ||
import { ParsedConfiguration } from '@angular/compiler-cli'; | ||
import { InjectionToken, FactoryProvider } from 'injection-js'; | ||
import * as path from 'path'; | ||
import * as ts from 'typescript'; | ||
import { BuildStep } from '../deprecations'; | ||
|
||
/** | ||
* TypeScript configuration used internally (marker typer). | ||
*/ | ||
export type TsConfig = ng.ParsedConfiguration; | ||
|
||
/** | ||
* Reads the default TypeScript configuration. | ||
*/ | ||
export function defaultTsConfigFactory() { | ||
return ng.readConfiguration(path.resolve(__dirname, '..', 'conf', 'tsconfig.ngc.json')); | ||
} | ||
|
||
export const DEFAULT_TS_CONFIG_TOKEN = new InjectionToken<TsConfig>('ng.v5.defaultTsConfig'); | ||
|
||
export const DEFAULT_TS_CONFIG_PROVIDER: FactoryProvider = { | ||
provide: DEFAULT_TS_CONFIG_TOKEN, | ||
useFactory: defaultTsConfigFactory, | ||
deps: [] | ||
}; | ||
|
||
/** | ||
* Prepares TypeScript Compiler and Angular Compiler options by overriding the default config | ||
* with entry point-specific values. | ||
*/ | ||
export const prepareTsConfigFactory: (def: TsConfig) => BuildStep = defaultTsConfig => ({ | ||
artefacts, | ||
entryPoint, | ||
pkg | ||
}) => { | ||
const basePath = path.dirname(entryPoint.entryFilePath); | ||
|
||
// Resolve defaults from DI token | ||
const tsConfig = { ...defaultTsConfig }; | ||
|
||
tsConfig.rootNames = [entryPoint.entryFilePath]; | ||
tsConfig.options.flatModuleId = entryPoint.moduleId; | ||
tsConfig.options.flatModuleOutFile = `${entryPoint.flatModuleFile}.js`; | ||
tsConfig.options.basePath = basePath; | ||
tsConfig.options.baseUrl = basePath; | ||
tsConfig.options.rootDir = basePath; | ||
tsConfig.options.outDir = artefacts.outDir; | ||
tsConfig.options.genDir = artefacts.outDir; | ||
|
||
if (entryPoint.languageLevel) { | ||
// ng.readConfiguration implicitly converts "es6" to "lib.es6.d.ts", etc. | ||
tsConfig.options.lib = entryPoint.languageLevel.map(lib => `lib.${lib}.d.ts`); | ||
} | ||
|
||
switch (entryPoint.jsxConfig) { | ||
case 'preserve': | ||
tsConfig.options.jsx = ts.JsxEmit.Preserve; | ||
break; | ||
case 'react': | ||
tsConfig.options.jsx = ts.JsxEmit.React; | ||
break; | ||
case 'react-native': | ||
tsConfig.options.jsx = ts.JsxEmit.ReactNative; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
artefacts.tsConfig = tsConfig; | ||
}; | ||
|
||
export const PREPARE_TS_CONFIG_TOKEN = new InjectionToken<BuildStep>('ng.v5.prepareTsConfig'); | ||
|
||
export const PREPARE_TS_CONFIG_PROVIDER: FactoryProvider = { | ||
provide: PREPARE_TS_CONFIG_TOKEN, | ||
useFactory: prepareTsConfigFactory, | ||
deps: [DEFAULT_TS_CONFIG_TOKEN] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters