@rstackjs/load-config 1.0.0 → 1.0.1

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.
@@ -0,0 +1,2 @@
1
+ /** Share the pending import while allowing a failed import to be retried. */
2
+ export declare const cachedImport: <T>(importer: () => Promise<T>) => (() => Promise<T>);
package/dist/index.js CHANGED
@@ -9,10 +9,18 @@ const getConfigExport = (configModule, exportName, configPath)=>{
9
9
  if (canReadExport(configModule) && Object.hasOwn(configModule, exportName)) return configModule[exportName];
10
10
  throw new Error(`Cannot find export ${exportName} in config file: ${configPath}`);
11
11
  };
12
+ const cachedImport = (importer)=>{
13
+ let promise;
14
+ return ()=>promise ??= importer().catch((error)=>{
15
+ promise = void 0;
16
+ throw error;
17
+ });
18
+ };
19
+ const getJiti = cachedImport(()=>import("jiti"));
12
20
  const loadWithJiti = async (configPath, exportName, fresh)=>{
13
21
  let createJiti;
14
22
  try {
15
- ({ createJiti } = await import("jiti"));
23
+ ({ createJiti } = await getJiti());
16
24
  } catch (error) {
17
25
  throw new Error('The "jiti" package is required to load this config. Install it with your package manager.', {
18
26
  cause: error
@@ -53,6 +61,7 @@ function withConfigMeta(config, meta) {
53
61
  function getConfigMeta(config) {
54
62
  if (null !== config && 'object' == typeof config && Object.hasOwn(config, CONFIG_META)) return config[CONFIG_META];
55
63
  }
64
+ const getFreshImport = cachedImport(()=>import("./freshImport.js"));
56
65
  const JS_CONFIG_REGEXP = /\.(?:js|mjs|cjs)$/;
57
66
  const loadWithNative = async (configPath, fresh)=>{
58
67
  const configFileURL = pathToFileURL(configPath).href;
@@ -63,7 +72,7 @@ const loadWithNative = async (configPath, fresh)=>{
63
72
  dependencies: []
64
73
  };
65
74
  }
66
- const { freshImport } = await import("./freshImport.js");
75
+ const { freshImport } = await getFreshImport();
67
76
  const freshImportResult = await freshImport(configFileURL);
68
77
  if (freshImportResult) return {
69
78
  configModule: freshImportResult.result,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rstackjs/load-config",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/rstackjs/load-config"