-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor template-transform-plugin to depend on babel-plugin-stage1-i…
…nline-hbs
- Loading branch information
Matthew Edwards
committed
Jun 1, 2022
1 parent
184796a
commit 94e2b7f
Showing
2 changed files
with
56 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,37 @@ | ||
import type * as Babel from '@babel/core'; | ||
import type { types as t } from '@babel/core'; | ||
import type { NodePath } from '@babel/traverse'; | ||
import { preprocess, print } from '@glimmer/syntax'; | ||
import make from '@embroider/core/src/babel-plugin-stage1-inline-hbs'; | ||
import { TemplateCompiler, TemplateCompilerParams } from '@embroider/core'; | ||
import { getEmberExports } from '@embroider/core/src/load-ember-template-compiler'; | ||
import { EmberENV } from '@embroider/core'; | ||
|
||
type TemplateTransform = () => { name: string; visitor: {} }; | ||
|
||
export type TemplateTransformPlugin = TemplateTransform | string; | ||
export interface Options { | ||
astTransforms?: Array<string | Function>; | ||
astTransforms: TemplateTransformPlugin[] | undefined; | ||
compilerPath: string; | ||
EmberENV: EmberENV; | ||
} | ||
|
||
interface State { | ||
opts: Options; | ||
localName: string | undefined; | ||
function resolvePlugins(plugins: TemplateTransformPlugin[]) { | ||
return plugins.map((somePlugin: TemplateTransformPlugin) => { | ||
// If it's a string attempt to resolve the path to a module. | ||
return typeof somePlugin === 'string' | ||
? require(somePlugin) // eslint-disable-line @typescript-eslint/no-require-imports | ||
: somePlugin; | ||
}); | ||
} | ||
|
||
export default function main(babel: typeof Babel) { | ||
let t = babel.types; | ||
|
||
return { | ||
visitor: { | ||
ImportDeclaration(path: NodePath<t.ImportDeclaration>, state: State) { | ||
if (path.node.source.value !== 'ember-cli-htmlbars') { | ||
return; | ||
} | ||
|
||
const specifier = path.node.specifiers.find( | ||
(s) => | ||
t.isImportSpecifier(s) && | ||
t.isIdentifier(s.imported) && | ||
s.imported.name === 'hbs' | ||
); | ||
|
||
if (!specifier) { | ||
return; | ||
} | ||
|
||
state.localName = specifier.local.name; | ||
}, | ||
CallExpression(path: NodePath<t.CallExpression>, state: State) { | ||
const localName = state.localName; | ||
|
||
if (!t.isIdentifier(path.node.callee)) { | ||
return; | ||
} | ||
|
||
const callee = path.node.callee; | ||
|
||
if (!localName || callee.name !== localName) { | ||
return; | ||
} | ||
export default make((options: Options) => { | ||
let { compilerPath, astTransforms: somePlugins = [], ...opts } = options; | ||
const astTransforms: TemplateTransform[] = resolvePlugins(somePlugins); | ||
|
||
let template = (path.node.arguments[0] as t.StringLiteral).value; | ||
|
||
let options = { | ||
astTransforms: [], | ||
...state.opts, | ||
}; | ||
|
||
const astTransforms = options.astTransforms.map((maybeFunc) => { | ||
// If it's a string attempt to resolve the path to a module. | ||
return typeof maybeFunc === 'string' | ||
? require(maybeFunc) // eslint-disable-line @typescript-eslint/no-require-imports | ||
: maybeFunc; | ||
}); | ||
|
||
if (astTransforms.length < 1) { | ||
return; | ||
} | ||
|
||
const ast = preprocess(template, { | ||
plugins: { | ||
ast: [...astTransforms], | ||
}, | ||
}); | ||
|
||
const augmentedTemplate = print(ast); | ||
|
||
// Create a new stringLiteral with the augmentedTemplate | ||
path.node.arguments[0] = t.stringLiteral(augmentedTemplate); | ||
}, | ||
const params: TemplateCompilerParams = { | ||
loadEmberTemplateCompiler: () => getEmberExports(compilerPath), | ||
plugins: { | ||
ast: astTransforms, | ||
}, | ||
...opts, | ||
}; | ||
} | ||
|
||
return new TemplateCompiler(params); | ||
}); |
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