Skip to content

Commit

Permalink
this runs everything through our preprocessor
Browse files Browse the repository at this point in the history
next we just need to wire back in the ast plugins
  • Loading branch information
ef4 committed Feb 24, 2019
1 parent a43d720 commit 48fc86b
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"broccoli-plugin": "^1.3.0",
"broccoli-source": "^1.1.0",
"debug": "^3.1.0",
"ember-cli-htmlbars": "^3.0.1",
"fs-extra": "^7.0.0",
"fs-tree-diff": "0.5.7",
"jsdom": "^12.0.0",
Expand Down
81 changes: 81 additions & 0 deletions packages/compat/src/apply-ast-transforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { readFileSync } from 'fs';
import HTMLBarsTransform, { Options as HTMLBarsOptions } from 'ember-cli-htmlbars';
import { Tree } from 'broccoli-plugin';
import { join } from 'path';

interface AST {
_deliberatelyOpaque: 'AST';
}

// see processString in ember-cli-htmlbars/index.js. We are going to build almost exactly the same transform, but instead of templateCompiler.precomiple we call ssyntax.print(yntax.preprocess())
interface PreprocessOptions {
contents: string; // the original source of the template
moduleName: string; // example: "ember-basic-dropdown/templates/components/basic-dropdown-content.hbs"
}

interface GlimmerSyntax {
preprocess: (html: string, options?: PreprocessOptions) => AST;
print: (ast: AST) => string;
}

let glimmerSyntaxCache: GlimmerSyntax | undefined;

// we could directly depend on @glimmer/syntax and have nice types and
// everything. But the problem is, we really want to use the exact version that
// the app itself is using, and its copy is bundled away inside
// ember-template-compiler.js.
function loadGlimmerSyntax(templateCompilerPath: string): GlimmerSyntax {
if (glimmerSyntaxCache) {
return glimmerSyntaxCache;
}
let orig = Object.create;
let grabbed: any[] = [];
(Object as any).create = function(proto: any, propertiesObject: any) {
let result = orig.call(this, proto, propertiesObject);
grabbed.push(result);
return result;
};
try {
eval(readFileSync(templateCompilerPath, 'utf8'));
} finally {
Object.create = orig;
}
for (let obj of grabbed) {
if (obj['@glimmer/syntax'] && obj['@glimmer/syntax'].print) {
// we found the loaded modules
glimmerSyntaxCache = {
print: obj['@glimmer/syntax'].print,
preprocess: obj['@glimmer/syntax'].preprocess,
};
return glimmerSyntaxCache;
}
}
throw new Error(`unable to find @glimmer/syntax methods in ${templateCompilerPath}`);
}

export default class extends HTMLBarsTransform {
private syntax: GlimmerSyntax;

constructor(inputTree: Tree, options: HTMLBarsOptions) {
options.name = 'embroider-apply-ast-transforms';
super(inputTree, options);
this.syntax = loadGlimmerSyntax(options.templateCompilerPath);

// unlike our parent class, we don't want to rename hbs to js
this.targetExtension = null;
}
processString(source: string, relativePath: string) {
console.log(`we are processing ${relativePath}`);
let ast = this.syntax.preprocess(source, {
contents: source,
moduleName: relativePath
});
return this.syntax.print(ast);
}
cacheKeyProcessString(source: string, relativePath: string) {
return `embroider-` + super.cacheKeyProcessString(source, relativePath);
}
baseDir() {
return join(__dirname, '..');
}
}
52 changes: 40 additions & 12 deletions packages/compat/src/v1-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { Package, PackageCache, BasicPackage, AddonMeta } from "@embroider/core"
import { AddonOptionsWithDefaults } from "./options";
import walkSync from 'walk-sync';
import AddToTree from "./add-to-tree";
import ApplyASTTransforms from './apply-ast-transforms';
import { Options as HTMLBarsOptions } from 'ember-cli-htmlbars';
import resolve from "resolve";

const stockTreeNames = Object.freeze([
'addon',
Expand Down Expand Up @@ -50,6 +53,18 @@ const dynamicTreeHooks = Object.freeze([

const appPublicationDir = '_app_';

let locatePreprocessRegistry: (addonInstance: any) => any;
{
let preprocessRegistry: any;
locatePreprocessRegistry = function(addonInstance: any) {
if (!preprocessRegistry) {
let cliPath = resolve.sync('ember-cli', { basedir: addonInstance._findHost().project.root });
preprocessRegistry = require(resolve.sync('ember-cli-preprocess-registry/preprocessors', { basedir: cliPath }));
}
return preprocessRegistry;
};
}

// This controls and types the interface between our new world and the classic
// v1 addon instance.
export default class V1Addon implements V1Package {
Expand Down Expand Up @@ -77,17 +92,25 @@ export default class V1Addon implements V1Package {
// a no-op transform here to avoid an exception coming out of ember-cli like
// "Addon templates were detected, but there are no template compilers
// registered".
registry.remove('template', 'ember-cli-htmlbars');
if (registry.load('htmlbars-ast-plugin').length > 0) {
todo(`${this.name} has a custom AST transform that we need to apply`);
let htmlbars = this.addonInstance.addons.find((a: any) => a.name === 'ember-cli-htmlbars');
if (htmlbars) {
registry.remove('template', 'ember-cli-htmlbars');
let options = htmlbars.htmlbarsOptions() as HTMLBarsOptions;
registry.add('template', {
name: 'embroider-addon-templates',
ext: 'hbs',
_addon: this,
toTree(tree: Tree): Tree {
if (registry.load('htmlbars-ast-plugin').length === 0) {
// when there are no custom AST transforms, we don't need to do
// anything at all.
return tree;
} else {
return new ApplyASTTransforms(tree, options);
}
}
});
}
registry.add('template', {
name: 'embroider-addon-templates',
ext: 'hbs',
toTree(tree: Tree): Tree {
return tree;
}
});
}

get name(): string {
Expand Down Expand Up @@ -167,9 +190,15 @@ export default class V1Addon implements V1Package {
if (includeCSS) {
tree = this.addonInstance.compileStyles(tree);
}
return this.addonInstance.preprocessJs(tree, '/', this.moduleName, {
tree = this.addonInstance.preprocessJs(tree, '/', this.moduleName, {
registry : this.addonInstance.registry
});
if (this.addonInstance.registry.load('template').length > 0) {
tree = locatePreprocessRegistry(this.addonInstance).preprocessTemplates(tree, {
registry: this.addonInstance.registry
});
}
return tree;
}

@Memoize()
Expand Down Expand Up @@ -267,7 +296,6 @@ export default class V1Addon implements V1Package {
srcDirs: [this.moduleName, `modules/${this.moduleName}`]
});
}
// todo: also invoke treeForAddonTemplates
} else if (this.hasStockTree('addon')) {
return this.transpile(this.stockTree('addon', {
exclude: ['styles/**']
Expand Down
19 changes: 19 additions & 0 deletions types/ember-cli-htmlbars/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

declare module 'ember-cli-htmlbars' {

import Plugin, { Tree } from "broccoli-plugin";

export interface Options {
templateCompilerPath: string;
name?: string;
}

export default class HTMLBarsTransform extends Plugin {
constructor(inputTree: Tree, options: Options)
build(): Promise<void>;
protected cacheKeyProcessString(contents: string, relativePath: string): string;
protected targetExtension: string | null;
}


}
Loading

0 comments on commit 48fc86b

Please sign in to comment.