Skip to content

Commit

Permalink
Merge pull request #1743 from embroider-build/htmlbars-cleanup
Browse files Browse the repository at this point in the history
Don't rely on htmlbars internals
  • Loading branch information
ef4 committed Dec 23, 2023
2 parents fd7859c + 3b02398 commit a47b450
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 88 deletions.
39 changes: 7 additions & 32 deletions packages/compat/src/compat-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ import { WatchedDir } from 'broccoli-source';
import resolve from 'resolve';
import { V1Config, WriteV1Config } from './v1-config';
import { WriteV1AppBoot, ReadV1AppBoot } from './v1-appboot';
import type {
AddonMeta,
EmberAppInstance,
OutputFileToInputFileMap,
PackageInfo,
AddonInstance,
} from '@embroider/core';
import type { AddonMeta, EmberAppInstance, OutputFileToInputFileMap, PackageInfo } from '@embroider/core';
import { writeJSONSync, ensureDirSync, copySync, readdirSync, pathExistsSync, existsSync } from 'fs-extra';
import AddToTree from './add-to-tree';
import DummyPackage from './dummy-package';
Expand All @@ -29,17 +23,12 @@ import Concat from 'broccoli-concat';
import mapKeys from 'lodash/mapKeys';
import SynthesizeTemplateOnlyComponents from './synthesize-template-only-components';
import { isEmberAutoImportDynamic, isInlinePrecompilePlugin } from './detect-babel-plugins';
import prepHtmlbarsAstPluginsForUnwrap from './prepare-htmlbars-ast-plugins';
import loadAstPlugins from './prepare-htmlbars-ast-plugins';
import { readFileSync } from 'fs';
import type { Options as HTMLBarsOptions } from 'ember-cli-htmlbars';
import semver from 'semver';
import type { Transform } from 'babel-plugin-ember-template-compilation';
import { CompatAppBuilder } from './compat-app-builder';

type EmberCliHTMLBarsAddon = AddonInstance & {
htmlbarsOptions(): HTMLBarsOptions;
};

interface Group {
outputFiles: OutputFileToInputFileMap;
implicitKey: '_implicitStyles' | '_implicitScripts';
Expand Down Expand Up @@ -585,25 +574,11 @@ export default class CompatApp {
}

get htmlbarsPlugins(): Transform[] {
let addon = this.legacyEmberAppInstance.project.addons.find(
(a: AddonInstance) => a.name === 'ember-cli-htmlbars'
) as unknown as EmberCliHTMLBarsAddon;
let options = addon.htmlbarsOptions();
if (options?.plugins?.ast) {
// even if the app was using @embroider/macros, we drop it from the config
// here in favor of our globally-configured one.
options.plugins.ast = options.plugins.ast.filter((p: any) => !isEmbroiderMacrosPlugin(p));
prepHtmlbarsAstPluginsForUnwrap(this.legacyEmberAppInstance.registry);

// classically, this list was backwards for silly historic reasons. But
// we're the compatibility system, so we're putting it back into
// reasonable order.
options.plugins.ast.reverse();

return options.plugins.ast;
} else {
return [];
}
let plugins = loadAstPlugins(this.legacyEmberAppInstance.registry);
// even if the app was using @embroider/macros, we drop it from the config
// here in favor of our globally-configured one.
plugins = plugins.filter((p: any) => !isEmbroiderMacrosPlugin(p));
return plugins;
}

// our own appTree. Not to be confused with the one that combines the app js
Expand Down
13 changes: 11 additions & 2 deletions packages/compat/src/prepare-htmlbars-ast-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Transform } from 'babel-plugin-ember-template-compilation';
import { join } from 'path';

