@jsenv/core 27.0.0-alpha.1 → 27.0.0-alpha.12

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 (115) hide show
  1. package/package.json +5 -6
  2. package/src/build/build.js +208 -78
  3. package/src/build/build_urls_generator.js +16 -7
  4. package/src/build/inject_service_worker_urls.js +25 -0
  5. package/src/build/inject_version_mappings.js +4 -3
  6. package/src/build/plugins/bundle_js_module/jsenv_plugin_bundle_js_module.js +16 -14
  7. package/src/dev/start_dev_server.js +6 -3
  8. package/src/execute/execute.js +7 -1
  9. package/src/omega/core_plugins/babel/{babel_helper → helpers}/babel_plugin_babel_helpers_as_jsenv_imports.js +1 -2
  10. package/src/omega/core_plugins/babel/helpers/babel_plugin_structure.js +69 -80
  11. package/src/omega/core_plugins/babel/jsenv_plugin_babel.js +19 -5
  12. package/src/omega/core_plugins/html_supervisor/client/uneval_exception.js +1 -1
  13. package/src/omega/core_plugins/import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js +16 -6
  14. package/src/omega/core_plugins/inject_globals/jsenv_plugin_inject_globals.js +27 -0
  15. package/src/omega/core_plugins/inline/jsenv_plugin_new_inline_content.js +1 -4
  16. package/src/omega/core_plugins.js +3 -0
  17. package/src/omega/file_url_converter.js +8 -50
  18. package/src/omega/kitchen.js +14 -15
  19. package/src/omega/runtime_support/features_compatibility.js +40 -1
  20. package/src/omega/runtime_support/runtime_support.js +48 -16
  21. package/src/omega/url_graph/url_graph_load.js +1 -0
  22. package/src/omega/url_graph.js +1 -0
  23. package/src/test/execute_plan.js +5 -3
  24. package/src/test/execute_test_plan.js +11 -1
  25. package/src/omega/core_plugins/babel/babel_helper/babel_helper_directory.js +0 -29
  26. package/src/omega/core_plugins/babel/babel_helper/client/.eslintrc.cjs +0 -24
  27. package/src/omega/core_plugins/babel/babel_helper/client/AsyncGenerator/AsyncGenerator.js +0 -81
  28. package/src/omega/core_plugins/babel/babel_helper/client/AwaitValue/AwaitValue.js +0 -3
  29. package/src/omega/core_plugins/babel/babel_helper/client/applyDecoratorDescriptor/applyDecoratorDescriptor.js +0 -33
  30. package/src/omega/core_plugins/babel/babel_helper/client/arrayLikeToArray/arrayLikeToArray.js +0 -7
  31. package/src/omega/core_plugins/babel/babel_helper/client/arrayWithHoles/arrayWithHoles.js +0 -4
  32. package/src/omega/core_plugins/babel/babel_helper/client/arrayWithoutHoles/arrayWithoutHoles.js +0 -6
  33. package/src/omega/core_plugins/babel/babel_helper/client/assertThisInitialized/assertThisInitialized.js +0 -7
  34. package/src/omega/core_plugins/babel/babel_helper/client/asyncGeneratorDelegate/asyncGeneratorDelegate.js +0 -40
  35. package/src/omega/core_plugins/babel/babel_helper/client/asyncIterator/asyncIterator.js +0 -65
  36. package/src/omega/core_plugins/babel/babel_helper/client/asyncToGenerator/asyncToGenerator.js +0 -34
  37. package/src/omega/core_plugins/babel/babel_helper/client/awaitAsyncGenerator/awaitAsyncGenerator.js +0 -5
  38. package/src/omega/core_plugins/babel/babel_helper/client/classApplyDescriptorDestructureSet/classApplyDescriptorDestructureSet.js +0 -20
  39. package/src/omega/core_plugins/babel/babel_helper/client/classApplyDescriptorGet/classApplyDescriptorGet.js +0 -6
  40. package/src/omega/core_plugins/babel/babel_helper/client/classApplyDescriptorSet/classApplyDescriptorSet.js +0 -13
  41. package/src/omega/core_plugins/babel/babel_helper/client/classCallCheck/classCallCheck.js +0 -5
  42. package/src/omega/core_plugins/babel/babel_helper/client/classCheckPrivateStaticAccess/classCheckPrivateStaticAccess.js +0 -5
  43. package/src/omega/core_plugins/babel/babel_helper/client/classCheckPrivateStaticFieldDescriptor/classCheckPrivateStaticFieldDescriptor.js +0 -6
  44. package/src/omega/core_plugins/babel/babel_helper/client/classExtractFieldDescriptor/classExtractFieldDescriptor.js +0 -7
  45. package/src/omega/core_plugins/babel/babel_helper/client/classNameTDZError/classNameTDZError.js +0 -4
  46. package/src/omega/core_plugins/babel/babel_helper/client/classPrivateFieldDestructureSet/classPrivateFieldDestructureSet.js +0 -7
  47. package/src/omega/core_plugins/babel/babel_helper/client/classPrivateFieldGet/classPrivateFieldGet.js +0 -7
  48. package/src/omega/core_plugins/babel/babel_helper/client/classPrivateFieldLooseBase/classPrivateFieldLooseBase.js +0 -6
  49. package/src/omega/core_plugins/babel/babel_helper/client/classPrivateFieldLooseKey/classPrivateFieldLooseKey.js +0 -5
  50. package/src/omega/core_plugins/babel/babel_helper/client/classPrivateFieldSet/classPrivateFieldSet.js +0 -8
  51. package/src/omega/core_plugins/babel/babel_helper/client/classPrivateMethodGet/classPrivateMethodGet.js +0 -6
  52. package/src/omega/core_plugins/babel/babel_helper/client/classPrivateMethodSet/classPrivateMethodSet.js +0 -3
  53. package/src/omega/core_plugins/babel/babel_helper/client/classStaticPrivateFieldSpecGet/classStaticPrivateFieldSpecGet.js +0 -9
  54. package/src/omega/core_plugins/babel/babel_helper/client/classStaticPrivateFieldSpecSet/classStaticPrivateFieldSpecSet.js +0 -15
  55. package/src/omega/core_plugins/babel/babel_helper/client/classStaticPrivateMethodGet/classStaticPrivateMethodGet.js +0 -6
  56. package/src/omega/core_plugins/babel/babel_helper/client/classStaticPrivateMethodSet/classStaticPrivateMethodSet.js +0 -3
  57. package/src/omega/core_plugins/babel/babel_helper/client/construct/construct.js +0 -16
  58. package/src/omega/core_plugins/babel/babel_helper/client/createClass/createClass.js +0 -15
  59. package/src/omega/core_plugins/babel/babel_helper/client/createForOfIteratorHelper/createForOfIteratorHelper.js +0 -60
  60. package/src/omega/core_plugins/babel/babel_helper/client/createForOfIteratorHelperLoose/createForOfIteratorHelperLoose.js +0 -23
  61. package/src/omega/core_plugins/babel/babel_helper/client/createRawReactElement/createRawReactElement.js +0 -50
  62. package/src/omega/core_plugins/babel/babel_helper/client/createSuper/createSuper.js +0 -22
  63. package/src/omega/core_plugins/babel/babel_helper/client/decorate/decorate.js +0 -403
  64. package/src/omega/core_plugins/babel/babel_helper/client/defaults/defaults.js +0 -11
  65. package/src/omega/core_plugins/babel/babel_helper/client/defineEnumerableProperties/defineEnumerableProperties.js +0 -23
  66. package/src/omega/core_plugins/babel/babel_helper/client/defineProperty/defineProperty.js +0 -18
  67. package/src/omega/core_plugins/babel/babel_helper/client/extends/extends.js +0 -14
  68. package/src/omega/core_plugins/babel/babel_helper/client/get/get.js +0 -19
  69. package/src/omega/core_plugins/babel/babel_helper/client/getPrototypeOf/getPrototypeOf.js +0 -4
  70. package/src/omega/core_plugins/babel/babel_helper/client/inherits/inherits.js +0 -19
  71. package/src/omega/core_plugins/babel/babel_helper/client/inheritsLoose/inheritsLoose.js +0 -7
  72. package/src/omega/core_plugins/babel/babel_helper/client/initializerDefineProperty/initializerDefineProperty.js +0 -10
  73. package/src/omega/core_plugins/babel/babel_helper/client/initializerWarningHelper/initializerWarningHelper.js +0 -6
  74. package/src/omega/core_plugins/babel/babel_helper/client/instanceof/instanceof.js +0 -6
  75. package/src/omega/core_plugins/babel/babel_helper/client/interopRequireDefault/interopRequireDefault.js +0 -3
  76. package/src/omega/core_plugins/babel/babel_helper/client/interopRequireWildcard/interopRequireWildcard.js +0 -37
  77. package/src/omega/core_plugins/babel/babel_helper/client/isNativeFunction/isNativeFunction.js +0 -4
  78. package/src/omega/core_plugins/babel/babel_helper/client/isNativeReflectConstruct/isNativeReflectConstruct.js +0 -21
  79. package/src/omega/core_plugins/babel/babel_helper/client/iterableToArray/iterableToArray.js +0 -7
  80. package/src/omega/core_plugins/babel/babel_helper/client/iterableToArrayLimit/iterableToArrayLimit.js +0 -36
  81. package/src/omega/core_plugins/babel/babel_helper/client/iterableToArrayLimitLoose/iterableToArrayLimitLoose.js +0 -10
  82. package/src/omega/core_plugins/babel/babel_helper/client/jsx/jsx.js +0 -50
  83. package/src/omega/core_plugins/babel/babel_helper/client/maybeArrayLike/maybeArrayLike.js +0 -10
  84. package/src/omega/core_plugins/babel/babel_helper/client/newArrowCheck/newArrowCheck.js +0 -5
  85. package/src/omega/core_plugins/babel/babel_helper/client/nonIterableRest/nonIterableRest.js +0 -5
  86. package/src/omega/core_plugins/babel/babel_helper/client/nonIterableSpread/nonIterableSpread.js +0 -5
  87. package/src/omega/core_plugins/babel/babel_helper/client/objectDestructuringEmpty/objectDestructuringEmpty.js +0 -3
  88. package/src/omega/core_plugins/babel/babel_helper/client/objectSpread/objectSpread.js +0 -23
  89. package/src/omega/core_plugins/babel/babel_helper/client/objectSpread2/objectSpread2.js +0 -41
  90. package/src/omega/core_plugins/babel/babel_helper/client/objectWithoutProperties/objectWithoutProperties.js +0 -19
  91. package/src/omega/core_plugins/babel/babel_helper/client/objectWithoutPropertiesLoose/objectWithoutPropertiesLoose.js +0 -13
  92. package/src/omega/core_plugins/babel/babel_helper/client/possibleConstructorReturn/possibleConstructorReturn.js +0 -10
  93. package/src/omega/core_plugins/babel/babel_helper/client/readOnlyError/readOnlyError.js +0 -4
  94. package/src/omega/core_plugins/babel/babel_helper/client/readme.md +0 -8
  95. package/src/omega/core_plugins/babel/babel_helper/client/set/set.js +0 -44
  96. package/src/omega/core_plugins/babel/babel_helper/client/setPrototypeOf/setPrototypeOf.js +0 -6
  97. package/src/omega/core_plugins/babel/babel_helper/client/skipFirstGeneratorNext/skipFirstGeneratorNext.js +0 -8
  98. package/src/omega/core_plugins/babel/babel_helper/client/slicedToArray/slicedToArray.js +0 -10
  99. package/src/omega/core_plugins/babel/babel_helper/client/slicedToArrayLoose/slicedToArrayLoose.js +0 -13
  100. package/src/omega/core_plugins/babel/babel_helper/client/superPropBase/superPropBase.js +0 -10
  101. package/src/omega/core_plugins/babel/babel_helper/client/taggedTemplateLiteral/taggedTemplateLiteral.js +0 -10
  102. package/src/omega/core_plugins/babel/babel_helper/client/taggedTemplateLiteralLoose/taggedTemplateLiteralLoose.js +0 -7
  103. package/src/omega/core_plugins/babel/babel_helper/client/tdz/tdz.js +0 -4
  104. package/src/omega/core_plugins/babel/babel_helper/client/temporalRef/temporalRef.js +0 -6
  105. package/src/omega/core_plugins/babel/babel_helper/client/temporalUndefined/temporalUndefined.js +0 -3
  106. package/src/omega/core_plugins/babel/babel_helper/client/toArray/toArray.js +0 -10
  107. package/src/omega/core_plugins/babel/babel_helper/client/toConsumableArray/toConsumableArray.js +0 -10
  108. package/src/omega/core_plugins/babel/babel_helper/client/toPrimitive/toPrimitive.js +0 -10
  109. package/src/omega/core_plugins/babel/babel_helper/client/toPropertyKey/toPropertyKey.js +0 -6
  110. package/src/omega/core_plugins/babel/babel_helper/client/typeof/typeof.js +0 -14
  111. package/src/omega/core_plugins/babel/babel_helper/client/unsupportedIterableToArray/unsupportedIterableToArray.js +0 -12
  112. package/src/omega/core_plugins/babel/babel_helper/client/wrapAsyncGenerator/wrapAsyncGenerator.js +0 -8
  113. package/src/omega/core_plugins/babel/babel_helper/client/wrapNativeSuper/wrapNativeSuper.js +0 -30
  114. package/src/omega/core_plugins/babel/babel_helper/client/wrapRegExp/wrapRegExp.js +0 -63
  115. package/src/omega/core_plugins/babel/babel_helper/client/writeOnlyError/writeOnlyError.js +0 -4
