Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: exclude node_modules paths by default
Browse files Browse the repository at this point in the history
antfu committed Apr 22, 2024
1 parent e9268f7 commit 4df029d
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ const FILENAME_VAR_NAME = "__injected_filename__"
const IMPORT_META_URL_VAR_NAME = "__injected_import_meta_url__"

export const JS_EXT_RE = /\.([mc]?[tj]s|[tj]sx)$/
const PATH_NODE_MODULES_RE = /[\/\\]node_modules[\/\\]/

function inferLoader(ext: string): Loader {
if (ext === ".mjs" || ext === ".cjs") return "js"
@@ -135,11 +136,6 @@ export const externalPlugin = ({
name: "bundle-require:external",
setup(ctx) {
ctx.onResolve({ filter: /.*/ }, async (args) => {
if (args.path[0] === "." || path.isAbsolute(args.path)) {
// Fallback to default
return
}

if (match(args.path, external)) {
return {
external: true,
@@ -151,6 +147,21 @@ export const externalPlugin = ({
return
}

if (args.path.match(PATH_NODE_MODULES_RE)) {
const resolved = args.path[0] === "."
? path.resolve(args.resolveDir, args.path)
: args.path
return {
path: resolved,
external: true,
}
}

if (args.path[0] === "." || path.isAbsolute(args.path)) {
// Fallback to default
return
}

// Most like importing from node_modules, mark external
return {
external: true,

0 comments on commit 4df029d

Please sign in to comment.