Skip to content

Commit

Permalink
fix: exclude node_modules paths by default (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Apr 22, 2024
1 parent e9268f7 commit 61f98ee
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
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 61f98ee

Please sign in to comment.