Skip to content

Commit

Permalink
fix: resolve and include scanDirs within node_modules for auto impo…
Browse files Browse the repository at this point in the history
…rt (#812)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
yassilah and pi0 authored Jan 10, 2023
1 parent 976d81d commit 4654e59
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { klona } from "klona/full";
import { camelCase } from "scule";
import { defu } from "defu";
import { resolveModuleExportNames, resolvePath as resolveModule } from "mlly";
// import escapeRE from 'escape-string-regexp'
import escapeRE from "escape-string-regexp";
import { withLeadingSlash, withoutTrailingSlash, withTrailingSlash } from "ufo";
import { isTest, isDebug } from "std-env";
import { findWorkspaceDir } from "pkg-types";
Expand Down Expand Up @@ -35,7 +35,7 @@ const NitroDefaults: NitroConfig = {
publicDir: "{{ output.dir }}/public",
},

// Featueres
// Features
experimental: {},
storage: {},
devStorage: {},
Expand All @@ -44,7 +44,7 @@ const NitroDefaults: NitroConfig = {
serverAssets: [],
plugins: [],
imports: {
exclude: [/[/\\]node_modules[/\\]/, /[/\\]\.git[/\\]/],
exclude: [],
presets: nitroImports,
},
virtual: {},
Expand Down Expand Up @@ -190,9 +190,30 @@ export async function loadOptions(
if (options.scanDirs.length === 0) {
options.scanDirs = [options.srcDir];
}
options.scanDirs = options.scanDirs.map((dir) =>
resolve(options.srcDir, dir)
);

if (options.imports && Array.isArray(options.imports.exclude)) {
options.imports.exclude.push(options.buildDir);
if (options.imports.exclude.length === 0) {
// Exclude .git and buildDir by default
options.imports.exclude.push(/[/\\]\.git[/\\]/);
options.imports.exclude.push(options.buildDir);

// Exclude all node modules that are not a scanDir
const scanDirsInNodeModules = options.scanDirs
.map((dir) => dir.match(/(?<=\/)node_modules\/(.+)$/)?.[1])
.filter(Boolean);
options.imports.exclude.push(
scanDirsInNodeModules.length
? new RegExp(
`node_modules\\/(?!${scanDirsInNodeModules
.map((dir) => escapeRE(dir))
.join("|")})`
)
: /[/\\]node_modules[/\\]/
);
}
}

// Normalise absolute auto-import paths for windows machines
Expand Down

0 comments on commit 4654e59

Please sign in to comment.