Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test interop with template-colocation-plugin #19

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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",
"@babel/preset-env": "^7.24.4",
"@swc-node/register": "^1.6.8",
"@types/babel__core": "^7.20.4",
Expand Down
77 changes: 77 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 4 additions & 2 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ try {
);
}

function builder(
export function builder(
exprPlugins: TransformOptions["plugins"],
modulePlugins?: TransformOptions["plugins"],
presets?: TransformOptions["presets"]
presets?: TransformOptions["presets"],
filename = "example.js"
): Builder {
function transformSrc(src: string) {
return transform(src, { plugins: exprPlugins, presets })!.code!;
Expand All @@ -43,6 +44,7 @@ function builder(
let transformedSrc = transform(src, {
plugins: modulePlugins ?? exprPlugins,
presets,
filename,
})!.code!;
let context = vm.createContext({ deps });
let m: vm.SourceTextModule;
Expand Down
80 changes: 80 additions & 0 deletions tests/plugin-interop-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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);
});
});
Loading