Skip to content

Commit

Permalink
improve alignment with virtual modules
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyraspopov committed Apr 4, 2024
1 parent 2011929 commit 9270453
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 34 deletions.
15 changes: 15 additions & 0 deletions packages/rollup-plugin-ephemeral/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) 2024 Oleksii Raspopov

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3 changes: 3 additions & 0 deletions packages/rollup-plugin-ephemeral/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# rollup-plugin-ephemeral

Like `@rollup/plugin-virtual` but sometimes you need virtual modules to behave like real files.
14 changes: 14 additions & 0 deletions packages/rollup-plugin-ephemeral/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "rollup-plugin-ephemeral",
"version": "0.0.1",
"type": "module",
"main": "./plugin.js",
"module": "./plugin.js",
"types": "./plugin.d.ts",
"keywords": [
"rollup-plugin"
],
"scripts": {
"test": "node --test --experimental-test-coverage"
}
}
4 changes: 4 additions & 0 deletions packages/rollup-plugin-ephemeral/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Plugin } from "rollup";

export function ephemeral(name: string, code: string): Plugin;
export default ephemeral;
21 changes: 21 additions & 0 deletions packages/rollup-plugin-ephemeral/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { resolve, dirname } from "node:path";

export function ephemeral(name, code) {
let resolvedId = resolve(name);
return {
name: `ephemeral_${name}`,
resolveId(id, importer) {
if (id === name) return name;
if (importer != null) {
let resolvedImporterId = resolve(dirname(importer), id);
if (resolvedImporterId === resolvedId) return resolvedId;
}
return null;
},
load(id) {
return id === name || id === resolvedId ? code : null;
},
};
}

export default ephemeral;
43 changes: 43 additions & 0 deletions packages/rollup-plugin-ephemeral/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { test } from "node:test";
import { equal } from "node:assert/strict";

import { resolve } from "node:path";

import { rollup } from "rollup";
import { ephemeral } from "./plugin.js";

test("generates as modules", async () => {
let bundle = await rollup({
input: "./index.js",
treeshake: false,
plugins: [
ephemeral(
"./index.js",
` import { num } from '../branch/index.js';
console.log(num); `,
),
ephemeral("../branch/module.js", "export let num = 13;"),
ephemeral(
"../branch/index.js",
` import { num as base } from './module.js';
export let num = base * 2;`,
),
],
});
let { output } = await bundle.generate({ format: "esm", preserveModules: true });
equal(output.length, 3);
});

test("loads a bare module ID from memory", () => {
let plugin = ephemeral("foo", "export default 42");
let resolved = plugin.resolveId("foo");
equal(resolved, "foo");
equal(plugin.load(resolved), "export default 42");
});

test("loads an absolute path from memory", () => {
let plugin = ephemeral("src/foo.js", "export default 42");
let resolved = plugin.resolveId("./foo.js", "src/main.js");
equal(resolved, resolve("src/foo.js"));
equal(plugin.load(resolved), "export default 42");
});
2 changes: 1 addition & 1 deletion packages/rollup-plugin-worker-url/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function worker() {
if (arg.type === "Literal") {
// TODO import.meta.url -> id, otherwise the value
let { id: fileId } = await this.resolve(arg.value, id);
let chunkId = this.emitFile({ type: "chunk", id: fileId });
let chunkId = this.emitFile({ type: "chunk", id: fileId, importer: id });
ms.overwrite(url.start, url.end, `import.meta.ROLLUP_FILE_URL_${chunkId}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rollup-plugin-worker-url/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from "node:test";
import { equal } from "node:assert/strict";

import { rollup } from "rollup";
import { ephemeral } from "../rollwright/ephemeral.js";
import { ephemeral } from "../rollup-plugin-ephemeral/plugin.js";
import worker from "./plugin.js";

test("simple worker import", async () => {
Expand Down
21 changes: 0 additions & 21 deletions packages/rollwright/ephemeral.js

This file was deleted.

20 changes: 9 additions & 11 deletions packages/rollwright/fixture.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { resolve, relative, dirname, basename } from "node:path";
import { resolve, dirname, basename } from "node:path";

import { test as base } from "@playwright/test";

import { rollup } from "rollup";
import { ephemeral } from "./ephemeral.js";
import { ephemeral } from "../rollup-plugin-ephemeral/plugin.js";
import { createCoverageCollector } from "./coverage-collector.js";
import { nodeResolve } from "@rollup/plugin-node-resolve";

Expand Down Expand Up @@ -49,11 +49,9 @@ async function execute({ page, rlcache, plugins, template, extensions, coverage

let server = new Hono();
let connect = null;
let pageContent = template.html ?? template;
server.get("/", (ctx) => ctx.html(pageContent));
server.get("/", (ctx) => ctx.html(template.html ?? template));

let root = template.root ?? relative(process.cwd(), dirname(testInfo.file));
let statics = serveStatic({ root });
let statics = serveStatic({ root: template.root ?? resolve(dirname(testInfo.file)) });
server.notFound(async (ctx) => {
let response = await statics(ctx, () => Promise.resolve(null));
return response != null ? response : ctx.body(null, 404);
Expand All @@ -68,10 +66,10 @@ async function execute({ page, rlcache, plugins, template, extensions, coverage
async function evaluate(fn, ...args) {
let hash = `step_${++id}`;
let code = `window.${hash} = ${fn.toString()};`;
let filename = resolve(dirname(testInfo.file), `${hash}_${basename(testInfo.file)}`);
let input = basename(filename);
let rootDir = dirname(testInfo.file);
let input = `${hash}_${basename(testInfo.file)}`;
inputs.push(input);
files.push(ephemeral(filename, code));
files.push(ephemeral(input, code));
let bundle = await rollup({
cache: rlcache.load(),
input: inputs,
Expand All @@ -91,7 +89,7 @@ async function execute({ page, rlcache, plugins, template, extensions, coverage
],
})
: [],
nodeResolve({ browser: true, rootDir: dirname(filename), extensions }),
nodeResolve({ browser: true, rootDir, extensions }),
...files,
],
logLevel: "silent",
Expand All @@ -101,7 +99,7 @@ async function execute({ page, rlcache, plugins, template, extensions, coverage
format: "esm",
generatedCode: "es2015",
preserveModules: true,
dir: dirname(filename),
dir: rootDir,
minifyInternalExports: false,
});
rlcache.save(bundle.cache);
Expand Down

0 comments on commit 9270453

Please sign in to comment.