bare-module-traverse 2.5.2 → 2.5.3

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/index.d.ts CHANGED
@@ -34,7 +34,18 @@ type AliasableExtension =
34
34
  | '.txt'
35
35
 
36
36
  interface TraverseOptions extends ResolveOptions {
37
+ /**
38
+ * The type assumed for a module that names none of its own, such as an
39
+ * ambiguous extension or a `data:` URL that no import reaches. See
40
+ * `constants` for possible values.
41
+ */
37
42
  defaultType?: number
43
+ /**
44
+ * The type of the import that names the entry. A `data:` URL of JavaScript
45
+ * takes its type from this: `REQUIRE` names a script and `IMPORT` a module,
46
+ * while `IMPORT | DYNAMIC` names either and so must carry a `type` attribute.
47
+ */
48
+ importType?: number
38
49
  artifacts?: boolean
39
50
  aliases?: Record<string, AliasableExtension>
40
51
  /**
package/index.js CHANGED
@@ -271,7 +271,7 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
271
271
  if (type === constants.SCRIPT || type === constants.MODULE) {
272
272
  yield* exports.imports(url, source, imports, artifacts, lexer, visited, {
273
273
  ...opts,
274
- referrerType: type
274
+ importType: 0
275
275
  })
276
276
  } else if (type === constants.ADDON && artifacts !== null) {
277
277
  yield* exports.addons(url, artifacts, visited, opts)
@@ -363,7 +363,7 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
363
363
  children: exports.module(url, null, {}, artifacts, visited, {
364
364
  ...opts,
365
365
  asset: true,
366
- referrerType: type
366
+ importType: 0
367
367
  }),
368
368
  deferred: true
369
369
  }
@@ -376,7 +376,7 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
376
376
  yield {
377
377
  children: exports.module(url, null, {}, artifacts, visited, {
378
378
  ...opts,
379
- referrerType: type
379
+ importType: 0
380
380
  }),
381
381
  deferred: false
382
382
  }
@@ -611,7 +611,10 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
611
611
  }
612
612
 
613
613
  yield {
614
- children: exports.module(resolution, source, attributes, artifacts, visited, opts),
614
+ children: exports.module(resolution, source, attributes, artifacts, visited, {
615
+ ...opts,
616
+ importType: entry.type
617
+ }),
615
618
  deferred: false
616
619
  }
617
620
 
@@ -876,22 +879,22 @@ function typeForAttribute(type) {
876
879
  }
877
880
 
878
881
  function dataURLModuleType(url, attributes, opts = {}) {
879
- const { defaultType = constants.SCRIPT, referrerType } = opts
880
-
881
882
  const { mime } = parseDataURL(url)
882
883
 
884
+ if (mime === null) {
885
+ throw errors.UNKNOWN_DATA_URL_MEDIA_TYPE(`Data URL '${url.href}' does not declare a media type`)
886
+ }
887
+
883
888
  const asserted = typeof attributes.type === 'string' ? typeForAttribute(attributes.type) : null
884
889
 
885
- if (mime === null || mime.subtype === 'javascript') {
890
+ if (mime.subtype === 'javascript') {
886
891
  if (asserted === constants.SCRIPT || asserted === constants.MODULE) return asserted
887
892
 
888
893
  if (asserted !== null) {
889
894
  throw errors.TYPE_INCOMPATIBLE(`Module '${url.href}' is not of type '${attributes.type}'`)
890
895
  }
891
896
 
892
- if (referrerType) return referrerType
893
-
894
- return defaultType === constants.MODULE ? constants.MODULE : constants.SCRIPT
897
+ return javaScriptDataURLModuleType(url, opts)
895
898
  }
896
899
 
897
900
  let type
@@ -915,6 +918,27 @@ function dataURLModuleType(url, attributes, opts = {}) {
915
918
  return type
916
919
  }
917
920
 
921
+ // A data URL carries no extension and 'text/javascript' spans both module
922
+ // systems, so the type follows how the module was named: `require()` names a
923
+ // script and `import` a module. A dynamic `import()` names either and so must
924
+ // say which, as must a caller naming a data URL with no import to read, who
925
+ // answers with `defaultType`.
926
+ function javaScriptDataURLModuleType(url, opts) {
927
+ const { defaultType = constants.SCRIPT, importType = 0 } = opts
928
+
929
+ if (importType & lex.constants.REQUIRE) return constants.SCRIPT
930
+
931
+ if (importType & lex.constants.IMPORT) {
932
+ if ((importType & lex.constants.DYNAMIC) === 0) return constants.MODULE
933
+
934
+ throw errors.AMBIGUOUS_MODULE_TYPE(
935
+ `Module '${url.href}' must declare whether it is of type 'script' or 'module'`
936
+ )
937
+ }
938
+
939
+ return defaultType === constants.MODULE ? constants.MODULE : constants.SCRIPT
940
+ }
941
+
918
942
  function parseDataURL(url) {
919
943
  const { pathname } = url
920
944
 
package/lib/errors.js CHANGED
@@ -49,6 +49,14 @@ module.exports = class ModuleTraverseError extends Error {
49
49
  return new ModuleTraverseError(msg, ModuleTraverseError.TYPE_INCOMPATIBLE)
50
50
  }
51
51
 
52
+ static AMBIGUOUS_MODULE_TYPE(msg) {
53
+ return new ModuleTraverseError(msg, ModuleTraverseError.AMBIGUOUS_MODULE_TYPE)
54
+ }
55
+
56
+ static UNKNOWN_DATA_URL_MEDIA_TYPE(msg) {
57
+ return new ModuleTraverseError(msg, ModuleTraverseError.UNKNOWN_DATA_URL_MEDIA_TYPE)
58
+ }
59
+
52
60
  static UNKNOWN_DATA_URL_CHARSET(msg, charset) {
53
61
  const err = new ModuleTraverseError(msg, ModuleTraverseError.UNKNOWN_DATA_URL_CHARSET)
54
62
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.5.2",
3
+ "version": "2.5.3",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {