Skip to content

Commit

Permalink
fix: disable internal emitDecoratorMetadata
Browse files Browse the repository at this point in the history
t the moment, we are always setting `emitDecoratorMetadata`, this causes a problem in ES2015 when using `forwardRef`.

With #1401 `emitDecoratorMetadata` is no longer needed unless using `tsickle`.

`createEmitCallback` has been updated for this reason and also their is a PR for compiler-cli to do the same angular/angular#32880
  • Loading branch information
alan-agius4 committed Oct 3, 2019
1 parent db5ad68 commit d0bf507
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 37 deletions.
15 changes: 15 additions & 0 deletions integration/samples/ctor-decorator/README.md
@@ -0,0 +1,15 @@
Sample library: Core folder layout
==================================

Folder layout from [Angulare core packages](https://github.com/angular/angular/blob/master/packages/core/public_api.ts):

```
|- package.json
|- public_api.ts
|- src
|- module.ts
|- foo
|- foo.ts
|- bar
|- bar.ts
```
6 changes: 6 additions & 0 deletions integration/samples/ctor-decorator/ng-package.json
@@ -0,0 +1,6 @@
{
"$schema": "../../../src/ng-package.schema.json",
"lib": {
"entryFile": "public_api.ts"
}
}
20 changes: 20 additions & 0 deletions integration/samples/ctor-decorator/package.json
@@ -0,0 +1,20 @@
{
"name": "@sample/ctor-decorator",
"description": "A sample library with folder layout from Angular ctor-decorator packages",
"version": "1.0.0-pre.0",
"private": true,
"repository": "https://github.com/ng-packagr/ng-packagr.git",
"sideEffects": true,
"peerDependencies": {
"@angular/common": "^4.1.3",
"@angular/core": "^4.1.3",
"@angular/router": "^4.1.3",
"@angular/http": "^4.1.3",
"rxjs": "^5.0.1",
"zone.js": "^0.8.4"
},
"scripts": {
"test":
"../../../node_modules/.bin/cross-env TS_NODE_PROJECT=../../../integration/tsconfig.specs.json ../../../node_modules/.bin/mocha --compilers ts:ts-node/register specs/**/*.ts"
}
}
2 changes: 2 additions & 0 deletions integration/samples/ctor-decorator/public_api.ts
@@ -0,0 +1,2 @@
export * from './src/angular.component';
export * from './src/angular.module';
27 changes: 27 additions & 0 deletions integration/samples/ctor-decorator/specs/es2015-api.ts
@@ -0,0 +1,27 @@
import { expect } from 'chai';
import * as fs from 'fs';
import * as path from 'path';

describe(`@sample/ctor-decorator`, () => {
describe(`fesm2015/ctor-decorator.js`, () => {
let BUNDLE;

before(() => {
BUNDLE = fs.readFileSync(path.resolve(__dirname, '../dist/fesm2015/sample-ctor-decorator.js'), 'utf-8');
});

it('should exist', () => {
expect(BUNDLE).to.be.ok;
});

it('should contain ctorParameters and __decorate', () => {
expect(BUNDLE).to.contain('type: ChangeDetectorRef');
expect(BUNDLE).to.contain('__decorate');
expect(BUNDLE).to.contain('ctorParameters');
});

it('should not contain any __metadata', () => {
expect(BUNDLE).not.to.contain('__metadata');
});
});
});
9 changes: 9 additions & 0 deletions integration/samples/ctor-decorator/src/angular.component.ts
@@ -0,0 +1,9 @@
import { Component, ChangeDetectorRef } from '@angular/core';

@Component({
selector: 'ng-component',
template: '<h1>Angular!</h1>',
})
export class AngularComponent {
constructor(cdr: ChangeDetectorRef) {}
}
10 changes: 10 additions & 0 deletions integration/samples/ctor-decorator/src/angular.module.ts
@@ -0,0 +1,10 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AngularComponent } from './angular.component';

@NgModule({
imports: [CommonModule],
declarations: [AngularComponent],
exports: [AngularComponent],
})
export class AngularModule {}
71 changes: 35 additions & 36 deletions src/lib/ngc/create-emit-callback.ts
Expand Up @@ -18,41 +18,43 @@ import * as api from '@angular/compiler-cli/src/transformers/api';

