@flex-development/mlly 1.0.0-alpha.1 → 1.0.0-alpha.11
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.
- package/CHANGELOG.md +431 -3
- package/README.md +6 -4
- package/changelog.config.ts +404 -0
- package/dist/enums/assert-type.d.mts +21 -0
- package/dist/enums/assert-type.mjs +10 -0
- package/dist/enums/assert-type.mjs.map +6 -0
- package/dist/enums/format.d.mts +17 -0
- package/dist/enums/format.mjs +13 -0
- package/dist/enums/format.mjs.map +6 -0
- package/dist/enums/index.d.mts +10 -0
- package/dist/enums/index.mjs +15 -0
- package/dist/enums/index.mjs.map +6 -0
- package/dist/enums/kind-specifier-syntax.d.mts +14 -0
- package/dist/enums/kind-specifier-syntax.mjs +10 -0
- package/dist/enums/kind-specifier-syntax.mjs.map +6 -0
- package/dist/enums/kind-specifier.d.mts +17 -0
- package/dist/enums/kind-specifier.mjs +11 -0
- package/dist/enums/kind-specifier.mjs.map +6 -0
- package/dist/enums/kind-statement-syntax.d.mts +22 -0
- package/dist/enums/kind-statement-syntax.mjs +18 -0
- package/dist/enums/kind-statement-syntax.mjs.map +6 -0
- package/dist/enums/kind-statement.d.mts +15 -0
- package/dist/enums/kind-statement.mjs +11 -0
- package/dist/enums/kind-statement.mjs.map +6 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +3 -2
- package/dist/index.mjs.map +6 -0
- package/dist/interfaces/import-assertions.d.mts +20 -0
- package/dist/interfaces/import-dynamic.d.mts +26 -6
- package/dist/interfaces/import-static.d.mts +22 -4
- package/dist/interfaces/index.d.mts +12 -0
- package/dist/interfaces/index.mjs +1 -0
- package/dist/interfaces/index.mjs.map +6 -0
- package/dist/interfaces/options-fill-module.d.mts +23 -0
- package/dist/interfaces/options-find-subpath.d.mts +49 -0
- package/dist/interfaces/options-get-format.d.mts +83 -0
- package/dist/interfaces/options-get-source.d.mts +63 -0
- package/dist/interfaces/options-parse-module-id.d.mts +33 -0
- package/dist/interfaces/options-parse-subpath.d.mts +31 -0
- package/dist/interfaces/options-resolve-alias.d.mts +26 -42
- package/dist/interfaces/options-resolve-module.d.mts +64 -0
- package/dist/interfaces/options-resolve.d.mts +7 -52
- package/dist/interfaces/package-scope.d.mts +25 -0
- package/dist/interfaces/parsed-data-url.d.mts +56 -0
- package/dist/interfaces/parsed-module-id.d.mts +80 -0
- package/dist/interfaces/parsed-subpath.d.mts +40 -0
- package/dist/interfaces/statement-export.d.mts +22 -9
- package/dist/interfaces/statement-import.d.mts +15 -4
- package/dist/interfaces/statement-require.d.mts +15 -4
- package/dist/interfaces/statement.d.mts +24 -8
- package/dist/internal/escape-reg-exp.d.mts +16 -0
- package/dist/internal/escape-reg-exp.mjs +10 -0
- package/dist/internal/escape-reg-exp.mjs.map +6 -0
- package/dist/internal/format-type-map.d.mts +17 -0
- package/dist/internal/format-type-map.mjs +13 -0
- package/dist/internal/format-type-map.mjs.map +6 -0
- package/dist/internal/get-specifier-kind.d.mts +17 -0
- package/dist/internal/get-specifier-kind.mjs +14 -0
- package/dist/internal/get-specifier-kind.mjs.map +6 -0
- package/dist/internal/get-subpaths.d.mts +25 -0
- package/dist/internal/get-subpaths.mjs +10 -0
- package/dist/internal/get-subpaths.mjs.map +6 -0
- package/dist/internal/is-array-index.d.mts +15 -0
- package/dist/internal/is-array-index.mjs +9 -0
- package/dist/internal/is-array-index.mjs.map +6 -0
- package/dist/internal/is-directory.d.mts +13 -0
- package/dist/internal/is-directory.mjs +16 -0
- package/dist/internal/is-directory.mjs.map +6 -0
- package/dist/internal/is-file.d.mts +13 -0
- package/dist/internal/is-file.mjs +16 -0
- package/dist/internal/is-file.mjs.map +6 -0
- package/dist/internal/is-function.d.mts +12 -0
- package/dist/internal/is-function.mjs +8 -0
- package/dist/internal/is-function.mjs.map +6 -0
- package/dist/internal/regex-encoded-sep.d.mts +13 -0
- package/dist/internal/regex-encoded-sep.mjs +6 -0
- package/dist/internal/regex-encoded-sep.mjs.map +6 -0
- package/dist/internal/regex-internal-specifier.d.mts +14 -0
- package/dist/internal/regex-internal-specifier.mjs +6 -0
- package/dist/internal/regex-internal-specifier.mjs.map +6 -0
- package/dist/internal/regex-package-name.d.mts +13 -0
- package/dist/internal/regex-package-name.mjs +6 -0
- package/dist/internal/regex-package-name.mjs.map +6 -0
- package/dist/internal/regex-package-path.d.mts +15 -0
- package/dist/internal/regex-package-path.mjs +6 -0
- package/dist/internal/regex-package-path.mjs.map +6 -0
- package/dist/internal/resolver.d.mts +130 -0
- package/dist/internal/resolver.mjs +500 -0
- package/dist/internal/resolver.mjs.map +6 -0
- package/dist/internal/validate-boolean.d.mts +20 -0
- package/dist/internal/validate-boolean.mjs +11 -0
- package/dist/internal/validate-boolean.mjs.map +6 -0
- package/dist/internal/validate-object.d.mts +23 -0
- package/dist/internal/validate-object.mjs +11 -0
- package/dist/internal/validate-object.mjs.map +6 -0
- package/dist/internal/validate-set.d.mts +23 -0
- package/dist/internal/validate-set.mjs +11 -0
- package/dist/internal/validate-set.mjs.map +6 -0
- package/dist/internal/validate-string.d.mts +20 -0
- package/dist/internal/validate-string.mjs +11 -0
- package/dist/internal/validate-string.mjs.map +6 -0
- package/dist/internal/validate-url-string.d.mts +22 -0
- package/dist/internal/validate-url-string.mjs +12 -0
- package/dist/internal/validate-url-string.mjs.map +6 -0
- package/dist/types/declaration.d.mts +1 -1
- package/dist/types/fn-change-ext.d.mts +28 -0
- package/dist/types/index.d.mts +8 -4
- package/dist/types/index.mjs +1 -0
- package/dist/types/index.mjs.map +6 -0
- package/dist/types/mime-type.d.mts +7 -5
- package/dist/types/module-id.d.mts +13 -0
- package/dist/types/module-specifier-type.d.mts +11 -0
- package/dist/types/protocol.d.mts +14 -0
- package/dist/types/syntax-kind-export.d.mts +12 -0
- package/dist/types/syntax-kind-import.d.mts +12 -0
- package/dist/types/syntax-kind-require.d.mts +12 -0
- package/dist/utils/compare-subpaths.d.mts +25 -0
- package/dist/utils/compare-subpaths.mjs +24 -0
- package/dist/utils/compare-subpaths.mjs.map +6 -0
- package/dist/utils/conditions.d.mts +13 -0
- package/dist/utils/conditions.mjs +6 -0
- package/dist/utils/conditions.mjs.map +6 -0
- package/dist/{lib → utils}/detect-syntax.d.mts +4 -4
- package/dist/{lib → utils}/detect-syntax.mjs +1 -0
- package/dist/utils/detect-syntax.mjs.map +6 -0
- package/dist/utils/extension-format-map.d.mts +16 -0
- package/dist/utils/extension-format-map.mjs +22 -0
- package/dist/utils/extension-format-map.mjs.map +6 -0
- package/dist/utils/extract-statements.d.mts +21 -0
- package/dist/{lib → utils}/extract-statements.mjs +4 -1
- package/dist/utils/extract-statements.mjs.map +6 -0
- package/dist/utils/fill-modules.d.mts +23 -0
- package/dist/utils/fill-modules.mjs +64 -0
- package/dist/utils/fill-modules.mjs.map +6 -0
- package/dist/utils/find-dynamic-imports.d.mts +18 -0
- package/dist/utils/find-dynamic-imports.mjs +39 -0
- package/dist/utils/find-dynamic-imports.mjs.map +6 -0
- package/dist/utils/find-exports.d.mts +21 -0
- package/dist/utils/find-exports.mjs +95 -0
- package/dist/utils/find-exports.mjs.map +6 -0
- package/dist/utils/find-requires.d.mts +18 -0
- package/dist/utils/find-requires.mjs +32 -0
- package/dist/utils/find-requires.mjs.map +6 -0
- package/dist/utils/find-static-imports.d.mts +18 -0
- package/dist/utils/find-static-imports.mjs +34 -0
- package/dist/utils/find-static-imports.mjs.map +6 -0
- package/dist/utils/find-subpath.d.mts +30 -0
- package/dist/utils/find-subpath.mjs +106 -0
- package/dist/utils/find-subpath.mjs.map +6 -0
- package/dist/utils/get-format.d.mts +24 -0
- package/dist/utils/get-format.mjs +111 -0
- package/dist/utils/get-format.mjs.map +6 -0
- package/dist/utils/get-source.d.mts +22 -0
- package/dist/utils/get-source.mjs +63 -0
- package/dist/utils/get-source.mjs.map +6 -0
- package/dist/{lib → utils}/has-cjs-syntax.d.mts +5 -2
- package/dist/utils/has-cjs-syntax.mjs +11 -0
- package/dist/utils/has-cjs-syntax.mjs.map +6 -0
- package/dist/{lib → utils}/has-esm-syntax.d.mts +5 -2
- package/dist/utils/has-esm-syntax.mjs +11 -0
- package/dist/utils/has-esm-syntax.mjs.map +6 -0
- package/dist/utils/index.d.mts +42 -0
- package/dist/utils/index.mjs +79 -0
- package/dist/utils/index.mjs.map +6 -0
- package/dist/utils/is-absolute-specifier.d.mts +19 -0
- package/dist/utils/is-absolute-specifier.mjs +25 -0
- package/dist/utils/is-absolute-specifier.mjs.map +6 -0
- package/dist/utils/is-bare-specifier.d.mts +19 -0
- package/dist/utils/is-bare-specifier.mjs +10 -0
- package/dist/utils/is-bare-specifier.mjs.map +6 -0
- package/dist/utils/is-exports-sugar.d.mts +25 -0
- package/dist/utils/is-exports-sugar.mjs +26 -0
- package/dist/utils/is-exports-sugar.mjs.map +6 -0
- package/dist/utils/is-relative-specifier.d.mts +20 -0
- package/dist/utils/is-relative-specifier.mjs +11 -0
- package/dist/utils/is-relative-specifier.mjs.map +6 -0
- package/dist/utils/lookup-package-scope.d.mts +26 -0
- package/dist/utils/lookup-package-scope.mjs +38 -0
- package/dist/utils/lookup-package-scope.mjs.map +6 -0
- package/dist/utils/parse-data-url.d.mts +25 -0
- package/dist/utils/parse-data-url.mjs +25 -0
- package/dist/utils/parse-data-url.mjs.map +6 -0
- package/dist/utils/parse-module-id.d.mts +22 -0
- package/dist/utils/parse-module-id.mjs +122 -0
- package/dist/utils/parse-module-id.mjs.map +6 -0
- package/dist/utils/parse-subpath.d.mts +35 -0
- package/dist/utils/parse-subpath.mjs +54 -0
- package/dist/utils/parse-subpath.mjs.map +6 -0
- package/dist/utils/pattern-character.d.mts +11 -0
- package/dist/utils/pattern-character.mjs +6 -0
- package/dist/utils/pattern-character.mjs.map +6 -0
- package/dist/utils/read-package-json.d.mts +26 -0
- package/dist/utils/read-package-json.mjs +43 -0
- package/dist/utils/read-package-json.mjs.map +6 -0
- package/dist/utils/resolve-alias.d.mts +20 -0
- package/dist/utils/resolve-alias.mjs +106 -0
- package/dist/utils/resolve-alias.mjs.map +6 -0
- package/dist/utils/resolve-aliases.d.mts +19 -0
- package/dist/utils/resolve-aliases.mjs +28 -0
- package/dist/utils/resolve-aliases.mjs.map +6 -0
- package/dist/utils/resolve-extensions.d.mts +14 -0
- package/dist/utils/resolve-extensions.mjs +22 -0
- package/dist/utils/resolve-extensions.mjs.map +6 -0
- package/dist/utils/resolve-module.d.mts +39 -0
- package/dist/utils/resolve-module.mjs +64 -0
- package/dist/utils/resolve-module.mjs.map +6 -0
- package/dist/utils/resolve-modules.d.mts +25 -0
- package/dist/{lib → utils}/resolve-modules.mjs +7 -10
- package/dist/utils/resolve-modules.mjs.map +6 -0
- package/dist/{lib → utils}/to-absolute-specifier.d.mts +6 -6
- package/dist/utils/to-absolute-specifier.mjs +8 -0
- package/dist/utils/to-absolute-specifier.mjs.map +6 -0
- package/dist/utils/to-bare-specifier.d.mts +42 -0
- package/dist/utils/to-bare-specifier.mjs +90 -0
- package/dist/utils/to-bare-specifier.mjs.map +6 -0
- package/dist/{lib → utils}/to-data-url.d.mts +6 -5
- package/dist/{lib → utils}/to-data-url.mjs +4 -0
- package/dist/utils/to-data-url.mjs.map +6 -0
- package/dist/utils/to-node-url.d.mts +19 -0
- package/dist/utils/to-node-url.mjs +10 -0
- package/dist/utils/to-node-url.mjs.map +6 -0
- package/dist/{lib → utils}/to-relative-specifier.d.mts +6 -6
- package/dist/{lib → utils}/to-relative-specifier.mjs +7 -3
- package/dist/utils/to-relative-specifier.mjs.map +6 -0
- package/dist/utils/to-url.d.mts +22 -0
- package/dist/utils/to-url.mjs +12 -0
- package/dist/utils/to-url.mjs.map +6 -0
- package/dist/utils/validate-assertions.d.mts +23 -0
- package/dist/utils/validate-assertions.mjs +52 -0
- package/dist/utils/validate-assertions.mjs.map +6 -0
- package/dist/utils/validate-exports.d.mts +24 -0
- package/dist/utils/validate-exports.mjs +72 -0
- package/dist/utils/validate-exports.mjs.map +6 -0
- package/package.json +106 -83
- package/src/enums/assert-type.ts +23 -0
- package/src/enums/format.ts +19 -0
- package/src/enums/index.ts +11 -0
- package/src/enums/kind-specifier-syntax.ts +16 -0
- package/src/enums/kind-specifier.ts +19 -0
- package/src/enums/kind-statement-syntax.ts +24 -0
- package/src/enums/kind-statement.ts +17 -0
- package/src/index.ts +9 -0
- package/src/interfaces/import-assertions.ts +24 -0
- package/src/interfaces/import-dynamic.ts +48 -0
- package/src/interfaces/import-static.ts +50 -0
- package/src/interfaces/index.ts +25 -0
- package/src/interfaces/options-fill-module.ts +26 -0
- package/src/interfaces/options-find-subpath.ts +56 -0
- package/src/interfaces/options-get-format.ts +92 -0
- package/src/interfaces/options-get-source.ts +70 -0
- package/src/interfaces/options-parse-module-id.ts +38 -0
- package/src/interfaces/options-parse-subpath.ts +36 -0
- package/src/interfaces/options-resolve-alias.ts +59 -0
- package/src/interfaces/options-resolve-module.ts +72 -0
- package/src/interfaces/options-resolve.ts +17 -0
- package/src/interfaces/package-scope.ts +30 -0
- package/src/interfaces/parsed-data-url.ts +63 -0
- package/src/interfaces/parsed-module-id.ts +90 -0
- package/src/interfaces/parsed-subpath.ts +47 -0
- package/src/interfaces/statement-export.ts +52 -0
- package/src/interfaces/statement-import.ts +45 -0
- package/src/interfaces/statement-require.ts +45 -0
- package/src/interfaces/statement.ts +64 -0
- package/src/internal/escape-reg-exp.ts +24 -0
- package/src/internal/format-type-map.ts +26 -0
- package/src/internal/get-specifier-kind.ts +38 -0
- package/src/internal/get-subpaths.ts +41 -0
- package/src/internal/is-array-index.ts +28 -0
- package/src/internal/is-directory.ts +25 -0
- package/src/internal/is-file.ts +25 -0
- package/src/internal/is-function.ts +16 -0
- package/src/internal/regex-encoded-sep.ts +15 -0
- package/src/internal/regex-internal-specifier.ts +17 -0
- package/src/internal/regex-package-name.ts +16 -0
- package/src/internal/regex-package-path.ts +18 -0
- package/src/internal/resolver.ts +783 -0
- package/src/internal/validate-boolean.ts +27 -0
- package/src/internal/validate-object.ts +30 -0
- package/src/internal/validate-set.ts +33 -0
- package/src/internal/validate-string.ts +27 -0
- package/src/internal/validate-url-string.ts +31 -0
- package/src/types/declaration.ts +22 -0
- package/src/types/fn-change-ext.ts +33 -0
- package/src/types/index.ts +14 -0
- package/src/types/mime-type.ts +19 -0
- package/src/types/module-id.ts +15 -0
- package/src/types/module-specifier-type.ts +13 -0
- package/src/types/protocol.ts +40 -0
- package/src/types/syntax-kind-export.ts +20 -0
- package/src/types/syntax-kind-import.ts +22 -0
- package/src/types/syntax-kind-require.ts +15 -0
- package/src/utils/compare-subpaths.ts +100 -0
- package/src/utils/conditions.ts +15 -0
- package/src/utils/detect-syntax.ts +40 -0
- package/src/utils/extension-format-map.ts +34 -0
- package/src/utils/extract-statements.ts +42 -0
- package/src/utils/fill-modules.ts +116 -0
- package/src/utils/find-dynamic-imports.ts +75 -0
- package/src/utils/find-exports.ts +164 -0
- package/src/utils/find-requires.ts +75 -0
- package/src/utils/find-static-imports.ts +89 -0
- package/src/utils/find-subpath.ts +241 -0
- package/src/utils/get-format.ts +222 -0
- package/src/utils/get-source.ts +129 -0
- package/src/utils/has-cjs-syntax.ts +44 -0
- package/src/utils/has-esm-syntax.ts +40 -0
- package/src/utils/index.ts +43 -0
- package/src/utils/is-absolute-specifier.ts +48 -0
- package/src/utils/is-bare-specifier.ts +30 -0
- package/src/utils/is-exports-sugar.ts +74 -0
- package/src/utils/is-relative-specifier.ts +34 -0
- package/src/utils/lookup-package-scope.ts +97 -0
- package/src/utils/parse-data-url.ts +71 -0
- package/src/utils/parse-module-id.ts +186 -0
- package/src/utils/parse-subpath.ts +173 -0
- package/src/utils/pattern-character.ts +13 -0
- package/src/utils/read-package-json.ts +101 -0
- package/src/utils/resolve-alias.ts +219 -0
- package/src/utils/resolve-aliases.ts +58 -0
- package/src/utils/resolve-extensions.ts +33 -0
- package/src/utils/resolve-module.ts +153 -0
- package/src/utils/resolve-modules.ts +62 -0
- package/src/utils/to-absolute-specifier.ts +30 -0
- package/src/utils/to-bare-specifier.ts +201 -0
- package/src/utils/to-data-url.ts +42 -0
- package/src/utils/to-node-url.ts +27 -0
- package/src/utils/to-relative-specifier.ts +47 -0
- package/src/utils/to-url.ts +31 -0
- package/src/utils/validate-assertions.ts +119 -0
- package/src/utils/validate-exports.ts +124 -0
- package/changelog.config.cts +0 -204
- package/dist/constants.d.mts +0 -19
- package/dist/constants.mjs +0 -24
- package/dist/interfaces/import-dynamic.mjs +0 -0
- package/dist/interfaces/import-static.mjs +0 -0
- package/dist/interfaces/options-resolve-alias.mjs +0 -0
- package/dist/interfaces/options-resolve.mjs +0 -0
- package/dist/interfaces/statement-export.mjs +0 -0
- package/dist/interfaces/statement-import.mjs +0 -0
- package/dist/interfaces/statement-require.mjs +0 -0
- package/dist/interfaces/statement.mjs +0 -0
- package/dist/internal/compiler-options-json.d.mts +0 -111
- package/dist/internal/compiler-options-json.mjs +0 -0
- package/dist/internal/constants.d.mts +0 -70
- package/dist/internal/constants.mjs +0 -18
- package/dist/internal/get-compiler-options.d.mts +0 -21
- package/dist/internal/get-compiler-options.mjs +0 -14
- package/dist/internal/index.d.mts +0 -7
- package/dist/internal/index.mjs +0 -5
- package/dist/lib/extract-statements.d.mts +0 -21
- package/dist/lib/find-dynamic-imports.d.mts +0 -15
- package/dist/lib/find-dynamic-imports.mjs +0 -20
- package/dist/lib/find-exports.d.mts +0 -15
- package/dist/lib/find-exports.mjs +0 -53
- package/dist/lib/find-requires.d.mts +0 -17
- package/dist/lib/find-requires.mjs +0 -19
- package/dist/lib/find-static-imports.d.mts +0 -15
- package/dist/lib/find-static-imports.mjs +0 -20
- package/dist/lib/has-cjs-syntax.mjs +0 -6
- package/dist/lib/has-esm-syntax.mjs +0 -6
- package/dist/lib/index.d.mts +0 -20
- package/dist/lib/index.mjs +0 -34
- package/dist/lib/resolve-alias.d.mts +0 -20
- package/dist/lib/resolve-alias.mjs +0 -41
- package/dist/lib/resolve-aliases.d.mts +0 -17
- package/dist/lib/resolve-aliases.mjs +0 -34
- package/dist/lib/resolve-module.d.mts +0 -31
- package/dist/lib/resolve-module.mjs +0 -74
- package/dist/lib/resolve-modules.d.mts +0 -19
- package/dist/lib/to-absolute-specifier.mjs +0 -20
- package/dist/lib/to-bare-specifier.d.mts +0 -28
- package/dist/lib/to-bare-specifier.mjs +0 -78
- package/dist/types/declaration.mjs +0 -0
- package/dist/types/ext.d.mts +0 -9
- package/dist/types/ext.mjs +0 -0
- package/dist/types/mime-type.mjs +0 -0
- package/dist/types/specifier-type.d.mts +0 -11
- package/dist/types/specifier-type.mjs +0 -0
- package/dist/types/statement-type.d.mts +0 -9
- package/dist/types/statement-type.mjs +0 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file parseSubpath
|
|
3
|
+
* @module mlly/utils/parseSubpath
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
ParseSubpathOptions,
|
|
8
|
+
ParsedModuleId,
|
|
9
|
+
ParsedSubpath
|
|
10
|
+
} from '#src/interfaces'
|
|
11
|
+
import getSubpaths from '#src/internal/get-subpaths'
|
|
12
|
+
import validateString from '#src/internal/validate-string'
|
|
13
|
+
import validateURLString from '#src/internal/validate-url-string'
|
|
14
|
+
import {
|
|
15
|
+
ERR_PACKAGE_IMPORT_NOT_DEFINED,
|
|
16
|
+
ERR_PACKAGE_PATH_NOT_EXPORTED,
|
|
17
|
+
type NodeError
|
|
18
|
+
} from '@flex-development/errnode'
|
|
19
|
+
import pathe from '@flex-development/pathe'
|
|
20
|
+
import type { Exports, Imports } from '@flex-development/pkg-types'
|
|
21
|
+
import { CompareResult, type Nullable } from '@flex-development/tutils'
|
|
22
|
+
import { URL, fileURLToPath, pathToFileURL } from 'node:url'
|
|
23
|
+
import compareSubpaths from './compare-subpaths'
|
|
24
|
+
import parseModuleId from './parse-module-id'
|
|
25
|
+
import PATTERN_CHARACTER from './pattern-character'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates an object representation of a subpath export or import from the given
|
|
29
|
+
* module `specifier` after ensuring the subpath defined in `specifier` is also
|
|
30
|
+
* defined in the given package `context`, a `package.json` [`exports`][1] or
|
|
31
|
+
* [`imports`][2] field.
|
|
32
|
+
*
|
|
33
|
+
* ::: warning
|
|
34
|
+
* Does **not** guarantee `specifier` resolves to an existing module.
|
|
35
|
+
* :::
|
|
36
|
+
*
|
|
37
|
+
* [1]: https://nodejs.org/api/packages.html#exports
|
|
38
|
+
* [2]: https://nodejs.org/api/packages.html#imports
|
|
39
|
+
*
|
|
40
|
+
* @see {@linkcode Exports}
|
|
41
|
+
* @see {@linkcode Imports}
|
|
42
|
+
* @see {@linkcode ParseSubpathOptions}
|
|
43
|
+
* @see {@linkcode ParsedSubpath}
|
|
44
|
+
* @see https://nodejs.org/api/packages.html#subpath-exports
|
|
45
|
+
* @see https://nodejs.org/api/packages.html#subpath-imports
|
|
46
|
+
*
|
|
47
|
+
* @param {string} specifier - Module specifier to evaluate
|
|
48
|
+
* @param {Exports | Imports | undefined} context - Package context
|
|
49
|
+
* @param {ParseSubpathOptions} options - Parsing options
|
|
50
|
+
* @return {ParsedSubpath} Object representing package subpath
|
|
51
|
+
* @throws {NodeError<Error | TypeError>} If `specifier` is not a string or the
|
|
52
|
+
* subpath defined in `specifier` is not defined in `context`
|
|
53
|
+
*/
|
|
54
|
+
const parseSubpath = (
|
|
55
|
+
specifier: string,
|
|
56
|
+
context: Exports | Imports | undefined,
|
|
57
|
+
options: ParseSubpathOptions
|
|
58
|
+
): ParsedSubpath => {
|
|
59
|
+
const { dir, internal = specifier.startsWith('#'), parent } = options
|
|
60
|
+
|
|
61
|
+
// ensure specifier is a string
|
|
62
|
+
validateString(specifier, 'specifier')
|
|
63
|
+
|
|
64
|
+
// ensure dir is an instance of URL or a string
|
|
65
|
+
validateURLString(dir, 'options.dir')
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Parsed module id.
|
|
69
|
+
*
|
|
70
|
+
* **Note**: Ensures {@linkcode specifier} begins with a valid package name or
|
|
71
|
+
* is a valid [subpath import][1].
|
|
72
|
+
*
|
|
73
|
+
* [1]: https://nodejs.org/api/packages.html#subpath-imports
|
|
74
|
+
*
|
|
75
|
+
* @const {ParsedModuleId} id
|
|
76
|
+
*/
|
|
77
|
+
const id: ParsedModuleId = parseModuleId(specifier, {
|
|
78
|
+
internal,
|
|
79
|
+
parent,
|
|
80
|
+
pkgname: !internal
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Absolute path to directory containing relevant `package.json` file.
|
|
85
|
+
*
|
|
86
|
+
* @const {string} pkgdir
|
|
87
|
+
*/
|
|
88
|
+
const pkgdir: string = fileURLToPath(dir).replace(/\/$/, '') + pathe.sep
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* URL of relevant `package.json` file.
|
|
92
|
+
*
|
|
93
|
+
* @const {URL} pkg
|
|
94
|
+
*/
|
|
95
|
+
const pkg: URL = new URL('package.json', pathToFileURL(pkgdir))
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Subpaths defined in {@linkcode context}.
|
|
99
|
+
*
|
|
100
|
+
* @const {string[]} keys
|
|
101
|
+
*/
|
|
102
|
+
const keys: string[] = getSubpaths(context, id.internal, pkg, parent)
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Subpath defined in {@linkcode context} that maps to {@linkcode specifier}.
|
|
106
|
+
*
|
|
107
|
+
* @var {Nullable<string>} key
|
|
108
|
+
*/
|
|
109
|
+
let key: Nullable<string> = null
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Subpath without entry prefix ({@linkcode key}).
|
|
113
|
+
*
|
|
114
|
+
* @var {string} base
|
|
115
|
+
*/
|
|
116
|
+
let base: string = ''
|
|
117
|
+
|
|
118
|
+
// match specifier to subpath defined in context
|
|
119
|
+
for (const pkgsubpath of keys) {
|
|
120
|
+
/**
|
|
121
|
+
* Index of {@linkcode PATTERN_CHARACTER} in {@linkcode pkgsubpath}.
|
|
122
|
+
*
|
|
123
|
+
* @const {number} pattern
|
|
124
|
+
*/
|
|
125
|
+
const pattern: number = pkgsubpath.indexOf(PATTERN_CHARACTER)
|
|
126
|
+
|
|
127
|
+
// no pattern character => subpath must be exact match
|
|
128
|
+
if (pattern === -1 && pkgsubpath === id.path) {
|
|
129
|
+
key = pkgsubpath
|
|
130
|
+
break
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// pattern character => try finding best match for subpath
|
|
134
|
+
if (pattern !== -1 && id.path.startsWith(pkgsubpath.slice(0, pattern))) {
|
|
135
|
+
/**
|
|
136
|
+
* Everything after pattern character (`*`) in {@linkcode pkgsubpath}.
|
|
137
|
+
*
|
|
138
|
+
* @const {string} trailer
|
|
139
|
+
*/
|
|
140
|
+
const trailer: string = pkgsubpath.slice(pattern + 1)
|
|
141
|
+
|
|
142
|
+
// best match found => reset key and base of subpath
|
|
143
|
+
if (
|
|
144
|
+
id.path.length >= pkgsubpath.length &&
|
|
145
|
+
id.path.endsWith(trailer) &&
|
|
146
|
+
compareSubpaths(key ?? '', pkgsubpath) === CompareResult.GREATER_THAN &&
|
|
147
|
+
pkgsubpath.lastIndexOf(PATTERN_CHARACTER) === pattern
|
|
148
|
+
) {
|
|
149
|
+
key = pkgsubpath
|
|
150
|
+
base = id.path.slice(pattern, id.path.length - trailer.length)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// throw if defined subpath was not found
|
|
156
|
+
if (key === null) {
|
|
157
|
+
let { parent } = options
|
|
158
|
+
|
|
159
|
+
// ensure parent is an instance of URL or a string
|
|
160
|
+
validateURLString(parent, 'options.parent')
|
|
161
|
+
|
|
162
|
+
// ensure parent is a path
|
|
163
|
+
parent = fileURLToPath(parent)
|
|
164
|
+
|
|
165
|
+
throw internal
|
|
166
|
+
? new ERR_PACKAGE_IMPORT_NOT_DEFINED(id.path, parent, pkgdir)
|
|
167
|
+
: new ERR_PACKAGE_PATH_NOT_EXPORTED(pkgdir, id.path, parent)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return { base, internal, key, raw: id.path, specifier: id.raw }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export default parseSubpath
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file PATTERN_CHARACTER
|
|
3
|
+
* @module mlly/utils/PATTERN_CHARACTER
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Character representing a subpath pattern.
|
|
8
|
+
*
|
|
9
|
+
* @const {string} PATTERN_CHARACTER
|
|
10
|
+
*/
|
|
11
|
+
const PATTERN_CHARACTER: string = '*'
|
|
12
|
+
|
|
13
|
+
export default PATTERN_CHARACTER
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file readPackageJson
|
|
3
|
+
* @module mlly/utils/readPackageJson
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import isFile from '#src/internal/is-file'
|
|
7
|
+
import validateString from '#src/internal/validate-string'
|
|
8
|
+
import validateURLString from '#src/internal/validate-url-string'
|
|
9
|
+
import type { ModuleId } from '#src/types'
|
|
10
|
+
import {
|
|
11
|
+
ERR_INVALID_PACKAGE_CONFIG,
|
|
12
|
+
type NodeError
|
|
13
|
+
} from '@flex-development/errnode'
|
|
14
|
+
import pathe from '@flex-development/pathe'
|
|
15
|
+
import type { PackageJson } from '@flex-development/pkg-types'
|
|
16
|
+
import { isEmptyString, isNIL, type Nullable } from '@flex-development/tutils'
|
|
17
|
+
import fs from 'node:fs'
|
|
18
|
+
import { fileURLToPath } from 'node:url'
|
|
19
|
+
import toURL from './to-url'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Reads a `package.json` file from the given directory.
|
|
23
|
+
*
|
|
24
|
+
* Returns `null` if a file is not found.
|
|
25
|
+
*
|
|
26
|
+
* @see {@linkcode ModuleId}
|
|
27
|
+
* @see {@linkcode PackageJson}
|
|
28
|
+
*
|
|
29
|
+
* @param {ModuleId} [dir='.'] - Id of directory containing `package.json` file
|
|
30
|
+
* @param {string?} [specifier] - Module specifier passed by user to initiate
|
|
31
|
+
* reading of `package.json` file
|
|
32
|
+
* @param {ModuleId?} [parent] - Id of module to resolve from
|
|
33
|
+
* @return {?PackageJson} `package.json` object or `null` if file is not found
|
|
34
|
+
* @throws {NodeError<Error | TypeError>} If `dir` is not a string or instance
|
|
35
|
+
* of {@linkcode URL}, if `specifier` is not a string, if `parent` is not a
|
|
36
|
+
* string, or if a `package.json` is file found and does not contain valid JSON
|
|
37
|
+
*/
|
|
38
|
+
const readPackageJson = (
|
|
39
|
+
dir: ModuleId = '.',
|
|
40
|
+
specifier?: string,
|
|
41
|
+
parent?: ModuleId
|
|
42
|
+
): Nullable<PackageJson> => {
|
|
43
|
+
// ensure dir is an instance of URL or a string
|
|
44
|
+
validateURLString(dir, 'dir')
|
|
45
|
+
|
|
46
|
+
// ensure specifier is a string
|
|
47
|
+
if (specifier !== undefined) validateString(specifier, 'specifier')
|
|
48
|
+
|
|
49
|
+
// ensure parent is an instance of URL or a string
|
|
50
|
+
if (parent !== undefined) validateURLString(parent, 'parent')
|
|
51
|
+
|
|
52
|
+
// ensure dir is a path
|
|
53
|
+
dir = fileURLToPath(toURL(dir))
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Absolute path to `package.json` file.
|
|
57
|
+
*
|
|
58
|
+
* @const {string} path
|
|
59
|
+
*/
|
|
60
|
+
const path: string = pathe.toNamespacedPath(pathe.join(dir, 'package.json'))
|
|
61
|
+
|
|
62
|
+
// return null if package.json file does not exist
|
|
63
|
+
if (!isFile(path)) return null
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Possible `package.json` object.
|
|
67
|
+
*
|
|
68
|
+
* @var {PackageJson} pkg
|
|
69
|
+
*/
|
|
70
|
+
let pkg: PackageJson
|
|
71
|
+
|
|
72
|
+
// try parsing package.json file
|
|
73
|
+
try {
|
|
74
|
+
pkg = JSON.parse(fs.readFileSync(path, 'utf8'))
|
|
75
|
+
} catch (e: unknown) {
|
|
76
|
+
/**
|
|
77
|
+
* String containing module specifier passed by user to initiate reading of
|
|
78
|
+
* `package.json` file and the location the module specifier was imported
|
|
79
|
+
* from.
|
|
80
|
+
*
|
|
81
|
+
* @var {string | undefined} base
|
|
82
|
+
*/
|
|
83
|
+
let base: string | undefined
|
|
84
|
+
|
|
85
|
+
// get base
|
|
86
|
+
switch (true) {
|
|
87
|
+
case specifier && !isNIL(parent) && !isEmptyString(parent):
|
|
88
|
+
base = `'${specifier}' from ${fileURLToPath(toURL(parent!))}`
|
|
89
|
+
break
|
|
90
|
+
case specifier?.startsWith('file:'):
|
|
91
|
+
base = fileURLToPath(specifier!)
|
|
92
|
+
break
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
throw new ERR_INVALID_PACKAGE_CONFIG(path, base, (e as SyntaxError).message)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return pkg
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export default readPackageJson
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file resolveAlias
|
|
3
|
+
* @module mlly/utils/resolveAlias
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ParsedModuleId, ResolveAliasOptions } from '#src/interfaces'
|
|
7
|
+
import regexp from '#src/internal/escape-reg-exp'
|
|
8
|
+
import validateBoolean from '#src/internal/validate-boolean'
|
|
9
|
+
import validateObject from '#src/internal/validate-object'
|
|
10
|
+
import validateSet from '#src/internal/validate-set'
|
|
11
|
+
import validateString from '#src/internal/validate-string'
|
|
12
|
+
import validateURLString from '#src/internal/validate-url-string'
|
|
13
|
+
import type { NodeError } from '@flex-development/errnode'
|
|
14
|
+
import pathe from '@flex-development/pathe'
|
|
15
|
+
import { CompareResult, isNIL, type Nullable } from '@flex-development/tutils'
|
|
16
|
+
import { URL, pathToFileURL } from 'node:url'
|
|
17
|
+
import compareSubpaths from './compare-subpaths'
|
|
18
|
+
import CONDITIONS from './conditions'
|
|
19
|
+
import parseModuleId from './parse-module-id'
|
|
20
|
+
import PATTERN_CHARACTER from './pattern-character'
|
|
21
|
+
import RESOLVE_EXTENSIONS from './resolve-extensions'
|
|
22
|
+
import resolveModule from './resolve-module'
|
|
23
|
+
import toBareSpecifier from './to-bare-specifier'
|
|
24
|
+
import toRelativeSpecifier from './to-relative-specifier'
|
|
25
|
+
import toURL from './to-url'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Resolves a path alias in the given module `specifier`; does nothing if a path
|
|
29
|
+
* match isn't found.
|
|
30
|
+
*
|
|
31
|
+
* @see {@linkcode ResolveAliasOptions}
|
|
32
|
+
*
|
|
33
|
+
* @async
|
|
34
|
+
*
|
|
35
|
+
* @param {string} specifier - Module specifier to evaluate
|
|
36
|
+
* @param {ResolveAliasOptions} [options={}] - Resolution options
|
|
37
|
+
* @return {Promise<string>} Resolved specifier or original module specifier
|
|
38
|
+
* @throws {NodeError}
|
|
39
|
+
*/
|
|
40
|
+
const resolveAlias = async (
|
|
41
|
+
specifier: string,
|
|
42
|
+
options: ResolveAliasOptions = {}
|
|
43
|
+
): Promise<string> => {
|
|
44
|
+
const {
|
|
45
|
+
absolute = true,
|
|
46
|
+
aliases = {},
|
|
47
|
+
condition = 'default',
|
|
48
|
+
conditions = CONDITIONS,
|
|
49
|
+
cwd = pathToFileURL('.'),
|
|
50
|
+
ext,
|
|
51
|
+
extensions = RESOLVE_EXTENSIONS,
|
|
52
|
+
parent = import.meta.url,
|
|
53
|
+
preserveSymlinks = false
|
|
54
|
+
} = options
|
|
55
|
+
|
|
56
|
+
// ensure specifier is a string
|
|
57
|
+
validateString(specifier, 'specifier')
|
|
58
|
+
|
|
59
|
+
// ensure option schemas
|
|
60
|
+
validateBoolean(absolute, 'options.absolute')
|
|
61
|
+
validateObject(aliases, 'options.aliases')
|
|
62
|
+
validateString(condition, 'options.condition')
|
|
63
|
+
validateURLString(cwd, 'options.cwd')
|
|
64
|
+
validateSet(extensions, 'options.extensions')
|
|
65
|
+
validateURLString(parent, 'options.parent')
|
|
66
|
+
validateBoolean(preserveSymlinks, 'options.preserveSymlinks')
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Parsed module id.
|
|
70
|
+
*
|
|
71
|
+
* **Note**: Ensures {@linkcode specifier} is a valid module specifier.
|
|
72
|
+
*
|
|
73
|
+
* @const {ParsedModuleId} id
|
|
74
|
+
*/
|
|
75
|
+
const id: ParsedModuleId = parseModuleId(specifier, {
|
|
76
|
+
parent: toURL(parent),
|
|
77
|
+
pkgname: false
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Path aliases defined in {@linkcode aliases}.
|
|
82
|
+
*
|
|
83
|
+
* @const {string[]} keys
|
|
84
|
+
*/
|
|
85
|
+
const keys: string[] = Object.keys(aliases).sort(compareSubpaths)
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Path alias in {@linkcode aliases} that maps to {@linkcode specifier}.
|
|
89
|
+
*
|
|
90
|
+
* @var {Nullable<string>} key
|
|
91
|
+
*/
|
|
92
|
+
let alias: Nullable<string> = null
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Everything after {@linkcode alias} in {@linkcode specifier}.
|
|
96
|
+
*
|
|
97
|
+
* @var {string} trail
|
|
98
|
+
*/
|
|
99
|
+
let trail: string = ''
|
|
100
|
+
|
|
101
|
+
// match specifier to alias
|
|
102
|
+
for (const key of keys) {
|
|
103
|
+
/**
|
|
104
|
+
* Index of {@linkcode PATTERN_CHARACTER} in {@linkcode key}.
|
|
105
|
+
*
|
|
106
|
+
* @const {number} p
|
|
107
|
+
*/
|
|
108
|
+
const pattern: number = key.indexOf(PATTERN_CHARACTER)
|
|
109
|
+
|
|
110
|
+
// no pattern character => need exact match for alias
|
|
111
|
+
if (pattern === -1 && key === id.raw) {
|
|
112
|
+
alias = key
|
|
113
|
+
break
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// pattern character => need best match for alias
|
|
117
|
+
if (pattern !== -1 && id.raw.startsWith(key.slice(0, pattern))) {
|
|
118
|
+
/**
|
|
119
|
+
* Everything after pattern character (`*`) in {@linkcode key}.
|
|
120
|
+
*
|
|
121
|
+
* @const {string} trailer
|
|
122
|
+
*/
|
|
123
|
+
const trailer: string = key.slice(pattern + 1)
|
|
124
|
+
|
|
125
|
+
// best match found => reset key and base
|
|
126
|
+
if (
|
|
127
|
+
id.raw.length >= key.length &&
|
|
128
|
+
id.raw.endsWith(trailer) &&
|
|
129
|
+
compareSubpaths(alias ?? '', key) === CompareResult.GREATER_THAN &&
|
|
130
|
+
key.lastIndexOf(PATTERN_CHARACTER) === pattern
|
|
131
|
+
) {
|
|
132
|
+
alias = key
|
|
133
|
+
trail = id.raw.slice(pattern, id.raw.length - trailer.length)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// exit early if alias was not found or alias does not map any paths
|
|
139
|
+
if (alias === null || isNIL(aliases[alias])) return specifier
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* URL of directory to resolve non-absolute modules from.
|
|
143
|
+
*
|
|
144
|
+
* @const {string} base
|
|
145
|
+
*/
|
|
146
|
+
const base: string = toURL(cwd).href.replace(/\/$/, '') + pathe.sep
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Resolved module URL.
|
|
150
|
+
*
|
|
151
|
+
* @var {Nullable<URL>} url
|
|
152
|
+
*/
|
|
153
|
+
let url: Nullable<URL> = null
|
|
154
|
+
|
|
155
|
+
// try resolving path alias
|
|
156
|
+
for (let segment of [aliases[alias]!].flat()) {
|
|
157
|
+
// replace pattern character in segment with trail of specifier
|
|
158
|
+
if (segment.includes(PATTERN_CHARACTER)) {
|
|
159
|
+
segment = segment.replace(
|
|
160
|
+
new RegExp(`${regexp(PATTERN_CHARACTER)}$`),
|
|
161
|
+
trail
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Absolute paths to modules to try resolving.
|
|
167
|
+
*
|
|
168
|
+
* @var {Set<string>} tries
|
|
169
|
+
*/
|
|
170
|
+
let tries: Set<string> = new Set(
|
|
171
|
+
[
|
|
172
|
+
'',
|
|
173
|
+
...[...extensions].map(ext => pathe.formatExt(ext)),
|
|
174
|
+
...[...extensions].map(ext => '/index' + pathe.formatExt(ext))
|
|
175
|
+
]
|
|
176
|
+
.map((suffix: string) => segment + suffix)
|
|
177
|
+
.filter((segment: string) => segment.length > 0)
|
|
178
|
+
.map((segment: string) => new URL(segment, base).pathname)
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
// add attempt to resolve possible package export
|
|
182
|
+
if (segment.startsWith('node_modules')) {
|
|
183
|
+
const [trypath = ''] = [...tries]
|
|
184
|
+
tries = new Set([trypath.replace(/.*\/node_modules\//, ''), ...tries])
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// find first resolvable module
|
|
188
|
+
for (const trypath of tries) {
|
|
189
|
+
try {
|
|
190
|
+
url = await resolveModule(trypath, {
|
|
191
|
+
condition,
|
|
192
|
+
conditions,
|
|
193
|
+
ext,
|
|
194
|
+
extensions,
|
|
195
|
+
parent: toURL(parent),
|
|
196
|
+
preserveSymlinks
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
// exit if module was resolved
|
|
200
|
+
break
|
|
201
|
+
} catch {
|
|
202
|
+
continue
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// stop checking path segments if path alias was resolved
|
|
207
|
+
if (url) break
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return url
|
|
211
|
+
? absolute
|
|
212
|
+
? url.href
|
|
213
|
+
: /\/node_modules\//.test(url.href)
|
|
214
|
+
? toBareSpecifier(url, toURL(parent), new Set(conditions))
|
|
215
|
+
: toRelativeSpecifier(url, toURL(parent))
|
|
216
|
+
: specifier
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export default resolveAlias
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file resolveAliases
|
|
3
|
+
* @module mlly/utils/resolveAliases
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { SpecifierSyntaxKind } from '#src/enums'
|
|
7
|
+
import type { ResolveAliasOptions } from '#src/interfaces'
|
|
8
|
+
import regexp from '#src/internal/escape-reg-exp'
|
|
9
|
+
import extractStatements from './extract-statements'
|
|
10
|
+
import resolveAlias from './resolve-alias'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Resolves path aliases in the given piece of source `code`.
|
|
14
|
+
*
|
|
15
|
+
* @see {@linkcode ResolveAliasOptions}
|
|
16
|
+
* @see {@linkcode resolveAlias}
|
|
17
|
+
*
|
|
18
|
+
* @async
|
|
19
|
+
*
|
|
20
|
+
* @param {string} code - Code to evaluate
|
|
21
|
+
* @param {ResolveAliasOptions} [options={}] - Resolve alias options
|
|
22
|
+
* @return {Promise<string>} `code` unmodified or with path aliases resolved
|
|
23
|
+
*/
|
|
24
|
+
const resolveAliases = async (
|
|
25
|
+
code: string,
|
|
26
|
+
options: ResolveAliasOptions = {}
|
|
27
|
+
): Promise<string> => {
|
|
28
|
+
for (const statement of extractStatements(code)) {
|
|
29
|
+
// do nothing if statement does not have specifier
|
|
30
|
+
if (!statement.specifier) continue
|
|
31
|
+
|
|
32
|
+
// ignore statements with dynamic specifiers
|
|
33
|
+
if (statement.specifier_syntax === SpecifierSyntaxKind.DYNAMIC) continue
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolved module specifier.
|
|
37
|
+
*
|
|
38
|
+
* @const {string} specifier
|
|
39
|
+
*/
|
|
40
|
+
const specifier: string = await resolveAlias(statement.specifier, options)
|
|
41
|
+
|
|
42
|
+
// do nothing if original specifier did not contain path alias
|
|
43
|
+
if (specifier === statement.specifier) continue
|
|
44
|
+
|
|
45
|
+
// replace path alias
|
|
46
|
+
code = code.replace(
|
|
47
|
+
statement.code,
|
|
48
|
+
statement.code.replace(
|
|
49
|
+
new RegExp(`(?<=["'])${regexp(statement.specifier)}(?=["'])`),
|
|
50
|
+
specifier
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return code
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default resolveAliases
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file RESOLVE_EXTENSIONS
|
|
3
|
+
* @module mlly/utils/RESOLVE_EXTENSIONS
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Ext } from '@flex-development/pathe'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Default resolvable file extensions.
|
|
10
|
+
*
|
|
11
|
+
* @see {@linkcode Ext}
|
|
12
|
+
*
|
|
13
|
+
* @const {Set<Ext>} RESOLVE_EXTENSIONS
|
|
14
|
+
*/
|
|
15
|
+
const RESOLVE_EXTENSIONS: Set<Ext> = new Set([
|
|
16
|
+
'.mjs',
|
|
17
|
+
'.mts',
|
|
18
|
+
'.cjs',
|
|
19
|
+
'.cts',
|
|
20
|
+
'.js',
|
|
21
|
+
'.ts',
|
|
22
|
+
'.jsx',
|
|
23
|
+
'.tsx',
|
|
24
|
+
'.css',
|
|
25
|
+
'.json',
|
|
26
|
+
'.node',
|
|
27
|
+
'.wasm',
|
|
28
|
+
'.d.mts',
|
|
29
|
+
'.d.cts',
|
|
30
|
+
'.d.ts'
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
export default RESOLVE_EXTENSIONS
|