@jsenv/core 28.1.2 → 28.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/js/script_type_module_supervisor.js +8 -13
  2. package/dist/js/supervisor.js +690 -504
  3. package/dist/main.js +13384 -13228
  4. package/package.json +6 -6
  5. package/readme.md +3 -3
  6. package/src/build/build.js +980 -713
  7. package/src/build/inject_global_version_mappings.js +5 -20
  8. package/src/build/start_build_server.js +2 -2
  9. package/src/dev/start_dev_server.js +6 -3
  10. package/src/execute/run.js +1 -1
  11. package/src/omega/compat/runtime_compat.js +9 -6
  12. package/src/omega/errors.js +3 -0
  13. package/src/omega/fetched_content_compliance.js +2 -2
  14. package/src/omega/kitchen.js +191 -146
  15. package/src/omega/server/file_service.js +104 -71
  16. package/src/omega/url_graph/url_graph_loader.js +77 -0
  17. package/src/omega/url_graph/url_info_transformations.js +12 -15
  18. package/src/omega/url_graph.js +118 -101
  19. package/src/plugins/autoreload/jsenv_plugin_autoreload_client.js +1 -0
  20. package/src/plugins/autoreload/jsenv_plugin_autoreload_server.js +34 -36
  21. package/src/plugins/autoreload/jsenv_plugin_hmr.js +3 -2
  22. package/src/plugins/bundling/js_module/{bundle_js_module.js → bundle_js_modules.js} +51 -14
  23. package/src/plugins/bundling/jsenv_plugin_bundling.js +2 -2
  24. package/src/plugins/import_meta_hot/jsenv_plugin_import_meta_hot.js +11 -0
  25. package/src/plugins/inline/jsenv_plugin_html_inline_content.js +73 -62
  26. package/src/plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +77 -89
  27. package/src/plugins/plugin_controller.js +26 -22
  28. package/src/plugins/server_events/jsenv_plugin_server_events_client_injection.js +1 -0
  29. package/src/plugins/supervisor/client/script_type_module_supervisor.js +7 -9
  30. package/src/plugins/supervisor/client/supervisor.js +99 -52
  31. package/src/plugins/supervisor/jsenv_plugin_supervisor.js +2 -4
  32. package/src/plugins/transpilation/as_js_classic/async-to-promises.js +16 -0
  33. package/src/plugins/transpilation/as_js_classic/convert_js_module_to_js_classic.js +85 -0
  34. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic.js +48 -190
  35. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic_conversion.js +104 -0
  36. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic_html.js +161 -240
  37. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic_library.js +91 -0
  38. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic_workers.js +19 -12
  39. package/src/plugins/transpilation/babel/jsenv_plugin_babel.js +1 -24
  40. package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +82 -52
  41. package/src/plugins/transpilation/jsenv_plugin_transpilation.js +12 -13
  42. package/src/plugins/url_analysis/html/html_urls.js +91 -34
  43. package/src/plugins/url_analysis/js/js_urls.js +5 -4
  44. package/src/plugins/url_resolution/jsenv_plugin_url_resolution.js +1 -0
  45. package/src/test/execute_plan.js +3 -8
  46. package/src/test/execute_test_plan.js +1 -1
  47. package/src/build/inject_service_worker_urls.js +0 -78
  48. package/src/build/resync_resource_hints.js +0 -112
  49. package/src/omega/url_graph/url_graph_load.js +0 -74