@@ -131,15 +131,6 @@ const rollupPluginJsenv = ({
131
131
  const rollupFileInfo = rollupResult[fileName]
132
132
  // there is 3 types of file: "placeholder", "asset", "chunk"
133
133
  if (rollupFileInfo.type === "chunk") {
134
- const { facadeModuleId } = rollupFileInfo
135
- let url
136
- if (facadeModuleId) {
137
- url = fileUrlConverter.asFileUrl(facadeModuleId)
138
- } else {
139
- const { sources } = rollupFileInfo.map
140
- const sourcePath = sources[sources.length - 1]
141
- url = fileUrlConverter.asFileUrl(sourcePath)
142
- }
143
134
  const jsModuleBundleUrlInfo = {
144
135
  // buildRelativeUrl: rollupFileInfo.fileName,
145
136
  data: {
@@ -149,6 +140,12 @@ const rollupPluginJsenv = ({
149
140
  content: rollupFileInfo.code,
150
141
  sourcemap: rollupFileInfo.map,
151
142
  }
143
+ let url
144
+ if (rollupFileInfo.facadeModuleId) {
145
+ url = fileUrlConverter.asFileUrl(rollupFileInfo.facadeModuleId)
146
+ } else {
147
+ url = new URL(rollupFileInfo.fileName, rootDirectoryUrl).href
148
+ }
152
149
  jsModuleBundleUrlInfos[url] = jsModuleBundleUrlInfo
153
150
  }
154
151
  })
@@ -171,13 +168,18 @@ const rollupPluginJsenv = ({
171
168
  },
172
169
  chunkFileNames: (chunkInfo) => {
173
170
  // preserves relative path parts:
174
- // the goal is to mantain the original relative path (relative to the root directory)
171
+ // the goal is to maintain the original relative path (relative to the root directory)
175
172
  // so that later in the build process, when resolving these urls, we are able to
176
173
  // re-resolve the specifier againt the original parent url and find the original url
177
- const { facadeModuleId } = chunkInfo
178
- const fileUrl = fileUrlConverter.asFileUrl(facadeModuleId)
179
- const relativePath = urlToRelativeUrl(fileUrl, rootDirectoryUrl)
180
- return relativePath
174
+ if (chunkInfo.facadeModuleId) {
175
+ const fileUrl = fileUrlConverter.asFileUrl(chunkInfo.facadeModuleId)
176
+ const relativePath = urlToRelativeUrl(fileUrl, rootDirectoryUrl)
177
+ return relativePath
178
+ }
179
+ // chunk generated dynamically by rollup to share code.
180
+ // we prefix with "__rollup__/" to avoid potential conflict of filename
181
+ // between this one and a file with the same name existing in the root directory
182
+ return `__rollup__/${chunkInfo.name}.js`
181
183
  },
182
184
  // https://rollupjs.org/guide/en/#outputpaths
183
185
  // paths: (id) => {
@@ -23,11 +23,12 @@ export const startDevServer = async ({
23
23
  certificate,
24
24
  privateKey,
25
25
  keepProcessAlive = true,
26
-
27
26
  rootDirectoryUrl,
28
- plugins = [],
29
27
 
28
+ injectedGlobals,
29
+ plugins = [],
30
30
  sourcemaps = "inline",
31
+
31
32
  autoreload = true,
32
33
  autoreloadPatterns = {
33
34
  "./**": true,
@@ -58,7 +59,9 @@ export const startDevServer = async ({
58
59
  urlGraph,
59
60
  plugins: [
60
61
  ...plugins,
61
- ...getCorePlugins(),
62
+ ...getCorePlugins({
63
+ injectedGlobals,
64
+ }),
62
65
  ...(autoreload
63
66
  ? [
64
67
  jsenvPluginAutoreload({
@@ -26,6 +26,7 @@ export const execute = async ({
26
26
  runtime,
27
27
  runtimeParams,
28
28
 
29
+ injectedGlobals,
29
30
  plugins = [],
30
31
  scenario = "dev",
31
32
  sourcemaps = "inline",
@@ -65,7 +66,12 @@ export const execute = async ({
65
66
  logger,
66
67
  rootDirectoryUrl,
67
68
  urlGraph,
68
- plugins: [...plugins, ...getCorePlugins()],
69
+ plugins: [
70
+ ...plugins,
71
+ ...getCorePlugins({
72
+ injectedGlobals,
73
+ }),
74
+ ],
69
75
  scenario,
70
76
  sourcemaps,
71
77
  })
@@ -1,11 +1,10 @@
1
1
  import { pathToFileURL } from "node:url"
2
2
 
3
3
  import { injectImport } from "@jsenv/utils/js_ast/babel_utils.js"
4
-
5
4
  import {
6
5
  getBabelHelperFileUrl,
7
6
  babelHelperNameFromUrl,
8
- } from "./babel_helper_directory.js"
7
+ } from "@jsenv/babel-plugins/main.js"
9
8
 
10
9
  // named import approach found here:
11
10
  // https://github.com/rollup/rollup-plugin-babel/blob/18e4232a450f320f44c651aa8c495f21c74d59ac/src/helperPlugin.js#L1
@@ -1,10 +1,6 @@
1
- import { createRequire } from "node:module"
2
-
3
- import { getBabelHelperFileUrl } from "../babel_helper/babel_helper_directory.js"
1
+ import { getBabelHelperFileUrl, requireBabelPlugin } from "@jsenv/babel-plugins"
4
2
  import { babelPluginCompatMap } from "./babel_plugins_compatibility.js"
5
3
 
6
- const require = createRequire(import.meta.url)
7
-
8
4
  export const getBaseBabelPluginStructure = ({
9
5
  url,
10
6
  isSupportedOnRuntime,
@@ -14,37 +10,32 @@ export const getBaseBabelPluginStructure = ({
14
10
  isJsModule,
15
11
  }) => {
16
12
  const isBabelPluginNeeded = (babelPluginName) => {
17
- return !isSupportedOnRuntime(
18
- babelPluginName,
19
- babelPluginCompatMap[babelPluginName],
20
- )
13
+ return !isSupportedOnRuntime(babelPluginCompatMap[babelPluginName])
21
14
  }
22
15
 
23
16
  const babelPluginStructure = {}
24
17
  if (isBabelPluginNeeded("proposal-numeric-separator")) {
25
- babelPluginStructure[
26
- "proposal-numeric-separator"
27
- ] = require("@babel/plugin-proposal-numeric-separator")
18
+ babelPluginStructure["proposal-numeric-separator"] = requireBabelPlugin(
19
+ "@babel/plugin-proposal-numeric-separator",
20
+ )
28
21
  }
29
22
  if (isBabelPluginNeeded("proposal-json-strings")) {
30
- babelPluginStructure[
31
- "proposal-json-strings"
32
- ] = require("@babel/plugin-proposal-json-strings")
23
+ babelPluginStructure["proposal-json-strings"] = requireBabelPlugin(
24
+ "@babel/plugin-proposal-json-strings",
25
+ )
33
26
  }
34
27
  if (isBabelPluginNeeded("proposal-object-rest-spread")) {
35
- babelPluginStructure[
36
- "proposal-object-rest-spread"
37
- ] = require("@babel/plugin-proposal-object-rest-spread")
28
+ babelPluginStructure["proposal-object-rest-spread"] = requireBabelPlugin(
29
+ "@babel/plugin-proposal-object-rest-spread",
30
+ )
38
31
  }
39
32
  if (isBabelPluginNeeded("proposal-optional-catch-binding")) {
40
- babelPluginStructure[
41
- "proposal-optional-catch-binding"
42
- ] = require("@babel/plugin-proposal-optional-catch-binding")
33
+ babelPluginStructure["proposal-optional-catch-binding"] =
34
+ requireBabelPlugin("@babel/plugin-proposal-optional-catch-binding")
43
35
  }
44
36
  if (isBabelPluginNeeded("proposal-unicode-property-regex")) {
45
- babelPluginStructure[
46
- "proposal-unicode-property-regex"
47
- ] = require("@babel/plugin-proposal-unicode-property-regex")
37
+ babelPluginStructure["proposal-unicode-property-regex"] =
38
+ requireBabelPlugin("@babel/plugin-proposal-unicode-property-regex")
48
39
  }
49
40
  if (
50
41
  isJsModule &&
@@ -52,94 +43,92 @@ export const getBaseBabelPluginStructure = ({
52
43
  !isSupportedOnRuntime("top_level_await")
53
44
  ) {
54
45
  babelPluginStructure["transform-async-to-promises"] = [
55
- require("babel-plugin-transform-async-to-promises"),
46
+ requireBabelPlugin("babel-plugin-transform-async-to-promises"),
56
47
  {
57
48
  topLevelAwait,
58
49
  },
59
50
  ]
60
51
  } else if (isBabelPluginNeeded("transform-async-to-promises")) {
61
- babelPluginStructure[
62
- "transform-async-to-promises"
63
- ] = require("babel-plugin-transform-async-to-promises")
52
+ babelPluginStructure["transform-async-to-promises"] = requireBabelPlugin(
53
+ "babel-plugin-transform-async-to-promises",
54
+ )
64
55
  }
65
56
  if (isBabelPluginNeeded("transform-arrow-functions")) {
66
- babelPluginStructure[
67
- "transform-arrow-functions"
68
- ] = require("@babel/plugin-transform-arrow-functions")
57
+ babelPluginStructure["transform-arrow-functions"] = requireBabelPlugin(
58
+ "@babel/plugin-transform-arrow-functions",
59
+ )
69
60
  }
70
61
  if (isBabelPluginNeeded("transform-block-scoped-functions")) {
71
- babelPluginStructure[
72
- "transform-block-scoped-functions"
73
- ] = require("@babel/plugin-transform-block-scoped-functions")
62
+ babelPluginStructure["transform-block-scoped-functions"] =
63
+ requireBabelPlugin("@babel/plugin-transform-block-scoped-functions")
74
64
  }
75
65
  if (isBabelPluginNeeded("transform-block-scoping")) {
76
- babelPluginStructure[
77
- "transform-block-scoping"
78
- ] = require("@babel/plugin-transform-block-scoping")
66
+ babelPluginStructure["transform-block-scoping"] = requireBabelPlugin(
67
+ "@babel/plugin-transform-block-scoping",
68
+ )
79
69
  }
80
70
  if (isBabelPluginNeeded("transform-classes")) {
81
- babelPluginStructure[
82
- "transform-classes"
83
- ] = require("@babel/plugin-transform-classes")
71
+ babelPluginStructure["transform-classes"] = requireBabelPlugin(
72
+ "@babel/plugin-transform-classes",
73
+ )
84
74
  }
85
75
  if (isBabelPluginNeeded("transform-computed-properties")) {
86
- babelPluginStructure[
87
- "transform-computed-properties"
88
- ] = require("@babel/plugin-transform-computed-properties")
76
+ babelPluginStructure["transform-computed-properties"] = requireBabelPlugin(
77
+ "@babel/plugin-transform-computed-properties",
78
+ )
89
79
  }
90
80
  if (isBabelPluginNeeded("transform-destructuring")) {
91
- babelPluginStructure[
92
- "transform-destructuring"
93
- ] = require("@babel/plugin-transform-destructuring")
81
+ babelPluginStructure["transform-destructuring"] = requireBabelPlugin(
82
+ "@babel/plugin-transform-destructuring",
83
+ )
94
84
  }
95
85
  if (isBabelPluginNeeded("transform-dotall-regex")) {
96
- babelPluginStructure[
97
- "transform-dotall-regex"
98
- ] = require("@babel/plugin-transform-dotall-regex")
86
+ babelPluginStructure["transform-dotall-regex"] = requireBabelPlugin(
87
+ "@babel/plugin-transform-dotall-regex",
88
+ )
99
89
  }
100
90
  if (isBabelPluginNeeded("transform-duplicate-keys")) {
101
- babelPluginStructure[
102
- "transform-duplicate-keys"
103
- ] = require("@babel/plugin-transform-duplicate-keys")
91
+ babelPluginStructure["transform-duplicate-keys"] = requireBabelPlugin(
92
+ "@babel/plugin-transform-duplicate-keys",
93
+ )
104
94
  }
105
95
  if (isBabelPluginNeeded("transform-exponentiation-operator")) {
106
- babelPluginStructure[
107
- "transform-exponentiation-operator"
108
- ] = require("@babel/plugin-transform-exponentiation-operator")
96
+ babelPluginStructure["transform-exponentiation-operator"] =
97
+ requireBabelPlugin("@babel/plugin-transform-exponentiation-operator")
109
98
  }
110
99
  if (isBabelPluginNeeded("transform-for-of")) {
111
- babelPluginStructure[
112
- "transform-for-of"
113
- ] = require("@babel/plugin-transform-for-of")
100
+ babelPluginStructure["transform-for-of"] = requireBabelPlugin(
101
+ "@babel/plugin-transform-for-of",
102
+ )
114
103
  }
115
104
  if (isBabelPluginNeeded("transform-function-name")) {
116
- babelPluginStructure[
117
- "transform-function-name"
118
- ] = require("@babel/plugin-transform-function-name")
105
+ babelPluginStructure["transform-function-name"] = requireBabelPlugin(
106
+ "@babel/plugin-transform-function-name",
107
+ )
119
108
  }
120
109
  if (isBabelPluginNeeded("transform-literals")) {
121
- babelPluginStructure[
122
- "transform-literals"
123
- ] = require("@babel/plugin-transform-literals")
110
+ babelPluginStructure["transform-literals"] = requireBabelPlugin(
111
+ "@babel/plugin-transform-literals",
112
+ )
124
113
  }
125
114
  if (isBabelPluginNeeded("transform-new-target")) {
126
- babelPluginStructure[
127
- "transform-new-target"
128
- ] = require("@babel/plugin-transform-new-target")
115
+ babelPluginStructure["transform-new-target"] = requireBabelPlugin(
116
+ "@babel/plugin-transform-new-target",
117
+ )
129
118
  }
130
119
  if (isBabelPluginNeeded("transform-object-super")) {
131
- babelPluginStructure[
132
- "transform-object-super"
133
- ] = require("@babel/plugin-transform-object-super")
120
+ babelPluginStructure["transform-object-super"] = requireBabelPlugin(
121
+ "@babel/plugin-transform-object-super",
122
+ )
134
123
  }
135
124
  if (isBabelPluginNeeded("transform-parameters")) {
136
- babelPluginStructure[
137
- "transform-parameters"
138
- ] = require("@babel/plugin-transform-parameters")
125
+ babelPluginStructure["transform-parameters"] = requireBabelPlugin(
126
+ "@babel/plugin-transform-parameters",
127
+ )
139
128
  }
140
129
  if (isBabelPluginNeeded("transform-regenerator")) {
141
130
  babelPluginStructure["transform-regenerator"] = [
142
- require("@babel/plugin-transform-regenerator"),
131
+ requireBabelPlugin("@babel/plugin-transform-regenerator"),
143
132
  {
144
133
  asyncGenerators: true,
145
134
  generators: true,
@@ -149,22 +138,22 @@ export const getBaseBabelPluginStructure = ({
149
138
  }
150
139
  if (isBabelPluginNeeded("transform-shorthand-properties")) {
151
140
  babelPluginStructure["transform-shorthand-properties"] = [
152
- require("@babel/plugin-transform-shorthand-properties"),
141
+ requireBabelPlugin("@babel/plugin-transform-shorthand-properties"),
153
142
  ]
154
143
  }
155
144
  if (isBabelPluginNeeded("transform-spread")) {
156
145
  babelPluginStructure["transform-spread"] = [
157
- require("@babel/plugin-transform-spread"),
146
+ requireBabelPlugin("@babel/plugin-transform-spread"),
158
147
  ]
159
148
  }
160
149
  if (isBabelPluginNeeded("transform-sticky-regex")) {
161
150
  babelPluginStructure["transform-sticky-regex"] = [
162
- require("@babel/plugin-transform-sticky-regex"),
151
+ requireBabelPlugin("@babel/plugin-transform-sticky-regex"),
163
152
  ]
164
153
  }
165
154
  if (isBabelPluginNeeded("transform-template-literals")) {
166
155
  babelPluginStructure["transform-template-literals"] = [
167
- require("@babel/plugin-transform-template-literals"),
156
+ requireBabelPlugin("@babel/plugin-transform-template-literals"),
168
157
  ]
169
158
  }
170
159
  if (
@@ -175,12 +164,12 @@ export const getBaseBabelPluginStructure = ({
175
164
  url !== getBabelHelperFileUrl("typeof")
176
165
  ) {
177
166
  babelPluginStructure["transform-typeof-symbol"] = [
178
- require("@babel/plugin-transform-typeof-symbol"),
167
+ requireBabelPlugin("@babel/plugin-transform-typeof-symbol"),
179
168
  ]
180
169
  }
181
170
  if (isBabelPluginNeeded("transform-unicode-regex")) {
182
171
  babelPluginStructure["transform-unicode-regex"] = [
183
- require("@babel/plugin-transform-unicode-regex"),
172
+ requireBabelPlugin("@babel/plugin-transform-unicode-regex"),
184
173
  ]
185
174
  }
186
175
  return babelPluginStructure
@@ -1,10 +1,11 @@
1
1
  import { applyBabelPlugins } from "@jsenv/utils/js_ast/apply_babel_plugins.js"
2
2
 
3
+ import { RUNTIME_SUPPORT } from "@jsenv/core/src/omega/runtime_support/runtime_support.js"
3
4
  import { getBaseBabelPluginStructure } from "./helpers/babel_plugin_structure.js"
5
+ import { babelPluginBabelHelpersAsJsenvImports } from "./helpers/babel_plugin_babel_helpers_as_jsenv_imports.js"
4
6
  import { babelPluginNewStylesheetAsJsenvImport } from "./new_stylesheet/babel_plugin_new_stylesheet_as_jsenv_import.js"
5
7
  import { babelPluginGlobalThisAsJsenvImport } from "./global_this/babel_plugin_global_this_as_jsenv_import.js"
6
8
  import { babelPluginRegeneratorRuntimeAsJsenvImport } from "./regenerator_runtime/babel_plugin_regenerator_runtime_as_jsenv_import.js"
7
- import { babelPluginBabelHelpersAsJsenvImports } from "./babel_helper/babel_plugin_babel_helpers_as_jsenv_imports.js"
8
9
 
9
10
  export const jsenvPluginBabel = ({
10
11
  getCustomBabelPlugins,
@@ -12,16 +13,29 @@ export const jsenvPluginBabel = ({
12
13
  } = {}) => {
13
14
  const transformWithBabel = async (urlInfo, context) => {
14
15
  const isJsModule = urlInfo.type === "js_module"
15
- const isWorker =
16
- urlInfo.subtype === "worker" || urlInfo.subtype === "service_worker"
17
- const { isSupportedOnRuntime, referenceUtils } = context
16
+ const isWorker = urlInfo.subtype === "worker"
17
+ const isServiceWorker = urlInfo.subtype === "service_worker"
18
+ const isWorkerContext = isWorker || isServiceWorker
19
+ let { runtimeSupport } = context
20
+ if (isServiceWorker) {
21
+ // when code is executed by a service worker we can assume
22
+ // the execution context supports more than the default one
23
+ // for instance arrow function are supported
24
+ runtimeSupport = RUNTIME_SUPPORT.add(runtimeSupport, "service_worker")
25
+ }
26
+ if (isWorker) {
27
+ runtimeSupport = RUNTIME_SUPPORT.add(runtimeSupport, "worker")
28
+ }
29
+ const { referenceUtils } = context
30
+ const isSupportedOnRuntime = (feature) =>
31
+ RUNTIME_SUPPORT.isSupported(runtimeSupport, feature)
18
32
  const babelPluginStructure = getBaseBabelPluginStructure({
19
33
  url: urlInfo.url,
20
34
  isSupportedOnRuntime,
21
35
  topLevelAwait,
22
36
  usesTopLevelAwait: urlInfo.data.usesTopLevelAwait,
23
37
  isJsModule,
24
- isWorker,
38
+ isWorkerContext,
25
39
  })
26
40
  if (getCustomBabelPlugins) {
27
41
  Object.assign(babelPluginStructure, getCustomBabelPlugins(context))
@@ -1,4 +1,4 @@
1
- import { uneval } from "@jsenv/core/node_modules/@jsenv/uneval/index.js" // WHY?
1
+ import { uneval } from "@jsenv/uneval"
2
2
 
3
3
  export const unevalException = (value) => {
4
4
  if (value && value.hasOwnProperty("toString")) {
@@ -32,26 +32,36 @@ export const jsenvPluginImportMetaScenarios = () => {
32
32
  }
33
33
  if (scenario === "dev") {
34
34
  dev.forEach((path) => {
35
- replace(path, true)
35
+ replace(path, "true")
36
36
  })
37
37
  } else if (scenario === "test") {
38
+ // test is also considered a dev environment
39
+ // just like the dev server can be used to debug test files
40
+ // without this people would have to write
41
+ // if (import.meta.dev || import.meta.test) or if (!import.meta.build)
42
+ dev.forEach((path) => {
43
+ replace(path, "true")
44
+ })
38
45
  test.forEach((path) => {
39
- replace(path, true)
46
+ replace(path, "true")
40
47
  })
41
48
  } else if (scenario === "build") {
49
+ // replacing by undefined might not be required
50
+ // as I suppose rollup would consider them as undefined
51
+ // but let's make it explicit to ensure code is properly tree-shaked
42
52
  dev.forEach((path) => {
43
- replace(path, undefined)
53
+ replace(path, "undefined")
44
54
  })
45
55
  test.forEach((path) => {
46
- replace(path, undefined)
56
+ replace(path, "undefined")
47
57
  })
48
58
  build.forEach((path) => {
49
- replace(path, true)
59
+ replace(path, "true")
50
60
  })
51
61
  }
52
62
  const magicSource = createMagicSource(content)
53
63
  replacements.forEach(({ path, value }) => {
54
- magicSource.overwrite({
64
+ magicSource.replace({
55
65
  start: path.node.start,
56
66
  end: path.node.end,
57
67
  replacement: value,
@@ -0,0 +1,27 @@
1
+ import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
2
+
3
+ export const jsenvPluginInjectGlobals = (globals = {}) => {
4
+ if (Object.keys(globals).length === 0) {
5
+ return []
6
+ }
7
+ const injectGlobals = (urlInfo) => {
8
+ const magicSource = createMagicSource(urlInfo.content)
9
+ const globalName =
10
+ urlInfo.subtype === "worker" || urlInfo.subtype === "service_worker"
11
+ ? "self"
12
+ : "window"
13
+ magicSource.prepend(
14
+ `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`,
15
+ )
16
+ return magicSource.toContentAndSourcemap()
17
+ }
18
+
19
+ return {
20
+ name: "jsenv:inject_globals",
21
+ appliesDuring: "*",
22
+ transform: {
23
+ js_module: injectGlobals,
24
+ js_classic: injectGlobals,
25
+ },
26
+ }
27
+ }
@@ -52,10 +52,7 @@ export const jsenvPluginNewInlineContent = ({ allowEscapeForVersioning }) => {
52
52
  columnEnd: inlineContentCall.columnEnd,
53
53
  })
54
54
  let { quote } = inlineContentCall
55
- if (
56
- quote === "`" &&
57
- !isSupportedOnRuntime("transform-template-literals")
58
- ) {
55
+ if (quote === "`" && !isSupportedOnRuntime("template_literals")) {
59
56
  // if quote is "`" and template literals are not supported
60
57
  // we'll use a regular string (single or double quote)
61
58
  // when rendering the string
@@ -10,6 +10,7 @@ import { jsenvPluginHtmlSupervisor } from "./core_plugins/html_supervisor/jsenv_
10
10
  import { jsenvPluginCommonJsGlobals } from "./core_plugins/commonjs_globals/jsenv_plugin_commonjs_globals.js"
11
11
  import { jsenvPluginImportAssertions } from "./core_plugins/import_assertions/jsenv_plugin_import_assertions.js"
12
12
  import { jsenvPluginImportMetaScenarios } from "./core_plugins/import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js"
13
+ import { jsenvPluginInjectGlobals } from "./core_plugins/inject_globals/jsenv_plugin_inject_globals.js"
13
14
  import { jsenvPluginBabel } from "./core_plugins/babel/jsenv_plugin_babel.js"
14
15
 
15
16
  export const getCorePlugins = ({
@@ -17,6 +18,7 @@ export const getCorePlugins = ({
17
18
  nodeEsmResolution,
18
19
  fileSystemMagicResolution,
19
20
  babel,
21
+ injectedGlobals,
20
22
  } = {}) => {
21
23
  const asFewAsPossible = false // useful during dev
22
24
  return [
@@ -27,6 +29,7 @@ export const getCorePlugins = ({
27
29
  jsenvPluginFileUrls(),
28
30
  jsenvPluginLeadingSlash(),
29
31
  jsenvPluginNodeEsmResolution(nodeEsmResolution), // before url resolution to handle "js_import_export" resolution
32
+ jsenvPluginInjectGlobals(injectedGlobals),
30
33
  jsenvPluginUrlResolution(),
31
34
  ...(asFewAsPossible
32
35
  ? []
@@ -1,59 +1,17 @@
1
1
  import { fileSystemPathToUrl, urlToFileSystemPath } from "@jsenv/filesystem"
2
2
 
3
3
  export const fileUrlConverter = {
4
- asUrlWithoutSpecialParams: (fileUrl) => {
5
- const urlObject = new URL(fileUrl)
6
- let { origin, pathname, searchParams, hash } = urlObject
7
- // origin is "null" for "file://" urls with Node.js
8
- if (origin === "null" && urlObject.href.startsWith("file:")) {
9
- origin = "file://"
10
- }
11
- if (searchParams.has("json_module")) {
12
- searchParams.delete("json_module")
13
- return `${origin}${pathname}.json_module${stringifyQuery(
14
- searchParams,
15
- )}${hash}`
16
- }
17
- if (searchParams.has("css_module")) {
18
- searchParams.delete("css_module")
19
- return `${origin}${pathname}.css_module${stringifyQuery(
20
- searchParams,
21
- )}${hash}`
22
- }
23
- if (searchParams.has("text_module")) {
24
- searchParams.delete("text_module")
25
- return `${origin}${pathname}.text_module${stringifyQuery(
26
- searchParams,
27
- )}${hash}`
28
- }
29
- return fileUrl
30
- },
31
4
  asFilePath: (fileUrl) => {
32
- const fileUrlWithoutSpecialParam =
33
- fileUrlConverter.asUrlWithoutSpecialParams(fileUrl)
34
- const filePath = urlToFileSystemPath(fileUrlWithoutSpecialParam)
35
- return filePath
5
+ const filePath = urlToFileSystemPath(fileUrl)
6
+ const urlObject = new URL(fileUrl)
7
+ const { searchParams } = urlObject
8
+ return `${filePath}${stringifyQuery(searchParams)}`
36
9
  },
37
10
  asFileUrl: (filePath) => {
38
- if (filePath.endsWith(".json_module")) {
39
- const jsonFileUrl = fileSystemPathToUrl(
40
- filePath.slice(0, -".json_module".length),
41
- )
42
- return `${jsonFileUrl}?json_module`
43
- }
44
- if (filePath.endsWith(".css_module")) {
45
- const cssFileUrl = fileSystemPathToUrl(
46
- filePath.slice(0, -".css_module".length),
47
- )
48
- return `${cssFileUrl}?css_module`
49
- }
50
- if (filePath.endsWith(".text_module")) {
51
- const textFileUrl = fileSystemPathToUrl(
52
- filePath.slice(0, -".text_module".length),
53
- )
54
- return `${textFileUrl}?text_module`
55
- }
56
- return fileSystemPathToUrl(filePath)
11
+ return decodeURIComponent(fileSystemPathToUrl(filePath)).replace(
12
+ /[=](?=&|$)/g,
13
+ "",
14
+ )
57
15
  },
58
16
  }
59
17