Skip to content

Commit 8fdac8f

Browse files
JoostKmhevery
authored andcommitted
fix(compiler-cli): expose the linker as a Babel plugin (#41918)
This allows the linker to be used as a true Babel plugin. In a Babel configuration file, include the linker as follows: ```js { plugins: [ '@angular/compiler-cli/linker/babel', ] } ``` or, if you need to specify configuration options: ```js { plugins: [ ['@angular/compiler-cli/linker/babel', {linkerJitMode: true}], ] } ``` PR Close #41918
1 parent 9759bca commit 8fdac8f

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

packages/compiler-cli/linker/babel/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
export {createEs2015LinkerPlugin} from './src/es2015_linker_plugin';
8+
import {defaultLinkerPlugin} from './src/babel_plugin';
9+
10+
export {createEs2015LinkerPlugin} from './src/es2015_linker_plugin';
11+
export default defaultLinkerPlugin;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {ConfigAPI, PluginObj} from '@babel/core';
9+
10+
import {NodeJSFileSystem} from '../../../src/ngtsc/file_system';
11+
import {ConsoleLogger, LogLevel} from '../../../src/ngtsc/logging';
12+
import {LinkerOptions} from '../../src/file_linker/linker_options';
13+
14+
import {createEs2015LinkerPlugin} from './es2015_linker_plugin';
15+
16+
/**
17+
* This is the Babel plugin definition that is provided as a default export from the package, such
18+
* that the plugin can be used using the module specifier of the package. This is the recommended
19+
* way of integrating the Angular Linker into a build pipeline other than the Angular CLI.
20+
*
21+
* When the module specifier `@angular/compiler-cli/linker/babel` is used as a plugin in a Babel
22+
* configuration, Babel invokes this function (by means of the default export) to create the plugin
23+
* instance according to the provided options.
24+
*
25+
* The linker plugin that is created uses the native NodeJS filesystem APIs to interact with the
26+
* filesystem. Any logging output is printed to the console.
27+
*
28+
* @param api Provides access to the Babel environment that is configuring this plugin.
29+
* @param options The plugin options that have been configured.
30+
*/
31+
export function defaultLinkerPlugin(api: ConfigAPI, options: Partial<LinkerOptions>): PluginObj {
32+
api.assertVersion(7);
33+
34+
return createEs2015LinkerPlugin({
35+
...options,
36+
fileSystem: new NodeJSFileSystem(),
37+
logger: new ConsoleLogger(LogLevel.info),
38+
});
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {transformSync} from '@babel/core';
9+
10+
describe('default babel plugin entry-point', () => {
11+
it('should work as a Babel plugin using the module specifier', () => {
12+
const result = transformSync(
13+
`
14+
import * as i0 from "@angular/core";
15+
16+
export class MyMod {}
17+
export class MyComponent {}
18+
19+
MyMod.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyMod, declarations: [MyComponent] });
20+
`,
21+
{
22+
plugins: [
23+
'@angular/compiler-cli/linker/babel',
24+
],
25+
filename: 'test.js',
26+
})!;
27+
28+
expect(result).not.toBeNull();
29+
expect(result.code).not.toContain('ɵɵngDeclareNgModule');
30+
expect(result.code).toContain('i0.ɵɵdefineNgModule');
31+
expect(result.code).not.toMatch(/declarations:\s*\[MyComponent]/);
32+
});
33+
34+
it('should be configurable', () => {
35+
const result = transformSync(
36+
`
37+
import * as i0 from "@angular/core";
38+
39+
export class MyMod {}
40+
export class MyComponent {}
41+
42+
MyMod.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyMod, declarations: [MyComponent] });
43+
`,
44+
{
45+
plugins: [
46+
['@angular/compiler-cli/linker/babel', {linkerJitMode: true}],
47+
],
48+
filename: 'test.js',
49+
})!;
50+
51+
expect(result).not.toBeNull();
52+
expect(result.code).not.toContain('ɵɵngDeclareNgModule');
53+
expect(result.code).toContain('i0.ɵɵdefineNgModule');
54+
expect(result.code).toMatch(/declarations:\s*\[MyComponent]/);
55+
});
56+
});

0 commit comments

Comments
 (0)