@equinor/fusion-imports 1.1.12-next.0 → 2.0.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.
Files changed (39) hide show
  1. package/README.md +121 -71
  2. package/dist/esm/error.js +23 -15
  3. package/dist/esm/error.js.map +1 -1
  4. package/dist/esm/import-config.js +21 -10
  5. package/dist/esm/import-config.js.map +1 -1
  6. package/dist/esm/import-json.js +17 -5
  7. package/dist/esm/import-json.js.map +1 -1
  8. package/dist/esm/import-meta-resolve-plugin.js +48 -13
  9. package/dist/esm/import-meta-resolve-plugin.js.map +1 -1
  10. package/dist/esm/import-script.js +20 -12
  11. package/dist/esm/import-script.js.map +1 -1
  12. package/dist/esm/index.js +15 -0
  13. package/dist/esm/index.js.map +1 -1
  14. package/dist/esm/markdown-plugin.js +22 -9
  15. package/dist/esm/markdown-plugin.js.map +1 -1
  16. package/dist/esm/resolve-config-file.js +23 -6
  17. package/dist/esm/resolve-config-file.js.map +1 -1
  18. package/dist/esm/version.js +1 -1
  19. package/dist/esm/version.js.map +1 -1
  20. package/dist/tsconfig.tsbuildinfo +1 -1
  21. package/dist/types/error.d.ts +23 -15
  22. package/dist/types/import-config.d.ts +47 -19
  23. package/dist/types/import-json.d.ts +18 -5
  24. package/dist/types/import-meta-resolve-plugin.d.ts +24 -6
  25. package/dist/types/import-script.d.ts +30 -22
  26. package/dist/types/index.d.ts +18 -3
  27. package/dist/types/markdown-plugin.d.ts +25 -11
  28. package/dist/types/resolve-config-file.d.ts +33 -10
  29. package/dist/types/version.d.ts +1 -1
  30. package/package.json +3 -3
  31. package/src/error.ts +23 -15
  32. package/src/import-config.ts +47 -19
  33. package/src/import-json.ts +18 -5
  34. package/src/import-meta-resolve-plugin.ts +48 -13
  35. package/src/import-script.ts +30 -22
  36. package/src/index.ts +19 -3
  37. package/src/markdown-plugin.ts +25 -11
  38. package/src/resolve-config-file.ts +33 -10
  39. package/src/version.ts +1 -1
package/README.md CHANGED
@@ -1,116 +1,166 @@
1
- # Fusion Imports
1
+ # @equinor/fusion-imports
2
2
 
