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 { EmitterVisitorContext } from './abstract_emitter';
|
---|
9 | import { AbstractJsEmitterVisitor } from './abstract_js_emitter';
|
---|
10 | import * as o from './output_ast';
|
---|
11 | export class JavaScriptEmitter {
|
---|
12 | emitStatements(genFilePath, stmts, preamble = '') {
|
---|
13 | const converter = new JsEmitterVisitor();
|
---|
14 | const ctx = EmitterVisitorContext.createRoot();
|
---|
15 | converter.visitAllStatements(stmts, ctx);
|
---|
16 | const preambleLines = preamble ? preamble.split('\n') : [];
|
---|
17 | converter.importsWithPrefixes.forEach((prefix, importedModuleName) => {
|
---|
18 | // Note: can't write the real word for import as it screws up system.js auto detection...
|
---|
19 | preambleLines.push(`var ${prefix} = req` +
|
---|
20 | `uire('${importedModuleName}');`);
|
---|
21 | });
|
---|
22 | const sm = ctx.toSourceMapGenerator(genFilePath, preambleLines.length).toJsComment();
|
---|
23 | const lines = [...preambleLines, ctx.toSource(), sm];
|
---|
24 | if (sm) {
|
---|
25 | // always add a newline at the end, as some tools have bugs without it.
|
---|
26 | lines.push('');
|
---|
27 | }
|
---|
28 | return lines.join('\n');
|
---|
29 | }
|
---|
30 | }
|
---|
31 | class JsEmitterVisitor extends AbstractJsEmitterVisitor {
|
---|
32 | constructor() {
|
---|
33 | super(...arguments);
|
---|
34 | this.importsWithPrefixes = new Map();
|
---|
35 | }
|
---|
36 | visitExternalExpr(ast, ctx) {
|
---|
37 | const { name, moduleName } = ast.value;
|
---|
38 | if (moduleName) {
|
---|
39 | let prefix = this.importsWithPrefixes.get(moduleName);
|
---|
40 | if (prefix == null) {
|
---|
41 | prefix = `i${this.importsWithPrefixes.size}`;
|
---|
42 | this.importsWithPrefixes.set(moduleName, prefix);
|
---|
43 | }
|
---|
44 | ctx.print(ast, `${prefix}.`);
|
---|
45 | }
|
---|
46 | ctx.print(ast, name);
|
---|
47 | return null;
|
---|
48 | }
|
---|
49 | visitDeclareVarStmt(stmt, ctx) {
|
---|
50 | super.visitDeclareVarStmt(stmt, ctx);
|
---|
51 | if (stmt.hasModifier(o.StmtModifier.Exported)) {
|
---|
52 | ctx.println(stmt, exportVar(stmt.name));
|
---|
53 | }
|
---|
54 | return null;
|
---|
55 | }
|
---|
56 | visitDeclareFunctionStmt(stmt, ctx) {
|
---|
57 | super.visitDeclareFunctionStmt(stmt, ctx);
|
---|
58 | if (stmt.hasModifier(o.StmtModifier.Exported)) {
|
---|
59 | ctx.println(stmt, exportVar(stmt.name));
|
---|
60 | }
|
---|
61 | return null;
|
---|
62 | }
|
---|
63 | visitDeclareClassStmt(stmt, ctx) {
|
---|
64 | super.visitDeclareClassStmt(stmt, ctx);
|
---|
65 | if (stmt.hasModifier(o.StmtModifier.Exported)) {
|
---|
66 | ctx.println(stmt, exportVar(stmt.name));
|
---|
67 | }
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | function exportVar(varName) {
|
---|
72 | return `Object.defineProperty(exports, '${varName}', { get: function() { return ${varName}; }});`;
|
---|
73 | }
|
---|
74 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoianNfZW1pdHRlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL2NvbXBpbGVyL3NyYy9vdXRwdXQvanNfZW1pdHRlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFDSCxPQUFPLEVBQUMscUJBQXFCLEVBQWdCLE1BQU0sb0JBQW9CLENBQUM7QUFDeEUsT0FBTyxFQUFDLHdCQUF3QixFQUFDLE1BQU0sdUJBQXVCLENBQUM7QUFDL0QsT0FBTyxLQUFLLENBQUMsTUFBTSxjQUF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