@@ -705,7 +705,6 @@ window.__supervisor__ = (() => {
705
705
  supervisor.setup = ({
706
706
  rootDirectoryUrl,
707
707
  logs,
708
- measurePerf,
709
708
  errorOverlay,
710
709
  errorBaseUrl,
711
710
  openInEditor,
@@ -718,8 +717,35 @@ window.__supervisor__ = (() => {
718
717
  })
719
718
 
720
719
  const supervisedScripts = []
721
- const scriptExecutionPromises = []
722
- supervisor.createExecution = ({ type, src, async, execute }) => {
720
+ const pendingPromises = []
721
+ // respect execution order
722
+ // - wait for classic scripts to be done (non async)
723
+ // - wait module script previous execution (non async)
724
+ // see https://gist.github.com/jakub-g/385ee6b41085303a53ad92c7c8afd7a6#typemodule-vs-non-module-typetextjavascript-vs-script-nomodule
725
+ const executionQueue = []
726
+ let executing = false
727
+ const addToExecutionQueue = async (execution) => {
728
+ if (execution.async) {
729
+ execution.start()
730
+ return
731
+ }
732
+ if (executing) {
733
+ executionQueue.push(execution)
734
+ return
735
+ }
736
+ startThenDequeue(execution)
737
+ }
738
+ const startThenDequeue = async (execution) => {
739
+ executing = true
740
+ const promise = execution.start()
741
+ await promise
742
+ executing = false
743
+ if (executionQueue.length) {
744
+ const nextExecution = executionQueue.shift()
745
+ startThenDequeue(nextExecution)
746
+ }
747
+ }
748
+ supervisor.addExecution = ({ type, src, async, execute }) => {
723
749
  const execution = {
724
750
  type,
725
751
  src,
@@ -733,82 +759,103 @@ window.__supervisor__ = (() => {
733
759
  return superviseExecution(execution, { isReload: true })
734
760
  }
735
761
  supervisedScripts.push(execution)
736
- return execution
762
+ return addToExecutionQueue(execution)
737
763
  }
738
764
  const superviseExecution = async (execution, { isReload }) => {
739
765
  if (logs) {
740
766
  console.group(`[jsenv] loading ${execution.type} ${execution.src}`)
741
767
  }
742
- if (measurePerf) {
743
- performance.mark(`execution_start`)
744
- }
745
768
  const executionResult = {
746
769
  status: "pending",
747
- startTime: Date.now(),
748
- endTime: null,
770
+ loadDuration: null,
771
+ executionDuration: null,
772
+ duration: null,
749
773
  exception: null,
750
774
  namespace: null,
751
775
  coverage: null,
752
776
  }
753
777
  executionResults[execution.src] = executionResult
754
- let resolveScriptExecutionPromise
755
- const scriptExecutionPromise = new Promise((resolve) => {
756
- resolveScriptExecutionPromise = () => {
757
- executionResult.endTime = Date.now()
758
- if (measurePerf) {
759
- performance.measure(`execution`, `execution_start`)
760
- }
761
- resolve()
778
+
779
+ const monitorScriptLoad = () => {
780
+ const loadStartTime = Date.now()
781
+ let resolveScriptLoadPromise
782
+ const scriptLoadPromise = new Promise((resolve) => {
783
+ resolveScriptLoadPromise = resolve
784
+ })
785
+ pendingPromises.push(scriptLoadPromise)
786
+ return () => {
787
+ const loadEndTime = Date.now()
788
+ executionResult.loadDuration = loadEndTime - loadStartTime
789
+ resolveScriptLoadPromise()
762
790
  }
763
- })
764
- scriptExecutionPromise.execution = execution
765
- scriptExecutionPromises.push(scriptExecutionPromise)
791
+ }
792
+ const monitorScriptExecution = () => {
793
+ const executionStartTime = Date.now()
794
+ let resolveExecutionPromise
795
+ const executionPromise = new Promise((resolve) => {
796
+ resolveExecutionPromise = resolve
797
+ })
798
+ pendingPromises.push(executionPromise)
799
+ return () => {
800
+ executionResult.coverage = window.__coverage__
801
+ executionResult.executionDuration = Date.now() - executionStartTime
802
+ executionResult.duration =
803
+ executionResult.loadDuration + executionResult.executionDuration
804
+ resolveExecutionPromise()
805
+ }
806
+ }
807
+
808
+ const onError = (e) => {
809
+ executionResult.status = "errored"
810
+ const exception = supervisor.createException({ reason: e })
811
+ if (exception.needsReport) {
812
+ supervisor.reportException(exception)
813
+ }
814
+ executionResult.exception = exception
815
+ }
816
+
817
+ const scriptLoadDone = monitorScriptLoad()
766
818
  try {
767
819
  const result = await execution.execute({ isReload })
768
- executionResult.status = "completed"
769
- executionResult.namespace = result
770
- executionResult.coverage = window.__coverage__
771
820
  if (logs) {
772
821
  console.log(`${execution.type} load ended`)
773
822
  console.groupEnd()
774
823
  }
775
- resolveScriptExecutionPromise()
776
- } catch (e) {
777
- executionResult.status = "errored"
778
- const exception = supervisor.createException({
779
- reason: e,
780
- })
781
- if (exception.needsReport) {
782
- supervisor.reportException(exception)
824
+ executionResult.status = "loaded"
825
+ scriptLoadDone()
826
+
827
+ const scriptExecutionDone = monitorScriptExecution()
828
+ if (execution.type === "js_classic") {
829
+ executionResult.status = "completed"
830
+ scriptExecutionDone()
831
+ } else if (execution.type === "js_module") {
832
+ result.executionPromise.then(
833
+ (namespace) => {
834
+ executionResult.status = "completed"
835
+ executionResult.namespace = namespace
836
+ scriptExecutionDone()
837
+ },
838
+ (e) => {
839
+ onError(e)
840
+ scriptExecutionDone()
841
+ },
842
+ )
783
843
  }
784
- executionResult.exception = exception
785
- executionResult.coverage = window.__coverage__
844
+ } catch (e) {
786
845
  if (logs) {
787
846
  console.groupEnd()
788
847
  }
789
- resolveScriptExecutionPromise()
848
+ onError(e)
849
+ scriptLoadDone()
790
850
  }
791
851
  }
792
852
 
793
- // respect execution order
794
- // - wait for classic scripts to be done (non async)
795
- // - wait module script previous execution (non async)
796
- // see https://gist.github.com/jakub-g/385ee6b41085303a53ad92c7c8afd7a6#typemodule-vs-non-module-typetextjavascript-vs-script-nomodule
797
- supervisor.getPreviousExecutionDonePromise = async () => {
798
- const previousNonAsyncScriptExecutions = scriptExecutionPromises.filter(
799
- (promise) => !promise.execution.async,
800
- )
801
- await Promise.all(previousNonAsyncScriptExecutions)
802
- }
803
853
  supervisor.superviseScript = async ({ src, async }) => {
804
854
  const { currentScript } = document
805
855
  const parentNode = currentScript.parentNode
806
- if (!async) {
807
- await supervisor.getPreviousExecutionDonePromise()
808
- }
809
856
  let nodeToReplace
810
857
  let currentScriptClone
811
- const execution = supervisor.createExecution({
858
+ return supervisor.addExecution({
812
859
  src,
813
860
  type: "js_classic",
814
861
  async,
@@ -858,7 +905,6 @@ window.__supervisor__ = (() => {
858
905
  }
859
906
  },
860
907
  })
861
- return execution.start()
862
908
  }
863
909
  supervisor.reloadSupervisedScript = ({ type, src }) => {
864
910
  const supervisedScript = supervisedScripts.find(
@@ -891,12 +937,13 @@ window.__supervisor__ = (() => {
891
937
  })
892
938
  await documentReadyPromise
893
939
  const waitScriptExecutions = async () => {
894
- const numberOfScripts = scriptExecutionPromises.length
895
- await Promise.all(scriptExecutionPromises)
940
+ const numberOfPromises = pendingPromises.length
941
+ await Promise.all(pendingPromises)
896
942
  // new scripts added while the other where executing
897
943
  // (should happen only on webkit where
898
944
  // script might be added after window load event)
899
- if (scriptExecutionPromises.length > numberOfScripts) {
945
+ await new Promise((resolve) => setTimeout(resolve))
946
+ if (pendingPromises.length > numberOfPromises) {
900
947
  await waitScriptExecutions()
901
948
  }
902
949
  }
@@ -70,10 +70,8 @@ export const jsenvPluginSupervisor = ({
70
70
  openInEditor = true,
71
71
  errorBaseUrl,
72
72
  }) => {
73
- const supervisorFileUrl = new URL(
74
- "./client/supervisor.js?js_classic",
75
- import.meta.url,
76
- ).href
73
+ const supervisorFileUrl = new URL("./client/supervisor.js", import.meta.url)
74
+ .href
77
75
  const scriptTypeModuleSupervisorFileUrl = new URL(
78
76
  "./client/script_type_module_supervisor.js",
79
77
  import.meta.url,
@@ -12,6 +12,7 @@ const defaultConfigValues = {
12
12
  minify: false,
13
13
  target: "es5",
14
14
  topLevelAwait: "disabled",
15
+ asyncAwait: true,
15
16
  };
16
17
  function readConfigKey(config, key) {
17
18
  if (Object.hasOwnProperty.call(config, key)) {
@@ -3649,6 +3650,9 @@ function default_1({ types, traverse, transformFromAst, version, }) {
3649
3650
  },
3650
3651
  },
3651
3652
  FunctionDeclaration(path) {
3653
+ if (!readConfigKey(this.opts, 'asyncAwait')) {
3654
+ return
3655
+ }
3652
3656
  const node = path.node;
3653
3657
  if (node.async) {
3654
3658
  const expression = types.functionExpression(undefined, node.params, node.body, node.generator, node.async);
@@ -3680,6 +3684,9 @@ function default_1({ types, traverse, transformFromAst, version, }) {
3680
3684
  }
3681
3685
  },
3682
3686
  ArrowFunctionExpression(path) {
3687
+ if (!readConfigKey(this.opts, 'asyncAwait')) {
3688
+ return
3689
+ }
3683
3690
  const node = path.node;
3684
3691
  if (node.async) {
3685
3692
  rewriteThisExpressions(path, path.getFunctionParent() || path.scope.getProgramParent().path);
@@ -3691,6 +3698,9 @@ function default_1({ types, traverse, transformFromAst, version, }) {
3691
3698
  }
3692
3699
  },
3693
3700
  FunctionExpression(path) {
3701
+ if (!readConfigKey(this.opts, 'asyncAwait')) {
3702
+ return
3703
+ }
3694
3704
  if (path.node.async) {
3695
3705
  const id = path.node.id;
3696
3706
  if (path.parentPath.isExportDefaultDeclaration() && id !== null && id !== undefined) {
@@ -3773,6 +3783,9 @@ function default_1({ types, traverse, transformFromAst, version, }) {
3773
3783
  }
3774
3784
  },
3775
3785
  ClassMethod(path) {
3786
+ if (!readConfigKey(this.opts, 'asyncAwait')) {
3787
+ return
3788
+ }
3776
3789
  if (path.node.async) {
3777
3790
  const body = path.get("body");
3778
3791
  if (path.node.kind === "method") {
@@ -3832,6 +3845,9 @@ function default_1({ types, traverse, transformFromAst, version, }) {
3832
3845
  }
3833
3846
  },
3834
3847
  ObjectMethod(path) {
3848
+ if (!readConfigKey(this.opts, 'asyncAwait')) {
3849
+ return
3850
+ }
3835
3851
  if (path.node.async) {
3836
3852
  if (path.node.kind === "method") {
3837
3853
  path.replaceWith(types.objectProperty(path.node.key, types.functionExpression(undefined, path.node.params, path.node.body, path.node.generator, path.node.async), path.node.computed, false, path.node.decorators));
@@ -0,0 +1,85 @@
1
+ import { readFileSync } from "@jsenv/filesystem"
2
+ import { createMagicSource, composeTwoSourcemaps } from "@jsenv/sourcemap"
3
+ import { applyBabelPlugins } from "@jsenv/ast"
4
+
5
+ import { requireFromJsenv } from "@jsenv/core/src/require_from_jsenv.js"
6
+ import { requireBabelPlugin } from "../babel/require_babel_plugin.js"
7
+ import { babelPluginTransformImportMetaUrl } from "./helpers/babel_plugin_transform_import_meta_url.js"
8
+
9
+ // import { jsenvPluginAsJsClassicLibrary } from "./jsenv_plugin_as_js_classic_library.js"
10
+ // because of https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/84
11
+ import customAsyncToPromises from "./async-to-promises.js"
12
+
13
+ export const convertJsModuleToJsClassic = async ({
14
+ systemJsInjection,
15
+ systemJsClientFileUrl,
16
+ urlInfo,
17
+ jsModuleUrlInfo,
18
+ }) => {
19
+ const jsClassicFormat =
20
+ // in general html file are entry points, but js can be entry point when:
21
+ // - passed in entryPoints to build
22
+ // - is used by web worker
23
+ // - the reference contains ?entry_point
24
+ // When js is entry point there can be no HTML to inject systemjs
25
+ // and systemjs must be injected into the js file
26
+ urlInfo.isEntryPoint &&
27
+ // if it's an entry point without dependency (it does not use import)
28
+ // then we can use UMD, otherwise we have to use systemjs
29
+ // because it is imported by systemjs
30
+ !jsModuleUrlInfo.data.usesImport
31
+ ? "umd"
32
+ : "system"
33
+ urlInfo.data.jsClassicFormat = jsClassicFormat
34
+ const { code, map } = await applyBabelPlugins({
35
+ babelPlugins: [
36
+ ...(jsClassicFormat === "system"
37
+ ? [
38
+ // proposal-dynamic-import required with systemjs for babel8:
39
+ // https://github.com/babel/babel/issues/10746
40
+ requireFromJsenv("@babel/plugin-proposal-dynamic-import"),
41
+ requireFromJsenv("@babel/plugin-transform-modules-systemjs"),
42
+ [
43
+ customAsyncToPromises,
44
+ {
45
+ asyncAwait: false, // already handled + we might not needs it at all
46
+ topLevelAwait: "return",
47
+ },
48
+ ],
49
+ ]
50
+ : [
51
+ [
52
+ requireBabelPlugin("babel-plugin-transform-async-to-promises"),
53
+ {
54
+ asyncAwait: false, // already handled + we might not needs it at all
55
+ topLevelAwait: "simple",
56
+ },
57
+ ],
58
+ babelPluginTransformImportMetaUrl,
59
+ requireFromJsenv("@babel/plugin-transform-modules-umd"),
60
+ ]),
61
+ ],
62
+ urlInfo: jsModuleUrlInfo,
63
+ })
64
+ let sourcemap = jsModuleUrlInfo.sourcemap
65
+ sourcemap = await composeTwoSourcemaps(sourcemap, map)
66
+ if (
67
+ systemJsInjection &&
68
+ jsClassicFormat === "system" &&
69
+ urlInfo.isEntryPoint
70
+ ) {
71
+ const magicSource = createMagicSource(code)
72
+ const systemjsCode = readFileSync(systemJsClientFileUrl, { as: "string" })
73
+ magicSource.prepend(`${systemjsCode}\n\n`)
74
+ const magicResult = magicSource.toContentAndSourcemap()
75
+ sourcemap = await composeTwoSourcemaps(sourcemap, magicResult.sourcemap)
76
+ return {
77
+ content: magicResult.content,
78
+ sourcemap,
79
+ }
80
+ }
81
+ return {
82
+ content: code,
83
+ sourcemap,
84
+ }
85
+ }
@@ -11,206 +11,64 @@
11
11
  * and prefer to unique identifier based solely on the specifier basename for instance
12
12
  */
13
13
 
14
- import { urlToFilename, injectQueryParams } from "@jsenv/urls"
15
- import { readFileSync } from "@jsenv/filesystem"
16
- import { createMagicSource, composeTwoSourcemaps } from "@jsenv/sourcemap"
17
- import { applyBabelPlugins } from "@jsenv/ast"
18
-
19
- import { requireFromJsenv } from "@jsenv/core/src/require_from_jsenv.js"
20
- import { requireBabelPlugin } from "../babel/require_babel_plugin.js"
21
- import { babelPluginTransformImportMetaUrl } from "./helpers/babel_plugin_transform_import_meta_url.js"
14
+ import { urlToFilename } from "@jsenv/urls"
15
+ import { jsenvPluginAsJsClassicConversion } from "./jsenv_plugin_as_js_classic_conversion.js"
22
16
  import { jsenvPluginAsJsClassicHtml } from "./jsenv_plugin_as_js_classic_html.js"
23
17
  import { jsenvPluginAsJsClassicWorkers } from "./jsenv_plugin_as_js_classic_workers.js"
24
- // because of https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/84
25
- import customAsyncToPromises from "./async-to-promises.js"
26
-
27
- export const jsenvPluginAsJsClassic = ({ systemJsInjection = true }) => {
28
- const systemJsClientFileUrl = new URL(
29
- "./client/s.js?js_classic",
30
- import.meta.url,
31
- ).href
32
-
33
- return [
34
- jsenvPluginAsJsClassicConversion({
35
- systemJsInjection,
36
- systemJsClientFileUrl,
37
- }),
38
- jsenvPluginAsJsClassicHtml({
39
- systemJsInjection,
40
- systemJsClientFileUrl,
41
- generateJsClassicFilename,
42
- }),
43
- jsenvPluginAsJsClassicWorkers({
44
- generateJsClassicFilename,
45
- }),
46
- ]
47
- }
18
+ import { jsenvPluginAsJsClassicLibrary } from "./jsenv_plugin_as_js_classic_library.js"
48
19
 
49
- // propagate ?as_js_classic to referenced urls
50
- // and perform the conversion during fetchUrlContent
51
- const jsenvPluginAsJsClassicConversion = ({
20
+ export const jsenvPluginAsJsClassic = ({
21
+ jsClassicLibrary,
22
+ jsClassicFallback,
52
23
  systemJsInjection,
53
- systemJsClientFileUrl,
54
24
  }) => {
55
- const propagateJsClassicSearchParam = (reference, context) => {
56
- const parentUrlInfo = context.urlGraph.getUrlInfo(reference.parentUrl)
25
+ const systemJsClientFileUrl = new URL("./client/s.js", import.meta.url).href
26
+
27
+ const generateJsClassicFilename = (url) => {
28
+ const filename = urlToFilename(url)
29
+ let [basename, extension] = splitFileExtension(filename)
30
+ const { searchParams } = new URL(url)
57
31
  if (
58
- !parentUrlInfo ||
59
- !new URL(parentUrlInfo.url).searchParams.has("as_js_classic")
32
+ searchParams.has("as_json_module") ||
33
+ searchParams.has("as_css_module") ||
34
+ searchParams.has("as_text_module")
60
35
  ) {
61
- return null
36
+ extension = ".js"
62
37
  }
63
- const urlTransformed = injectQueryParams(reference.url, {
64
- as_js_classic: "",
65
- })
66
- reference.filename = generateJsClassicFilename(reference.url)
67
- return urlTransformed
68
- }
69
-
70
- return {
71
- name: "jsenv:as_js_classic_conversion",
72
- appliesDuring: "*",
73
- redirectUrl: {
74
- // We want to propagate transformation of js module to js classic to:
75
- // - import specifier (static/dynamic import + re-export)
76
- // - url specifier when inside System.register/_context.import()
77
- // (because it's the transpiled equivalent of static and dynamic imports)
78
- // And not other references otherwise we could try to transform inline resources
79
- // or specifiers inside new URL()...
80
- js_import_export: propagateJsClassicSearchParam,
81
- js_url_specifier: (reference, context) => {
82
- if (
83
- reference.subtype === "system_register_arg" ||
84
- reference.subtype === "system_import_arg"
85
- ) {
86
- return propagateJsClassicSearchParam(reference, context)
87
- }
88
- return null
89
- },
90
- },
91
- fetchUrlContent: async (urlInfo, context) => {
92
- const originalUrlInfo = await context.fetchOriginalUrlInfo({
93
- urlInfo,
94
- context,
95
- searchParam: "as_js_classic",
96
- // override the expectedType to "js_module"
97
- // because when there is ?as_js_classic it means the underlying resource
98
- // is a js_module
99
- expectedType: "js_module",
100
- })
101
- if (!originalUrlInfo) {
102
- return null
103
- }
104
- const jsClassicFormat =
105
- // in general html file are entry points, but js can be entry point when:
106
- // - passed in entryPoints to build
107
- // - is used by web worker
108
- // - the reference contains ?entry_point
109
- // When js is entry point there can be no HTML to inject systemjs
110
- // and systemjs must be injected into the js file
111
- urlInfo.isEntryPoint &&
112
- // if it's an entry point without dependency (it does not use import)
113
- // then we can use UMD, otherwise we have to use systemjs
114
- // because it is imported by systemjs
115
- !originalUrlInfo.data.usesImport
116
- ? "umd"
117
- : "system"
118
- const { content, sourcemap } = await convertJsModuleToJsClassic({
119
- systemJsInjection,
120
- systemJsClientFileUrl,
121
- urlInfo,
122
- originalUrlInfo,
123
- jsClassicFormat,
124
- })
125
- urlInfo.data.jsClassicFormat = jsClassicFormat
126
- return {
127
- content,
128
- contentType: "text/javascript",
129
- type: "js_classic",
130
- originalUrl: originalUrlInfo.originalUrl,
131
- originalContent: originalUrlInfo.originalContent,
132
- sourcemap,
133
- }
134
- },
135
- }
136
- }
137
-
138
- const generateJsClassicFilename = (url) => {
139
- const filename = urlToFilename(url)
140
- let [basename, extension] = splitFileExtension(filename)
141
- const { searchParams } = new URL(url)
142
- if (
143
- searchParams.has("as_json_module") ||
144
- searchParams.has("as_css_module") ||
145
- searchParams.has("as_text_module")
146
- ) {
147
- extension = ".js"
148
- }
149
- return `${basename}.nomodule${extension}`
150
- }
151
-
152
- const splitFileExtension = (filename) => {
153
- const dotLastIndex = filename.lastIndexOf(".")
154
- if (dotLastIndex === -1) {
155
- return [filename, ""]
38
+ return `${basename}.nomodule${extension}`
156
39
  }
157
- return [filename.slice(0, dotLastIndex), filename.slice(dotLastIndex)]
158
- }
159
40
 
160
- const convertJsModuleToJsClassic = async ({
161
- systemJsInjection,
162
- systemJsClientFileUrl,
163
- urlInfo,
164
- originalUrlInfo,
165
- jsClassicFormat,
166
- }) => {
167
- const { code, map } = await applyBabelPlugins({
168
- babelPlugins: [
169
- ...(jsClassicFormat === "system"
170
- ? [
171
- // propposal-dynamic-import required with systemjs for babel8:
172
- // https://github.com/babel/babel/issues/10746
173
- requireFromJsenv("@babel/plugin-proposal-dynamic-import"),
174
- requireFromJsenv("@babel/plugin-transform-modules-systemjs"),
175
- [
176
- customAsyncToPromises,
177
- {
178
- topLevelAwait: "return",
179
- },
180
- ],
181
- ]
182
- : [
183
- [
184
- requireBabelPlugin("babel-plugin-transform-async-to-promises"),
185
- {
186
- topLevelAwait: "simple",
187
- },
188
- ],
189
- babelPluginTransformImportMetaUrl,
190
- requireFromJsenv("@babel/plugin-transform-modules-umd"),
191
- ]),
192
- ],
193
- urlInfo: originalUrlInfo,
194
- })
195
- let sourcemap = originalUrlInfo.sourcemap
196
- sourcemap = await composeTwoSourcemaps(sourcemap, map)
197
- if (
198
- systemJsInjection &&
199
- jsClassicFormat === "system" &&
200
- urlInfo.isEntryPoint
201
- ) {
202
- const magicSource = createMagicSource(code)
203
- const systemjsCode = readFileSync(systemJsClientFileUrl, { as: "string" })
204
- magicSource.prepend(`${systemjsCode}\n\n`)
205
- const magicResult = magicSource.toContentAndSourcemap()
206
- sourcemap = await composeTwoSourcemaps(sourcemap, magicResult.sourcemap)
207
- return {
208
- content: magicResult.content,
209
- sourcemap,
41
+ const splitFileExtension = (filename) => {
42
+ const dotLastIndex = filename.lastIndexOf(".")
43
+ if (dotLastIndex === -1) {
44
+ return [filename, ""]
210
45
  }
46
+ return [filename.slice(0, dotLastIndex), filename.slice(dotLastIndex)]
211
47
  }
212
- return {
213
- content: code,
214
- sourcemap,
215
- }
48
+
49
+ return [
50
+ ...(jsClassicLibrary
51
+ ? [
52
+ jsenvPluginAsJsClassicLibrary({
53
+ systemJsInjection,
54
+ systemJsClientFileUrl,
55
+ generateJsClassicFilename,
56
+ }),
57
+ ]
58
+ : []),
59
+ ...(jsClassicFallback
60
+ ? [
61
+ jsenvPluginAsJsClassicHtml({
62
+ systemJsInjection,
63
+ systemJsClientFileUrl,
64
+ }),
65
+ jsenvPluginAsJsClassicWorkers(),
66
+ jsenvPluginAsJsClassicConversion({
67
+ systemJsInjection,
68
+ systemJsClientFileUrl,
69
+ generateJsClassicFilename,
70
+ }),
71
+ ]
72
+ : []),
73
+ ]
216
74
  }