// @link https://github.com/angular/angular/blob/83d207d/packages/compiler-cli/src/main.ts#L42-L84
export function createEmitCallback(options: api.CompilerOptions): api.TsEmitCallback | undefined {
const transformDecorators = options.annotationsAs !== 'decorators';
const transformDecorators = options.enableIvy === false && options.annotationsAs !== 'decorators';
const transformTypesToClosure = options.annotateForClosureCompiler;
if (!transformDecorators && !transformTypesToClosure) {
return undefined;
}
if (transformDecorators) {
// This is needed as a workaround for https://github.com/angular/tsickle/issues/635
// Otherwise tsickle might emit references to non imported values
// as TypeScript elided the import.
options.emitDecoratorMetadata = true;
}
const tsickleHost: Pick<
tsickle.TsickleHost,
| 'shouldSkipTsickleProcessing'
| 'pathToModuleName'
| 'shouldIgnoreWarningsForPath'
| 'fileNameToModuleId'
| 'googmodule'
| 'untyped'
| 'convertIndexImportShorthand'
| 'transformDecorators'
| 'transformTypesToClosure'
> = {
shouldSkipTsickleProcessing: fileName => /\.d\.ts$/.test(fileName) || GENERATED_FILES.test(fileName),
pathToModuleName: (_context, _importPath) => '',
shouldIgnoreWarningsForPath: _filePath => false,
fileNameToModuleId: fileName => fileName,
googmodule: false,
untyped: true,
convertIndexImportShorthand: false,
transformDecorators,
transformTypesToClosure,
};

if (options.annotateForClosureCompiler || options.annotationsAs === 'static fields') {
if (transformDecorators) {
// This is needed as a workaround for https://github.com/angular/tsickle/issues/635
// Otherwise tsickle might emit references to non imported values
// as TypeScript elided the import.
options.emitDecoratorMetadata = true;
}

const tsickleHost: Pick<
tsickle.TsickleHost,
| 'shouldSkipTsickleProcessing'
| 'pathToModuleName'
| 'shouldIgnoreWarningsForPath'
| 'fileNameToModuleId'
| 'googmodule'
| 'untyped'
| 'convertIndexImportShorthand'
| 'transformDecorators'
| 'transformTypesToClosure'
> = {
shouldSkipTsickleProcessing: fileName => /\.d\.ts$/.test(fileName) || GENERATED_FILES.test(fileName),
pathToModuleName: (_context, _importPath) => '',
shouldIgnoreWarningsForPath: _filePath => false,
fileNameToModuleId: fileName => fileName,
googmodule: false,
untyped: true,
convertIndexImportShorthand: false,
transformDecorators,
transformTypesToClosure,
};

return ({
program,
targetSourceFile,
Expand All @@ -63,9 +65,10 @@ export function createEmitCallback(options: api.CompilerOptions): api.TsEmitCall
host,
options,
}) =>
// tslint:disable-next-line:no-require-imports only depend on tsickle if requested
require('tsickle').emitWithTsickle(
program,
{ ...tsickleHost, options, moduleResolutionHost: host },
{ ...tsickleHost, options, host, moduleResolutionHost: host },
host,
options,
targetSourceFile,
Expand All @@ -77,11 +80,7 @@ export function createEmitCallback(options: api.CompilerOptions): api.TsEmitCall
afterTs: customTransformers.after,
},
);
} else {
return ({ program, targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers = {} }) =>
program.emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, {
after: customTransformers.after,
before: customTransformers.before,
});
}

return undefined;
}
1 change: 0 additions & 1 deletion src/lib/ts/tsconfig.ts
Expand Up @@ -16,7 +16,6 @@ export function readDefaultTsConfig(fileName?: string): ng.ParsedConfiguration {
const extraOptions: ng.CompilerOptions = {
moduleResolution: ts.ModuleResolutionKind.NodeJs,
target: ts.ScriptTarget.ES2015,
emitDecoratorMetadata: true,
experimentalDecorators: true,

// sourcemaps
Expand Down

0 comments on commit d0bf507

Please sign in to comment.