Skip to content

Commit

Permalink
Merge pull request #1276 from embroider-build/e-template-compilation-…
Browse files Browse the repository at this point in the history
…refactor

Streamline template compilation, part 2
  • Loading branch information
ef4 authored Nov 17, 2022
2 parents aea93d7 + 00ca884 commit 752d74b
Show file tree
Hide file tree
Showing 46 changed files with 3,217 additions and 3,099 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = {
eqeqeq: ['error', 'smart'],
'no-debugger': 'error',
'no-new-wrappers': 'error',
'no-redeclare': 'error',
'no-unused-labels': 'error',
'no-var': 'error',

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
supports source-to-source transformation (and it offers new capabilities to
AST transform authors that we would like to be available everywhere).

## `@embroider/shared-internals`

- BREAKING: options format of `hbsToJS` changed

# Release 2022-10-06.0

## `@embroider/core` 1.8.3 -> 1.9.0 minor
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"tests/v2-addon-template"
],
"nohoist": [
"**/@types/broccoli-plugin"
"**/@types/broccoli-plugin",
"**/babel-plugin-ember-template-compilation"
]
},
"scripts": {
Expand Down
6 changes: 0 additions & 6 deletions packages/babel-loader-8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,3 @@ module.exports = require('babel-loader').custom(babel => {
},
};
});

function pluginMatches(pattern) {
return function (plugin) {
return plugin && pattern.test(Array.isArray(plugin) ? plugin[0] : plugin);
};
}
5 changes: 3 additions & 2 deletions packages/compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/babel__code-frame": "^7.0.2",
"@types/yargs": "^17.0.3",
"assert-never": "^1.1.0",
"babel-plugin-ember-template-compilation": "^2.0.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babylon": "^6.18.0",
"bind-decorator": "^1.0.11",
Expand Down Expand Up @@ -64,7 +65,7 @@
"devDependencies": {
"@embroider/sample-transforms": "0.0.0",
"@embroider/test-support": "0.36.0",
"@glimmer/syntax": "0.80.0",
"@glimmer/syntax": "^0.84.2",
"@types/babel__core": "^7.1.14",
"@types/babel__generator": "^7.6.2",
"@types/babel__template": "^7.4.0",
Expand All @@ -77,7 +78,7 @@
"@types/node": "^15.12.2",
"@types/resolve": "^1.20.0",
"@types/semver": "^7.3.6",
"@types/strip-bom": "^4.0.1",
"code-equality-assertions": "^0.7.0",
"ember-cli-htmlbars-3": "npm:ember-cli-htmlbars@3",
"ember-cli-htmlbars-inline-precompile": "^2.1.0",
"ember-engines": "^0.8.19",
Expand Down
30 changes: 7 additions & 23 deletions packages/compat/src/compat-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import {
AppBuilder,
EmberENV,
Package,
TemplateCompilerPlugins,
Resolver,
NodeTemplateCompiler,
AddonPackage,
} from '@embroider/core';
import V1InstanceCache from './v1-instance-cache';
Expand All @@ -35,7 +32,7 @@ import bind from 'bind-decorator';
import { pathExistsSync } from 'fs-extra';
import { tmpdir } from '@embroider/shared-internals';
import { Options as AdjustImportsOptions } from '@embroider/core/src/babel-plugin-adjust-imports';
import { getEmberExports } from '@embroider/core/src/load-ember-template-compiler';
import type { Transform } from 'babel-plugin-ember-template-compilation';

interface TreeNames {
appJS: BroccoliNode;
Expand Down Expand Up @@ -325,15 +322,16 @@ class CompatAppAdapter implements AppAdapter<TreeNames> {
}

@Memoize()
templateResolver(): Resolver {
resolverTransform(): Transform | undefined {
return new CompatResolver({
emberVersion: this.activeAddonChildren().find(a => a.name === 'ember-source')!.packageJSON.version,
root: this.root,
modulePrefix: this.modulePrefix(),
podModulePrefix: this.podModulePrefix(),
options: this.options,
activePackageRules: this.activeRules(),
adjustImportsOptionsPath: this.adjustImportsOptionsPath(),
});
}).astTransformer();
}

@Memoize()
Expand Down Expand Up @@ -382,28 +380,14 @@ class CompatAppAdapter implements AppAdapter<TreeNames> {
// rules.
@Memoize()
private internalTemplateResolver(): CompatResolver {
let resolver = new CompatResolver({
return new CompatResolver({
emberVersion: this.activeAddonChildren().find(a => a.name === 'ember-source')!.packageJSON.version,
root: this.root,
modulePrefix: this.modulePrefix(),
options: this.options,
activePackageRules: this.activeRules(),
adjustImportsOptions: this.makeAdjustImportOptions(false),
});

const compilerPath = resolveSync(this.templateCompilerPath(), { basedir: this.root });
const { cacheKey: compilerChecksum } = getEmberExports(compilerPath);
// It's ok that this isn't a fully configured template compiler. We're only
// using it to parse component snippets out of rules.
resolver.astTransformer(
new NodeTemplateCompiler({
compilerPath,
compilerChecksum,

EmberENV: {},
plugins: {},
})
);
return resolver;
}

private extraImports() {
Expand All @@ -428,7 +412,7 @@ class CompatAppAdapter implements AppAdapter<TreeNames> {
return flatten(output);
}

htmlbarsPlugins(): TemplateCompilerPlugins {
htmlbarsPlugins(): Transform[] {
return this.oldPackage.htmlbarsPlugins;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/compat/src/dasherize-component-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ export function dasherize(key: string) {

return name;
}

const NAME_FROM_SNIPPET = /<(?:([^\s/]+).*>)|(?:{{\s?component\s+['"]([^'"]+)['"])|(?:\{\{([^\s]+).*\}\})/;
export function snippetToDasherizedName(snippet: string): string | undefined {
let result = NAME_FROM_SNIPPET.exec(snippet);
if (result) {
return dasherize(result[1] ?? result[2] ?? result[3]);
}
}
7 changes: 6 additions & 1 deletion packages/compat/src/dependency-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ export function expandModuleRules(absPath: string, moduleRules: ModuleRules, res
if (moduleRules.dependsOnComponents) {
for (let snippet of moduleRules.dependsOnComponents) {
let found = resolver.resolveComponentSnippet(snippet, moduleRules);
for (let { absPath: target, runtimeName } of found.modules) {
if (found.jsModule) {
let { absPath: target, runtimeName } = found.jsModule;
output.push({ absPath, target: explicitRelative(dirname(absPath), target), runtimeName });
}
if (found.hbsModule) {
let { absPath: target, runtimeName } = found.hbsModule;
output.push({ absPath, target: explicitRelative(dirname(absPath), target), runtimeName });
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/src/hbs-to-js-broccoli-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class TemplateCompileTree extends Filter {
}

processString(source: string, relativePath: string) {
return hbsToJS(source, relativePath);
return hbsToJS(source, { filename: relativePath });
}
baseDir() {
return join(__dirname, '..');
Expand Down
Loading

0 comments on commit 752d74b

Please sign in to comment.