export default function prepHtmlbarsAstPluginsForUnwrap(registry: any): void {
for (let wrapper of registry.load('htmlbars-ast-plugin')) {
export default function loadAstPlugins(registry: any): Transform[] {
let wrappers = registry.load('htmlbars-ast-plugin');
for (let wrapper of wrappers) {
const { plugin, parallelBabel, baseDir, cacheKey } = wrapper;
if (plugin) {
// if the parallelBabel options were set on the wrapper, but not on the plugin, add it
Expand Down Expand Up @@ -29,4 +31,11 @@ export default function prepHtmlbarsAstPluginsForUnwrap(registry: any): void {
}
}
}
let plugins = wrappers.map((wrapper: any) => wrapper.plugin);

// the plugins in the registry historically run in backwards order for dumb
// reasons. Embroider keeps them in sensible order, so here is where we do the
// compatibility switch.
plugins.reverse();
return plugins;
}
44 changes: 20 additions & 24 deletions packages/compat/src/v1-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import semver from 'semver';
import rewriteAddonTree from './rewrite-addon-tree';
import { mergeWithAppend } from './merges';
import type { AddonMeta, PackageCache, AddonInstance, AddonTreePath } from '@embroider/core';
import { debug } from '@embroider/core';
import { debug, findTopmostAddon } from '@embroider/core';
import type Options from './options';
import walkSync from 'walk-sync';
import ObserveTree from './observe-tree';
import type { Options as HTMLBarsOptions } from 'ember-cli-htmlbars';
import { isEmbroiderMacrosPlugin } from '@embroider/macros/src/node';
import type { TransformOptions, PluginItem } from '@babel/core';
import modulesCompat from './modules-compat';
Expand All @@ -31,7 +30,7 @@ import {
} from './detect-babel-plugins';
import HbsToJSBroccoliPlugin from './hbs-to-js-broccoli-plugin';
import { fromPairs } from 'lodash';
import prepHtmlbarsAstPluginsForUnwrap from './prepare-htmlbars-ast-plugins';
import loadAstPlugins from './prepare-htmlbars-ast-plugins';
import getRealAddon from './get-real-addon';
import type { Options as EtcOptions } from 'babel-plugin-ember-template-compilation';
import type CompatApp from './compat-app';
Expand Down Expand Up @@ -116,27 +115,24 @@ export default class V1Addon {
// this is only defined when there are custom AST transforms that need it
@Memoize()
private get templateCompilerBabelPlugin(): PluginItem | undefined {
let htmlbars = this.addonInstance.addons.find(a => a.name === 'ember-cli-htmlbars');
if (htmlbars) {
let options = (htmlbars as any).htmlbarsOptions() as HTMLBarsOptions;
if (options?.plugins?.ast) {
// our macros don't run here in stage1
options.plugins.ast = options.plugins.ast.filter((p: any) => !isEmbroiderMacrosPlugin(p));
prepHtmlbarsAstPluginsForUnwrap(this.addonInstance.registry);
if (options.plugins.ast.length > 0) {
let opts: EtcOptions = {
compilerPath: options.templateCompilerPath,
targetFormat: 'hbs',
enableLegacyModules: [
'ember-cli-htmlbars',
'ember-cli-htmlbars-inline-precompile',
'htmlbars-inline-precompile',
],
transforms: options.plugins.ast as any,
};
return [require.resolve('babel-plugin-ember-template-compilation'), opts];
}
}
let plugins = loadAstPlugins(this.addonInstance.registry);
// our macros don't run here in stage1
plugins = plugins.filter((p: any) => !isEmbroiderMacrosPlugin(p));
if (plugins.length > 0) {
let compilerPath = require.resolve('ember-source/dist/ember-template-compiler.js', {
paths: [findTopmostAddon(this.addonInstance).parent.root],
});
let opts: EtcOptions = {
compilerPath,
targetFormat: 'hbs',
enableLegacyModules: [
'ember-cli-htmlbars',
'ember-cli-htmlbars-inline-precompile',
'htmlbars-inline-precompile',
],
transforms: plugins,
};
return [require.resolve('babel-plugin-ember-template-compilation'), opts];
}
}

Expand Down
22 changes: 0 additions & 22 deletions types/ember-cli-htmlbars/index.d.ts

This file was deleted.

8 changes: 0 additions & 8 deletions types/ember-cli-htmlbars/package.json

This file was deleted.

0 comments on commit a47b450

Please sign in to comment.