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(externals): absolute paths are inlined #1429

Merged
merged 8 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 4 additions & 1 deletion src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ export const plugins = [
defu(nitro.options.externals, {
outDir: nitro.options.output.serverDir,
moduleDirectories: nitro.options.nodeModulesDirs,
external: [...(nitro.options.dev ? [nitro.options.buildDir] : [])],
external: [
...(nitro.options.dev ? [nitro.options.buildDir] : []),
...nitro.options.nodeModulesDirs,
],
inline: [
"#",
"~",
Expand Down
79 changes: 43 additions & 36 deletions src/rollup/plugins/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ export function externals(opts: NodeExternalsOptions): Plugin {
};

// Normalize options
const inlineMatchers = (opts.inline || []).map((p) => normalizeMatcher(p));
const externalMatchers = (opts.external || []).map((p) =>
normalizeMatcher(p)
);
const inlineMatchers = (opts.inline || [])
.map((p) => normalizeMatcher(p))
.sort((a, b) => b.score - a.score);
const externalMatchers = (opts.external || [])
.map((p) => normalizeMatcher(p))
.sort((a, b) => b.score - a.score);

return {
name: "node-externals",
Expand All @@ -71,21 +73,15 @@ export function externals(opts: NodeExternalsOptions): Plugin {
// Normalize path (windows)
const id = normalize(originalId);

// Id without .../node_modules/
const idWithoutNodeModules = id.split("node_modules/").pop();

// Check for explicit inlines
for (const matcher of inlineMatchers) {
if (matcher(id, importer)) {
return null;
}
}

// Check for explicit externals
for (const matcher of externalMatchers) {
if (matcher(id, importer)) {
return { id, external: true };
}
// Check for explicit inlines and externals
const inlineMatch = inlineMatchers.find((m) => m(id, importer));
const externalMatch = externalMatchers.find((m) => m(id, importer));
if (
inlineMatch &&
(!externalMatch ||
(externalMatch && inlineMatch.score > externalMatch.score))
) {
return null;
}

// Resolve id using rollup resolver
Expand Down Expand Up @@ -515,23 +511,34 @@ async function isFile(file: string) {
}
}

export function normalizeMatcher(
pattern:
| string
| RegExp
| ((id: string, importer?: string) => Promise<boolean> | boolean)
) {
if (typeof pattern === "string") {
const _pattern = normalize(pattern);
return (id: string) => {
const idWithoutNodeModules = id.split("node_modules/").pop();
return (
id.startsWith(_pattern) || idWithoutNodeModules.startsWith(_pattern)
);
};
type Matcher = ((
id: string,
importer?: string
) => Promise<boolean> | boolean) & { score?: number };

export function normalizeMatcher(input: string | RegExp | Matcher): Matcher {
if (typeof input === "function") {
input.score = input.score ?? 10_000;
return input;
}
if (typeof pattern === "function") {
return pattern;

if (input instanceof RegExp) {
const matcher = ((id: string) => input.test(id)) as Matcher;
matcher.score = input.toString().length;
Object.defineProperty(matcher, "name", { value: `match(${input})` });
return matcher;
}
return (id: string) => pattern.test(id);

if (typeof input === "string") {
const pattern = normalize(input);
const matcher = ((id: string) => {
const idWithoutNodeModules = id.split("node_modules/").pop();
return id.startsWith(pattern) || idWithoutNodeModules.startsWith(pattern);
}) as Matcher;
matcher.score = input.length;
Object.defineProperty(matcher, "name", { value: `match(${pattern})` });
return matcher;
}

throw new Error(`Invalid matcher or pattern: ${input}`);
}
6 changes: 6 additions & 0 deletions test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fileURLToPath } from "node:url";
import { defineNitroConfig } from "../../src/config";

export default defineNitroConfig({
Expand All @@ -20,6 +21,11 @@ export default defineNitroConfig({
devProxy: {
"/proxy/example": { target: "https://example.com", changeOrigin: true },
},
alias: {
"#fixture-nitro-utils-extra-absolute": fileURLToPath(
new URL("node_modules/@fixture/nitro-utils/extra2.mjs", import.meta.url)
),
},
serverAssets: [
{
baseName: "files",
Expand Down
1 change: 1 addition & 0 deletions test/fixture/node_modules/@fixture/nitro-utils/extra2.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/fixture/node_modules/@fixture/nitro-utils/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixture/routes/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import depLib from "@fixture/nitro-lib";
import subpathLib from "@fixture/nitro-lib/subpath";
// @ts-ignore
import extraUtils from "@fixture/nitro-utils/extra";
// @ts-ignore
import extraUtilsAbsolute from "#fixture-nitro-utils-extra-absolute";

export default defineEventHandler(() => {
return {
Expand All @@ -16,5 +18,6 @@ export default defineEventHandler(() => {
depLib, // expected to all be 2.0.0
subpathLib, // expected to 2.0.0
extraUtils,
extraUtilsAbsolute,
};
});
3 changes: 3 additions & 0 deletions test/presets/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ describe("nitro:preset:node", async () => {
expect(
existsSync(resolve(serverNodeModules, "@fixture/nitro-utils/extra.mjs"))
).toBe(true);
expect(
existsSync(resolve(serverNodeModules, "@fixture/nitro-utils/extra2.mjs"))
).toBe(true);
});
});
Loading