Skip to content

Commit

Permalink
keep babel out of the core template compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Mar 28, 2021
1 parent ca63cef commit a7fc231
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
16 changes: 2 additions & 14 deletions packages/core/src/template-compiler-common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import stripBom from 'strip-bom';
import { Resolver, ResolvedDep } from './resolver';
import { join, sep } from 'path';
import { PluginItem, transform } from '@babel/core';
import type { PluginItem } from '@babel/core';
import { Memoize } from 'typescript-memoize';
import wrapLegacyHbsPluginIfNeeded from 'wrap-legacy-hbs-plugin-if-needed';
import adjustImportsPlugin from './babel-plugin-adjust-imports';

export interface Plugins {
ast?: unknown[];
Expand Down Expand Up @@ -161,18 +160,7 @@ export class TemplateCompiler {
lines.push(`import { createTemplateFactory } from '@ember/template-factory';`);
lines.push(`export default createTemplateFactory(${compiled});`);

let src = lines.join('\n');
if (this.resolver) {
return transform(src, {
filename: moduleName,
generatorOpts: {
compact: false,
},
plugins: [[adjustImportsPlugin, this.resolver.adjustImportsOptions]],
})!.code!;
} else {
return src;
}
return lines.join('\n');
}

// Applies all custom AST transforms and emits the results still as
Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/template-compiler-node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Resolver } from './resolver';
import { join } from 'path';
import type { PluginItem } from '@babel/core';
import { PluginItem, transform } from '@babel/core';
import type { Params as InlineBabelParams } from './babel-plugin-inline-hbs';
import { Plugins } from './ember-template-compiler-types';
import { getEmberExports } from './load-ember-template-compiler';
import { TemplateCompiler } from './template-compiler-common';
import adjustImportsPlugin from './babel-plugin-adjust-imports';

export interface NodeTemplateCompilerParams {
compilerPath: string;
Expand All @@ -23,6 +24,22 @@ export class NodeTemplateCompiler extends TemplateCompiler {
});
}

compile(moduleName: string, contents: string) {
let src = super.compile(moduleName, contents);
let resolver = this.params.resolver;
if (resolver) {
return transform(src, {
filename: moduleName,
generatorOpts: {
compact: false,
},
plugins: [[adjustImportsPlugin, resolver.adjustImportsOptions]],
})!.code!;
} else {
return src;
}
}

// Use applyTransforms on the contents of inline hbs template strings inside
// Javascript.
inlineTransformsBabelPlugin(): PluginItem {
Expand Down

0 comments on commit a7fc231

Please sign in to comment.