silgi 0.17.8 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- const version = "0.17.8";
1
+ const version = "0.18.0";
2
2
  const peerDependencies = {
3
3
  "@fastify/deepmerge": "^2.0.2",
4
4
  "@nuxt/kit": "^3.15.3",
@@ -27,6 +27,7 @@ const dependencies = {
27
27
  "dot-prop": "catalog:",
28
28
  dotenv: "catalog:",
29
29
  "escape-string-regexp": "catalog:",
30
+ exsolve: "^1.0.4",
30
31
  globby: "catalog:",
31
32
  hookable: "catalog:",
32
33
  ignore: "catalog:",
package/dist/cli/dev.mjs CHANGED
@@ -24,6 +24,7 @@ import 'mlly';
24
24
  import 'dev-jiti';
25
25
  import 'node:url';
26
26
  import 'defu';
27
+ import 'exsolve';
27
28
  import 'globby';
28
29
  import 'ignore';
29
30
  import '@oxc-parser/wasm';
@@ -23,6 +23,7 @@ import './compatibility.mjs';
23
23
  import 'semver/functions/satisfies.js';
24
24
  import 'node:url';
25
25
  import 'defu';
26
+ import 'exsolve';
26
27
  import 'globby';
27
28
  import 'ignore';
28
29
  import '@oxc-parser/wasm';
@@ -2,19 +2,20 @@ import { existsSync, promises, readFileSync, writeFileSync, mkdirSync } from 'no
2
2
  import { readdir, readFile } from 'node:fs/promises';
3
3
  import consola$1, { consola } from 'consola';
4
4
  import { createHooks, createDebugger } from 'hookable';
5
- import { join, resolve, isAbsolute, relative, dirname, basename, extname } from 'pathe';
5
+ import { join, resolve, relative, dirname, basename, extname, isAbsolute } from 'pathe';
6
6
  import { useSilgiCLI, silgiCLICtx } from 'silgi/core';
7
- import { relativeWithDot, hash, resolveAlias, resolvePath, writeFile, parseServices, normalizeTemplate, useLogger, addTemplate, initRuntimeConfig, hasError as hasError$1, resolveSilgiPath, isDirectory } from 'silgi/kit';
7
+ import { relativeWithDot, hash, resolveAlias, directoryToURL, writeFile, parseServices, normalizeTemplate, useLogger, addTemplate, initRuntimeConfig, hasError as hasError$1, resolveSilgiPath, isDirectory } from 'silgi/kit';
8
8
  import { runtimeDir } from 'silgi/runtime/meta';
9
9
  import { autoImportTypes } from 'silgi/types';
10
10
  import { scanExports, createUnimport, toExports } from 'unimport';
11
11
  import { c as createRouteRules } from '../_chunks/routeRules.mjs';
12
12
  import { p as prepareEnv } from './env.mjs';
13
- import { resolveModuleExportNames, resolve as resolve$1, resolvePath as resolvePath$1, parseNodeModulePath, lookupNodeModuleSubpath } from 'mlly';
13
+ import { resolveModuleExportNames, resolvePath, parseNodeModulePath, lookupNodeModuleSubpath } from 'mlly';
14
14
  import { createJiti } from 'dev-jiti';
15
15
  import { a as hasInstalledModule, h as hasError } from './compatibility.mjs';
16
- import { pathToFileURL, fileURLToPath } from 'node:url';
16
+ import { fileURLToPath } from 'node:url';
17
17
  import defu, { defu as defu$1 } from 'defu';
18
+ import { resolveModuleURL } from 'exsolve';
18
19
  import { isRelative, withTrailingSlash } from 'ufo';
19
20
  import { globby } from 'globby';
20
21
  import ignore from 'ignore';
@@ -387,70 +388,81 @@ async function installModule(moduleToInstall, silgi = useSilgiCLI(), inlineOptio
387
388
  }
388
389
  }
389
390
 
390
- const logger$1 = consola;
391
- async function _resolveSilgiModule(mod, silgi) {
392
- let _url;
391
+ const MissingModuleMatcher = /Cannot find module\s+['"]?([^'")\s]+)['"]?/i;
392
+ async function _resolveSilgiModule(silgiModule, silgi) {
393
+ let resolvedModulePath;
393
394
  let buildTimeModuleMeta = {};
394
395
  const jiti = createJiti(silgi.options.rootDir, {
395
396
  alias: silgi.options.alias
396
397
  });
397
- if (typeof mod === "string") {
398
- const paths = /* @__PURE__ */ new Set();
399
- mod = resolveAlias(mod, silgi.options.alias);
400
- if (isRelative(mod)) {
401
- mod = resolve(silgi.options.rootDir, mod);
398
+ if (typeof silgiModule === "string") {
399
+ silgiModule = resolveAlias(silgiModule, silgi.options.alias);
400
+ if (isRelative(silgiModule)) {
401
+ silgiModule = resolve(silgi.options.rootDir, silgiModule);
402
402
  }
403
- paths.add(join(mod, "module"));
404
- paths.add(mod);
405
- for (const path of paths) {
406
- try {
407
- const src = isAbsolute(path) ? pathToFileURL(await resolvePath(path, { fallbackToOriginal: false, extensions: silgi.options.extensions })).href : await resolve$1(path, { url: silgi.options.modulesDir.map((m) => pathToFileURL(m.replace(/\/node_modules\/?$/, ""))), extensions: silgi.options.extensions });
408
- mod = await jiti.import(src, {
409
- default: true,
410
- conditions: silgi.options.conditions
411
- });
412
- _url = fileURLToPath(new URL(src));
413
- const moduleMetadataPath = new URL("module.json", src);
414
- if (existsSync(moduleMetadataPath)) {
415
- buildTimeModuleMeta = JSON.parse(await promises.readFile(moduleMetadataPath, "utf-8"));
416
- } else {
417
- if (typeof mod === "function") {
418
- const meta = await mod.getMeta?.();
419
- const _exports = await scanExports(_url, true);
420
- buildTimeModuleMeta = {
421
- ...meta,
422
- exports: _exports.map(({ from, ...rest }) => rest)
423
- };
424
- }
403
+ try {
404
+ console.log(silgi.options.conditions);
405
+ const src = resolveModuleURL(silgiModule, {
406
+ from: silgi.options.modulesDir.map((m) => directoryToURL(m.replace(/\/node_modules\/?$/, "/"))),
407
+ suffixes: ["silgi", "silgi/index", "module", "module/index", "", "index"],
408
+ extensions: [".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"]
409
+ // Maybe add https://github.com/unjs/exsolve/blob/dfff3e9bbc4a3a173a2d56b9b9ff731ab15598be/src/resolve.ts#L7
410
+ // conditions: silgi.options.conditions,
411
+ });
412
+ resolvedModulePath = fileURLToPath(src);
413
+ const resolvedSilgiModule = await jiti.import(src, { default: true });
414
+ if (typeof resolvedSilgiModule !== "function") {
415
+ throw new TypeError(`Nuxt module should be a function: ${silgiModule}.`);
416
+ }
417
+ silgiModule = await jiti.import(src, {
418
+ default: true,
419
+ conditions: silgi.options.conditions
420
+ });
421
+ const moduleMetadataPath = new URL("module.json", src);
422
+ if (existsSync(moduleMetadataPath)) {
423
+ buildTimeModuleMeta = JSON.parse(await promises.readFile(moduleMetadataPath, "utf-8"));
424
+ } else {
425
+ if (typeof silgiModule === "function") {
426
+ const meta = await silgiModule.getMeta?.();
427
+ const _exports = await scanExports(resolvedModulePath, true);
428
+ buildTimeModuleMeta = {
429
+ ...meta,
430
+ exports: _exports.map(({ from, ...rest }) => rest)
431
+ };
425
432
  }
426
- break;
427
- } catch (error) {
428
- const code = error.code;
429
- if (code === "MODULE_NOT_FOUND" || code === "ERR_PACKAGE_PATH_NOT_EXPORTED" || code === "ERR_MODULE_NOT_FOUND" || code === "ERR_UNSUPPORTED_DIR_IMPORT" || code === "ENOTDIR") {
430
- continue;
433
+ }
434
+ } catch (error) {
435
+ const code = error.code;
436
+ if (code === "MODULE_NOT_FOUND" || code === "ERR_PACKAGE_PATH_NOT_EXPORTED" || code === "ERR_MODULE_NOT_FOUND" || code === "ERR_UNSUPPORTED_DIR_IMPORT" || code === "ENOTDIR") {
437
+ throw new TypeError(`Could not load \`${silgiModule}\`. Is it installed?`);
438
+ }
439
+ if (code === "MODULE_NOT_FOUND" || code === "ERR_MODULE_NOT_FOUND") {
440
+ const module = MissingModuleMatcher.exec(error.message)?.[1];
441
+ if (module && !module.includes(silgiModule)) {
442
+ throw new TypeError(`Error while importing module \`${silgiModule}\`: ${error}`);
431
443
  }
432
- logger$1.error(`Error while importing module \`${mod}\`: ${error}`);
433
- throw error;
434
444
  }
435
445
  }
436
446
  }
437
447
  if (!buildTimeModuleMeta) {
438
- throw new Error(`Module ${mod} is not a valid Silgi module`);
448
+ throw new Error(`Module ${silgiModule} is not a valid Silgi module`);
439
449
  }
440
- if (typeof mod === "function") {
450
+ if (typeof silgiModule === "function") {
441
451
  if (silgi.scanModules.some((m) => m.meta?.configKey === buildTimeModuleMeta.configKey)) {
442
452
  throw new Error(`Module with key \`${buildTimeModuleMeta.configKey}\` already exists`);
443
453
  }
444
- const options = await mod.getOptions?.() || {};
454
+ const options = await silgiModule.getOptions?.() || {};
445
455
  if (options) {
446
456
  silgi.options._c12.config[buildTimeModuleMeta.configKey] = defu(
447
457
  silgi.options._c12.config[buildTimeModuleMeta.configKey] || {},
448
458
  options || {}
449
459
  );
460
+ } else {
461
+ throw new TypeError(`Could not load \`${silgiModule}\`. Is it installed?`);
450
462
  }
451
463
  silgi.scanModules.push({
452
464
  meta: buildTimeModuleMeta,
453
- entryPath: _url,
465
+ entryPath: resolvedModulePath || "",
454
466
  installed: false,
455
467
  options
456
468
  });
@@ -2126,7 +2138,7 @@ async function writeTypesAndFiles(silgi) {
2126
2138
  }
2127
2139
  let path = resolveAlias$1(i.from, silgi.options.alias);
2128
2140
  if (isAbsolute(path)) {
2129
- const resolvedPath = await resolvePath$1(i.from, {
2141
+ const resolvedPath = await resolvePath(i.from, {
2130
2142
  url: silgi.options.nodeModulesDirs
2131
2143
  }).catch(() => null);
2132
2144
  if (resolvedPath) {
@@ -6,6 +6,7 @@ import { IncomingMessage } from 'node:http';
6
6
 
7
7
  declare function hasError(type: SilgiCLI['errors'][0]['type'], silgi?: SilgiCLI): boolean;
8
8
 
9
+ declare function directoryToURL(dir: string): URL;
9
10
  /**
10
11
  * Resolve a module from a given root path using an algorithm patterned on
11
12
  * the upcoming `import.meta.resolve`. It returns a file URL
@@ -149,4 +150,4 @@ declare const MODE_RE: RegExp;
149
150
  declare function hasSilgiModule(moduleKey: string, silgi?: SilgiCLI): boolean;
150
151
  declare function hasInstalledModule(moduleKey: string, silgi?: SilgiCLI): boolean;
151
152
 
152
- export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, getIpAddress, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, parseServices, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, serviceParseModule, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
153
+ export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, directoryToURL, filterInPlace, getIpAddress, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, parseServices, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, serviceParseModule, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
@@ -6,6 +6,7 @@ import { IncomingMessage } from 'node:http';
6
6
 
7
7
  declare function hasError(type: SilgiCLI['errors'][0]['type'], silgi?: SilgiCLI): boolean;
8
8
 
9
+ declare function directoryToURL(dir: string): URL;
9
10
  /**
10
11
  * Resolve a module from a given root path using an algorithm patterned on
11
12
  * the upcoming `import.meta.resolve`. It returns a file URL
@@ -149,4 +150,4 @@ declare const MODE_RE: RegExp;
149
150
  declare function hasSilgiModule(moduleKey: string, silgi?: SilgiCLI): boolean;
150
151
  declare function hasInstalledModule(moduleKey: string, silgi?: SilgiCLI): boolean;
151
152
 
152
- export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, getIpAddress, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, parseServices, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, serviceParseModule, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
153
+ export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, directoryToURL, filterInPlace, getIpAddress, hasError, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, parseServices, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, serviceParseModule, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
@@ -1,5 +1,6 @@
1
1
  import { c as checkSilgiCompatibility } from '../cli/compatibility.mjs';
2
2
  export { h as hasError } from '../cli/compatibility.mjs';
3
+ import { pathToFileURL, fileURLToPath } from 'node:url';
3
4
  import { resolvePath as resolvePath$1 } from 'mlly';
4
5
  import fsp from 'node:fs/promises';
5
6
  import consola, { consola as consola$1 } from 'consola';
@@ -12,7 +13,6 @@ import { useSilgi, tryUseSilgiCLI, useSilgiCLI, tryUseSilgi } from 'silgi/core';
12
13
  import { defu } from 'defu';
13
14
  import { withLeadingSlash } from 'ufo';
14
15
  import { existsSync, promises } from 'node:fs';
15
- import { fileURLToPath } from 'node:url';
16
16
  import { resolveAlias as resolveAlias$1 } from 'pathe/utils';
17
17
  import destr from 'destr';
18
18
  import { klona } from 'klona';
@@ -20,6 +20,9 @@ import { hash as hash$2 } from 'silgi/kit';
20
20
  import 'semver/functions/satisfies.js';
21
21
  import 'silgi/meta';
22
22
 
23
+ function directoryToURL(dir) {
24
+ return pathToFileURL(`${dir}/`);
25
+ }
23
26
  async function tryResolveModule(id, url = import.meta.url) {
24
27
  try {
25
28
  return await resolvePath$1(id, { url });
@@ -296,6 +299,7 @@ async function isDirectory(path) {
296
299
  return (await promises.lstat(path)).isDirectory();
297
300
  }
298
301
  function resolveAlias(path, alias) {
302
+ alias ||= tryUseSilgiCLI()?.options.alias || {};
299
303
  return resolveAlias$1(path, alias || {});
300
304
  }
301
305
  function createResolver(base) {
@@ -571,4 +575,4 @@ function isValidIp(ip) {
571
575
  return false;
572
576
  }
573
577
 
574
- export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, filterInPlace, getIpAddress, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory$1 as isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, parseServices, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, serviceParseModule, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
578
+ export { MODE_RE, addTemplate, applyEnv, createResolver, defineSilgiModule, defineSilgiPreset, directoryToURL, filterInPlace, getIpAddress, hasInstalledModule, hasSilgiModule, hash, initRuntimeConfig, ipAddress, isDirectory$1 as isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, parseServices, prettyPath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, serviceParseModule, toArray, tryResolveModule, useLogger, useRequest, useSilgiRuntimeConfig, writeFile };
@@ -1,4 +1,4 @@
1
- const version = "0.17.8";
1
+ const version = "0.18.0";
2
2
  const peerDependencies = {
3
3
  "@fastify/deepmerge": "^2.0.2",
4
4
  "@nuxt/kit": "^3.15.3",
@@ -27,6 +27,7 @@ const dependencies = {
27
27
  "dot-prop": "catalog:",
28
28
  dotenv: "catalog:",
29
29
  "escape-string-regexp": "catalog:",
30
+ exsolve: "^1.0.4",
30
31
  globby: "catalog:",
31
32
  hookable: "catalog:",
32
33
  ignore: "catalog:",
@@ -1,4 +1,4 @@
1
- const version = "0.17.8";
1
+ const version = "0.18.0";
2
2
  const peerDependencies = {
3
3
  "@fastify/deepmerge": "^2.0.2",
4
4
  "@nuxt/kit": "^3.15.3",
@@ -27,6 +27,7 @@ const dependencies = {
27
27
  "dot-prop": "catalog:",
28
28
  dotenv: "catalog:",
29
29
  "escape-string-regexp": "catalog:",
30
+ exsolve: "^1.0.4",
30
31
  globby: "catalog:",
31
32
  hookable: "catalog:",
32
33
  ignore: "catalog:",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "silgi",
3
3
  "type": "module",
4
- "version": "0.17.8",
4
+ "version": "0.18.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "exports": {
@@ -126,6 +126,7 @@
126
126
  "dot-prop": "^9.0.0",
127
127
  "dotenv": "^16.4.7",
128
128
  "escape-string-regexp": "^5.0.0",
129
+ "exsolve": "^1.0.4",
129
130
  "globby": "^14.1.0",
130
131
  "hookable": "^5.5.3",
131
132
  "ignore": "^7.0.3",