Skip to content

Commit

Permalink
fix(rollup): use mlly as fallback resolver when externals disabled (#948
Browse files Browse the repository at this point in the history
)

* fix(rollup): use mlly as fallback resolver when externals disabled

* more readable
  • Loading branch information
pi0 authored Feb 15, 2023
1 parent 7d71f7a commit be1ac07
Showing 1 changed file with 23 additions and 2 deletions.
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

0 comments on commit be1ac07

Please sign in to comment.