@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,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file findSubpath
|
|
3
|
+
* @module mlly/utils/findSubpath
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { FindSubpathOptions } from '#src/interfaces'
|
|
7
|
+
import getSubpaths from '#src/internal/get-subpaths'
|
|
8
|
+
import validateBoolean from '#src/internal/validate-boolean'
|
|
9
|
+
import validateSet from '#src/internal/validate-set'
|
|
10
|
+
import validateString from '#src/internal/validate-string'
|
|
11
|
+
import validateURLString from '#src/internal/validate-url-string'
|
|
12
|
+
import type { NodeError } from '@flex-development/errnode'
|
|
13
|
+
import pathe from '@flex-development/pathe'
|
|
14
|
+
import type { Exports, Imports } from '@flex-development/pkg-types'
|
|
15
|
+
import { isNIL, type Nullable } from '@flex-development/tutils'
|
|
16
|
+
import { URL } from 'node:url'
|
|
17
|
+
import compareSubpaths from './compare-subpaths'
|
|
18
|
+
import CONDITIONS from './conditions'
|
|
19
|
+
import isExportsSugar from './is-exports-sugar'
|
|
20
|
+
import PATTERN_CHARACTER from './pattern-character'
|
|
21
|
+
import toURL from './to-url'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Finds the subpath defined in `context`, a `package.json` [`exports`][1] or
|
|
25
|
+
* [`imports`][2] field, that maps to the given package `target`.
|
|
26
|
+
*
|
|
27
|
+
* Supports extensionless targets. Returns `null` if a subpath is not found.
|
|
28
|
+
*
|
|
29
|
+
* [1]: https://nodejs.org/api/packages.html#exports
|
|
30
|
+
* [2]: https://nodejs.org/api/packages.html#imports
|
|
31
|
+
*
|
|
32
|
+
* @see {@linkcode Exports}
|
|
33
|
+
* @see {@linkcode FindSubpathOptions}
|
|
34
|
+
* @see {@linkcode Imports}
|
|
35
|
+
* @see https://nodejs.org/api/packages.html#subpath-exports
|
|
36
|
+
* @see https://nodejs.org/api/packages.html#subpath-imports
|
|
37
|
+
*
|
|
38
|
+
* @param {string} target - Package target to find in `context`
|
|
39
|
+
* @param {Exports | Imports | undefined} context - Package context
|
|
40
|
+
* @param {FindSubpathOptions} options - Search options
|
|
41
|
+
* @return {Nullable<string>} Subpath defined in `context` or `null`
|
|
42
|
+
* @throws {NodeError<Error | TypeError>}
|
|
43
|
+
*/
|
|
44
|
+
const findSubpath = (
|
|
45
|
+
target: string,
|
|
46
|
+
context: Exports | Imports | undefined,
|
|
47
|
+
options: FindSubpathOptions
|
|
48
|
+
): Nullable<string> => {
|
|
49
|
+
const {
|
|
50
|
+
condition = 'default',
|
|
51
|
+
conditions = CONDITIONS,
|
|
52
|
+
dir,
|
|
53
|
+
internal = false,
|
|
54
|
+
parent
|
|
55
|
+
} = options
|
|
56
|
+
|
|
57
|
+
// exit early if context is nil
|
|
58
|
+
if (isNIL(context)) return null
|
|
59
|
+
|
|
60
|
+
// ensure target is a string
|
|
61
|
+
validateString(target, 'target')
|
|
62
|
+
|
|
63
|
+
// exit early if target is an exactish match
|
|
64
|
+
if (typeof context === 'string') {
|
|
65
|
+
if (target === context || target === pathe.changeExt(context, '')) {
|
|
66
|
+
return '.'
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ensure option schemas
|
|
71
|
+
validateString(condition, 'options.condition')
|
|
72
|
+
validateSet(conditions, 'options.conditions')
|
|
73
|
+
validateURLString(dir, 'options.dir')
|
|
74
|
+
validateBoolean(internal, 'options.internal')
|
|
75
|
+
validateURLString(parent, 'options.parent')
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Finds the subpath defined in `context`, a `package.json` [`exports`][1] or
|
|
79
|
+
* [`imports`][2] field, that maps to the given package `target`.
|
|
80
|
+
*
|
|
81
|
+
* Returns `null` if a subpath is not found.
|
|
82
|
+
*
|
|
83
|
+
* [1]: https://nodejs.org/api/packages.html#exports
|
|
84
|
+
* [2]: https://nodejs.org/api/packages.html#imports
|
|
85
|
+
*
|
|
86
|
+
* @param {string} target - Package target to find in `context`
|
|
87
|
+
* @param {Exports | Imports | undefined} context - Package context
|
|
88
|
+
* @param {string} [key='.'] - Subpath in `context` being checked
|
|
89
|
+
* @return {Nullable<string>} Subpath defined in `context` or `null`
|
|
90
|
+
*/
|
|
91
|
+
const find = (
|
|
92
|
+
target: string,
|
|
93
|
+
context: Exports | Imports | undefined,
|
|
94
|
+
key: string = '.'
|
|
95
|
+
): Nullable<string> => {
|
|
96
|
+
/**
|
|
97
|
+
* Subpath defined in {@linkcode context} that maps to {@linkcode target}.
|
|
98
|
+
*
|
|
99
|
+
* @var {Nullable<string>} subpath
|
|
100
|
+
*/
|
|
101
|
+
let subpath: Nullable<string> = null
|
|
102
|
+
|
|
103
|
+
// match target to subpath
|
|
104
|
+
switch (true) {
|
|
105
|
+
case !isNIL(context) && typeof context === 'object':
|
|
106
|
+
case typeof context === 'string':
|
|
107
|
+
/**
|
|
108
|
+
* URL of directory containing relevant `package.json` file.
|
|
109
|
+
*
|
|
110
|
+
* @const {string} pkgdir
|
|
111
|
+
*/
|
|
112
|
+
const pkgdir: string = toURL(dir).href.replace(/\/$/, '') + pathe.sep
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* URL of relevant `package.json` file.
|
|
116
|
+
*
|
|
117
|
+
* @const {URL} pkg
|
|
118
|
+
*/
|
|
119
|
+
const pkg: URL = new URL('package.json', pkgdir)
|
|
120
|
+
|
|
121
|
+
// convert package context to object if using exports sugar
|
|
122
|
+
if (!internal && isExportsSugar(context, pkg, parent)) {
|
|
123
|
+
context = { [key]: context } as Record<string, Exports>
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// context is now an object
|
|
127
|
+
context = context as Record<string, Exports>
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Subpaths defined in {@linkcode context}.
|
|
131
|
+
*
|
|
132
|
+
* **Note**: Sorted from least to greatest.
|
|
133
|
+
*
|
|
134
|
+
* @see {@linkcode compareSubpaths}
|
|
135
|
+
*
|
|
136
|
+
* @const {string[]} keys
|
|
137
|
+
*/
|
|
138
|
+
const subpaths: string[] = getSubpaths(
|
|
139
|
+
context,
|
|
140
|
+
internal,
|
|
141
|
+
pkg,
|
|
142
|
+
parent
|
|
143
|
+
).sort((s1, s2) => compareSubpaths(s1, s2) * -1)
|
|
144
|
+
|
|
145
|
+
// match target to subpath defined in context
|
|
146
|
+
for (const pkgsubpath of subpaths) {
|
|
147
|
+
/**
|
|
148
|
+
* Current package target being checked.
|
|
149
|
+
*
|
|
150
|
+
* @var {Exports} tar
|
|
151
|
+
*/
|
|
152
|
+
let tar: Exports = context[pkgsubpath]!
|
|
153
|
+
|
|
154
|
+
// find subpath
|
|
155
|
+
switch (true) {
|
|
156
|
+
case Array.isArray(tar):
|
|
157
|
+
tar = tar as string[]
|
|
158
|
+
|
|
159
|
+
// try matching target based first match in target array
|
|
160
|
+
for (const item of tar) {
|
|
161
|
+
subpath = find(target, item, pkgsubpath)
|
|
162
|
+
if (subpath) break
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
break
|
|
166
|
+
case typeof tar === 'object' && !isNIL(tar):
|
|
167
|
+
tar = tar as Record<string, Exports>
|
|
168
|
+
|
|
169
|
+
// try matching target based on export conditions
|
|
170
|
+
for (const prop of Object.getOwnPropertyNames(tar)) {
|
|
171
|
+
if (prop === condition || conditions.has(prop)) {
|
|
172
|
+
subpath = find(target, tar[prop], pkgsubpath)
|
|
173
|
+
if (subpath) break
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
break
|
|
178
|
+
case typeof tar === 'string':
|
|
179
|
+
tar = tar as string
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* {@linkcode tar} without file extension.
|
|
183
|
+
*
|
|
184
|
+
* @const {string} tar_no_ext
|
|
185
|
+
*/
|
|
186
|
+
const tar_ne: string = pathe.changeExt(tar, '')
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Index of {@linkcode PATTERN_CHARACTER} in {@linkcode tar}.
|
|
190
|
+
*
|
|
191
|
+
* @const {number} pattern
|
|
192
|
+
*/
|
|
193
|
+
const pattern: number = tar.indexOf(PATTERN_CHARACTER)
|
|
194
|
+
|
|
195
|
+
switch (true) {
|
|
196
|
+
// target is an exactish match
|
|
197
|
+
case target === tar:
|
|
198
|
+
case target === tar_ne:
|
|
199
|
+
case pattern === -1 && (target === tar || target === tar_ne):
|
|
200
|
+
subpath = pkgsubpath
|
|
201
|
+
break
|
|
202
|
+
// pattern character => try finding best match for target
|
|
203
|
+
case pattern !== -1 && target.startsWith(tar.slice(0, pattern)):
|
|
204
|
+
/**
|
|
205
|
+
* Boolean indicating if {@linkcode target} ends with the
|
|
206
|
+
* characters after the pattern character (`*`) in
|
|
207
|
+
* {@linkcode tar}.
|
|
208
|
+
*
|
|
209
|
+
* @const {boolean} match
|
|
210
|
+
*/
|
|
211
|
+
const match: boolean =
|
|
212
|
+
target.length >= tar.length &&
|
|
213
|
+
tar.lastIndexOf(PATTERN_CHARACTER) === pattern &&
|
|
214
|
+
(target.endsWith(tar.slice(pattern + 1)) ||
|
|
215
|
+
target.endsWith(tar_ne.slice(pattern + 1)))
|
|
216
|
+
|
|
217
|
+
// set subpath if match was found
|
|
218
|
+
if (match) subpath = pkgsubpath
|
|
219
|
+
|
|
220
|
+
break
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
break
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// stop searching for subpath if subpath has been found
|
|
227
|
+
if (subpath) break
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
break
|
|
231
|
+
default:
|
|
232
|
+
break
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return subpath
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return find(target, context)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export default findSubpath
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file getFormat
|
|
3
|
+
* @module mlly/utils/getFormat
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Format } from '#src/enums'
|
|
7
|
+
import type { GetFormatOptions, PackageScope } from '#src/interfaces'
|
|
8
|
+
import type { ModuleId } from '#src/types'
|
|
9
|
+
import {
|
|
10
|
+
ERR_UNKNOWN_FILE_EXTENSION,
|
|
11
|
+
type NodeError
|
|
12
|
+
} from '@flex-development/errnode'
|
|
13
|
+
import { isBuiltin } from '@flex-development/is-builtin'
|
|
14
|
+
import pathe, { type Ext } from '@flex-development/pathe'
|
|
15
|
+
import type { EmptyString, Nilable, Nullable } from '@flex-development/tutils'
|
|
16
|
+
import type { URL } from 'node:url'
|
|
17
|
+
import EXTENSION_FORMAT_MAP from './extension-format-map'
|
|
18
|
+
import lookupPackageScope from './lookup-package-scope'
|
|
19
|
+
import parseDataURL from './parse-data-url'
|
|
20
|
+
import toURL from './to-url'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves a module format for the given module `id`.
|
|
24
|
+
*
|
|
25
|
+
* @see {@linkcode Format}
|
|
26
|
+
* @see {@linkcode GetFormatOptions}
|
|
27
|
+
* @see {@linkcode ModuleId}
|
|
28
|
+
*
|
|
29
|
+
* @async
|
|
30
|
+
*
|
|
31
|
+
* @param {ModuleId} id - Module id to evaluate
|
|
32
|
+
* @param {GetFormatOptions?} [options={}] - Format retrieval options
|
|
33
|
+
* @return {Promise<Nilable<Format>>} Module format, `null`, or `undefined`
|
|
34
|
+
* @throws {NodeError<TypeError>}
|
|
35
|
+
*/
|
|
36
|
+
const getFormat = async (
|
|
37
|
+
id: ModuleId,
|
|
38
|
+
options: GetFormatOptions = {}
|
|
39
|
+
): Promise<Nilable<Format>> => {
|
|
40
|
+
const {
|
|
41
|
+
base,
|
|
42
|
+
experimental_json_modules = true,
|
|
43
|
+
experimental_network_imports = false,
|
|
44
|
+
experimental_wasm_modules = false,
|
|
45
|
+
extension_format_map = EXTENSION_FORMAT_MAP,
|
|
46
|
+
ignore_errors = false,
|
|
47
|
+
req
|
|
48
|
+
} = options
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Module {@linkcode id} as {@linkcode URL}.
|
|
52
|
+
*
|
|
53
|
+
* @const {URL} url
|
|
54
|
+
*/
|
|
55
|
+
const url: URL = toURL(id, base)
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Extracts a [MIME type][1] from a {@linkcode URL} href, {@linkcode URL}
|
|
59
|
+
* pathname, or [`Content-Type`][2] header and converts it to a module format.
|
|
60
|
+
*
|
|
61
|
+
* [1]: https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types
|
|
62
|
+
* [2]: https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Type
|
|
63
|
+
*
|
|
64
|
+
* @param {Nullable<string>} value - `URL` component or `content-type` header
|
|
65
|
+
* @param {boolean} [content_type=false] - `value` is `content-type` header?
|
|
66
|
+
* @return {Nilable<Format>} Module format, `null`, or `undefined`
|
|
67
|
+
*/
|
|
68
|
+
const mimeToFormat = (
|
|
69
|
+
value: Nullable<string>,
|
|
70
|
+
content_type: boolean = false
|
|
71
|
+
): Nilable<Format> => {
|
|
72
|
+
/**
|
|
73
|
+
* Module format for {@linkcode value}.
|
|
74
|
+
*
|
|
75
|
+
* @var {Nilable<Format>} format
|
|
76
|
+
*/
|
|
77
|
+
let format: Nilable<Format> = null
|
|
78
|
+
|
|
79
|
+
// do nothing if value is empty
|
|
80
|
+
/* c8 ignore next */ if (!value) return format
|
|
81
|
+
|
|
82
|
+
// sanitize content-type header
|
|
83
|
+
if (content_type) value = value.replace(/;.*/, ',')
|
|
84
|
+
|
|
85
|
+
// add data: protocol to value
|
|
86
|
+
if (!value.startsWith('data:')) value = `data:${value}`
|
|
87
|
+
|
|
88
|
+
// get format based on mime type
|
|
89
|
+
try {
|
|
90
|
+
switch (parseDataURL(value).mime) {
|
|
91
|
+
case 'application/javascript':
|
|
92
|
+
case 'application/typescript':
|
|
93
|
+
case 'text/javascript':
|
|
94
|
+
format = Format.MODULE
|
|
95
|
+
break
|
|
96
|
+
case 'application/json':
|
|
97
|
+
format = experimental_json_modules ? Format.JSON : null
|
|
98
|
+
break
|
|
99
|
+
case 'application/wasm':
|
|
100
|
+
format = experimental_wasm_modules ? Format.WASM : null
|
|
101
|
+
break
|
|
102
|
+
default:
|
|
103
|
+
break
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
format = ignore_errors ? undefined : null
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return format
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Module format for {@linkcode id}.
|
|
114
|
+
*
|
|
115
|
+
* @var {Nilable<Format>} format
|
|
116
|
+
*/
|
|
117
|
+
let format: Nilable<Format> = null
|
|
118
|
+
|
|
119
|
+
// get format based on url protocol
|
|
120
|
+
switch (url.protocol) {
|
|
121
|
+
case 'data:':
|
|
122
|
+
format = mimeToFormat(url.href)
|
|
123
|
+
break
|
|
124
|
+
case 'file:':
|
|
125
|
+
/**
|
|
126
|
+
* File extension of {@linkcode url.pathname}.
|
|
127
|
+
*
|
|
128
|
+
* @const {EmptyString | Ext}
|
|
129
|
+
*/
|
|
130
|
+
const ext: EmptyString | Ext = pathe.extname(url.pathname)
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Package scope result for {@linkcode url}.
|
|
134
|
+
*
|
|
135
|
+
* @const {Nullable<PackageScope>} scope
|
|
136
|
+
*/
|
|
137
|
+
const scope: Nullable<PackageScope> = lookupPackageScope(url)
|
|
138
|
+
|
|
139
|
+
switch (true) {
|
|
140
|
+
// get format based on package type
|
|
141
|
+
case scope && ext === '.js':
|
|
142
|
+
format =
|
|
143
|
+
scope!.pkgjson.type === Format.MODULE
|
|
144
|
+
? Format.MODULE
|
|
145
|
+
: Format.COMMONJS
|
|
146
|
+
break
|
|
147
|
+
// get format based on file extension
|
|
148
|
+
case extension_format_map.has(ext):
|
|
149
|
+
format = extension_format_map.get(ext)!
|
|
150
|
+
|
|
151
|
+
switch (true) {
|
|
152
|
+
case format === Format.JSON && !experimental_json_modules:
|
|
153
|
+
case format === Format.WASM && !experimental_wasm_modules:
|
|
154
|
+
format = ignore_errors ? undefined : null
|
|
155
|
+
break
|
|
156
|
+
default:
|
|
157
|
+
break
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
break
|
|
161
|
+
// ignore file extension errors
|
|
162
|
+
case ignore_errors:
|
|
163
|
+
format = undefined
|
|
164
|
+
break
|
|
165
|
+
default:
|
|
166
|
+
/**
|
|
167
|
+
* Recommended fix for [`ERR_UNKNOWN_FILE_EXTENSION`][1].
|
|
168
|
+
*
|
|
169
|
+
* [1]: https://nodejs.org/api/errors.html#err_unknown_file_extension
|
|
170
|
+
*
|
|
171
|
+
* @var {string?} suggestion
|
|
172
|
+
*/
|
|
173
|
+
let suggestion: string | undefined
|
|
174
|
+
|
|
175
|
+
// add recommended fix for ERR_UNKNOWN_FILE_EXTENSION if package is
|
|
176
|
+
// esm-only and module id does not include file extension
|
|
177
|
+
if (scope && scope.pkgjson.type === Format.MODULE && ext === '') {
|
|
178
|
+
const { pkg } = scope
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Basename of {@linkcode url.pathname}
|
|
182
|
+
*
|
|
183
|
+
* @const {string} basename
|
|
184
|
+
*/
|
|
185
|
+
const basename: string = pathe.basename(url.pathname)
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Relative path from {@linkcode pkg} to {@linkcode url.pathname}.
|
|
189
|
+
*
|
|
190
|
+
* @const {string} relative
|
|
191
|
+
*/
|
|
192
|
+
const relative: string = pathe.relative(pkg, url.pathname).slice(1)
|
|
193
|
+
|
|
194
|
+
suggestion = `Loading extensionless files is not supported inside of "type":"module" package.json contexts. The package.json file ${pkg} caused this "type":"module" context. Try changing ${url.pathname} to have a file extension. Note the "bin" field of package.json can point to a file with an extension, for example {"type":"module","bin":{"${basename}":"${relative}.js"}}`
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, url.pathname, suggestion)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
break
|
|
201
|
+
case 'http:':
|
|
202
|
+
case 'https:':
|
|
203
|
+
if (experimental_network_imports) {
|
|
204
|
+
const { default: fetch } = await import('node-fetch')
|
|
205
|
+
const { headers } = await fetch(url.href, req)
|
|
206
|
+
format = mimeToFormat(headers.get('content-type'), true)
|
|
207
|
+
} else {
|
|
208
|
+
format = ignore_errors ? undefined : null
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
break
|
|
212
|
+
case 'node:':
|
|
213
|
+
if (isBuiltin(url)) format = Format.BUILTIN
|
|
214
|
+
break
|
|
215
|
+
default:
|
|
216
|
+
break
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return format
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export default getFormat
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file getSource
|
|
3
|
+
* @module mlly/utils/getSource
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Format } from '#src/enums'
|
|
7
|
+
import type { GetSourceOptions } from '#src/interfaces'
|
|
8
|
+
import type { ModuleId } from '#src/types'
|
|
9
|
+
import {
|
|
10
|
+
ERR_UNSUPPORTED_ESM_URL_SCHEME,
|
|
11
|
+
type NodeError
|
|
12
|
+
} from '@flex-development/errnode'
|
|
13
|
+
import fs from 'node:fs/promises'
|
|
14
|
+
import os from 'node:os'
|
|
15
|
+
import type { URL } from 'node:url'
|
|
16
|
+
import util from 'node:util'
|
|
17
|
+
import parseDataURL from './parse-data-url'
|
|
18
|
+
import toURL from './to-url'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves source code for the given module `id`.
|
|
22
|
+
*
|
|
23
|
+
* @see {@linkcode GetSourceOptions}
|
|
24
|
+
* @see {@linkcode ModuleId}
|
|
25
|
+
* @see https://nodejs.org/docs/latest-v19.x/api/esm.html#loadurl-context-nextload
|
|
26
|
+
*
|
|
27
|
+
* @async
|
|
28
|
+
*
|
|
29
|
+
* @param {ModuleId} id - Module id to evaluate
|
|
30
|
+
* @param {GetSourceOptions?} [options={}] - Source code retrieval options
|
|
31
|
+
* @return {Promise<Uint8Array | string | undefined>} Source code for `id`
|
|
32
|
+
* @throws {NodeError} If unsupported URL scheme is encountered
|
|
33
|
+
*/
|
|
34
|
+
const getSource = async (
|
|
35
|
+
id: ModuleId,
|
|
36
|
+
options: GetSourceOptions = {}
|
|
37
|
+
): Promise<Uint8Array | string | undefined> => {
|
|
38
|
+
const {
|
|
39
|
+
base,
|
|
40
|
+
experimental_network_imports = false,
|
|
41
|
+
format,
|
|
42
|
+
ignore_errors = false,
|
|
43
|
+
req
|
|
44
|
+
} = options
|
|
45
|
+
|
|
46
|
+
// exit early if format is Format.BUILTIN
|
|
47
|
+
if (format === Format.BUILTIN) return undefined
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Module {@linkcode id} as {@linkcode URL}.
|
|
51
|
+
*
|
|
52
|
+
* @const {URL} url
|
|
53
|
+
*/
|
|
54
|
+
const url: URL = toURL(id, base)
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* [`ERR_UNSUPPORTED_ESM_URL_SCHEME`][1] check.
|
|
58
|
+
*
|
|
59
|
+
* [1]: https://nodejs.org/api/errors.html#err_unsupported_esm_url_scheme
|
|
60
|
+
*
|
|
61
|
+
* @var {boolean} err
|
|
62
|
+
*/
|
|
63
|
+
let err: boolean = false
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Source code for {@linkcode id}.
|
|
67
|
+
*
|
|
68
|
+
* @var {Uint8Array | string | undefined} source
|
|
69
|
+
*/
|
|
70
|
+
let source: Uint8Array | string | undefined = ''
|
|
71
|
+
|
|
72
|
+
// get source code based on url protocol
|
|
73
|
+
switch (url.protocol) {
|
|
74
|
+
case 'data:':
|
|
75
|
+
const { base64, data } = parseDataURL(url)
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Buffer encoding used to create a {@linkcode Buffer} instance from
|
|
79
|
+
* {@linkcode data}.
|
|
80
|
+
*
|
|
81
|
+
* @const {BufferEncoding} encoding
|
|
82
|
+
*/
|
|
83
|
+
const encoding: BufferEncoding = base64 ? 'base64' : 'utf8'
|
|
84
|
+
|
|
85
|
+
source = Buffer.from(decodeURIComponent(data), encoding).toString()
|
|
86
|
+
break
|
|
87
|
+
case 'file:':
|
|
88
|
+
source = await fs.readFile(url, 'utf8')
|
|
89
|
+
break
|
|
90
|
+
case 'http:':
|
|
91
|
+
case 'https:':
|
|
92
|
+
if (experimental_network_imports) {
|
|
93
|
+
const { default: fetch } = await import('node-fetch')
|
|
94
|
+
source = await (await fetch(url.href, req)).text()
|
|
95
|
+
} else {
|
|
96
|
+
err = true
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
break
|
|
100
|
+
case 'node:':
|
|
101
|
+
source = undefined
|
|
102
|
+
break
|
|
103
|
+
default:
|
|
104
|
+
err = true
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// throw if unsupported url scheme was encountered
|
|
108
|
+
if (!ignore_errors && err) {
|
|
109
|
+
/**
|
|
110
|
+
* Supported URL schemes.
|
|
111
|
+
*
|
|
112
|
+
* @const {string[]} schemes
|
|
113
|
+
*/
|
|
114
|
+
const schemes: string[] = ['data', 'file']
|
|
115
|
+
|
|
116
|
+
// update supported schemes if support for network based modules is enabled
|
|
117
|
+
if (experimental_network_imports) schemes.push('http', 'https')
|
|
118
|
+
|
|
119
|
+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(
|
|
120
|
+
url,
|
|
121
|
+
schemes,
|
|
122
|
+
os.platform() === 'win32'
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return format === Format.WASM ? new util.TextEncoder().encode(source) : source
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export default getSource
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file hasCJSSyntax
|
|
3
|
+
* @module mlly/utils/hasCJSSyntax
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import validateString from '#src/internal/validate-string'
|
|
7
|
+
import type { NodeError } from '@flex-development/errnode'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Detects if `code` contains CommonJS syntax. Ignores matches in comments.
|
|
11
|
+
*
|
|
12
|
+
* Syntax patterns:
|
|
13
|
+
*
|
|
14
|
+
* - `__dirname`
|
|
15
|
+
* - `__filename`
|
|
16
|
+
* - `await import`
|
|
17
|
+
* - `exports`
|
|
18
|
+
* - `module.exports`
|
|
19
|
+
* - `require`
|
|
20
|
+
* - `require.*`
|
|
21
|
+
*
|
|
22
|
+
* @see https://regex101.com/r/Co77SJ
|
|
23
|
+
*
|
|
24
|
+
* @param {string} code - Code to evaluate
|
|
25
|
+
* @return {boolean} `true` if `code` contains cjs syntax, `false` otherwise
|
|
26
|
+
* @throws {NodeError<TypeError>} If `code` is not a string
|
|
27
|
+
*/
|
|
28
|
+
const hasCJSSyntax = (code: string): boolean => {
|
|
29
|
+
validateString(code, 'code')
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* CommonJS syntax regex.
|
|
33
|
+
*
|
|
34
|
+
* @see https://regex101.com/r/Co77SJ
|
|
35
|
+
*
|
|
36
|
+
* @const {RegExp} CJS_SYNTAX_REGEX
|
|
37
|
+
*/
|
|
38
|
+
const CJS_SYNTAX_REGEX: RegExp =
|
|
39
|
+
/(?<!(?:\/\/|\*).*)((?:module\.)?exports(?:\.\w+|(?<!\s+=))|require(?=\(.)|require\.\w+|__dirname|__filename|await import)/gm
|
|
40
|
+
|
|
41
|
+
return !!code.match(CJS_SYNTAX_REGEX)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default hasCJSSyntax
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file hasESMSyntax
|
|
3
|
+
* @module mlly/utils/hasESMSyntax
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import validateString from '#src/internal/validate-string'
|
|
7
|
+
import type { NodeError } from '@flex-development/errnode'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Detects if `code` contains ESM syntax. Ignores matches in comments.
|
|
11
|
+
*
|
|
12
|
+
* Syntax patterns:
|
|
13
|
+
*
|
|
14
|
+
* - `export` (declaration, default, named, star)
|
|
15
|
+
* - `import` (default, dynamic, named, star)
|
|
16
|
+
* - `import.meta.(env|resolve|url)`
|
|
17
|
+
*
|
|
18
|
+
* @see https://regex101.com/r/xfreyy
|
|
19
|
+
*
|
|
20
|
+
* @param {string} code - Code to evaluate
|
|
21
|
+
* @return {boolean} `true` if `code` contains esm syntax, `false` otherwise
|
|
22
|
+
* @throws {NodeError<TypeError>} If `code` is not a string
|
|
23
|
+
*/
|
|
24
|
+
const hasESMSyntax = (code: string): boolean => {
|
|
25
|
+
validateString(code, 'code')
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* ESM syntax regex.
|
|
29
|
+
*
|
|
30
|
+
* @see https://regex101.com/r/xfreyy
|
|
31
|
+
*
|
|
32
|
+
* @const {RegExp} ESM_SYNTAX_REGEX
|
|
33
|
+
*/
|
|
34
|
+
const ESM_SYNTAX_REGEX: RegExp =
|
|
35
|
+
/(?<!(?:\/\/|\*).*)((?:export|import)[\s\w*,{}]*(?=\sfrom)|export\b\s*(?:[*{]|async function|(?:abstract\s)?class|const|default|enum|function|interface|let|type|var)|await import|import\.meta\.(?:env(?:\.\w+)?|resolve|url))/gm
|
|
36
|
+
|
|
37
|
+
return !!code.match(ESM_SYNTAX_REGEX)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default hasESMSyntax
|