3
- This package provides utility functions for handling imports.
3
+ Utilities for importing and transpiling TypeScript, JavaScript, and JSON configuration files at **Node.js runtime** using [esbuild](https://esbuild.github.io/).
4
4
 
5
- > [!important]
6
- > This module is meant for usage of __file__ content, not __url__
5
+ > [!IMPORTANT]
6
+ > This package works with **file paths**, not URLs. It is designed for loading configuration scripts at runtime in Node.js CLI tools and build pipelines.
7
7
 
8
- ## Usage
8
+ ## Installation
9
9
 
10
- This module is primary for loading configuration from scripts runtime.
10
+ ```bash
11
+ pnpm add @equinor/fusion-imports
12
+ ```
13
+
14
+ ## When to use
11
15
 
12
- ### importConfig
16
+ | Scenario | Function |
17
+ |---|---|
18
+ | Load a config by basename, auto-resolving `.ts` → `.js` → `.json` | `importConfig` |
19
+ | Bundle & import a single TypeScript/JS module at runtime | `importScript` |
20
+ | Read and parse a JSON file from disk | `importJSON` |
21
+ | Find the first accessible config file for a basename | `resolveConfigFile` |
13
22
 
14
- `importConfig` will resolve the provided basename by looking for `TypeScript`, then `JavaScript` and then `json`. Internally it will call [importScript](#importscript) and [importJSON](#importjson)
23
+ ## Quick start
15
24
 
16
- ```ts
25
+ ```typescript
17
26
  import { importConfig } from '@equinor/fusion-imports';
18
27
 
19
- type MyConfig {
28
+ interface AppSettings {
20
29
  name: string;
21
- bar: {
22
- foobar: number;
23
- }
24
- };
25
-
26
- type ScriptModule = {
27
- generateConfig: (options: any) => MyConfig;
30
+ port: number;
28
31
  }
29
32
 
30
- const config = importConfig('my-config', {
31
- script: {
32
- resolve: (module: ScriptModule) => {
33
- return module.generateConfig(someOptions);
34
- }
35
- }
36
- });
33
+ // Resolves app.config.ts app.config.js → app.config.json
34
+ const { config, path } = await importConfig<AppSettings>('app.config');
35
+ console.log(`Loaded ${path}:`, config);
37
36
  ```
38
37
 
39
- __example file: `./my-config.ts`__
40
- ```ts
41
- import { base } from './baseConfig.ts'
38
+ ## API
42
39
 
43
- export function generateConfig(options: any): MyConfig {
44
- return {
45
- name: 'foo'
46
- ...base(options)
47
- }
40
+ ### `importConfig<C>(basename, options?)`
41
+
42
+ Resolves a configuration file by basename, probing extensions in order (`.ts`, `.mjs`, `.js`, `.json`). JSON files are parsed directly; script files are bundled with esbuild and dynamically imported.
43
+
44
+ ```typescript
45
+ import { importConfig } from '@equinor/fusion-imports';
46
+
47
+ interface MyConfig {
48
+ name: string;
49
+ bar: { foobar: number };
48
50
  }
51
+
52
+ interface ScriptModule {
53
+ generateConfig: (options: { env: string }) => MyConfig;
54
+ }
55
+
56
+ const { config } = await importConfig<MyConfig, ScriptModule>('my-config', {
57
+ script: {
58
+ resolve: (module) => module.generateConfig({ env: 'production' }),
59
+ },
60
+ });
49
61
  ```
50
62
 
51
- #### resolve
63
+ By default the module's `default` export is used as the config value. Provide `script.resolve` to extract a different export or call a factory function.
52
64
 
53
- `importConfig` will try to resolve `module.default` as default
65
+ #### Options
54
66
 
55
- ```ts
56
- // my-script.ts
57
- const config = importConfig<number>('my-config');
67
+ | Option | Type | Description |
68
+ |---|---|---|
69
+ | `baseDir` | `string` | Base directory for file resolution (default: `process.cwd()`) |
70
+ | `extensions` | `string[]` | Extensions to probe (default: `['.ts', '.mjs', '.js', '.json']`) |
71
+ | `script.resolve` | `(module) => C` | Extracts the config value from the imported module |
72
+ | `script.esBuildOptions` | `ImportScriptOptions` | Esbuild overrides forwarded to `importScript` |
58
73
 
59
- // my-config.ts
60
- export default 1;
61
- ```
74
+ ### `importScript<M>(entryPoint, options?)`
62
75
 
63
- ### importScript
76
+ Bundles a TypeScript or JavaScript file with esbuild (ESM, external packages) and dynamically imports the output. Two built-in esbuild plugins are included automatically:
64
77
 
65
- Method for loading a script module. This function will use `EsBuild` under the hood, which is useful for loading external script within runtime of other script since it will transpile within the context of the target script.
78
+ - **`importMetaResolvePlugin`** resolves `import.meta.resolve()` calls for relative paths at build time.
79
+ - **`rawMarkdownPlugin`** — supports `?raw` imports for `.md` / `.mdx` files.
66
80
 
67
- #### EsBuild
81
+ ```typescript
82
+ import { importScript } from '@equinor/fusion-imports';
68
83
 
69
- `ImportScriptOptions` is a restricted set of `EsBuild.BuildOptions` which means that you could provide options for compiling the import content.
84
+ interface Greeting { greet(name: string): string }
85
+ const mod = await importScript<Greeting>('./greet.ts');
86
+ console.log(mod.greet('World'));
87
+ ```
70
88
 
71
- **plugins**
89
+ #### Esbuild customisation
72
90
 
73
- our implementation does not use any plugins, but working with mono-repos you most likely need to add plugins like:
74
- - [esbuild-plugin-alias](https://www.npmjs.com/package/esbuild-plugin-alias)
75
- - [esbuild-plugin-tsc](https://www.npmjs.com/package/esbuild-plugin-tsc)
76
- - [...more](https://github.com/esbuild/community-plugins)
91
+ `ImportScriptOptions` inherits all `esbuild.BuildOptions` except `entryPoints`, `bundle`, and `format`. You can pass custom plugins, define aliases, or change the output directory:
77
92
 
93
+ ```typescript
94
+ import { importScript } from '@equinor/fusion-imports';
95
+ import alias from 'esbuild-plugin-alias';
78
96
 
79
- ### importJSON
97
+ const mod = await importScript('./entry.ts', {
98
+ plugins: [alias({ '@app': './src' })],
99
+ outfile: '/tmp/bundle.js',
100
+ });
101
+ ```
80
102
 
81
- Method for loading json from disk. Will read content and parse.
103
+ ### `importJSON<T>(filePath, encoding?)`
82
104
 
83
- ### Plugins
105
+ Reads a JSON file from disk and returns the parsed content. Throws with the original error as `cause` if the file is unreadable or contains invalid JSON.
106
+
107
+ ```typescript
108
+ import { importJSON } from '@equinor/fusion-imports';
84
109
 
85
- The `importScript` function includes built-in plugins to handle common import scenarios:
110
+ interface Manifest { name: string; version: string }
111
+ const manifest = await importJSON<Manifest>('./package.json');
112
+ ```
86
113
 
87
- #### createMarkdownRawPlugin
114
+ ### `resolveConfigFile(baseName, options?)`
88
115
 
89
- Creates an esbuild plugin that handles `?raw` imports for markdown files. This plugin is automatically included when using `importScript`, but can also be used independently.
116
+ Returns the absolute path of the first accessible configuration file matching the given basename and extension list. Useful when you need the path without importing the file.
90
117
 
91
- ```ts
92
- import { importScript, createMarkdownRawPlugin } from '@equinor/fusion-imports';
118
+ ```typescript
119
+ import { resolveConfigFile } from '@equinor/fusion-imports';
93
120
 
94
- // The plugin is automatically included, but you can also add it explicitly
95
- const module = await importScript('./my-script.ts', {
96
- plugins: [createMarkdownRawPlugin()],
121
+ const configPath = await resolveConfigFile('app.config', {
122
+ baseDir: '/project',
123
+ extensions: ['.ts', '.json'],
97
124
  });
98
125
  ```
99
126
 
100
- **Usage in imported files:**
127
+ ### Plugins
101
128
 
102
- ```ts
103
- // my-script.ts
104
- import readmeContent from '../../README.md?raw';
129
+ #### `rawMarkdownPlugin(options?)`
105
130
 
106
- export default readmeContent; // Returns the raw markdown content as a string
131
+ Esbuild plugin that intercepts `?raw` imports for markdown files and returns
132
+ their content as a default-exported string. Included automatically by `importScript`.
133
+
134
+ ```typescript
135
+ // In a file bundled by importScript:
136
+ import readme from '../../README.md?raw';
137
+ console.log(readme); // raw markdown string
107
138
  ```
108
139
 
109
- The plugin:
110
- - Intercepts imports ending with `?raw`
111
- - Resolves paths relative to the importing file (handles `../` correctly)
112
- - Reads the file content and exports it as a default string export
140
+ #### `createImportMetaResolvePlugin()`
141
+
142
+ Esbuild plugin that replaces `import.meta.resolve('./relative')` calls with
143
+ resolved `file://` URLs at build time. Included automatically by `importScript`.
144
+
145
+ ### Error handling
146
+
147
+ The package provides two error classes for filesystem failures:
113
148
 
114
- #### createImportMetaResolvePlugin
149
+ | Error | Thrown when |
150
+ |---|---|
151
+ | `FileNotFoundError` | File does not exist (`ENOENT`) |
152
+ | `FileNotAccessibleError` | File exists but cannot be read (`EACCES`, `EISDIR`) |
115
153
 
116
- Creates an esbuild plugin that handles `import.meta.resolve()` calls. This plugin is automatically included when using `importScript`.
154
+ Both extend `Error` and attach the original Node.js error as `cause`:
155
+
156
+ ```typescript
157
+ import { importConfig, FileNotFoundError } from '@equinor/fusion-imports';
158
+
159
+ try {
160
+ await importConfig('missing');
161
+ } catch (error) {
162
+ if (error instanceof FileNotFoundError) {
163
+ console.error('Config not found:', error.message);
164
+ }
165
+ }
166
+ ```
package/dist/esm/error.js CHANGED
@@ -1,10 +1,20 @@
1
1
  /**
2
- * Represents an error that is thrown when a specified file cannot be found.
2
+ * Error thrown when a file cannot be found on disk (e.g. ENOENT).
3
3
  *
4
- * @extends {Error}
4
+ * Thrown by {@link processAccessError} when the underlying `fs` error code is
5
+ * `ENOENT`. Callers can use `instanceof FileNotFoundError` to distinguish
6
+ * "missing" from "permission denied" failures.
5
7
  *
6
- * @param message - A descriptive message providing details about the missing file.
7
- * @param options - Optional error options that can be used to provide additional context.
8
+ * @example
9
+ * ```typescript
10
+ * try {
11
+ * await resolveConfigFile('app.config');
12
+ * } catch (error) {
13
+ * if (error instanceof FileNotFoundError) {
14
+ * console.error('Config file does not exist');
15
+ * }
16
+ * }
17
+ * ```
8
18
  */
9
19
  export class FileNotFoundError extends Error {
10
20
  constructor(message, options) {
@@ -13,11 +23,11 @@ export class FileNotFoundError extends Error {
13
23
  }
14
24
  }
15
25
  /**
16
- * Represents an error that occurs when a file is not accessible.
26
+ * Error thrown when a file exists but cannot be accessed (e.g. EACCES or EISDIR).
17
27
  *
18
- * @extends Error
19
- * @param message - A descriptive message providing details about the error.
20
- * @param options - Optional error options that can include additional metadata or cause information.
28
+ * Thrown by {@link processAccessError} for permission or path-type errors.
29
+ * Callers can use `instanceof FileNotAccessibleError` to distinguish
30
+ * access failures from missing-file failures.
21
31
  */
22
32
  export class FileNotAccessibleError extends Error {
23
33
  constructor(message, options) {
@@ -26,14 +36,12 @@ export class FileNotAccessibleError extends Error {
26
36
  }
27
37
  }
28
38
  /**
29
- * Processes an access error and throws a specific error based on the error code.
39
+ * Converts a raw `fs` access error into a typed {@link FileNotFoundError},
40
+ * {@link FileNotAccessibleError}, or generic `Error` based on the errno code.
30
41
  *
31
- * @param error - The error object to process, typically of type `unknown`.
32
- * @param path - The file path associated with the error.
33
- *
34
- * @throws FileNotFoundError - If the error code is `ENOENT`, indicating the file was not found.
35
- * @throws FileNotAccessibleError - If the error code is `EACCES`, indicating the file is not accessible.
36
- * @throws Error - For any other error codes, indicating an unknown error occurred.
42
+ * @param error - The caught error, typically a `NodeJS.ErrnoException`.
43
+ * @param path - Absolute or relative file path that triggered the error.
44
+ * @returns A typed error instance with the original error set as `cause`.
37
45
  */
38
46
  export const processAccessError = (error, path) => {
39
47
  switch (error.code) {
@@ -1 +1 @@
1
- {"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YAAY,OAAe,EAAE,OAAsB;QACjD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C,YAAY,OAAe,EAAE,OAAsB;QACjD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAc,EAAE,IAAY,EAAS,EAAE;IACxE,QAAS,KAA+B,CAAC,IAAI,EAAE,CAAC;QAC9C,KAAK,QAAQ;YACX,OAAO,IAAI,iBAAiB,CAAC,mBAAmB,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5E,KAAK,QAAQ;YACX,OAAO,IAAI,sBAAsB,CAAC,wBAAwB,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QACtF,KAAK,QAAQ;YACX,OAAO,IAAI,sBAAsB,CAAC,wBAAwB,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QACtF;YACE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 -3,20 +3,31 @@ import { importScript } from './import-script.js';
3
3
  import { importJSON } from './import-json.js';
4
4
  import { resolveConfigFile } from './resolve-config-file.js';
5
5
  /**
6
- * Imports a configuration file based on the provided basename and options.
6
+ * Loads a configuration file by basename, automatically resolving the file
7
+ * extension (`.ts` → `.mjs` → `.js` → `.json`) and using the appropriate
8
+ * import strategy.
7
9
  *
8
- * @template C - The type of the configuration content.
9
- * @template M - The type of the ES module, defaults to `EsmModule`.
10
+ * - **JSON files** are read and parsed with {@link importJSON}.
11
+ * - **Script files** (TypeScript / JavaScript) are bundled with esbuild via
12
+ * {@link importScript} and the module's `default` export is returned unless
13
+ * a custom `script.resolve` callback is provided.
10
14
  *
11
- * @param {string} basename - The base name of the configuration file to import.
12
- * @param {ImportConfigOptions<C, M>} [options={}] - Optional parameters for importing the configuration.
13
- * @param {string} [options.baseDir] - The base directory to resolve the configuration file from.
14
- * @param {string[]} [options.extensions] - The list of file extensions to consider when resolving the configuration file.
15
- * @param {object} [options.script] - Additional script options for importing non-JSON files.
15
+ * @template C - The configuration value type.
16
+ * @template M - The shape of the ES module when importing a script file.
16
17
  *
17
- * @returns {Promise<C>} A promise that resolves to the imported configuration content.
18
+ * @param basename - File basename (without extension), or an array of
19
+ * basenames to try in order.
20
+ * @param options - Import behaviour overrides (base directory, extensions,
21
+ * script resolver, esbuild options).
22
+ * @returns An {@link ImportConfigResult} containing the resolved path,
23
+ * extension, and loaded configuration value.
24
+ * @throws {FileNotFoundError} When no matching file is found for any basename.
25
+ * @throws {Error} When esbuild bundling or JSON parsing fails.
18
26
  *
19
- * @throws Will throw an error if the configuration file cannot be resolved or imported.
27
+ * @example
28
+ * ```typescript
29
+ * const { config } = await importConfig<AppSettings>('app.config');
30
+ * ```
20
31
  */
21
32
  export async function importConfig(basename, options = {}) {
22
33
  const { baseDir, extensions, script } = options;
@@ -1 +1 @@
1
- {"version":3,"file":"import-config.js","sourceRoot":"","sources":["../../src/import-config.ts"],"names":[],"mappings":"AAAA,OA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,OAAO,EAAE,YAAY,EAA4C,MAAM,oBAAoB,CAAC;AAC5F,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAoD7D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,QAA2B,EAC3B,UAAqC,EAAE;IAEvC,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAChD,MAAM,QAAQ,GAAG,MAAM,iBAAiB,CAAC,QAAQ,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,CAAC;IAC5E,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEvC,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE;QAC/B,QAAQ,OAAO,EAAE,CAAC;YAChB,KAAK,OAAO;gBACV,OAAO,UAAU,CAAI,QAAQ,CAAC,CAAC;YACjC,OAAO,CAAC,CAAC,CAAC;gBACR,MAAM,OAAO,GAAG,MAAM,EAAE,OAAO,IAAI,CAAC,CAAC,MAAS,EAAE,EAAE,CAAC,MAAM,CAAC,OAAY,CAAC,CAAC;gBACxE,MAAM,MAAM,GAAG,MAAM,YAAY,CAAI,QAAQ,EAAE,MAAM,EAAE,cAAc,CAAC,CAAC;gBACvE,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;IACH,CAAC,CAAC,EAAE,CAAC;IACL,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,SAAS,EAAE,OAAO;QAClB,MAAM;KACP,CAAC;AACJ,CAAC;AAED,eAAe,YAAY,CAAC"}
@@ -1,11 +1,23 @@
1
1
  import { readFile } from 'node:fs/promises';
2
2
  /**
3
- * Asynchronously imports and parses a JSON file.
3
+ * Reads a JSON file from disk and returns the parsed content.
4
4
  *
5
- * @template T - The expected type of the parsed JSON content. It can be a record, string, or number.
6
- * @param {string} filePath - The path to the JSON file to be imported.
7
- * @returns {Promise<T>} A promise that resolves to the parsed JSON content.
8
- * @throws {Error} If the file cannot be read or the content is not valid JSON.
5
+ * Use this when you need to load a plain JSON configuration file without
6
+ * esbuild transpilation. For mixed TypeScript / JSON config resolution,
7
+ * prefer {@link importConfig}.
8
+ *
9
+ * @template T - Expected shape of the parsed JSON. Constrained to valid JSON value types.
10
+ * @param filePath - Absolute or relative path to the `.json` file.
11
+ * @param encoding - Character encoding used when reading the file (default `'utf-8'`).
12
+ * @returns The parsed JSON content cast to `T`.
13
+ * @throws {Error} When the file cannot be read or contains invalid JSON, with
14
+ * the original error attached as `cause`.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * interface AppManifest { name: string; version: string }
19
+ * const manifest = await importJSON<AppManifest>('./app.manifest.json');
20
+ * ```
9
21
  */
10
22
  export const importJSON = async (filePath, encoding = 'utf-8') => {
11
23
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"import-json.js","sourceRoot":"","sources":["../../src/import-json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAI5C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAC7B,QAAgB,EAChB,WAA2B,OAAO,EACtB,EAAE;IACd,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QACnD,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,oCAAoC,QAAQ,GAAG,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACrF,CAAC;AACH,CAAC,CAAC;AAEF,eAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"import-json.js","sourceRoot":"","sources":["../../src/import-json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAK5C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAC7B,QAAgB,EAChB,WAA2B,OAAO,EACtB,EAAE;IACd,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QACnD,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,oCAAoC,QAAQ,GAAG,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACrF,CAAC;AACH,CAAC,CAAC;AAEF,eAAe,UAAU,CAAC"}
@@ -1,10 +1,15 @@
1
1
  import path from 'node:path';
2
2
  import { pathToFileURL } from 'node:url';
3
3
  /**
4
- * Transforms import.meta.resolve() calls in code to resolved file:// URLs
5
- * @param code - The code to transform
6
- * @param baseDir - The base directory for resolving relative paths
7
- * @returns The transformed code
4
+ * Replaces `import.meta.resolve('./relative')` calls in source code with
5
+ * static `file://` URLs resolved against `baseDir`.
6
+ *
7
+ * Only relative specifiers (starting with `./` or `../`) are transformed;
8
+ * bare package specifiers are left untouched.
9
+ *
10
+ * @param code - Source code string to transform.
11
+ * @param baseDir - Directory used to resolve relative specifiers.
12
+ * @returns The transformed source code.
8
13
  */
9
14
  function transformImportMetaResolve(code, baseDir) {
10
15
  return code.replace(/import\.meta\.resolve\((['"`])([^'"`]+)\1\)/g, (match, quote, specifier) => {
@@ -24,7 +29,11 @@ function transformImportMetaResolve(code, baseDir) {
24
29
  });
25
30
  }
26
31
  /**
27
- * Gets the appropriate esbuild loader for a file extension
32
+ * Returns the esbuild loader name matching the file extension, or `undefined`
33
+ * for unsupported extensions.
34
+ *
35
+ * @param filePath - File path to inspect.
36
+ * @returns The loader identifier, or `undefined`.
28
37
  */
29
38
  function getLoader(filePath) {
30
39
  const ext = path.extname(filePath);
@@ -40,7 +49,10 @@ function getLoader(filePath) {
40
49
  return undefined;
41
50
  }
42
51
  /**
43
- * Safely reads a file, returning undefined on error
52
+ * Reads a file and returns its content, or `undefined` if reading fails.
53
+ *
54
+ * @param filePath - Absolute path to read.
55
+ * @returns File content or `undefined` on any I/O error.
44
56
  */
45
57
  async function readFileSafe(filePath) {
46
58
  try {
@@ -52,7 +64,12 @@ async function readFileSafe(filePath) {
52
64
  }
53
65
  }
54
66
  /**
55
- * Collects output files from esbuild result (handles both write: true and write: false)
67
+ * Gathers JavaScript output files from an esbuild `BuildResult`, supporting
68
+ * both in-memory (`write: false`) and on-disk (`write: true` with metafile)
69
+ * modes.
70
+ *
71
+ * @param result - The esbuild build result.
72
+ * @returns Array of `{ path, text }` objects for each `.js` / `.mjs` output.
56
73
  */
57
74
  async function collectOutputFiles(result) {
58
75
  const files = [];
@@ -86,13 +103,31 @@ async function collectOutputFiles(result) {
86
103
  return files;
87
104
  }
88
105
  /**
89
- * Creates an esbuild plugin that transforms `import.meta.resolve()` calls
90
- * to resolved file:// URLs at build time.
106
+ * Creates an esbuild plugin that statically resolves `import.meta.resolve()`
107
+ * calls for relative paths at build time.
108
+ *
109
+ * Esbuild does not handle `import.meta.resolve()` during bundling — it
110
+ * preserves the calls as runtime expressions. This plugin intercepts source
111
+ * files and bundled output to replace relative-path calls with pre-resolved
112
+ * `file://` URLs, ensuring they work correctly in the final bundle.
113
+ *
114
+ * The plugin is included automatically by {@link importScript} but can be
115
+ * added explicitly to a custom esbuild configuration.
116
+ *
117
+ * @returns An esbuild `Plugin` instance.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * import { build } from 'esbuild';
122
+ * import { createImportMetaResolvePlugin } from '@equinor/fusion-imports';
91
123
  *
92
- * This plugin is necessary because esbuild doesn't handle `import.meta.resolve()`
93
- * calls during bundling - it leaves them as runtime calls. For local imports
94
- * (relative paths), we need to resolve them at build time so they work correctly
95
- * in the bundled output.
124
+ * await build({
125
+ * entryPoints: ['src/index.ts'],
126
+ * plugins: [createImportMetaResolvePlugin()],
127
+ * bundle: true,
128
+ * format: 'esm',
129
+ * });
130
+ * ```
96
131
  */
97
132
  export const importMetaResolvePlugin = () => {
98
133
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"import-meta-resolve-plugin.js","sourceRoot":"","sources":["../../src/import-meta-resolve-plugin.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC;;;;;GAKG;AACH,SAAS,0BAA0B,CAAC,IAAY,EAAE,OAAe;IAC/D,OAAO,IAAI,CAAC,OAAO,CACjB,8CAA8C,EAC9C,CAAC,KAAa,EAAE,KAAa,EAAE,SAAiB,EAAE,EAAE;QAClD,IAAI,CAAC;YACH,gEAAgE;YAChE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;gBAChE,OAAO,KAAK,CAAC;YACf,CAAC;YAED,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;YACtD,MAAM,OAAO,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC;YACjD,OAAO,GAAG,KAAK,GAAG,OAAO,GAAG,KAAK,EAAE,CAAC;QACtC,CAAC;QAAC,MAAM,CAAC;YACP,sCAAsC;YACtC,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,SAAS,CAAC,QAAgB;IACjC,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACnC,IAAI,GAAG,KAAK,MAAM,IAAI,GAAG,KAAK,MAAM,EAAE,CAAC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ACxE,CAAC,CAAC,CAAC;YAEH,iCAAiC;YACjC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;gBAC3B,IAAI,CAAC,aAAa,EAAE,CAAC;oBACnB,OAAO;gBACT,CAAC;gBAED,MAAM,KAAK,GAAG,MAAM,kBAAkB,CAAC,MAAM,CAAC,CAAC;gBAC/C,MAAM,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;gBAE5C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;oBACzB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,qBAAqB,CAAC,EAAE,CAAC;wBAC/C,SAAS;oBACX,CAAC;oBAED,MAAM,eAAe,GAAG,0BAA0B,CAAC,IAAI,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;oBAC7E,IAAI,eAAe,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC;wBAClC,IAAI,CAAC;4BACH,MAAM,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,CAAC;wBAC1D,CAAC;wBAAC,OAAO,KAAK,EAAE,CAAC;4BACf,OAAO,CAAC,IAAI,CAAC,yCAAyC,IAAI,CAAC,IAAI,GAAG,EAAE,KAAK,CAAC,CAAC;wBAC7E,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
@@ -7,22 +7,30 @@ import { readPackageUp } from 'read-package-up';
7
7
  import { importMetaResolvePlugin } from './import-meta-resolve-plugin.js';
8
8
  import { rawMarkdownPlugin } from './markdown-plugin.js';
9
9
  /**
10
- * Asynchronously imports a script file using esbuild to bundle it.
10
+ * Bundles a TypeScript or JavaScript file with esbuild and dynamically imports
11
+ * the resulting ESM module.
11
12
  *
12
- * @template M - The type of the module to be imported.
13
- * @param {string} entryPoint - The path to the script file to be imported.
14
- * @param {ImportScriptOptions} [options] - Optional build options to customize the bundling process.
15
- * @returns {Promise<M>} A promise that resolves to the imported module.
16
- * @throws {Error} Throws an error if the bundling process fails or no output files are generated.
13
+ * The function resolves the entry point, bundles it in ESM format with all
14
+ * npm packages marked as external, and loads the output via `import()`. Two
15
+ * built-in esbuild plugins are included automatically:
16
+ *
17
+ * - {@link createImportMetaResolvePlugin | importMetaResolvePlugin} resolves
18
+ * `import.meta.resolve()` calls for relative paths at build time.
19
+ * - {@link rawMarkdownPlugin} — supports `?raw` imports for markdown files.
20
+ *
21
+ * @template M - Module shape returned by the dynamic import.
22
+ * @param entryPoint - Path to the script file to bundle and import.
23
+ * @param options - Optional esbuild build options (see {@link ImportScriptOptions}).
24
+ * @returns The imported module.
25
+ * @throws {FileNotFoundError} When the entry point does not exist.
26
+ * @throws {FileNotAccessibleError} When the entry point cannot be read.
27
+ * @throws {Error} When esbuild bundling fails or produces no output.
17
28
  *
18
29
  * @example
19
30
  * ```typescript
20
- * import { importScript } from './import-script';
21
- *
22
- * (async () => {
23
- * const myModule = await importScript<MyModuleType>('./path/to/my-module.ts');
24
- * // Use myModule here
25
- * })();
31
+ * interface MyModule { greet(name: string): string }
32
+ * const mod = await importScript<MyModule>('./greet.ts');
33
+ * console.log(mod.greet('World'));
26
34
  * ```
27
35
  */
28
36
  export const importScript = async (entryPoint, options) => {
@@ -1 +1 @@
1
- {"version":3,"file":"import-script.js","sourceRoot":"","sources":["../../src/import-script.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqB,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,uBAAu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m/index.js CHANGED
@@ -1,3 +1,18 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Utilities for importing and transpiling TypeScript, JavaScript, and JSON
5
+ * configuration files at Node.js runtime using esbuild.
6
+ *
7
+ * Use {@link importConfig} to load a configuration file by basename,
8
+ * {@link importScript} to bundle and import a single script module, or
9
+ * {@link importJSON} to read and parse a JSON file from disk.
10
+ *
11
+ * @remarks
12
+ * This package is designed for **file-based** imports, not URL-based.
13
+ * It bundles the target with esbuild (ESM, external packages) and
14
+ * loads the result via dynamic `import()`.
15
+ */
1
16
  export { importScript } from './import-script.js';
2
17
  export { importJSON } from './import-json.js';
3
18
  export { default, importConfig, } from './import-config.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EACL,OAAO,EACP,YAAY,GAGb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,uBAAuB,IAAI,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAC3G,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAEzD,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,YAAY,EAA4C,MAAM,oBAAoB,CAAC;AAC5F,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EACL,OAAO,EACP,YAAY,GAGb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,iBAAiB,EAAiC,MAAM,0BAA0B,CAAC;AAC5F,OAAO,EAAE,uBAAuB,IAAI,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAC3G,OAAO,EAAE,iBAAiB,EAAiC,MAAM,sBAAsB,CAAC;AAExF,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC"}