From 7c48947754bce2f881d153eb3c490f2940814c80 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 30 Jun 2022 00:17:38 +0200 Subject: [PATCH] feat: try resolving paths as npm package --- src/loader.ts | 15 +++++++++++++++ test/fixture/.gitignore | 1 + test/fixture/node_modules/c12-npm-test/config.ts | 3 +++ .../node_modules/c12-npm-test/package.json | 7 +++++++ test/index.test.ts | 8 ++++++++ 5 files changed, 34 insertions(+) create mode 100644 test/fixture/.gitignore create mode 100644 test/fixture/node_modules/c12-npm-test/config.ts create mode 100644 test/fixture/node_modules/c12-npm-test/package.json diff --git a/src/loader.ts b/src/loader.ts index a9c23a4..db59349 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -153,15 +153,21 @@ async function extendConfig (config, opts: LoadConfigOptions) { const GIT_PREFIXES = ['github:', 'gitlab:', 'bitbucket:', 'https://'] +// https://github.com/dword-design/package-name-regex +const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/ + const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false }) async function resolveConfig (source: string, opts: LoadConfigOptions): Promise { + // Custom user resolver if (opts.resolve) { const res = await opts.resolve(source, opts) if (res) { return res } } + + // Download git URLs and resolve to local path if (GIT_PREFIXES.some(prefix => source.startsWith(prefix))) { const url = new URL(source) const subPath = url.pathname.split('/').slice(2).join('/') @@ -173,6 +179,15 @@ async function resolveConfig (source: string, opts: LoadConfigOptions): Promise< await gittar.extract(tarFile, tmpdir) source = resolve(tmpdir, subPath) } + + // Try resolving as npm package + if (NPM_PACKAGE_RE.test(source)) { + try { + source = jiti.resolve(source, { paths: [opts.cwd] }) + } catch (_err) {} + } + + // Import from local fs const isDir = !extname(source) const cwd = resolve(opts.cwd, isDir ? source : dirname(source)) if (isDir) { source = opts.configFile } diff --git a/test/fixture/.gitignore b/test/fixture/.gitignore new file mode 100644 index 0000000..cf4bab9 --- /dev/null +++ b/test/fixture/.gitignore @@ -0,0 +1 @@ +!node_modules diff --git a/test/fixture/node_modules/c12-npm-test/config.ts b/test/fixture/node_modules/c12-npm-test/config.ts new file mode 100644 index 0000000..b4bb95a --- /dev/null +++ b/test/fixture/node_modules/c12-npm-test/config.ts @@ -0,0 +1,3 @@ +export default { + npmConfig: true +} diff --git a/test/fixture/node_modules/c12-npm-test/package.json b/test/fixture/node_modules/c12-npm-test/package.json new file mode 100644 index 0000000..50e4bdc --- /dev/null +++ b/test/fixture/node_modules/c12-npm-test/package.json @@ -0,0 +1,7 @@ +{ + "name": "c12-npm-test", + "version": "0.0.0", + "exports": { + ".": "./config.ts" + } +} diff --git a/test/index.test.ts b/test/index.test.ts index a1cf306..cd736b8 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -40,6 +40,7 @@ describe('c12', () => { "configFile": true, "defaultConfig": true, "devConfig": true, + "npmConfig": true, "overriden": true, "rcFile": true, "virtual": true, @@ -117,6 +118,13 @@ describe('c12', () => { "configFile": "/fixture/config.dev.ts", "cwd": "/fixture", }, + { + "config": { + "npmConfig": true, + }, + "configFile": "/fixture/node_modules/c12-npm-test/config.ts", + "cwd": "/fixture/node_modules/c12-npm-test", + }, ] `) })