Skip to content

Commit

Permalink
feat: support exportConditions and add worker default conditions (#…
Browse files Browse the repository at this point in the history
…1401)

Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
Hebilicious and pi0 authored Jul 12, 2023
1 parent 03540d9 commit f12b38e
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 27 deletions.
46 changes: 46 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ export async function loadOptions(
// Resolve plugin paths
options.plugins = options.plugins.map((p) => resolvePath(p, options));

// Export conditions
options.exportConditions = _resolveExportConditions(
options.exportConditions,
{ dev: options.dev, node: options.node }
);

// Add open-api endpoint
if (options.dev && options.experimental.openAPI) {
options.handlers.push({
Expand Down Expand Up @@ -458,3 +464,43 @@ export function normalizeRouteRules(
}
return normalizedRules;
}

function _resolveExportConditions(
conditions: string[] = [],
opts: { dev: boolean; node: boolean }
) {
const resolvedConditions: string[] = [];

// 1. Add dev or production
resolvedConditions.push(opts.dev ? "development" : "production");

// 2. Add user specified conditions
resolvedConditions.push(...conditions);

// 3. Add runtime conditions (node or web)
if (opts.node) {
resolvedConditions.push("node");
} else {
// https://runtime-keys.proposal.wintercg.org/
resolvedConditions.push(
"wintercg",
"worker",
"web",
"browser",
"workerd",
"edge-light",
"lagon",
"netlify",
"edge-routine",
"deno"
);
}

// 4. Add default conditions
resolvedConditions.push("import", "module", "default");

// Dedup with preserving order
return resolvedConditions.filter(
(c, i) => resolvedConditions.indexOf(c) === i
);
}
10 changes: 2 additions & 8 deletions src/presets/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import { defineNitroPreset } from "../preset";
export const bun = defineNitroPreset({
extends: "node-server",
entry: "#internal/nitro/entries/bun",
externals: {
traceInclude: ["ofetch", "uncrypto", "node-fetch-native"].map((id) =>
resolvePathSync(id, {
url: import.meta.url,
conditions: ["bun"],
})
),
},
// https://bun.sh/docs/runtime/modules#resolution
exportConditions: ["bun", "worker", "module", "node", "default", "browser"],
commands: {
preview: "bun run ./server/index.mjs",
},
Expand Down
1 change: 1 addition & 0 deletions src/presets/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Nitro } from "../types";
export const cloudflareModule = defineNitroPreset({
extends: "base-worker",
entry: "#internal/nitro/entries/cloudflare-module",
exportConditions: ["workerd"],
commands: {
preview: "npx wrangler dev ./server/index.mjs --site ./public --local",
deploy: "npx wrangler deploy",
Expand Down
1 change: 1 addition & 0 deletions src/presets/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Nitro } from "../types";
export const cloudflarePages = defineNitroPreset({
extends: "cloudflare",
entry: "#internal/nitro/entries/cloudflare-pages",
exportConditions: ["workerd"],
commands: {
preview: "npx wrangler pages dev ./",
deploy: "npx wrangler pages deploy ./",
Expand Down
1 change: 1 addition & 0 deletions src/presets/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Nitro } from "../types";
export const cloudflare = defineNitroPreset({
extends: "base-worker",
entry: "#internal/nitro/entries/cloudflare",
exportConditions: ["workerd"],
commands: {
preview: "npx wrangler dev ./server/index.mjs --site ./public --local",
deploy: "npx wrangler deploy",
Expand Down
1 change: 1 addition & 0 deletions src/presets/deno-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineNitroPreset } from "../preset";

export const denoDeploy = defineNitroPreset({
entry: "#internal/nitro/entries/deno-deploy",
exportConditions: ["deno"],
node: false,
noExternals: true,
serveStatic: "deno",
Expand Down
1 change: 1 addition & 0 deletions src/presets/deno-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ImportMetaRe } from "../rollup/plugins/import-meta";
export const denoServer = defineNitroPreset({
extends: "node-server",
entry: "#internal/nitro/entries/deno-server",
exportConditions: ["deno"],
commands: {
preview: "deno task --config ./deno.json start",
},
Expand Down
1 change: 1 addition & 0 deletions src/presets/lagon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface LagonFunctionConfig {
export const lagon = defineNitroPreset({
extends: "base-worker",
entry: "#internal/nitro/entries/lagon",
exportConditions: ["lagon"],
commands: {
preview: "npm run dev --prefix ./",
deploy: "npm run deploy --prefix ./",
Expand Down
1 change: 1 addition & 0 deletions src/presets/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const netlifyBuilder = defineNitroPreset({
export const netlifyEdge = defineNitroPreset({
extends: "base-worker",
entry: "#internal/nitro/entries/netlify-edge",
exportConditions: ["netlify"],
output: {
serverDir: "{{ rootDir }}/.netlify/edge-functions/server",
publicDir: "{{ rootDir }}/dist",
Expand Down
1 change: 1 addition & 0 deletions src/presets/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const vercel = defineNitroPreset({
export const vercelEdge = defineNitroPreset({
extends: "base-worker",
entry: "#internal/nitro/entries/vercel-edge",
exportConditions: ["edge-light"],
output: {
dir: "{{ rootDir }}/.vercel/output",
serverDir: "{{ output.dir }}/functions/__nitro.func",
Expand Down
18 changes: 3 additions & 15 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ 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";
import inject from "@rollup/plugin-inject";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { isWindows } from "std-env";
import { visualizer } from "rollup-plugin-visualizer";
import * as unenv from "unenv";
Expand Down Expand Up @@ -404,13 +404,7 @@ export const plugins = [
processCwd: nitro.options.rootDir,
exportsOnly: true,
},
exportConditions: [
"default",
nitro.options.dev ? "development" : "production",
"module",
"node",
"import",
],
exportConditions: nitro.options.exportConditions,
})
)
);
Expand All @@ -425,13 +419,7 @@ export const plugins = [
modulePaths: nitro.options.nodeModulesDirs,
// 'module' is intentionally not supported because of externals
mainFields: ["main"],
exportConditions: [
"default",
nitro.options.dev ? "development" : "production",
"module",
"node",
"import",
],
exportConditions: nitro.options.exportConditions,
})
);

Expand Down
8 changes: 4 additions & 4 deletions src/rollup/plugins/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ export function externals(opts: NodeExternalsOptions): Plugin {
}

// Trace used files using nft
const _fileTrace = await nodeFileTrace(
[...trackedExternals],
opts.traceOptions
);
const _fileTrace = await nodeFileTrace([...trackedExternals], {
conditions: opts.exportConditions,
...opts.traceOptions,
});

// Read package.json with cache
const packageJSONCache = new Map(); // pkgDir => contents
Expand Down
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

0 comments on commit f12b38e

Please sign in to comment.