Skip to content

Commit

Permalink
Test interop with template-colocation-plugin
Browse files Browse the repository at this point in the history
This test demonstrates the failure from #16

I think the fix will go in `@embroider/shared-internals` but once it does this will tell us if we got it right.
  • Loading branch information
ef4 committed Jan 16, 2024
1 parent e1fa550 commit 22ede22
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@babel/plugin-proposal-decorators": "^7.23.3",
"@babel/plugin-transform-class-properties": "^7.23.3",
"@babel/plugin-transform-private-methods": "^7.23.3",
"@embroider/shared-internals": "^2.5.1",
"@swc-node/register": "^1.6.8",
"@types/babel__core": "^7.20.4",
"@types/node": "^20.9.1",
Expand Down
108 changes: 107 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ try {
);
}

function builder(
export function builder(
exprPlugins: TransformOptions["plugins"],
modulePlugins?: TransformOptions["plugins"]
modulePlugins?: TransformOptions["plugins"],
filename = "example.js"
): Builder {
function expression(src: string, scope: Record<string, any>) {
let transformedSrc = transform(
Expand All @@ -28,7 +29,7 @@ function builder(
return (${src})
})
`,
{ plugins: exprPlugins }
{ plugins: exprPlugins, filename }
)!.code!;
let fn = eval(transformedSrc);
return fn(...Object.values(scope));
Expand All @@ -37,6 +38,7 @@ function builder(
async function module(src: string, deps: Record<string, any>) {
let transformedSrc = transform(src, {
plugins: modulePlugins ?? exprPlugins,
filename,
})!.code!;
let context = vm.createContext({ deps });
let m: vm.SourceTextModule;
Expand Down
79 changes: 79 additions & 0 deletions tests/plugin-interop-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { module, test } from "qunit";
import { builder, Builder } from "./helpers.ts";
import { type LegacyClassDecorator } from "../src/runtime.ts";
import * as runtimeImpl from "../src/runtime.ts";
import ourDecorators from "../src/index.ts";
import { createRequire } from "node:module";
import { mkdtemp, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";

const require = createRequire(import.meta.url);
const Colocation = require("@embroider/shared-internals/src/template-colocation-plugin");

module("plugin-interop", (hooks) => {
let build: Builder;

hooks.before(async () => {
let dir = await mkdtemp(join(tmpdir(), "decorator-transforms-"));
await writeFile(join(dir, "example.hbs"), "");
build = builder(
[],
[
[
ourDecorators,
{ runtime: { import: "decorator-transforms/runtime" } },
],
[Colocation],
],
join(dir, "example.js")
);
});

test("colocation", async (assert) => {
let red: LegacyClassDecorator = (target) => {
return class extends target {
get red() {
return "#ff0000";
}
};
};

let setComponents = 0;

let { default: Example } = await build.module(
`
import red from "red";
import { precompileTemplate } from '@ember/template-compilation';
const __COLOCATED_TEMPLATE__ = precompileTemplate("Hello world")
@red
export default class Example {
}
`,
{
"decorator-transforms/runtime": runtimeImpl,
red: { default: red },
"@ember/component": {
setComponentTemplate: function (_template: unknown, obj: unknown) {
setComponents++;
return obj;
},
},
"@ember/template-compilation": {
precompileTemplate: function (a: string) {
return a;
},
},
"./example.hbs": {
default: "",
},
}
);

assert.strictEqual(new Example().red, "#ff0000");
assert.strictEqual(setComponents, 1);
});
});

0 comments on commit 22ede22

Please sign in to comment.