Skip to content

Commit

Permalink
fix: resolve and include scanDirs within node_modules for auto import
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 10, 2023
1 parent 7f0f94f commit 65b8fbe
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 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 Down Expand Up @@ -190,13 +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)) {
if (options.imports.exclude.length === 0) {
options.imports.exclude.push(/[/\\]node_modules[/\\]/, /[/\\]\.git[/\\]/);
}
// Exclude .git and buildDir by default
options.imports.exclude.push(/[/\\]\.git[/\\]/);
options.imports.exclude.push(options.buildDir);

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 65b8fbe

Please sign in to comment.