Skip to content

Commit

Permalink
fix(raw): allow importing relative paths (#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Mar 21, 2024
1 parent 00c308c commit 70cf983
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/rollup/plugins/import-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function importMeta(nitro: Nitro): Plugin {
return;
}
const url =
nitro.options.node && isEntry
nitro.options.node && isEntry && !code.includes("ROLLUP_NO_REPLACE")
? "_import_meta_url_"
: '"file:///_entry.js"';
const envImport = nitro.options.node
Expand Down
16 changes: 12 additions & 4 deletions src/rollup/plugins/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function raw(opts: RawOptions = {}): Plugin {

return {
name: "raw",
resolveId(id) {
async resolveId(id, importer) {
if (id === HELPER_ID) {
return id;
}
Expand All @@ -38,10 +38,18 @@ export function raw(opts: RawOptions = {}): Plugin {
isRawId = true;
}

// TODO: Support reasolving. Blocker is CommonJS custom resolver!
if (isRawId) {
return { id: "\0raw:" + id };
if (!isRawId) {
return;
}

const resolvedId = (await this.resolve(id, importer, { skipSelf: true }))
?.id;

if (!resolvedId || resolvedId.startsWith("\0")) {
return resolvedId;
}

return { id: "\0raw:" + resolvedId };
},
load(id) {
if (id === HELPER_ID) {
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/routes/assets/md.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default eventHandler(async (event) => {
const md = await import("../../assets/test.md" as string).then(
(r) => r.default
);
return md;
});
11 changes: 9 additions & 2 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,20 @@ export function testNitro(
const { data: html, status: htmlStatus } = await callHandler({
url: "/file?filename=index.html",
});
expect(htmlStatus).toBe(200);
expect(html).toContain("<h1>nitro is amazing!</h1>");

const { data: txtFile, status: txtStatus } = await callHandler({
url: "/file?filename=test.txt",
});
expect(htmlStatus).toBe(200);
expect(html).toContain("<h1>nitro is amazing!</h1>");
expect(txtStatus).toBe(200);
expect(txtFile).toContain("this is an asset from a text file from nitro");

const { data: mdFile, status: mdStatus } = await callHandler({
url: "/assets/md",
});
expect(mdStatus).toBe(200);
expect(mdFile).toContain("# Hello world");
});

if (ctx.nitro!.options.serveStatic) {
Expand Down

0 comments on commit 70cf983

Please sign in to comment.