@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,783 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Internal - Resolver
|
|
3
|
+
* @module mlly/internal/Resolver
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
PackageScope,
|
|
8
|
+
ParsedModuleId,
|
|
9
|
+
ParsedSubpath
|
|
10
|
+
} from '#src/interfaces'
|
|
11
|
+
import type { ModuleId } from '#src/types'
|
|
12
|
+
import CONDITIONS from '#src/utils/conditions'
|
|
13
|
+
import isExportsSugar from '#src/utils/is-exports-sugar'
|
|
14
|
+
import isRelativeSpecifier from '#src/utils/is-relative-specifier'
|
|
15
|
+
import lookupPackageScope from '#src/utils/lookup-package-scope'
|
|
16
|
+
import parseModuleId from '#src/utils/parse-module-id'
|
|
17
|
+
import parseSubpath from '#src/utils/parse-subpath'
|
|
18
|
+
import PATTERN_CHARACTER from '#src/utils/pattern-character'
|
|
19
|
+
import readPackageJson from '#src/utils/read-package-json'
|
|
20
|
+
import toNodeURL from '#src/utils/to-node-url'
|
|
21
|
+
import toURL from '#src/utils/to-url'
|
|
22
|
+
import {
|
|
23
|
+
ERR_INVALID_MODULE_SPECIFIER,
|
|
24
|
+
ERR_INVALID_PACKAGE_CONFIG,
|
|
25
|
+
ERR_INVALID_PACKAGE_TARGET,
|
|
26
|
+
ERR_MODULE_NOT_FOUND,
|
|
27
|
+
ERR_PACKAGE_IMPORT_NOT_DEFINED,
|
|
28
|
+
ERR_PACKAGE_PATH_NOT_EXPORTED,
|
|
29
|
+
ERR_UNSUPPORTED_DIR_IMPORT,
|
|
30
|
+
ErrorCode,
|
|
31
|
+
type NodeError
|
|
32
|
+
} from '@flex-development/errnode'
|
|
33
|
+
import { isBuiltin } from '@flex-development/is-builtin'
|
|
34
|
+
import pathe from '@flex-development/pathe'
|
|
35
|
+
import type {
|
|
36
|
+
Exports,
|
|
37
|
+
Imports,
|
|
38
|
+
ImportsKey,
|
|
39
|
+
PackageJson
|
|
40
|
+
} from '@flex-development/pkg-types'
|
|
41
|
+
import { isNIL, type Nullable } from '@flex-development/tutils'
|
|
42
|
+
import fs from 'node:fs'
|
|
43
|
+
import { URL, fileURLToPath, pathToFileURL } from 'node:url'
|
|
44
|
+
import regexp from './escape-reg-exp'
|
|
45
|
+
import isArrayIndex from './is-array-index'
|
|
46
|
+
import isDirectory from './is-directory'
|
|
47
|
+
import isFile from './is-file'
|
|
48
|
+
import PACKAGE_NAME_REGEX from './regex-package-name'
|
|
49
|
+
import PACKAGE_PATH_REGEX from './regex-package-path'
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* ECMAScript (ES) module resolver.
|
|
53
|
+
*
|
|
54
|
+
* @see https://nodejs.org/api/esm.html#resolver-algorithm
|
|
55
|
+
*
|
|
56
|
+
* @class
|
|
57
|
+
*/
|
|
58
|
+
class Resolver {
|
|
59
|
+
/**
|
|
60
|
+
* Resolves `specifier` according to the [ESM Resolver algorithm][1].
|
|
61
|
+
*
|
|
62
|
+
* [1]: https://nodejs.org/api/esm.html#esm_resolver_algorithm
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
*
|
|
66
|
+
* @param {string} specifier - Module specifier to resolve
|
|
67
|
+
* @param {ModuleId} parent - URL of module to resolve from
|
|
68
|
+
* @param {string?} [condition='default'] - Export condition to apply
|
|
69
|
+
* @param {Set<string>?} [conditions=CONDITIONS] - Export conditions
|
|
70
|
+
* @param {boolean?} [preserveSymlinks=false] - Skip resolving symlinks
|
|
71
|
+
* @return {URL} Resolved module URL
|
|
72
|
+
* @throws {NodeError}
|
|
73
|
+
*/
|
|
74
|
+
public resolveModule(
|
|
75
|
+
specifier: string,
|
|
76
|
+
parent: ModuleId,
|
|
77
|
+
condition: string = 'default',
|
|
78
|
+
conditions: Set<string> = CONDITIONS,
|
|
79
|
+
preserveSymlinks: boolean = false
|
|
80
|
+
): URL {
|
|
81
|
+
// ensure parent is an instance of URL
|
|
82
|
+
parent = toURL(parent)
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Remote module check for {@linkcode parent}.
|
|
86
|
+
*
|
|
87
|
+
* @const {boolean} remote
|
|
88
|
+
*/
|
|
89
|
+
const remote: boolean = /^https?:$/.test(parent.protocol)
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Module resolution error.
|
|
93
|
+
*
|
|
94
|
+
* @var {NodeError} error
|
|
95
|
+
*/
|
|
96
|
+
let error: NodeError = new ERR_MODULE_NOT_FOUND(
|
|
97
|
+
specifier,
|
|
98
|
+
remote ? parent.href : fileURLToPath(parent),
|
|
99
|
+
PACKAGE_PATH_REGEX.test(specifier) ? 'package' : 'module'
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Resolved module URL.
|
|
104
|
+
*
|
|
105
|
+
* @var {Nullable<URL>} url
|
|
106
|
+
*/
|
|
107
|
+
let url: Nullable<URL> = null
|
|
108
|
+
|
|
109
|
+
// try resolving module
|
|
110
|
+
try {
|
|
111
|
+
switch (true) {
|
|
112
|
+
case isRelativeSpecifier(specifier):
|
|
113
|
+
case pathe.isAbsolute(specifier):
|
|
114
|
+
url = new URL(specifier, parent)
|
|
115
|
+
break
|
|
116
|
+
case !remote && specifier.startsWith('#'):
|
|
117
|
+
url = this.resolvePackageImport(
|
|
118
|
+
specifier,
|
|
119
|
+
parent,
|
|
120
|
+
condition,
|
|
121
|
+
conditions
|
|
122
|
+
)
|
|
123
|
+
break
|
|
124
|
+
default:
|
|
125
|
+
try {
|
|
126
|
+
url = new URL(specifier)
|
|
127
|
+
} catch {
|
|
128
|
+
if (!remote) {
|
|
129
|
+
url = this.resolvePackage(
|
|
130
|
+
specifier,
|
|
131
|
+
parent,
|
|
132
|
+
condition,
|
|
133
|
+
conditions
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
break
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// finalize resolution
|
|
142
|
+
if (url && url.protocol === 'file:') {
|
|
143
|
+
/**
|
|
144
|
+
* Absolute path to resolved module.
|
|
145
|
+
*
|
|
146
|
+
* @var {string} path
|
|
147
|
+
*/
|
|
148
|
+
const path: string = fileURLToPath(url)
|
|
149
|
+
|
|
150
|
+
// throw if resolved module is not a file
|
|
151
|
+
if (!isFile(path)) {
|
|
152
|
+
throw isDirectory(path)
|
|
153
|
+
? new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(parent))
|
|
154
|
+
: new ERR_MODULE_NOT_FOUND(path, fileURLToPath(parent), 'module')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// resolve symlinks
|
|
158
|
+
if (!preserveSymlinks) {
|
|
159
|
+
// preserve fragment and query portions of module url
|
|
160
|
+
const { hash, search } = url
|
|
161
|
+
|
|
162
|
+
// remove symlinks in module url
|
|
163
|
+
url = pathToFileURL(fs.realpathSync(path))
|
|
164
|
+
|
|
165
|
+
// re-add fragment and query portions of module url
|
|
166
|
+
url.hash = hash
|
|
167
|
+
url.search = search
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
} catch (e: unknown) {
|
|
171
|
+
error = e as NodeError
|
|
172
|
+
url = null
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// throw if specifier could not be resolved
|
|
176
|
+
if (!url) throw error
|
|
177
|
+
|
|
178
|
+
return url
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Resolves a package path using a combination of the following algorithms:
|
|
183
|
+
*
|
|
184
|
+
* - `PACKAGE_RESOLVE`
|
|
185
|
+
* - `PACKAGE_SELF_RESOLVE`
|
|
186
|
+
*
|
|
187
|
+
* @see https://nodejs.org/api/esm.html#resolver-algorithm-specification
|
|
188
|
+
*
|
|
189
|
+
* @public
|
|
190
|
+
*
|
|
191
|
+
* @param {string} specifier - Module specifier to resolve
|
|
192
|
+
* @param {ModuleId} parent - URL of module to resolve from
|
|
193
|
+
* @param {string?} [condition='default'] - Export condition to apply
|
|
194
|
+
* @param {Set<string>?} [conditions=CONDITIONS] - Export conditions
|
|
195
|
+
* @return {URL} Resolved package path URL
|
|
196
|
+
* @throws {NodeError}
|
|
197
|
+
*/
|
|
198
|
+
public resolvePackage(
|
|
199
|
+
specifier: string,
|
|
200
|
+
parent: ModuleId,
|
|
201
|
+
condition: string = 'default',
|
|
202
|
+
conditions: Set<string> = CONDITIONS
|
|
203
|
+
): URL {
|
|
204
|
+
/**
|
|
205
|
+
* Object representation of {@linkcode specifier}.
|
|
206
|
+
*
|
|
207
|
+
* @const {ParsedModuleId} id
|
|
208
|
+
*/
|
|
209
|
+
const id: ParsedModuleId = parseModuleId(specifier, {
|
|
210
|
+
parent,
|
|
211
|
+
pkgname: !isBuiltin(specifier)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
// exit early if specifier is builtin module
|
|
215
|
+
if (isBuiltin(id.raw)) return new URL(toNodeURL(id.raw))
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Directory to end package scope search.
|
|
219
|
+
*
|
|
220
|
+
* @const {URL} stopdir
|
|
221
|
+
*/
|
|
222
|
+
const stopdir: URL = pathToFileURL('.' + pathe.sep)
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Package scope lookup result.
|
|
226
|
+
*
|
|
227
|
+
* @var {Nullable<PackageScope>} scope
|
|
228
|
+
*/
|
|
229
|
+
let scope: Nullable<PackageScope> = lookupPackageScope(
|
|
230
|
+
parent,
|
|
231
|
+
stopdir,
|
|
232
|
+
specifier,
|
|
233
|
+
parent
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
// try self resolve
|
|
237
|
+
if (scope?.pkgjson.name === id.pkg && !isNIL(scope.pkgjson.exports)) {
|
|
238
|
+
return this.resolvePackageExport(
|
|
239
|
+
id.raw,
|
|
240
|
+
scope.pkg,
|
|
241
|
+
parent,
|
|
242
|
+
condition,
|
|
243
|
+
conditions
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Resolved package path URL.
|
|
249
|
+
*
|
|
250
|
+
* @var {Nullable<URL>} url
|
|
251
|
+
*/
|
|
252
|
+
let url: Nullable<URL> = null
|
|
253
|
+
|
|
254
|
+
// reset scope to try resolving external package
|
|
255
|
+
if (scope) {
|
|
256
|
+
let { dir: pkgdir } = scope
|
|
257
|
+
|
|
258
|
+
// lookup package scope for external package
|
|
259
|
+
while (pkgdir !== '.') {
|
|
260
|
+
scope = lookupPackageScope(
|
|
261
|
+
new URL(`node_modules/${id.raw}`, pkgdir + pathe.sep),
|
|
262
|
+
stopdir,
|
|
263
|
+
specifier,
|
|
264
|
+
parent
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
// stop search if package scope was found for external package
|
|
268
|
+
if (scope) break
|
|
269
|
+
|
|
270
|
+
// continue package scope search
|
|
271
|
+
pkgdir = pathe.dirname(pkgdir)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// try resolving external package
|
|
276
|
+
switch (true) {
|
|
277
|
+
case scope && !isNIL(scope.pkgjson.exports):
|
|
278
|
+
url = this.resolvePackageExport(
|
|
279
|
+
specifier,
|
|
280
|
+
scope!.pkg,
|
|
281
|
+
parent,
|
|
282
|
+
condition,
|
|
283
|
+
conditions
|
|
284
|
+
)
|
|
285
|
+
break
|
|
286
|
+
case scope && id.path === '.':
|
|
287
|
+
url = this.resolvePackageMain(scope!.pkg, scope!.pkgjson, parent)
|
|
288
|
+
break
|
|
289
|
+
case !!scope:
|
|
290
|
+
url = new URL(id.path, scope!.pkg)
|
|
291
|
+
break
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// throw if package url was not resolved
|
|
295
|
+
if (!url) throw new ERR_MODULE_NOT_FOUND(id.pkg, fileURLToPath(parent))
|
|
296
|
+
|
|
297
|
+
return url
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Resolves a subpath export according to the `PACKAGE_EXPORTS_RESOLVE`
|
|
302
|
+
* algorithm.
|
|
303
|
+
*
|
|
304
|
+
* @see https://nodejs.org/api/esm.html#resolver-algorithm-specification
|
|
305
|
+
* @see https://nodejs.org/api/packages.html#subpath-exports
|
|
306
|
+
*
|
|
307
|
+
* @public
|
|
308
|
+
*
|
|
309
|
+
* @param {string} specifier - Module specifier containing subpath
|
|
310
|
+
* @param {ModuleId} pkg - URL of relevant `package.json` file
|
|
311
|
+
* @param {ModuleId} parent - URL of module to resolve from
|
|
312
|
+
* @param {string?} [condition='default'] - Export condition to apply
|
|
313
|
+
* @param {Set<string>?} [conditions=CONDITIONS] - Export conditions
|
|
314
|
+
* @return {URL} Resolved package export URL
|
|
315
|
+
* @throws {NodeError<Error | TypeError>}
|
|
316
|
+
*/
|
|
317
|
+
public resolvePackageExport(
|
|
318
|
+
specifier: string,
|
|
319
|
+
pkg: ModuleId,
|
|
320
|
+
parent: ModuleId,
|
|
321
|
+
condition: string = 'default',
|
|
322
|
+
conditions: Set<string> = CONDITIONS
|
|
323
|
+
): URL {
|
|
324
|
+
/**
|
|
325
|
+
* URL of directory containing `package.json` file.
|
|
326
|
+
*
|
|
327
|
+
* @var {URL} dir
|
|
328
|
+
*/
|
|
329
|
+
const dir: URL = new URL('.', pkg)
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Possible `package.json` object.
|
|
333
|
+
*
|
|
334
|
+
* @const {PackageJson} pkgjson
|
|
335
|
+
*/
|
|
336
|
+
const pkgjson: Nullable<PackageJson> = readPackageJson(
|
|
337
|
+
dir,
|
|
338
|
+
specifier,
|
|
339
|
+
parent
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Package `exports`.
|
|
344
|
+
*
|
|
345
|
+
* @var {Exports | undefined} exports
|
|
346
|
+
*/
|
|
347
|
+
let exports: Exports | undefined = pkgjson?.exports
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Object representation of package subpath.
|
|
351
|
+
*
|
|
352
|
+
* @const {ParsedSubpath} subpath
|
|
353
|
+
*/
|
|
354
|
+
const subpath: ParsedSubpath = parseSubpath(specifier, exports, {
|
|
355
|
+
dir,
|
|
356
|
+
parent
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
// convert exports to object if using exports main sugar
|
|
360
|
+
if (!subpath.internal && isExportsSugar(exports, pkg, parent)) {
|
|
361
|
+
exports = { '.': exports } as Record<string, Exports>
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Resolved package export URL.
|
|
366
|
+
*
|
|
367
|
+
* @const {Nullable<URL>} url
|
|
368
|
+
*/
|
|
369
|
+
const url: Nullable<URL> = this.resolvePackageTarget(
|
|
370
|
+
dir,
|
|
371
|
+
(exports as Nullable<Record<string, Exports>>)?.[subpath.key],
|
|
372
|
+
subpath.base,
|
|
373
|
+
subpath.key,
|
|
374
|
+
parent,
|
|
375
|
+
subpath.internal,
|
|
376
|
+
condition,
|
|
377
|
+
conditions
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
// throw if subpath export was not resolved
|
|
381
|
+
if (!url) {
|
|
382
|
+
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
|
|
383
|
+
fileURLToPath(dir),
|
|
384
|
+
subpath.raw,
|
|
385
|
+
fileURLToPath(parent)
|
|
386
|
+
)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return url
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Resolves a subpath import according to the `PACKAGE_IMPORTS_RESOLVE`
|
|
394
|
+
* algorithm.
|
|
395
|
+
*
|
|
396
|
+
* @see https://nodejs.org/api/esm.html#resolver-algorithm-specification
|
|
397
|
+
* @see https://nodejs.org/api/packages.html#subpath-imports
|
|
398
|
+
*
|
|
399
|
+
* @public
|
|
400
|
+
*
|
|
401
|
+
* @param {string} specifier - Module specifier containing subpath
|
|
402
|
+
* @param {ModuleId} parent - URL of module to resolve from
|
|
403
|
+
* @param {string?} [condition='default'] - Export condition to apply
|
|
404
|
+
* @param {Set<string>?} [conditions=CONDITIONS] - Export conditions
|
|
405
|
+
* @return {URL} Resolved package import URL
|
|
406
|
+
* @throws {NodeError<Error | TypeError>}
|
|
407
|
+
*/
|
|
408
|
+
public resolvePackageImport(
|
|
409
|
+
specifier: string,
|
|
410
|
+
parent: ModuleId,
|
|
411
|
+
condition: string = 'default',
|
|
412
|
+
conditions: Set<string> = CONDITIONS
|
|
413
|
+
): URL {
|
|
414
|
+
/**
|
|
415
|
+
* Parsed module id.
|
|
416
|
+
*
|
|
417
|
+
* Ensures {@linkcode specifier} is a valid internal specifier.
|
|
418
|
+
*
|
|
419
|
+
* @const {ParsedModuleId} id
|
|
420
|
+
*/
|
|
421
|
+
const id: ParsedModuleId = parseModuleId(specifier, {
|
|
422
|
+
internal: true,
|
|
423
|
+
parent
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Package scope result for {@linkcode parent}.
|
|
428
|
+
*
|
|
429
|
+
* @const {Nullable<PackageScope>} scope
|
|
430
|
+
*/
|
|
431
|
+
const scope: Nullable<PackageScope> = lookupPackageScope(
|
|
432
|
+
parent,
|
|
433
|
+
pathToFileURL('.'),
|
|
434
|
+
specifier,
|
|
435
|
+
parent
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Resolved package import URL.
|
|
440
|
+
*
|
|
441
|
+
* @var {Nullable<URL>} url
|
|
442
|
+
*/
|
|
443
|
+
let url: Nullable<URL> = null
|
|
444
|
+
|
|
445
|
+
// try resolving subpath import
|
|
446
|
+
if (scope) {
|
|
447
|
+
/**
|
|
448
|
+
* Package `imports`.
|
|
449
|
+
*
|
|
450
|
+
* @var {Imports | undefined} imports
|
|
451
|
+
*/
|
|
452
|
+
const imports: Imports | undefined = scope.pkgjson.imports
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Object representation of package subpath.
|
|
456
|
+
*
|
|
457
|
+
* @const {ParsedSubpath} subpath
|
|
458
|
+
*/
|
|
459
|
+
const subpath: ParsedSubpath = parseSubpath(id.raw, imports, {
|
|
460
|
+
dir: scope.dir,
|
|
461
|
+
internal: true,
|
|
462
|
+
parent
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
url = this.resolvePackageTarget(
|
|
466
|
+
scope.dir,
|
|
467
|
+
imports?.[subpath.key as ImportsKey],
|
|
468
|
+
subpath.base,
|
|
469
|
+
subpath.key,
|
|
470
|
+
parent,
|
|
471
|
+
subpath.internal,
|
|
472
|
+
condition,
|
|
473
|
+
conditions
|
|
474
|
+
)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// throw if subpath import was not resolved
|
|
478
|
+
if (!url) {
|
|
479
|
+
throw new ERR_PACKAGE_IMPORT_NOT_DEFINED(
|
|
480
|
+
specifier,
|
|
481
|
+
fileURLToPath(parent),
|
|
482
|
+
/* c8 ignore next */ scope?.dir ? fileURLToPath(scope.dir) : undefined
|
|
483
|
+
)
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return url
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Resolves a package entry point using the legacy CommonJS `main` resolution
|
|
491
|
+
* alogirthm:
|
|
492
|
+
*
|
|
493
|
+
* 1. let M = pkgdir `pkgjson.main`
|
|
494
|
+
* 2. TRY(M, M.js, M.json, M.node, M/index.js, M/index.json, M/index.node)
|
|
495
|
+
* 4. TRY(pkgdir/index.js, pkgdir/index.json, pkgdir/index.node)
|
|
496
|
+
* 5. ERR_MODULE_NOT_FOUND
|
|
497
|
+
*
|
|
498
|
+
* @todo emit deprecation messages
|
|
499
|
+
*
|
|
500
|
+
* @public
|
|
501
|
+
*
|
|
502
|
+
* @param {ModuleId} pkg - URL of relevant `package.json` file
|
|
503
|
+
* @param {PackageJson} pkgjson - Relevant `package.json` object
|
|
504
|
+
* @param {ModuleId} parent - URL of module to resolve from
|
|
505
|
+
* @return {URL} Package entry point URL
|
|
506
|
+
* @throws {NodeError} If package entry point is not found
|
|
507
|
+
*/
|
|
508
|
+
public resolvePackageMain(
|
|
509
|
+
pkg: ModuleId,
|
|
510
|
+
pkgjson: PackageJson,
|
|
511
|
+
parent: ModuleId
|
|
512
|
+
): URL {
|
|
513
|
+
/**
|
|
514
|
+
* Possible inputs for package entry point URL.
|
|
515
|
+
*
|
|
516
|
+
* @const {string[]} tries
|
|
517
|
+
*/
|
|
518
|
+
const tries: string[] = [
|
|
519
|
+
...(pkgjson.main
|
|
520
|
+
? [
|
|
521
|
+
pkgjson.main,
|
|
522
|
+
`./${pkgjson.main}.js`,
|
|
523
|
+
`./${pkgjson.main}.json`,
|
|
524
|
+
`./${pkgjson.main}.node`,
|
|
525
|
+
`./${pkgjson.main}/index.js`,
|
|
526
|
+
`./${pkgjson.main}/index.json`,
|
|
527
|
+
`./${pkgjson.main}/index.node`
|
|
528
|
+
]
|
|
529
|
+
: []),
|
|
530
|
+
'./index.js',
|
|
531
|
+
'./index.json',
|
|
532
|
+
'./index.node'
|
|
533
|
+
]
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Package entry point URL.
|
|
537
|
+
*
|
|
538
|
+
* @var {Nullable<URL>} url
|
|
539
|
+
*/
|
|
540
|
+
let url: Nullable<URL> = null
|
|
541
|
+
|
|
542
|
+
// try resolving package entry point
|
|
543
|
+
for (const input of tries) {
|
|
544
|
+
if (isFile((url = new URL(input, pkg)))) break
|
|
545
|
+
url = null
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// throw if package entry point was not resolved
|
|
549
|
+
if (!url) {
|
|
550
|
+
throw new ERR_MODULE_NOT_FOUND(
|
|
551
|
+
fileURLToPath(new URL('.', pkg)),
|
|
552
|
+
fileURLToPath(parent)
|
|
553
|
+
)
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
return url
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Resolves a package target according to the `PACKAGE_TARGET_RESOLVE`
|
|
561
|
+
* algorithm.
|
|
562
|
+
*
|
|
563
|
+
* @see https://nodejs.org/api/esm.html#resolver-algorithm-specification
|
|
564
|
+
*
|
|
565
|
+
* @todo emit deprecation messages
|
|
566
|
+
*
|
|
567
|
+
* @public
|
|
568
|
+
*
|
|
569
|
+
* @param {ModuleId} dir - URL of directory containing relevant `package.json`
|
|
570
|
+
* @param {Exports | undefined} target - Package `exports` or `imports` target
|
|
571
|
+
* @param {string} subpath - Package subpath without entry prefix (`key`)
|
|
572
|
+
* @param {string} key - Subpath defined in relevant `package.json` file
|
|
573
|
+
* @param {ModuleId} parent - URL of module to resolve from
|
|
574
|
+
* @param {boolean?} [internal=false] - Package `imports` hint
|
|
575
|
+
* @param {string?} [condition='default'] - Export condition to apply
|
|
576
|
+
* @param {Set<string>?} [conditions=CONDITIONS] - Export conditions
|
|
577
|
+
* @return {Nullable<URL>} Resolved package target URL or `null`
|
|
578
|
+
* @throws {NodeError<Error | TypeError>}
|
|
579
|
+
*/
|
|
580
|
+
public resolvePackageTarget(
|
|
581
|
+
dir: ModuleId,
|
|
582
|
+
target: Exports | undefined,
|
|
583
|
+
subpath: string,
|
|
584
|
+
key: string,
|
|
585
|
+
parent: ModuleId,
|
|
586
|
+
internal: boolean = false,
|
|
587
|
+
condition: string = 'default',
|
|
588
|
+
conditions: Set<string> = CONDITIONS
|
|
589
|
+
): Nullable<URL> {
|
|
590
|
+
/**
|
|
591
|
+
* Resolved package target URL.
|
|
592
|
+
*
|
|
593
|
+
* @var {Nullable<URL>} url
|
|
594
|
+
*/
|
|
595
|
+
let url: Nullable<URL> = null
|
|
596
|
+
|
|
597
|
+
// try resolving package target
|
|
598
|
+
switch (true) {
|
|
599
|
+
case target === null:
|
|
600
|
+
break
|
|
601
|
+
case Array.isArray(target):
|
|
602
|
+
/**
|
|
603
|
+
* Possible package target resolution error.
|
|
604
|
+
*
|
|
605
|
+
* @var {NodeError | undefined} error
|
|
606
|
+
*/
|
|
607
|
+
let error: NodeError | undefined
|
|
608
|
+
|
|
609
|
+
// try resolving package target based on first match in target array
|
|
610
|
+
for (const item of target as string[]) {
|
|
611
|
+
try {
|
|
612
|
+
url = this.resolvePackageTarget(
|
|
613
|
+
dir,
|
|
614
|
+
item,
|
|
615
|
+
subpath,
|
|
616
|
+
key,
|
|
617
|
+
parent,
|
|
618
|
+
internal,
|
|
619
|
+
condition,
|
|
620
|
+
conditions
|
|
621
|
+
)
|
|
622
|
+
} catch (e: unknown) {
|
|
623
|
+
error = e as NodeError
|
|
624
|
+
|
|
625
|
+
/* c8 ignore next */
|
|
626
|
+
if (error.code !== ErrorCode.ERR_INVALID_PACKAGE_TARGET) throw error
|
|
627
|
+
|
|
628
|
+
continue
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// stop resolution attempts if target was resolved
|
|
632
|
+
if (url) break
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// throw if error was encountered and package target was not resolved
|
|
636
|
+
if (error && !url) throw error
|
|
637
|
+
|
|
638
|
+
break
|
|
639
|
+
case typeof target === 'object':
|
|
640
|
+
target = target as Record<string, Exports>
|
|
641
|
+
|
|
642
|
+
for (const prop of Object.getOwnPropertyNames(target)) {
|
|
643
|
+
// ensure prop is not an array index
|
|
644
|
+
if (isArrayIndex(prop)) {
|
|
645
|
+
throw new ERR_INVALID_PACKAGE_CONFIG(
|
|
646
|
+
fileURLToPath(dir),
|
|
647
|
+
fileURLToPath(parent),
|
|
648
|
+
'"exports" cannot contain numeric property keys'
|
|
649
|
+
)
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// try resolving package target based on condition
|
|
653
|
+
if (prop === condition || conditions.has(prop)) {
|
|
654
|
+
url = this.resolvePackageTarget(
|
|
655
|
+
dir,
|
|
656
|
+
target[prop],
|
|
657
|
+
subpath,
|
|
658
|
+
key,
|
|
659
|
+
parent,
|
|
660
|
+
internal,
|
|
661
|
+
condition,
|
|
662
|
+
conditions
|
|
663
|
+
)
|
|
664
|
+
|
|
665
|
+
// stop resolution attempts if target was resolved
|
|
666
|
+
if (url) break
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
break
|
|
671
|
+
case typeof target === 'string':
|
|
672
|
+
target = target as string
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Subpath pattern check.
|
|
676
|
+
*
|
|
677
|
+
* @const {boolean} pattern
|
|
678
|
+
*/
|
|
679
|
+
const pattern: boolean = key.includes(PATTERN_CHARACTER)
|
|
680
|
+
|
|
681
|
+
switch (true) {
|
|
682
|
+
case subpath && !pattern && !target.endsWith(pathe.sep):
|
|
683
|
+
throw new ERR_INVALID_PACKAGE_TARGET(
|
|
684
|
+
fileURLToPath(dir),
|
|
685
|
+
key,
|
|
686
|
+
target,
|
|
687
|
+
internal,
|
|
688
|
+
fileURLToPath(parent)
|
|
689
|
+
)
|
|
690
|
+
case internal && PACKAGE_NAME_REGEX.test(target):
|
|
691
|
+
url = this.resolvePackage(target, parent, condition, conditions)
|
|
692
|
+
break
|
|
693
|
+
case target.startsWith('.' + pathe.sep):
|
|
694
|
+
/**
|
|
695
|
+
* Returns a regex pattern matching invalid path segments.
|
|
696
|
+
*
|
|
697
|
+
* Invalid segments include:
|
|
698
|
+
*
|
|
699
|
+
* - `'node_modules/'` (decoded and encoded)
|
|
700
|
+
*
|
|
701
|
+
* @param {'deprecated'?} type - Pattern type
|
|
702
|
+
* @return {RegExp} Regex pattern matching invalid path segments
|
|
703
|
+
*/
|
|
704
|
+
const invalidSegmentRegex = (type?: 'deprecated'): RegExp => {
|
|
705
|
+
return new RegExp(
|
|
706
|
+
`(^|\\\\|\\/)((\\.|%2e)(\\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))${
|
|
707
|
+
type === 'deprecated' ? '' : '?'
|
|
708
|
+
}(\\\\|\\/|$)`,
|
|
709
|
+
'i'
|
|
710
|
+
)
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// check target for invalid segments
|
|
714
|
+
if (invalidSegmentRegex().test(target.slice(2))) {
|
|
715
|
+
if (invalidSegmentRegex('deprecated').test(target.slice(2))) {
|
|
716
|
+
throw new ERR_INVALID_PACKAGE_TARGET(
|
|
717
|
+
fileURLToPath(dir),
|
|
718
|
+
key,
|
|
719
|
+
target,
|
|
720
|
+
internal,
|
|
721
|
+
fileURLToPath(parent)
|
|
722
|
+
)
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// set resolved package target url using target
|
|
727
|
+
url = new URL(
|
|
728
|
+
target,
|
|
729
|
+
pathToFileURL(fileURLToPath(dir).replace(/\/$/, '') + pathe.sep)
|
|
730
|
+
)
|
|
731
|
+
|
|
732
|
+
// check subpath for invalid segments
|
|
733
|
+
if (subpath) {
|
|
734
|
+
if (invalidSegmentRegex().test(subpath)) {
|
|
735
|
+
if (invalidSegmentRegex('deprecated').test(subpath)) {
|
|
736
|
+
throw new ERR_INVALID_MODULE_SPECIFIER(
|
|
737
|
+
key.replace(PATTERN_CHARACTER, subpath),
|
|
738
|
+
[
|
|
739
|
+
`request is not a valid match in pattern "${key}" for`,
|
|
740
|
+
`the "${internal ? 'imports' : 'exports'}" resolution`,
|
|
741
|
+
`of ${fileURLToPath(dir).replace(/\/$/, '')}/package.json`
|
|
742
|
+
].join(' '),
|
|
743
|
+
fileURLToPath(parent)
|
|
744
|
+
)
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// set resolved package target url using subpath
|
|
749
|
+
url = new URL(
|
|
750
|
+
url.href.replace(
|
|
751
|
+
new RegExp(regexp(PATTERN_CHARACTER), 'g'),
|
|
752
|
+
subpath
|
|
753
|
+
)
|
|
754
|
+
)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
break
|
|
758
|
+
default:
|
|
759
|
+
throw new ERR_INVALID_PACKAGE_TARGET(
|
|
760
|
+
fileURLToPath(dir),
|
|
761
|
+
key,
|
|
762
|
+
target,
|
|
763
|
+
internal,
|
|
764
|
+
fileURLToPath(parent)
|
|
765
|
+
)
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
break
|
|
769
|
+
default:
|
|
770
|
+
throw new ERR_INVALID_PACKAGE_TARGET(
|
|
771
|
+
fileURLToPath(dir),
|
|
772
|
+
key,
|
|
773
|
+
target,
|
|
774
|
+
internal,
|
|
775
|
+
fileURLToPath(parent)
|
|
776
|
+
)
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
return url
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
export default Resolver
|