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

feat: support exportConditions and add worker default conditions #1401

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 2 additions & 18 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { defu } from "defu";
// import terser from "@rollup/plugin-terser"; // TODO: Investigate jiti issue
import type { RollupWasmOptions } from "@rollup/plugin-wasm";
import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import alias from "@rollup/plugin-alias";
import json from "@rollup/plugin-json";
import wasmPlugin from "@rollup/plugin-wasm";
Expand Down Expand Up @@ -36,6 +35,7 @@ import { raw } from "./plugins/raw";
import { storage } from "./plugins/storage";
import { importMeta } from "./plugins/import-meta";
import { appConfig } from "./plugins/app-config";
import { nodeResolvePlugin } from "./plugins/node-resolve";

export type RollupConfig = InputOptions & { output: OutputOptions };

Expand Down Expand Up @@ -417,23 +417,7 @@ export const plugins = [
}

// https://github.com/rollup/plugins/tree/master/packages/node-resolve
rollupConfig.plugins.push(
nodeResolve({
extensions,
preferBuiltins: !!nitro.options.node,
rootDir: nitro.options.rootDir,
modulePaths: nitro.options.nodeModulesDirs,
// 'module' is intentionally not supported because of externals
mainFields: ["main"],
exportConditions: [
"default",
nitro.options.dev ? "development" : "production",
"module",
"node",
"import",
],
})
);
rollupConfig.plugins.push(nodeResolvePlugin(nitro, extensions));

// Automatically mock unresolved externals
// rollupConfig.plugins.push(autoMock())
Expand Down
88 changes: 88 additions & 0 deletions src/rollup/plugins/node-resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { kebabCase } from "scule";
import type { Nitro, NitroOptions } from "../../types";

const rKeys = (runtimeKeys: string | string[], conditions: string[]) => {
const keys = Array.isArray(runtimeKeys) ? runtimeKeys : [runtimeKeys];
return [...keys, ...conditions.filter((c) => !new Set(keys).has(c))];
};

// https://github.com/rollup/plugins/tree/master/packages/node-resolve
export const nodeResolvePlugin = (nitro: Nitro, extensions: string[]) => {
const defaultExportConditions = [
"default",
nitro.options.dev ? "development" : "production",
"module",
"node",
"import",
];

/**
* Attempt to match generic non-node runtime first,
* then try to match providers runtime keys,
* then finally browser, deno and node.
* https://runtime-keys.proposal.wintercg.org/
*/
const workerExportConditions = [
"wintercg",
"worker",
"web",
"workerd",
"edge-light",
"lagon",
"netlify",
"edge-routine",
"browser",
"import",
"module",
"deno",
"default",
nitro.options.dev ? "development" : "production",
"node",
];

const getExportConditions = (preset: NitroOptions["preset"]) => {
switch (kebabCase(preset)) {
case "node-server":
case "node-cluster":
case "node": {
return rKeys("node", defaultExportConditions);
}
case "bun": {
return rKeys("bun", defaultExportConditions);
}
case "deno": {
return rKeys("deno", workerExportConditions);
}
case "netlify-edge": {
return rKeys("netlify", workerExportConditions);
}
case "lagon": {
return rKeys("lagon", workerExportConditions);
}
case "vercel-edge": {
return rKeys(["edge-light", "http"], workerExportConditions);
}
case "cloudflare-pages":
case "cloudflare-module":
case "cloudflare": {
return rKeys("workerd", workerExportConditions);
}
default: {
return defaultExportConditions;
}
}
};

return nodeResolve({
extensions,
preferBuiltins: !!nitro.options.node,
rootDir: nitro.options.rootDir,
modulePaths: nitro.options.nodeModulesDirs,
// 'module' is intentionally not supported because of externals
mainFields: ["main"],
exportConditions:
nitro.options.exportConditions ??
getExportConditions(nitro.options.preset),
});
};
1 change: 1 addition & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export interface NitroOptions extends PresetOptions {
analyze: false | PluginVisualizerOptions;
replace: Record<string, string | ((id: string) => string)>;
commonJS?: RollupCommonJSOptions;
exportConditions?: string[];

// Advanced
typescript: {
Expand Down