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

add unenv plugin, fix global esbuild options #104

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"esbuild": "^0.23.0",
"fast-glob": "^3.3.2",
"magicast": "^0.3.4",
"zod": "^3.23.8"
"zod": "^3.23.8",
"unenv": "^1.10.0"
}
}
41 changes: 20 additions & 21 deletions packages/vercel/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ResolvedConfig } from 'vite';
import glob from 'fast-glob';
import { builtinModules } from 'module'
import path, { basename } from 'path';
import { getOutput, getRoot, pathRelativeTo } from './utils';
import { build, BuildOptions, type Plugin } from 'esbuild';
Expand All @@ -14,6 +13,7 @@ import { generateCode, loadFile, type ASTNode } from 'magicast';
import { getNodeVersion } from '@vercel/build-utils';
import { nodeFileTrace } from '@vercel/nft';
import { findRoot } from '@manypkg/find-root';
import { unenvPlugin } from './esbuild/unenvPlugin';

export function getAdditionalEndpoints(resolvedConfig: ResolvedConfig) {
return (resolvedConfig.vercel?.additionalEndpoints ?? []).map((e) => ({
Expand Down Expand Up @@ -92,29 +92,29 @@ const vercelOgPlugin = (ctx: { found: boolean; index: string }): Plugin => {
};
};

const standardBuildOptions: BuildOptions = {
bundle: true,
target: 'es2022',
format: 'esm',
platform: 'node',
logLevel: 'info',
logOverride: {
'ignored-bare-import': 'verbose',
'require-resolve-not-external': 'verbose',
},
minify: false,
plugins: [],
define: {
'process.env.NODE_ENV': '"production"',
'import.meta.env.NODE_ENV': '"production"',
},
};

export async function buildFn(
resolvedConfig: ResolvedConfig,
entry: ViteVercelApiEntry,
buildOptions?: BuildOptions,
) {
const standardBuildOptions: BuildOptions = {
bundle: true,
target: 'es2022',
format: 'esm',
platform: 'node',
logLevel: 'info',
logOverride: {
'ignored-bare-import': 'verbose',
'require-resolve-not-external': 'verbose',
},
minify: false,
plugins: [],
define: {
'process.env.NODE_ENV': '"production"',
'import.meta.env.NODE_ENV': '"production"',
},
};

assert(
entry.destination.length > 0,
`Endpoint ${
Expand Down Expand Up @@ -156,7 +156,6 @@ export async function buildFn(

if (entry.edge) {
delete options.platform;
options.external = [...builtinModules, ...builtinModules.map((m) => `node:${m}`)]
options.conditions = [
'edge-light',
'worker',
Expand All @@ -165,7 +164,7 @@ export async function buildFn(
'import',
'require',
];
options.plugins?.push(edgeWasmPlugin);
options.plugins?.push(edgeWasmPlugin, unenvPlugin());
options.format = 'esm';
} else if (options.format === 'esm') {
options.banner = {
Expand Down
223 changes: 223 additions & 0 deletions packages/vercel/src/esbuild/unenvPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// taken from https://github.com/cloudflare/workers-sdk/blob/e24939c53475228e12a3c5228aa652c6473a889f/packages/wrangler/src/deployment-bundle/esbuild-plugins/hybrid-nodejs-compat.ts

import type { Plugin, PluginBuild } from 'esbuild';
import { builtinModules } from 'node:module';
import nodePath from 'node:path';
import { env, nodeless, vercel } from 'unenv';
import { packagePath } from '../utils';

const REQUIRED_NODE_BUILT_IN_NAMESPACE = 'node-built-in-modules';

export const unenvPlugin: () => Plugin = () => {
const { alias, inject, external } = env(nodeless, vercel);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { Buffer, ...rest } = inject;
return {
name: 'unenv',
setup(build) {
handleRequireCallsToNodeJSBuiltins(build);
handleAliasedNodeJSPackages(build, alias, external);
handleNodeJSGlobals(build, rest);
},
};
};

/**
* We must convert `require()` calls for Node.js to a virtual ES Module that can be imported avoiding the require calls.
* We do this by creating a special virtual ES module that re-exports the library in an onLoad handler.
* The onLoad handler is triggered by matching the "namespace" added to the resolve.
*/
function handleRequireCallsToNodeJSBuiltins(build: PluginBuild) {
const NODEJS_MODULES_RE = new RegExp(
`^(node:)?(${builtinModules.join('|')})$`,
);
build.onResolve({ filter: NODEJS_MODULES_RE }, (args) => {
if (args.kind === 'require-call') {
return {
path: args.path,
namespace: REQUIRED_NODE_BUILT_IN_NAMESPACE,
};
}
});
build.onLoad(
{ filter: /.*/, namespace: REQUIRED_NODE_BUILT_IN_NAMESPACE },
({ path }) => {
return {
contents: [
`import libDefault from '${path}';`,
'export default libDefault;',
].join('\n'),
loader: 'js',
};
},
);
}

function handleAliasedNodeJSPackages(
build: PluginBuild,
alias: Record<string, string>,
external: string[],
) {
// esbuild expects alias paths to be absolute
const aliasAbsolute = Object.fromEntries(
Object.entries(alias)
.map(([key, value]) => {
let resolvedAliasPath;
try {
resolvedAliasPath = require.resolve(value);
} catch (e) {
// this is an alias for package that is not installed in the current app => ignore
resolvedAliasPath = '';
}

return [key, resolvedAliasPath.replace(/\.cjs$/, '.mjs')];
})
.filter((entry) => entry[1] !== ''),
);
const UNENV_ALIAS_RE = new RegExp(
`^(${Object.keys(aliasAbsolute).join('|')})$`,
);

build.onResolve({ filter: UNENV_ALIAS_RE }, (args) => {
// Resolve the alias to its absolute path and potentially mark it as external
return {
path: aliasAbsolute[args.path],
external: external.includes(alias[args.path]),
};
});
}

/**
* Inject node globals defined in unenv's `inject` config via virtual modules
*/
function handleNodeJSGlobals(
build: PluginBuild,
inject: Record<string, string | string[]>,
) {
const UNENV_GLOBALS_RE = /_virtual_unenv_global_polyfill-([^.]+)\.js$/;

build.initialOptions.inject = [
...(build.initialOptions.inject ?? []),
//convert unenv's inject keys to absolute specifiers of custom virtual modules that will be provided via a custom onLoad
...Object.keys(inject).map((globalName) =>
nodePath.resolve(
packagePath,
`_virtual_unenv_global_polyfill-${encodeToLowerCase(globalName)}.js`,
),
),
];

build.onResolve({ filter: UNENV_GLOBALS_RE }, ({ path }) => ({ path }));

build.onLoad({ filter: UNENV_GLOBALS_RE }, ({ path }) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const globalName = decodeFromLowerCase(path.match(UNENV_GLOBALS_RE)![1]);
const globalMapping = inject[globalName];

if (typeof globalMapping === 'string') {
const globalPolyfillSpecifier = globalMapping;
return {
contents: `
import globalVar from "${globalPolyfillSpecifier}";

${
/*
// ESBuild's inject doesn't actually touch globalThis, so let's do it ourselves
// by creating an exportable so that we can preserve the globalThis assignment if
// the ${globalName} was found in the app, or tree-shake it, if it wasn't
// see https://esbuild.github.io/api/#inject
*/ ''
}
const exportable =
${
/*
// mark this as a PURE call so it can be ignored and tree-shaken by ESBuild,
// when we don't detect 'process', 'global.process', or 'globalThis.process'
// in the app code
// see https://esbuild.github.io/api/#tree-shaking-and-side-effects
*/ ''
}
/* @__PURE__ */ (() => {
return globalThis.${globalName} = globalVar;
})();

export {
exportable as '${globalName}',
exportable as 'globalThis.${globalName}',
}
`,
};
}
const [moduleName, exportName] = inject[globalName];

return {
contents: `
import { ${exportName} } from "${moduleName}";

${
/*
// ESBuild's inject doesn't actually touch globalThis, so let's do it ourselves
// by creating an exportable so that we can preserve the globalThis assignment if
// the ${globalName} was found in the app, or tree-shake it, if it wasn't
// see https://esbuild.github.io/api/#inject
*/ ''
}
const exportable =
${
/*
// mark this as a PURE call so it can be ignored and tree-shaken by ESBuild,
// when we don't detect 'process', 'global.process', or 'globalThis.process'
// in the app code
// see https://esbuild.github.io/api/#tree-shaking-and-side-effects
*/ ''
}
/* @__PURE__ */ (() => {
return globalThis.${globalName} = ${exportName};
})();

export {
exportable as '${globalName}',
exportable as 'global.${globalName}',
exportable as 'globalThis.${globalName}'
}
`,
};
});
}

/**
* Encodes a case sensitive string to lowercase string by prefixing all uppercase letters
* with $ and turning them into lowercase letters.
*
* This function exists because ESBuild requires that all resolved paths are case insensitive.
* Without this transformation, ESBuild will clobber /foo/bar.js with /foo/Bar.js
*
* This is important to support `inject` config for `performance` and `Performance` introduced
* in https://github.com/unjs/unenv/pull/257
*/
export function encodeToLowerCase(str: string): string {
return str
.replaceAll(/\$/g, () => '$$')
.replaceAll(/[A-Z]/g, (letter) => `$${letter.toLowerCase()}`);
}

/**
* Decodes a string lowercased using `encodeToLowerCase` to the original strings
*/
export function decodeFromLowerCase(str: string): string {
let out = '';
let i = 0;
while (i < str.length - 1) {
if (str[i] === '$') {
i++;
out += str[i].toUpperCase();
} else {
out += str[i];
}
i++;
}
if (i < str.length) {
out += str[i];
}
return out;
}
16 changes: 16 additions & 0 deletions packages/vercel/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { normalizePath, ResolvedConfig, UserConfig } from 'vite';
import path from 'path';
import { createRequire } from 'module';

export function getRoot(config: UserConfig | ResolvedConfig): string {
return normalizePath(config.root || process.cwd());
Expand Down Expand Up @@ -30,3 +31,18 @@ export function pathRelativeTo(
path.relative(normalizePath(path.join(root, rel)), filePath),
);
}

function getOwnPackagePath() {
const require_ = createRequire(import.meta.url);
// vercel/dist/index.cjs
const resolved = require_.resolve('vite-plugin-vercel');
return path.resolve(
resolved,
// vercel/dist
'..',
// vercel
'..',
);
}
Comment on lines +35 to +46
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work once released?


export const packagePath = getOwnPackagePath();
1 change: 1 addition & 0 deletions packages/vercel/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default defineConfig([
dts: {
entry: './src/index.ts',
},
shims: true,
},
]);
Loading
Loading