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(rollup): use mlly as fallback resolver when externals disabled #948

Merged
merged 2 commits into from
Feb 15, 2023
Merged
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
25 changes: 23 additions & 2 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pathToFileURL } from "node:url";
import { createRequire } from "node:module";
import { createRequire, builtinModules } from "node:module";
import { dirname, join, normalize, relative, resolve } from "pathe";
import type { InputOptions, OutputOptions, Plugin } from "rollup";
import { defu } from "defu";
Expand All @@ -15,7 +15,7 @@ import { isWindows } from "std-env";
import { visualizer } from "rollup-plugin-visualizer";
import * as unenv from "unenv";
import type { Preset } from "unenv";
import { sanitizeFilePath } from "mlly";
import { sanitizeFilePath, resolvePath } from "mlly";
import unimportPlugin from "unimport/unplugin";
import { hash } from "ohash";
import type { Nitro } from "../types";
Expand Down Expand Up @@ -347,10 +347,31 @@ export const plugins = [
rollupConfig.plugins.push({
name: "no-externals",
async resolveId(id, from, options) {
if (
nitro.options.node &&
(id.startsWith("node:") || builtinModules.includes(id))
) {
return { id, external: true };
}
const resolved = await this.resolve(id, from, {
...options,
skipSelf: true,
});
if (!resolved) {
const _resolved = await resolvePath(id, {
url: nitro.options.nodeModulesDirs,
conditions: [
"default",
nitro.options.dev ? "development" : "production",
"module",
"node",
"import",
],
}).catch(() => null);
if (_resolved) {
return { id: _resolved, external: false };
}
}
if (!resolved || resolved.external) {
throw new Error(
`Cannot resolve ${JSON.stringify(id)} from ${JSON.stringify(
Expand Down