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

fix(raw): allow importing relative paths #2289

Merged
merged 2 commits into from Mar 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rollup/plugins/import-meta.ts
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
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
@@ -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
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