@jsenv/core 23.8.2 → 23.8.7
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/dist/jsenv_browser_system.js +46 -39
- package/dist/jsenv_browser_system.js.map +14 -14
- package/dist/jsenv_compile_proxy.js.map +6 -6
- package/dist/jsenv_exploring_index.js.map +5 -5
- package/dist/jsenv_exploring_redirector.js.map +12 -12
- package/dist/jsenv_toolbar.js.map +7 -7
- package/dist/jsenv_toolbar_injector.js.map +5 -5
- package/helpers/babel/.eslintrc.cjs +24 -24
- package/helpers/babel/AsyncGenerator/AsyncGenerator.js +81 -81
- package/helpers/babel/AwaitValue/AwaitValue.js +3 -3
- package/helpers/babel/applyDecoratorDescriptor/applyDecoratorDescriptor.js +33 -33
- package/helpers/babel/arrayLikeToArray/arrayLikeToArray.js +7 -7
- package/helpers/babel/arrayWithHoles/arrayWithHoles.js +4 -4
- package/helpers/babel/arrayWithoutHoles/arrayWithoutHoles.js +6 -6
- package/helpers/babel/assertThisInitialized/assertThisInitialized.js +7 -7
- package/helpers/babel/asyncGeneratorDelegate/asyncGeneratorDelegate.js +40 -40
- package/helpers/babel/asyncIterator/asyncIterator.js +12 -12
- package/helpers/babel/asyncToGenerator/asyncToGenerator.js +34 -34
- package/helpers/babel/awaitAsyncGenerator/awaitAsyncGenerator.js +5 -5
- package/helpers/babel/classApplyDescriptorDestructureSet/classApplyDescriptorDestructureSet.js +20 -20
- package/helpers/babel/classApplyDescriptorGet/classApplyDescriptorGet.js +6 -6
- package/helpers/babel/classApplyDescriptorSet/classApplyDescriptorSet.js +13 -13
- package/helpers/babel/classCallCheck/classCallCheck.js +5 -5
- package/helpers/babel/classCheckPrivateStaticAccess/classCheckPrivateStaticAccess.js +5 -5
- package/helpers/babel/classCheckPrivateStaticFieldDescriptor/classCheckPrivateStaticFieldDescriptor.js +6 -6
- package/helpers/babel/classExtractFieldDescriptor/classExtractFieldDescriptor.js +7 -7
- package/helpers/babel/classNameTDZError/classNameTDZError.js +4 -4
- package/helpers/babel/classPrivateFieldDestructureSet/classPrivateFieldDestructureSet.js +7 -7
- package/helpers/babel/classPrivateFieldGet/classPrivateFieldGet.js +7 -7
- package/helpers/babel/classPrivateFieldLooseBase/classPrivateFieldLooseBase.js +6 -6
- package/helpers/babel/classPrivateFieldLooseKey/classPrivateFieldLooseKey.js +5 -5
- package/helpers/babel/classPrivateFieldSet/classPrivateFieldSet.js +8 -8
- package/helpers/babel/classPrivateMethodGet/classPrivateMethodGet.js +6 -6
- package/helpers/babel/classPrivateMethodSet/classPrivateMethodSet.js +3 -3
- package/helpers/babel/classStaticPrivateFieldSpecGet/classStaticPrivateFieldSpecGet.js +9 -9
- package/helpers/babel/classStaticPrivateFieldSpecSet/classStaticPrivateFieldSpecSet.js +15 -15
- package/helpers/babel/classStaticPrivateMethodGet/classStaticPrivateMethodGet.js +6 -6
- package/helpers/babel/classStaticPrivateMethodSet/classStaticPrivateMethodSet.js +3 -3
- package/helpers/babel/construct/construct.js +16 -16
- package/helpers/babel/createClass/createClass.js +15 -15
- package/helpers/babel/createForOfIteratorHelper/createForOfIteratorHelper.js +60 -60
- package/helpers/babel/createForOfIteratorHelperLoose/createForOfIteratorHelperLoose.js +23 -23
- package/helpers/babel/createRawReactElement/createRawReactElement.js +50 -50
- package/helpers/babel/createSuper/createSuper.js +22 -22
- package/helpers/babel/decorate/decorate.js +403 -403
- package/helpers/babel/defaults/defaults.js +11 -11
- package/helpers/babel/defineEnumerableProperties/defineEnumerableProperties.js +23 -23
- package/helpers/babel/defineProperty/defineProperty.js +18 -18
- package/helpers/babel/extends/extends.js +14 -14
- package/helpers/babel/get/get.js +13 -13
- package/helpers/babel/getPrototypeOf/getPrototypeOf.js +4 -4
- package/helpers/babel/inherits/inherits.js +15 -15
- package/helpers/babel/inheritsLoose/inheritsLoose.js +7 -7
- package/helpers/babel/initializerDefineProperty/initializerDefineProperty.js +10 -10
- package/helpers/babel/initializerWarningHelper/initializerWarningHelper.js +6 -6
- package/helpers/babel/instanceof/instanceof.js +6 -6
- package/helpers/babel/interopRequireDefault/interopRequireDefault.js +3 -3
- package/helpers/babel/interopRequireWildcard/interopRequireWildcard.js +37 -37
- package/helpers/babel/isNativeFunction/isNativeFunction.js +4 -4
- package/helpers/babel/isNativeReflectConstruct/isNativeReflectConstruct.js +21 -21
- package/helpers/babel/iterableToArray/iterableToArray.js +7 -7
- package/helpers/babel/iterableToArrayLimit/iterableToArrayLimit.js +36 -36
- package/helpers/babel/iterableToArrayLimitLoose/iterableToArrayLimitLoose.js +10 -10
- package/helpers/babel/jsx/jsx.js +45 -45
- package/helpers/babel/maybeArrayLike/maybeArrayLike.js +10 -10
- package/helpers/babel/newArrowCheck/newArrowCheck.js +5 -5
- package/helpers/babel/nonIterableRest/nonIterableRest.js +5 -5
- package/helpers/babel/nonIterableSpread/nonIterableSpread.js +5 -5
- package/helpers/babel/objectDestructuringEmpty/objectDestructuringEmpty.js +3 -3
- package/helpers/babel/objectSpread/objectSpread.js +23 -23
- package/helpers/babel/objectSpread2/objectSpread2.js +33 -33
- package/helpers/babel/objectWithoutProperties/objectWithoutProperties.js +19 -19
- package/helpers/babel/objectWithoutPropertiesLoose/objectWithoutPropertiesLoose.js +13 -13
- package/helpers/babel/possibleConstructorReturn/possibleConstructorReturn.js +10 -10
- package/helpers/babel/readOnlyError/readOnlyError.js +4 -4
- package/helpers/babel/readme.md +9 -9
- package/helpers/babel/set/set.js +44 -44
- package/helpers/babel/setPrototypeOf/setPrototypeOf.js +6 -6
- package/helpers/babel/skipFirstGeneratorNext/skipFirstGeneratorNext.js +8 -8
- package/helpers/babel/slicedToArray/slicedToArray.js +10 -10
- package/helpers/babel/slicedToArrayLoose/slicedToArrayLoose.js +13 -13
- package/helpers/babel/superPropBase/superPropBase.js +10 -10
- package/helpers/babel/taggedTemplateLiteral/taggedTemplateLiteral.js +10 -10
- package/helpers/babel/taggedTemplateLiteralLoose/taggedTemplateLiteralLoose.js +7 -7
- package/helpers/babel/tdz/tdz.js +4 -4
- package/helpers/babel/temporalRef/temporalRef.js +6 -6
- package/helpers/babel/temporalUndefined/temporalUndefined.js +3 -3
- package/helpers/babel/toArray/toArray.js +10 -10
- package/helpers/babel/toConsumableArray/toConsumableArray.js +10 -10
- package/helpers/babel/toPrimitive/toPrimitive.js +10 -10
- package/helpers/babel/toPropertyKey/toPropertyKey.js +6 -6
- package/helpers/babel/typeof/typeof.js +14 -14
- package/helpers/babel/unsupportedIterableToArray/unsupportedIterableToArray.js +12 -12
- package/helpers/babel/wrapAsyncGenerator/wrapAsyncGenerator.js +8 -8
- package/helpers/babel/wrapNativeSuper/wrapNativeSuper.js +30 -30
- package/helpers/babel/wrapRegExp/wrapRegExp.js +63 -63
- package/helpers/babel/writeOnlyError/writeOnlyError.js +4 -4
- package/helpers/regenerator-runtime/regenerator-runtime.js +748 -748
- package/{LICENSE → license} +21 -21
- package/package.json +2 -2
- package/src/buildProject.js +300 -300
- package/src/execute.js +184 -184
- package/src/internal/browser-launcher/jsenv-browser-system.js +203 -199
- package/src/internal/building/buildUsingRollup.js +2 -10
- package/src/internal/compiling/babel_plugin_import_assertions.js +121 -121
- package/src/internal/compiling/babel_plugin_import_metadata.js +22 -22
- package/src/internal/compiling/babel_plugin_import_visitor.js +84 -84
- package/src/internal/compiling/compile-directory/getOrGenerateCompiledFile.js +268 -268
- package/src/internal/compiling/compile-directory/updateMeta.js +154 -154
- package/src/internal/compiling/compile-directory/validateCache.js +265 -265
- package/src/internal/compiling/compileFile.js +233 -224
- package/src/internal/compiling/compileHtml.js +550 -550
- package/src/internal/compiling/createCompiledFileService.js +291 -291
- package/src/internal/compiling/html_source_file_service.js +403 -404
- package/src/internal/compiling/js-compilation-service/jsenvTransform.js +272 -270
- package/src/internal/compiling/jsenvCompilerForHtml.js +374 -308
- package/src/internal/compiling/jsenvCompilerForJavaScript.js +2 -0
- package/src/internal/compiling/startCompileServer.js +1086 -1048
- package/src/internal/compiling/transformResultToCompilationResult.js +220 -220
- package/src/internal/executing/coverage/babel_plugin_instrument.js +90 -90
- package/src/internal/executing/coverage/reportToCoverage.js +193 -187
- package/src/internal/executing/executePlan.js +183 -183
- package/src/internal/executing/launchAndExecute.js +458 -458
- package/src/internal/generateGroupMap/featuresCompatMap.js +29 -0
- package/src/internal/generateGroupMap/jsenvBabelPluginCompatMap.js +1 -8
- package/src/internal/runtime/createBrowserRuntime/scanBrowserRuntimeFeatures.js +246 -246
- package/src/internal/runtime/createNodeRuntime/scanNodeRuntimeFeatures.js +112 -112
- package/src/internal/runtime/s.js +727 -727
- package/src/internal/toolbar/jsenv-logo.svg +144 -144
- package/src/internal/toolbar/toolbar.main.css +196 -196
- package/src/internal/toolbar/toolbar.main.js +227 -227
- package/src/internal/url_conversion.js +317 -317
- package/src/startExploring.js +309 -309
|
@@ -1,270 +1,272 @@
|
|
|
1
|
-
import { urlToFileSystemPath } from "@jsenv/filesystem"
|
|
2
|
-
|
|
3
|
-
import { require } from "@jsenv/core/src/internal/require.js"
|
|
4
|
-
import { babelPluginTransformImportMeta } from "@jsenv/core/src/internal/babel_plugin_transform_import_meta.js"
|
|
5
|
-
import {
|
|
6
|
-
getMinimalBabelPluginMap,
|
|
7
|
-
babelPluginsFromBabelPluginMap,
|
|
8
|
-
extractSyntaxBabelPluginMap,
|
|
9
|
-
} from "@jsenv/core/src/internal/compiling/babel_plugins.js"
|
|
10
|
-
import { babelPluginImportMetadata } from "@jsenv/core/src/internal/compiling/babel_plugin_import_metadata.js"
|
|
11
|
-
|
|
12
|
-
import { findAsyncPluginNameInBabelPluginMap } from "./findAsyncPluginNameInBabelPluginMap.js"
|
|
13
|
-
import { ansiToHTML } from "./ansiToHTML.js"
|
|
14
|
-
import { babelPluginRegeneratorRuntimeAsJsenvImport } from "./babel_plugin_regenerator_runtime_as_jsenv_import.js"
|
|
15
|
-
import { babelPluginBabelHelpersAsJsenvImports } from "./babel_plugin_babel_helpers_as_jsenv_imports.js"
|
|
16
|
-
import { filePathToBabelHelperName } from "./babelHelper.js"
|
|
17
|
-
|
|
18
|
-
export const jsenvTransform = async ({
|
|
19
|
-
code,
|
|
20
|
-
map, // optional
|
|
21
|
-
ast, // optional
|
|
22
|
-
url,
|
|
23
|
-
relativeUrl, // optional
|
|
24
|
-
|
|
25
|
-
babelPluginMap,
|
|
26
|
-
moduleOutFormat,
|
|
27
|
-
importMetaFormat,
|
|
28
|
-
|
|
29
|
-
babelHelpersInjectionAsImport,
|
|
30
|
-
allowTopLevelAwait,
|
|
31
|
-
transformTopLevelAwait,
|
|
32
|
-
transformGenerator,
|
|
33
|
-
regeneratorRuntimeImportPath,
|
|
34
|
-
sourcemapEnabled,
|
|
35
|
-
}) => {
|
|
36
|
-
const transformModulesSystemJs = require("@babel/plugin-transform-modules-systemjs")
|
|
37
|
-
const proposalDynamicImport = require("@babel/plugin-proposal-dynamic-import")
|
|
38
|
-
|
|
39
|
-
const inputPath = computeInputPath(url)
|
|
40
|
-
|
|
41
|
-
// https://babeljs.io/docs/en/options
|
|
42
|
-
const options = {
|
|
43
|
-
filename: inputPath,
|
|
44
|
-
filenameRelative: relativeUrl,
|
|
45
|
-
inputSourceMap: map,
|
|
46
|
-
configFile: false,
|
|
47
|
-
babelrc: false, // trust only these options, do not read any babelrc config file
|
|
48
|
-
ast: true,
|
|
49
|
-
sourceMaps: sourcemapEnabled,
|
|
50
|
-
sourceFileName: inputPath,
|
|
51
|
-
// https://babeljs.io/docs/en/options#parseropts
|
|
52
|
-
parserOpts: {
|
|
53
|
-
allowAwaitOutsideFunction: allowTopLevelAwait,
|
|
54
|
-
},
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const babelHelperName = filePathToBabelHelperName(inputPath)
|
|
58
|
-
// to prevent typeof circular dependency
|
|
59
|
-
if (babelHelperName === "typeof") {
|
|
60
|
-
const babelPluginMapWithoutTransformTypeOf = { ...babelPluginMap }
|
|
61
|
-
delete babelPluginMapWithoutTransformTypeOf["transform-typeof-symbol"]
|
|
62
|
-
babelPluginMap = babelPluginMapWithoutTransformTypeOf
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (transformGenerator) {
|
|
66
|
-
babelPluginMap = {
|
|
67
|
-
...babelPluginMap,
|
|
68
|
-
"regenerator-runtime-as-jsenv-import": [
|
|
69
|
-
babelPluginRegeneratorRuntimeAsJsenvImport,
|
|
70
|
-
{
|
|
71
|
-
regeneratorRuntimeImportPath,
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
babelPluginMap = {
|
|
78
|
-
...getMinimalBabelPluginMap(),
|
|
79
|
-
"transform-import-meta": [
|
|
80
|
-
babelPluginTransformImportMeta,
|
|
81
|
-
{
|
|
82
|
-
replaceImportMeta: (
|
|
83
|
-
metaPropertyName,
|
|
84
|
-
{ replaceWithImport, replaceWithValue },
|
|
85
|
-
) => {
|
|
86
|
-
if (metaPropertyName === "url") {
|
|
87
|
-
if (importMetaFormat === "esmodule") {
|
|
88
|
-
// keep native version
|
|
89
|
-
return
|
|
90
|
-
}
|
|
91
|
-
if (importMetaFormat === "systemjs") {
|
|
92
|
-
// systemjs will handle it
|
|
93
|
-
return
|
|
94
|
-
}
|
|
95
|
-
if (importMetaFormat === "commonjs") {
|
|
96
|
-
replaceWithImport({
|
|
97
|
-
from: `@jsenv/core/helpers/import-meta/import-meta-url-commonjs.js`,
|
|
98
|
-
})
|
|
99
|
-
return
|
|
100
|
-
}
|
|
101
|
-
if (importMetaFormat === "global") {
|
|
102
|
-
replaceWithImport({
|
|
103
|
-
from: `@jsenv/core/helpers/import-meta/import-meta-url-global.js`,
|
|
104
|
-
})
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (metaPropertyName === "resolve") {
|
|
111
|
-
if (importMetaFormat === "esmodule") {
|
|
112
|
-
// keep native version
|
|
113
|
-
return
|
|
114
|
-
}
|
|
115
|
-
if (importMetaFormat === "systemjs") {
|
|
116
|
-
// systemjs will handle it
|
|
117
|
-
return
|
|
118
|
-
}
|
|
119
|
-
if (importMetaFormat === "commonjs") {
|
|
120
|
-
throw createParseError({
|
|
121
|
-
message: `import.meta.resolve() not supported with commonjs format`,
|
|
122
|
-
})
|
|
123
|
-
}
|
|
124
|
-
if (importMetaFormat === "global") {
|
|
125
|
-
throw createParseError({
|
|
126
|
-
message: `import.meta.resolve() not supported with global format`,
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
return
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
replaceWithValue(undefined)
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
],
|
|
136
|
-
...babelPluginMap,
|
|
137
|
-
...(babelHelpersInjectionAsImport
|
|
138
|
-
? {
|
|
139
|
-
"babel-helpers-as-jsenv-imports": [
|
|
140
|
-
babelPluginBabelHelpersAsJsenvImports,
|
|
141
|
-
],
|
|
142
|
-
}
|
|
143
|
-
: {}),
|
|
144
|
-
"import-metadata": [babelPluginImportMetadata],
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const asyncPluginName = findAsyncPluginNameInBabelPluginMap(babelPluginMap)
|
|
148
|
-
|
|
149
|
-
if (
|
|
150
|
-
moduleOutFormat === "systemjs" &&
|
|
151
|
-
transformTopLevelAwait &&
|
|
152
|
-
asyncPluginName
|
|
153
|
-
) {
|
|
154
|
-
const babelPluginMapWithoutAsync = {
|
|
155
|
-
...babelPluginMap,
|
|
156
|
-
"proposal-dynamic-import": [proposalDynamicImport],
|
|
157
|
-
"transform-modules-systemjs": [transformModulesSystemJs],
|
|
158
|
-
}
|
|
159
|
-
delete babelPluginMapWithoutAsync[asyncPluginName]
|
|
160
|
-
|
|
161
|
-
// put body inside something like (async () => {})()
|
|
162
|
-
const result = await babelTransform({
|
|
163
|
-
ast,
|
|
164
|
-
code,
|
|
165
|
-
options: {
|
|
166
|
-
...options,
|
|
167
|
-
plugins: babelPluginsFromBabelPluginMap(babelPluginMapWithoutAsync),
|
|
168
|
-
},
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
// we need to retranspile the await keywords now wrapped
|
|
172
|
-
// inside Systemjs function.
|
|
173
|
-
// They are ignored, at least by transform-async-to-promises
|
|
174
|
-
// see https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/26
|
|
175
|
-
const { babelSyntaxPluginMap } = extractSyntaxBabelPluginMap(babelPluginMap)
|
|
176
|
-
const finalResult = await babelTransform({
|
|
177
|
-
// ast: result.ast,
|
|
178
|
-
code: result.code,
|
|
179
|
-
options: {
|
|
180
|
-
...options,
|
|
181
|
-
// about inputSourceMap see
|
|
182
|
-
// https://github.com/babel/babel/blob/eac4c5bc17133c2857f2c94c1a6a8643e3b547a7/packages/babel-core/src/transformation/file/generate.js#L57
|
|
183
|
-
// https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-core/src/transformation/file/merge-map.js#L6s
|
|
184
|
-
inputSourceMap: result.map,
|
|
185
|
-
plugins: babelPluginsFromBabelPluginMap({
|
|
186
|
-
...babelSyntaxPluginMap,
|
|
187
|
-
[asyncPluginName]: babelPluginMap[asyncPluginName],
|
|
188
|
-
}),
|
|
189
|
-
},
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
return {
|
|
193
|
-
...result,
|
|
194
|
-
...finalResult,
|
|
195
|
-
metadata: { ...result.metadata, ...finalResult.metadata },
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const babelTransformReturnValue = await babelTransform({
|
|
200
|
-
ast,
|
|
201
|
-
code,
|
|
202
|
-
options: {
|
|
203
|
-
...options,
|
|
204
|
-
plugins: babelPluginsFromBabelPluginMap({
|
|
205
|
-
...babelPluginMap,
|
|
206
|
-
...(moduleOutFormat === "systemjs"
|
|
207
|
-
? {
|
|
208
|
-
"proposal-dynamic-import": [proposalDynamicImport],
|
|
209
|
-
"
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
]
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
parseError
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
1
|
+
import { urlToFileSystemPath } from "@jsenv/filesystem"
|
|
2
|
+
|
|
3
|
+
import { require } from "@jsenv/core/src/internal/require.js"
|
|
4
|
+
import { babelPluginTransformImportMeta } from "@jsenv/core/src/internal/babel_plugin_transform_import_meta.js"
|
|
5
|
+
import {
|
|
6
|
+
getMinimalBabelPluginMap,
|
|
7
|
+
babelPluginsFromBabelPluginMap,
|
|
8
|
+
extractSyntaxBabelPluginMap,
|
|
9
|
+
} from "@jsenv/core/src/internal/compiling/babel_plugins.js"
|
|
10
|
+
import { babelPluginImportMetadata } from "@jsenv/core/src/internal/compiling/babel_plugin_import_metadata.js"
|
|
11
|
+
|
|
12
|
+
import { findAsyncPluginNameInBabelPluginMap } from "./findAsyncPluginNameInBabelPluginMap.js"
|
|
13
|
+
import { ansiToHTML } from "./ansiToHTML.js"
|
|
14
|
+
import { babelPluginRegeneratorRuntimeAsJsenvImport } from "./babel_plugin_regenerator_runtime_as_jsenv_import.js"
|
|
15
|
+
import { babelPluginBabelHelpersAsJsenvImports } from "./babel_plugin_babel_helpers_as_jsenv_imports.js"
|
|
16
|
+
import { filePathToBabelHelperName } from "./babelHelper.js"
|
|
17
|
+
|
|
18
|
+
export const jsenvTransform = async ({
|
|
19
|
+
code,
|
|
20
|
+
map, // optional
|
|
21
|
+
ast, // optional
|
|
22
|
+
url,
|
|
23
|
+
relativeUrl, // optional
|
|
24
|
+
|
|
25
|
+
babelPluginMap,
|
|
26
|
+
moduleOutFormat,
|
|
27
|
+
importMetaFormat,
|
|
28
|
+
|
|
29
|
+
babelHelpersInjectionAsImport,
|
|
30
|
+
allowTopLevelAwait,
|
|
31
|
+
transformTopLevelAwait,
|
|
32
|
+
transformGenerator,
|
|
33
|
+
regeneratorRuntimeImportPath,
|
|
34
|
+
sourcemapEnabled,
|
|
35
|
+
}) => {
|
|
36
|
+
const transformModulesSystemJs = require("@babel/plugin-transform-modules-systemjs")
|
|
37
|
+
const proposalDynamicImport = require("@babel/plugin-proposal-dynamic-import")
|
|
38
|
+
|
|
39
|
+
const inputPath = computeInputPath(url)
|
|
40
|
+
|
|
41
|
+
// https://babeljs.io/docs/en/options
|
|
42
|
+
const options = {
|
|
43
|
+
filename: inputPath,
|
|
44
|
+
filenameRelative: relativeUrl,
|
|
45
|
+
inputSourceMap: map,
|
|
46
|
+
configFile: false,
|
|
47
|
+
babelrc: false, // trust only these options, do not read any babelrc config file
|
|
48
|
+
ast: true,
|
|
49
|
+
sourceMaps: sourcemapEnabled,
|
|
50
|
+
sourceFileName: inputPath,
|
|
51
|
+
// https://babeljs.io/docs/en/options#parseropts
|
|
52
|
+
parserOpts: {
|
|
53
|
+
allowAwaitOutsideFunction: allowTopLevelAwait,
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const babelHelperName = filePathToBabelHelperName(inputPath)
|
|
58
|
+
// to prevent typeof circular dependency
|
|
59
|
+
if (babelHelperName === "typeof") {
|
|
60
|
+
const babelPluginMapWithoutTransformTypeOf = { ...babelPluginMap }
|
|
61
|
+
delete babelPluginMapWithoutTransformTypeOf["transform-typeof-symbol"]
|
|
62
|
+
babelPluginMap = babelPluginMapWithoutTransformTypeOf
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (transformGenerator) {
|
|
66
|
+
babelPluginMap = {
|
|
67
|
+
...babelPluginMap,
|
|
68
|
+
"regenerator-runtime-as-jsenv-import": [
|
|
69
|
+
babelPluginRegeneratorRuntimeAsJsenvImport,
|
|
70
|
+
{
|
|
71
|
+
regeneratorRuntimeImportPath,
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
babelPluginMap = {
|
|
78
|
+
...getMinimalBabelPluginMap(),
|
|
79
|
+
"transform-import-meta": [
|
|
80
|
+
babelPluginTransformImportMeta,
|
|
81
|
+
{
|
|
82
|
+
replaceImportMeta: (
|
|
83
|
+
metaPropertyName,
|
|
84
|
+
{ replaceWithImport, replaceWithValue },
|
|
85
|
+
) => {
|
|
86
|
+
if (metaPropertyName === "url") {
|
|
87
|
+
if (importMetaFormat === "esmodule") {
|
|
88
|
+
// keep native version
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
if (importMetaFormat === "systemjs") {
|
|
92
|
+
// systemjs will handle it
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
if (importMetaFormat === "commonjs") {
|
|
96
|
+
replaceWithImport({
|
|
97
|
+
from: `@jsenv/core/helpers/import-meta/import-meta-url-commonjs.js`,
|
|
98
|
+
})
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
if (importMetaFormat === "global") {
|
|
102
|
+
replaceWithImport({
|
|
103
|
+
from: `@jsenv/core/helpers/import-meta/import-meta-url-global.js`,
|
|
104
|
+
})
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (metaPropertyName === "resolve") {
|
|
111
|
+
if (importMetaFormat === "esmodule") {
|
|
112
|
+
// keep native version
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
if (importMetaFormat === "systemjs") {
|
|
116
|
+
// systemjs will handle it
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
if (importMetaFormat === "commonjs") {
|
|
120
|
+
throw createParseError({
|
|
121
|
+
message: `import.meta.resolve() not supported with commonjs format`,
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
if (importMetaFormat === "global") {
|
|
125
|
+
throw createParseError({
|
|
126
|
+
message: `import.meta.resolve() not supported with global format`,
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
replaceWithValue(undefined)
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
...babelPluginMap,
|
|
137
|
+
...(babelHelpersInjectionAsImport
|
|
138
|
+
? {
|
|
139
|
+
"babel-helpers-as-jsenv-imports": [
|
|
140
|
+
babelPluginBabelHelpersAsJsenvImports,
|
|
141
|
+
],
|
|
142
|
+
}
|
|
143
|
+
: {}),
|
|
144
|
+
"import-metadata": [babelPluginImportMetadata],
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const asyncPluginName = findAsyncPluginNameInBabelPluginMap(babelPluginMap)
|
|
148
|
+
|
|
149
|
+
if (
|
|
150
|
+
moduleOutFormat === "systemjs" &&
|
|
151
|
+
transformTopLevelAwait &&
|
|
152
|
+
asyncPluginName
|
|
153
|
+
) {
|
|
154
|
+
const babelPluginMapWithoutAsync = {
|
|
155
|
+
...babelPluginMap,
|
|
156
|
+
"proposal-dynamic-import": [proposalDynamicImport],
|
|
157
|
+
"transform-modules-systemjs": [transformModulesSystemJs],
|
|
158
|
+
}
|
|
159
|
+
delete babelPluginMapWithoutAsync[asyncPluginName]
|
|
160
|
+
|
|
161
|
+
// put body inside something like (async () => {})()
|
|
162
|
+
const result = await babelTransform({
|
|
163
|
+
ast,
|
|
164
|
+
code,
|
|
165
|
+
options: {
|
|
166
|
+
...options,
|
|
167
|
+
plugins: babelPluginsFromBabelPluginMap(babelPluginMapWithoutAsync),
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
// we need to retranspile the await keywords now wrapped
|
|
172
|
+
// inside Systemjs function.
|
|
173
|
+
// They are ignored, at least by transform-async-to-promises
|
|
174
|
+
// see https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/26
|
|
175
|
+
const { babelSyntaxPluginMap } = extractSyntaxBabelPluginMap(babelPluginMap)
|
|
176
|
+
const finalResult = await babelTransform({
|
|
177
|
+
// ast: result.ast,
|
|
178
|
+
code: result.code,
|
|
179
|
+
options: {
|
|
180
|
+
...options,
|
|
181
|
+
// about inputSourceMap see
|
|
182
|
+
// https://github.com/babel/babel/blob/eac4c5bc17133c2857f2c94c1a6a8643e3b547a7/packages/babel-core/src/transformation/file/generate.js#L57
|
|
183
|
+
// https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-core/src/transformation/file/merge-map.js#L6s
|
|
184
|
+
inputSourceMap: result.map,
|
|
185
|
+
plugins: babelPluginsFromBabelPluginMap({
|
|
186
|
+
...babelSyntaxPluginMap,
|
|
187
|
+
[asyncPluginName]: babelPluginMap[asyncPluginName],
|
|
188
|
+
}),
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
...result,
|
|
194
|
+
...finalResult,
|
|
195
|
+
metadata: { ...result.metadata, ...finalResult.metadata },
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const babelTransformReturnValue = await babelTransform({
|
|
200
|
+
ast,
|
|
201
|
+
code,
|
|
202
|
+
options: {
|
|
203
|
+
...options,
|
|
204
|
+
plugins: babelPluginsFromBabelPluginMap({
|
|
205
|
+
...babelPluginMap,
|
|
206
|
+
...(moduleOutFormat === "systemjs"
|
|
207
|
+
? {
|
|
208
|
+
"proposal-dynamic-import": [proposalDynamicImport],
|
|
209
|
+
...(moduleOutFormat === "systemjs"
|
|
210
|
+
? { "transform-modules-systemjs": [transformModulesSystemJs] }
|
|
211
|
+
: {}),
|
|
212
|
+
}
|
|
213
|
+
: {}),
|
|
214
|
+
}),
|
|
215
|
+
},
|
|
216
|
+
})
|
|
217
|
+
code = babelTransformReturnValue.code
|
|
218
|
+
map = babelTransformReturnValue.map
|
|
219
|
+
ast = babelTransformReturnValue.ast
|
|
220
|
+
const { metadata } = babelTransformReturnValue
|
|
221
|
+
return { code, map, metadata, ast }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const computeInputPath = (url) => {
|
|
225
|
+
if (url.startsWith("file://")) {
|
|
226
|
+
return urlToFileSystemPath(url)
|
|
227
|
+
}
|
|
228
|
+
return url
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const babelTransform = async ({ ast, code, options }) => {
|
|
232
|
+
const { transformAsync, transformFromAstAsync } = await import("@babel/core")
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
if (ast) {
|
|
236
|
+
const result = await transformFromAstAsync(ast, code, options)
|
|
237
|
+
return result
|
|
238
|
+
}
|
|
239
|
+
return await transformAsync(code, options)
|
|
240
|
+
} catch (error) {
|
|
241
|
+
if (error && error.code === "BABEL_PARSE_ERROR") {
|
|
242
|
+
const message = error.message
|
|
243
|
+
const messageWithoutAnsi = message.replace(ansiRegex, "")
|
|
244
|
+
throw createParseError({
|
|
245
|
+
cause: error,
|
|
246
|
+
message: messageWithoutAnsi,
|
|
247
|
+
messageHTML: ansiToHTML(message),
|
|
248
|
+
filename: options.filename,
|
|
249
|
+
lineNumber: error.loc.line,
|
|
250
|
+
columnNumber: error.loc.column,
|
|
251
|
+
})
|
|
252
|
+
}
|
|
253
|
+
throw error
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const pattern = [
|
|
258
|
+
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
|
|
259
|
+
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
|
|
260
|
+
].join("|")
|
|
261
|
+
const ansiRegex = new RegExp(pattern, "g")
|
|
262
|
+
|
|
263
|
+
const createParseError = ({ message, cause, ...data }) => {
|
|
264
|
+
const parseError = new Error(message, { cause })
|
|
265
|
+
parseError.code = "PARSE_ERROR"
|
|
266
|
+
parseError.data = {
|
|
267
|
+
message,
|
|
268
|
+
...data,
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return parseError
|
|
272
|
+
}
|