@atlaspack/transformer-js 3.2.3-canary.19 → 3.2.3-canary.191
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/lib/JSTransformer.js +123 -34
- package/lib/types/JSTransformer.d.ts +3 -0
- package/package.json +16 -13
- package/src/{JSTransformer.js → JSTransformer.ts} +168 -80
package/lib/JSTransformer.js
CHANGED
|
@@ -25,6 +25,13 @@ function _rust() {
|
|
|
25
25
|
};
|
|
26
26
|
return data;
|
|
27
27
|
}
|
|
28
|
+
function _assert() {
|
|
29
|
+
const data = _interopRequireDefault(require("assert"));
|
|
30
|
+
_assert = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
28
35
|
function _browserslist() {
|
|
29
36
|
const data = _interopRequireDefault(require("browserslist"));
|
|
30
37
|
_browserslist = function () {
|
|
@@ -130,6 +137,9 @@ const CONFIG_SCHEMA = {
|
|
|
130
137
|
}
|
|
131
138
|
}]
|
|
132
139
|
},
|
|
140
|
+
addReactDisplayName: {
|
|
141
|
+
type: 'boolean'
|
|
142
|
+
},
|
|
133
143
|
magicComments: {
|
|
134
144
|
type: 'boolean'
|
|
135
145
|
},
|
|
@@ -160,38 +170,46 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
160
170
|
config,
|
|
161
171
|
options
|
|
162
172
|
}) {
|
|
163
|
-
let
|
|
173
|
+
let packageJson = await config.getPackage();
|
|
164
174
|
let isJSX, pragma, pragmaFrag, jsxImportSource, automaticJSXRuntime, reactRefresh, decorators, useDefineForClassFields;
|
|
165
175
|
if (config.isSource) {
|
|
166
|
-
var
|
|
176
|
+
var _packageJson$dependen2, _packageJson$devDepen2, _packageJson$peerDepe2, _await$config$getConf;
|
|
167
177
|
let reactLib;
|
|
168
|
-
if (
|
|
178
|
+
if (packageJson !== null && packageJson !== void 0 && packageJson.alias && packageJson.alias['react']) {
|
|
169
179
|
// e.g.: `{ alias: { "react": "preact/compat" } }`
|
|
170
180
|
reactLib = 'react';
|
|
171
181
|
} else {
|
|
172
182
|
// Find a dependency that we can map to a JSX pragma
|
|
173
183
|
reactLib = Object.keys(JSX_PRAGMA).find(libName => {
|
|
174
|
-
var
|
|
175
|
-
return (
|
|
184
|
+
var _packageJson$dependen, _packageJson$devDepen, _packageJson$peerDepe;
|
|
185
|
+
return (packageJson === null || packageJson === void 0 || (_packageJson$dependen = packageJson.dependencies) === null || _packageJson$dependen === void 0 ? void 0 : _packageJson$dependen[libName]) || (packageJson === null || packageJson === void 0 || (_packageJson$devDepen = packageJson.devDependencies) === null || _packageJson$devDepen === void 0 ? void 0 : _packageJson$devDepen[libName]) || (packageJson === null || packageJson === void 0 || (_packageJson$peerDepe = packageJson.peerDependencies) === null || _packageJson$peerDepe === void 0 ? void 0 : _packageJson$peerDepe[libName]);
|
|
176
186
|
});
|
|
177
187
|
}
|
|
178
|
-
reactRefresh = options.hmrOptions && options.mode === 'development' && Boolean((
|
|
179
|
-
|
|
180
|
-
|
|
188
|
+
reactRefresh = options.hmrOptions && options.mode === 'development' && Boolean((packageJson === null || packageJson === void 0 || (_packageJson$dependen2 = packageJson.dependencies) === null || _packageJson$dependen2 === void 0 ? void 0 : _packageJson$dependen2.react) || (packageJson === null || packageJson === void 0 || (_packageJson$devDepen2 = packageJson.devDependencies) === null || _packageJson$devDepen2 === void 0 ? void 0 : _packageJson$devDepen2.react) || (packageJson === null || packageJson === void 0 || (_packageJson$peerDepe2 = packageJson.peerDependencies) === null || _packageJson$peerDepe2 === void 0 ? void 0 : _packageJson$peerDepe2.react));
|
|
189
|
+
const compilerOptions = (_await$config$getConf = await config.getConfigFrom(options.projectRoot + '/index', ['tsconfig.json', 'jsconfig.json'], {
|
|
190
|
+
readTracking: true
|
|
191
|
+
})) === null || _await$config$getConf === void 0 || (_await$config$getConf = _await$config$getConf.contents) === null || _await$config$getConf === void 0 ? void 0 : _await$config$getConf.compilerOptions;
|
|
181
192
|
|
|
182
193
|
// Use explicitly defined JSX options in tsconfig.json over inferred values from dependencies.
|
|
183
|
-
pragma = (compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.jsxFactory) || (
|
|
184
|
-
|
|
194
|
+
pragma = (compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.jsxFactory) || (
|
|
195
|
+
// @ts-expect-error TS7053
|
|
196
|
+
reactLib ? JSX_PRAGMA[reactLib].pragma : undefined);
|
|
197
|
+
pragmaFrag = (compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.jsxFragmentFactory) || (
|
|
198
|
+
// @ts-expect-error TS7053
|
|
199
|
+
reactLib ? JSX_PRAGMA[reactLib].pragmaFrag : undefined);
|
|
185
200
|
if ((compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.jsx) === 'react-jsx' || (compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.jsx) === 'react-jsxdev' || compilerOptions !== null && compilerOptions !== void 0 && compilerOptions.jsxImportSource) {
|
|
186
201
|
jsxImportSource = compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.jsxImportSource;
|
|
187
202
|
automaticJSXRuntime = true;
|
|
188
203
|
} else if (reactLib) {
|
|
189
|
-
var _JSX_PRAGMA$effective,
|
|
190
|
-
let effectiveReactLib =
|
|
204
|
+
var _JSX_PRAGMA$effective, _packageJson$dependen3, _packageJson$devDepen3, _packageJson$peerDepe3, _semver$minVersion;
|
|
205
|
+
let effectiveReactLib = packageJson !== null && packageJson !== void 0 && packageJson.alias && packageJson.alias['react'] === 'preact/compat' ? 'preact' : reactLib;
|
|
206
|
+
// @ts-expect-error TS7053
|
|
191
207
|
let automaticVersion = (_JSX_PRAGMA$effective = JSX_PRAGMA[effectiveReactLib]) === null || _JSX_PRAGMA$effective === void 0 ? void 0 : _JSX_PRAGMA$effective.automatic;
|
|
192
|
-
let reactLibVersion = (
|
|
208
|
+
let reactLibVersion = (packageJson === null || packageJson === void 0 || (_packageJson$dependen3 = packageJson.dependencies) === null || _packageJson$dependen3 === void 0 ? void 0 : _packageJson$dependen3[effectiveReactLib]) || (packageJson === null || packageJson === void 0 || (_packageJson$devDepen3 = packageJson.devDependencies) === null || _packageJson$devDepen3 === void 0 ? void 0 : _packageJson$devDepen3[effectiveReactLib]) || (packageJson === null || packageJson === void 0 || (_packageJson$peerDepe3 = packageJson.peerDependencies) === null || _packageJson$peerDepe3 === void 0 ? void 0 : _packageJson$peerDepe3[effectiveReactLib]);
|
|
209
|
+
// @ts-expect-error TS2322
|
|
193
210
|
reactLibVersion = reactLibVersion ? _semver().default.validRange(reactLibVersion) : null;
|
|
194
|
-
let minReactLibVersion = reactLibVersion !== null && reactLibVersion !== '*' ?
|
|
211
|
+
let minReactLibVersion = reactLibVersion !== null && reactLibVersion !== '*' ? // @ts-expect-error TS2345
|
|
212
|
+
(_semver$minVersion = _semver().default.minVersion(reactLibVersion)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.toString() : null;
|
|
195
213
|
automaticJSXRuntime = automaticVersion && !(compilerOptions !== null && compilerOptions !== void 0 && compilerOptions.jsxFactory) && minReactLibVersion != null && _semver().default.satisfies(minReactLibVersion, automaticVersion, {
|
|
196
214
|
includePrerelease: true
|
|
197
215
|
});
|
|
@@ -215,7 +233,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
215
233
|
|
|
216
234
|
// Check if we should ignore fs calls
|
|
217
235
|
// See https://github.com/defunctzombie/node-browser-resolve#skip
|
|
218
|
-
let ignoreFS =
|
|
236
|
+
let ignoreFS = packageJson && packageJson.browser && typeof packageJson.browser === 'object' && packageJson.browser.fs === false;
|
|
219
237
|
let conf = await config.getConfigFrom(options.projectRoot + '/index', [], {
|
|
220
238
|
packageKey: '@atlaspack/transformer-js'
|
|
221
239
|
});
|
|
@@ -223,8 +241,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
223
241
|
let inlineFS = !ignoreFS;
|
|
224
242
|
let inlineConstants = false;
|
|
225
243
|
let magicComments = false;
|
|
244
|
+
let addReactDisplayName = false;
|
|
226
245
|
if (conf && conf.contents) {
|
|
227
|
-
var _conf$contents, _conf$contents2, _conf$contents3, _conf$contents4;
|
|
246
|
+
var _conf$contents, _conf$contents2, _conf$contents3, _conf$contents4, _conf$contents5;
|
|
228
247
|
_utils().validateSchema.diagnostic(CONFIG_SCHEMA, {
|
|
229
248
|
data: conf.contents,
|
|
230
249
|
// FIXME
|
|
@@ -234,10 +253,18 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
234
253
|
},
|
|
235
254
|
// FIXME
|
|
236
255
|
'@atlaspack/transformer-js', 'Invalid config for @atlaspack/transformer-js');
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
256
|
+
addReactDisplayName =
|
|
257
|
+
// @ts-expect-error TS2339
|
|
258
|
+
((_conf$contents = conf.contents) === null || _conf$contents === void 0 ? void 0 : _conf$contents.addReactDisplayName) ?? addReactDisplayName;
|
|
259
|
+
// @ts-expect-error TS2339
|
|
260
|
+
magicComments = ((_conf$contents2 = conf.contents) === null || _conf$contents2 === void 0 ? void 0 : _conf$contents2.magicComments) ?? magicComments;
|
|
261
|
+
// @ts-expect-error TS2339
|
|
262
|
+
inlineEnvironment = ((_conf$contents3 = conf.contents) === null || _conf$contents3 === void 0 ? void 0 : _conf$contents3.inlineEnvironment) ?? inlineEnvironment;
|
|
263
|
+
// @ts-expect-error TS2339
|
|
264
|
+
inlineFS = ((_conf$contents4 = conf.contents) === null || _conf$contents4 === void 0 ? void 0 : _conf$contents4.inlineFS) ?? inlineFS;
|
|
265
|
+
inlineConstants =
|
|
266
|
+
// @ts-expect-error TS2339
|
|
267
|
+
((_conf$contents5 = conf.contents) === null || _conf$contents5 === void 0 ? void 0 : _conf$contents5.unstable_inlineConstants) ?? inlineConstants;
|
|
241
268
|
}
|
|
242
269
|
return {
|
|
243
270
|
isJSX,
|
|
@@ -248,6 +275,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
248
275
|
inlineEnvironment,
|
|
249
276
|
inlineFS,
|
|
250
277
|
inlineConstants,
|
|
278
|
+
addReactDisplayName,
|
|
251
279
|
reactRefresh,
|
|
252
280
|
decorators,
|
|
253
281
|
useDefineForClassFields,
|
|
@@ -280,17 +308,22 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
280
308
|
for (let browser of browsers) {
|
|
281
309
|
let [name, version] = browser.split(' ');
|
|
282
310
|
if (BROWSER_MAPPING.hasOwnProperty(name)) {
|
|
311
|
+
// @ts-expect-error TS7053
|
|
283
312
|
name = BROWSER_MAPPING[name];
|
|
284
313
|
if (!name) {
|
|
285
314
|
continue;
|
|
286
315
|
}
|
|
287
316
|
}
|
|
288
317
|
let [major, minor = '0', patch = '0'] = version.split('-')[0].split('.');
|
|
318
|
+
// @ts-expect-error TS2345
|
|
289
319
|
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
|
|
290
320
|
continue;
|
|
291
321
|
}
|
|
292
322
|
let semverVersion = `${major}.${minor}.${patch}`;
|
|
323
|
+
|
|
324
|
+
// @ts-expect-error TS2345
|
|
293
325
|
if (targets[name] == null || _semver().default.gt(targets[name], semverVersion)) {
|
|
326
|
+
// @ts-expect-error TS7053
|
|
294
327
|
targets[name] = semverVersion;
|
|
295
328
|
}
|
|
296
329
|
}
|
|
@@ -305,6 +338,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
305
338
|
if (options.env.NODE_ENV != null) {
|
|
306
339
|
env.NODE_ENV = options.env.NODE_ENV;
|
|
307
340
|
}
|
|
341
|
+
if (process.env.ATLASPACK_BUILD_ENV === 'test') {
|
|
342
|
+
env.ATLASPACK_BUILD_ENV = 'test';
|
|
343
|
+
}
|
|
308
344
|
} else if (Array.isArray(config === null || config === void 0 ? void 0 : config.inlineEnvironment)) {
|
|
309
345
|
for (let match of (0, _utils().globMatch)(Object.keys(options.env), config.inlineEnvironment)) {
|
|
310
346
|
env[match] = String(options.env[match]);
|
|
@@ -322,23 +358,40 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
322
358
|
if (asset.type === 'ts') {
|
|
323
359
|
isJSX = false;
|
|
324
360
|
} else if (!isJSX) {
|
|
361
|
+
// @ts-expect-error TS7053
|
|
325
362
|
isJSX = Boolean(JSX_EXTENSIONS[asset.type]);
|
|
326
363
|
}
|
|
327
364
|
}
|
|
328
365
|
let macroAssets = [];
|
|
329
366
|
let {
|
|
367
|
+
// @ts-expect-error TS2339
|
|
330
368
|
dependencies,
|
|
369
|
+
// @ts-expect-error TS2339
|
|
331
370
|
code: compiledCode,
|
|
371
|
+
// @ts-expect-error TS2339
|
|
332
372
|
map,
|
|
373
|
+
// @ts-expect-error TS2339
|
|
333
374
|
shebang,
|
|
375
|
+
// @ts-expect-error TS2339
|
|
334
376
|
hoist_result,
|
|
377
|
+
// @ts-expect-error TS2339
|
|
335
378
|
symbol_result,
|
|
379
|
+
// @ts-expect-error TS2339
|
|
380
|
+
is_empty_or_empty_export,
|
|
381
|
+
// @ts-expect-error TS2339
|
|
336
382
|
needs_esm_helpers,
|
|
383
|
+
// @ts-expect-error TS2339
|
|
337
384
|
diagnostics,
|
|
385
|
+
// @ts-expect-error TS2339
|
|
338
386
|
used_env,
|
|
387
|
+
// @ts-expect-error TS2339
|
|
339
388
|
has_node_replacements,
|
|
389
|
+
// @ts-expect-error TS2339
|
|
340
390
|
is_constant_module,
|
|
341
|
-
|
|
391
|
+
// @ts-expect-error TS2339
|
|
392
|
+
conditions,
|
|
393
|
+
// @ts-expect-error TS2339
|
|
394
|
+
magic_comments
|
|
342
395
|
} = await (_rust().transformAsync || _rust().transform)({
|
|
343
396
|
filename: asset.filePath,
|
|
344
397
|
code,
|
|
@@ -349,7 +402,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
349
402
|
insert_node_globals: !asset.env.isNode() && asset.env.sourceType !== 'script',
|
|
350
403
|
node_replacer: asset.env.isNode(),
|
|
351
404
|
is_browser: asset.env.isBrowser(),
|
|
352
|
-
is_worker: asset.env.isWorker(),
|
|
405
|
+
is_worker: asset.env.isWorker() || asset.env.isTesseract(),
|
|
353
406
|
env,
|
|
354
407
|
is_type_script: asset.type === 'ts' || asset.type === 'tsx',
|
|
355
408
|
is_jsx: isJSX,
|
|
@@ -358,7 +411,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
358
411
|
automatic_jsx_runtime: Boolean(config === null || config === void 0 ? void 0 : config.automaticJSXRuntime),
|
|
359
412
|
jsx_import_source: config === null || config === void 0 ? void 0 : config.jsxImportSource,
|
|
360
413
|
is_development: options.mode === 'development',
|
|
361
|
-
react_refresh: asset.env.isBrowser() && !asset.env.isLibrary && !asset.env.isWorker() && !asset.env.isWorklet() && Boolean(config === null || config === void 0 ? void 0 : config.reactRefresh),
|
|
414
|
+
react_refresh: asset.env.isBrowser() && !asset.env.isLibrary && !asset.env.isWorker() && !asset.env.isTesseract() && !asset.env.isWorklet() && Boolean(config === null || config === void 0 ? void 0 : config.reactRefresh),
|
|
362
415
|
decorators: Boolean(config === null || config === void 0 ? void 0 : config.decorators),
|
|
363
416
|
use_define_for_class_fields: Boolean(config === null || config === void 0 ? void 0 : config.useDefineForClassFields),
|
|
364
417
|
targets,
|
|
@@ -373,16 +426,18 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
373
426
|
standalone: asset.query.has('standalone'),
|
|
374
427
|
inline_constants: config.inlineConstants,
|
|
375
428
|
conditional_bundling: options.featureFlags.conditionalBundlingApi,
|
|
376
|
-
|
|
429
|
+
hmr_improvements: options.featureFlags.hmrImprovements,
|
|
430
|
+
computed_properties_fix: options.featureFlags.unusedComputedPropertyFix,
|
|
431
|
+
add_display_name: Boolean(config.addReactDisplayName),
|
|
432
|
+
exports_rebinding_optimisation: options.featureFlags.exportsRebindingOptimisation,
|
|
433
|
+
magic_comments: Boolean(config === null || config === void 0 ? void 0 : config.magicComments) || (0, _featureFlags().getFeatureFlag)('supportWebpackChunkName'),
|
|
377
434
|
callMacro: asset.isSource ? async (err, src, exportName, args, loc) => {
|
|
378
435
|
let mod;
|
|
379
436
|
try {
|
|
380
437
|
mod = await options.packageManager.require(src, asset.filePath);
|
|
381
438
|
|
|
382
439
|
// Default interop for CommonJS modules.
|
|
383
|
-
if (exportName === 'default' && !mod.__esModule &&
|
|
384
|
-
// $FlowFixMe
|
|
385
|
-
Object.prototype.toString.call(config) !== '[object Module]') {
|
|
440
|
+
if (exportName === 'default' && !mod.__esModule && Object.prototype.toString.call(config) !== '[object Module]') {
|
|
386
441
|
mod = {
|
|
387
442
|
default: mod
|
|
388
443
|
};
|
|
@@ -398,7 +453,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
398
453
|
}
|
|
399
454
|
try {
|
|
400
455
|
if (typeof mod[exportName] === 'function') {
|
|
401
|
-
|
|
456
|
+
let ctx = {
|
|
402
457
|
// Allows macros to emit additional assets to add as dependencies (e.g. css).
|
|
403
458
|
addAsset(a) {
|
|
404
459
|
let k = String(macroAssets.length);
|
|
@@ -406,6 +461,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
406
461
|
if (asset.env.sourceMap) {
|
|
407
462
|
// Generate a source map that maps each line of the asset to the original macro call.
|
|
408
463
|
map = new (_sourceMap().default)(options.projectRoot);
|
|
464
|
+
// @ts-expect-error TS2304
|
|
409
465
|
let mappings = [];
|
|
410
466
|
let line = 1;
|
|
411
467
|
for (let i = 0; i <= a.content.length; i++) {
|
|
@@ -426,6 +482,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
426
482
|
}
|
|
427
483
|
map.addIndexedMappings(mappings);
|
|
428
484
|
if (originalMap) {
|
|
485
|
+
// @ts-expect-error TS2345
|
|
429
486
|
map.extends(originalMap);
|
|
430
487
|
} else {
|
|
431
488
|
map.setSourceContent(asset.filePath, code.toString());
|
|
@@ -457,7 +514,8 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
457
514
|
invalidateOnBuild() {
|
|
458
515
|
asset.invalidateOnBuild();
|
|
459
516
|
}
|
|
460
|
-
}
|
|
517
|
+
};
|
|
518
|
+
return mod[exportName].apply(ctx, args);
|
|
461
519
|
} else {
|
|
462
520
|
throw new Error(`"${exportName}" in "${src}" is not a function.`);
|
|
463
521
|
}
|
|
@@ -479,7 +537,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
479
537
|
} : null
|
|
480
538
|
});
|
|
481
539
|
if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
|
|
482
|
-
asset.meta.conditions = conditions.map(
|
|
540
|
+
asset.meta.conditions = conditions.map(
|
|
541
|
+
// @ts-expect-error TS7006
|
|
542
|
+
c => ({
|
|
483
543
|
key: c.key,
|
|
484
544
|
ifTruePlaceholder: c.if_true_placeholder,
|
|
485
545
|
ifFalsePlaceholder: c.if_false_placeholder
|
|
@@ -508,12 +568,17 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
508
568
|
return location;
|
|
509
569
|
};
|
|
510
570
|
if (diagnostics) {
|
|
511
|
-
let errors = diagnostics.filter(
|
|
512
|
-
|
|
571
|
+
let errors = diagnostics.filter(
|
|
572
|
+
// @ts-expect-error TS7006
|
|
573
|
+
d => d.severity === 'Error' || d.severity === 'SourceError' && asset.isSource);
|
|
574
|
+
let warnings = diagnostics.filter(
|
|
575
|
+
// @ts-expect-error TS7006
|
|
576
|
+
d => d.severity === 'Warning' || d.severity === 'SourceError' && !asset.isSource);
|
|
513
577
|
let convertDiagnostic = diagnostic => {
|
|
514
578
|
var _diagnostic$code_high;
|
|
515
579
|
let message = diagnostic.message;
|
|
516
580
|
if (message === 'SCRIPT_ERROR') {
|
|
581
|
+
// @ts-expect-error TS7053
|
|
517
582
|
let err = SCRIPT_ERRORS[asset.env.context];
|
|
518
583
|
message = (err === null || err === void 0 ? void 0 : err.message) || SCRIPT_ERRORS.browser.message;
|
|
519
584
|
}
|
|
@@ -536,6 +601,8 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
536
601
|
codeHighlights: [(0, _diagnostic().convertSourceLocationToHighlight)(asset.env.loc, 'The environment was originally created here')]
|
|
537
602
|
});
|
|
538
603
|
}
|
|
604
|
+
|
|
605
|
+
// @ts-expect-error TS7053
|
|
539
606
|
let err = SCRIPT_ERRORS[asset.env.context];
|
|
540
607
|
if (err) {
|
|
541
608
|
if (!res.hints) {
|
|
@@ -580,6 +647,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
580
647
|
env: {
|
|
581
648
|
context: 'web-worker',
|
|
582
649
|
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
|
|
650
|
+
// @ts-expect-error TS2322
|
|
583
651
|
outputFormat,
|
|
584
652
|
loc
|
|
585
653
|
},
|
|
@@ -629,6 +697,13 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
629
697
|
});
|
|
630
698
|
} else if (dep.kind === 'File') {
|
|
631
699
|
asset.invalidateOnFileChange(dep.specifier);
|
|
700
|
+
} else if (dep.kind === 'Id') {
|
|
701
|
+
// Record parcelRequire calls so that the dev packager can add them as dependencies.
|
|
702
|
+
// This allows the HMR runtime to collect parents across async boundaries (through runtimes).
|
|
703
|
+
// TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
|
|
704
|
+
asset.meta.hmrDeps ??= [];
|
|
705
|
+
(0, _assert().default)(Array.isArray(asset.meta.hmrDeps));
|
|
706
|
+
asset.meta.hmrDeps.push(dep.specifier);
|
|
632
707
|
} else {
|
|
633
708
|
let meta = {
|
|
634
709
|
kind: dep.kind
|
|
@@ -676,6 +751,12 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
676
751
|
outputFormat,
|
|
677
752
|
loc: convertLoc(dep.loc)
|
|
678
753
|
};
|
|
754
|
+
if ((0, _featureFlags().getFeatureFlag)('supportWebpackChunkName')) {
|
|
755
|
+
let chunkName = magic_comments[dep.specifier];
|
|
756
|
+
if (chunkName) {
|
|
757
|
+
meta.chunkName = chunkName;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
679
760
|
}
|
|
680
761
|
|
|
681
762
|
// Always bundle helpers, even with includeNodeModules: false, except if this is a library.
|
|
@@ -695,6 +776,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
695
776
|
idx = dep.specifier.indexOf('/', idx + 1);
|
|
696
777
|
}
|
|
697
778
|
let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
|
|
779
|
+
// @ts-expect-error TS7053
|
|
698
780
|
range = _package.default.dependencies[module];
|
|
699
781
|
}
|
|
700
782
|
asset.addDependency({
|
|
@@ -706,6 +788,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
706
788
|
meta,
|
|
707
789
|
resolveFrom: isHelper ? __filename : undefined,
|
|
708
790
|
range,
|
|
791
|
+
// @ts-expect-error TS2322
|
|
709
792
|
env
|
|
710
793
|
});
|
|
711
794
|
}
|
|
@@ -717,10 +800,12 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
717
800
|
exported,
|
|
718
801
|
local,
|
|
719
802
|
loc,
|
|
720
|
-
is_esm
|
|
803
|
+
is_esm,
|
|
804
|
+
is_static_binding_safe
|
|
721
805
|
} of hoist_result.exported_symbols) {
|
|
722
806
|
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
723
|
-
isEsm: is_esm
|
|
807
|
+
isEsm: is_esm,
|
|
808
|
+
isStaticBindingSafe: is_static_binding_safe
|
|
724
809
|
});
|
|
725
810
|
}
|
|
726
811
|
|
|
@@ -802,6 +887,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
802
887
|
// (and the asset has side effects), or the asset is wrapped.
|
|
803
888
|
// This allows accessing symbols that don't exist without errors in symbol propagation.
|
|
804
889
|
if (hoist_result.has_cjs_exports || !hoist_result.is_esm && asset.sideEffects && deps.size === 0 && Object.keys(hoist_result.exported_symbols).length === 0 || hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*')) {
|
|
890
|
+
if (is_empty_or_empty_export) {
|
|
891
|
+
asset.meta.emptyFileStarReexport = true;
|
|
892
|
+
}
|
|
805
893
|
asset.symbols.set('*', `$${asset.id}$exports`);
|
|
806
894
|
}
|
|
807
895
|
asset.meta.hasCJSExports = hoist_result.has_cjs_exports;
|
|
@@ -884,6 +972,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
884
972
|
let sourceMap = new (_sourceMap().default)(options.projectRoot);
|
|
885
973
|
sourceMap.addVLQMap(JSON.parse(map));
|
|
886
974
|
if (originalMap) {
|
|
975
|
+
// @ts-expect-error TS2345
|
|
887
976
|
sourceMap.extends(originalMap);
|
|
888
977
|
}
|
|
889
978
|
asset.setMap(sourceMap);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/transformer-js",
|
|
3
|
-
"version": "3.2.3-canary.
|
|
3
|
+
"version": "3.2.3-canary.191+eedcbc408",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -9,10 +9,13 @@
|
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
11
11
|
},
|
|
12
|
-
"main": "lib/JSTransformer.js",
|
|
13
|
-
"source": "src/JSTransformer.
|
|
12
|
+
"main": "./lib/JSTransformer.js",
|
|
13
|
+
"source": "./src/JSTransformer.ts",
|
|
14
|
+
"types": "./lib/JSTransformer.d.ts",
|
|
14
15
|
"scripts": {
|
|
15
|
-
"test": "mocha"
|
|
16
|
+
"test": "mocha",
|
|
17
|
+
"check-ts": "tsc --emitDeclarationOnly --rootDir src",
|
|
18
|
+
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
16
19
|
},
|
|
17
20
|
"engines": {
|
|
18
21
|
"node": ">= 16.0.0"
|
|
@@ -22,12 +25,12 @@
|
|
|
22
25
|
"src"
|
|
23
26
|
],
|
|
24
27
|
"dependencies": {
|
|
25
|
-
"@atlaspack/diagnostic": "2.14.1-canary.
|
|
26
|
-
"@atlaspack/feature-flags": "2.14.1-canary.
|
|
27
|
-
"@atlaspack/plugin": "2.14.5-canary.
|
|
28
|
-
"@atlaspack/rust": "3.2.1-canary.
|
|
29
|
-
"@atlaspack/utils": "2.14.5-canary.
|
|
30
|
-
"@atlaspack/workers": "2.14.5-canary.
|
|
28
|
+
"@atlaspack/diagnostic": "2.14.1-canary.259+eedcbc408",
|
|
29
|
+
"@atlaspack/feature-flags": "2.14.1-canary.259+eedcbc408",
|
|
30
|
+
"@atlaspack/plugin": "2.14.5-canary.191+eedcbc408",
|
|
31
|
+
"@atlaspack/rust": "3.2.1-canary.191+eedcbc408",
|
|
32
|
+
"@atlaspack/utils": "2.14.5-canary.191+eedcbc408",
|
|
33
|
+
"@atlaspack/workers": "2.14.5-canary.191+eedcbc408",
|
|
31
34
|
"@parcel/source-map": "^2.1.1",
|
|
32
35
|
"@swc/helpers": "^0.5.15",
|
|
33
36
|
"browserslist": "^4.6.6",
|
|
@@ -36,8 +39,8 @@
|
|
|
36
39
|
"semver": "^7.5.2"
|
|
37
40
|
},
|
|
38
41
|
"peerDependencies": {
|
|
39
|
-
"@atlaspack/core": "
|
|
42
|
+
"@atlaspack/core": "2.23.5"
|
|
40
43
|
},
|
|
41
44
|
"type": "commonjs",
|
|
42
|
-
"gitHead": "
|
|
43
|
-
}
|
|
45
|
+
"gitHead": "eedcbc408fc1e86a2a8e25f1a41c57146d8529e1"
|
|
46
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import type {
|
|
3
2
|
JSONObject,
|
|
4
3
|
EnvMap,
|
|
@@ -12,6 +11,7 @@ import type {Diagnostic} from '@atlaspack/diagnostic';
|
|
|
12
11
|
import SourceMap from '@parcel/source-map';
|
|
13
12
|
import {Transformer} from '@atlaspack/plugin';
|
|
14
13
|
import {transform, transformAsync} from '@atlaspack/rust';
|
|
14
|
+
import invariant from 'assert';
|
|
15
15
|
import browserslist from 'browserslist';
|
|
16
16
|
import semver from 'semver';
|
|
17
17
|
import nullthrows from 'nullthrows';
|
|
@@ -26,7 +26,7 @@ import {getFeatureFlag} from '@atlaspack/feature-flags';
|
|
|
26
26
|
const JSX_EXTENSIONS = {
|
|
27
27
|
jsx: true,
|
|
28
28
|
tsx: true,
|
|
29
|
-
};
|
|
29
|
+
} as const;
|
|
30
30
|
|
|
31
31
|
const JSX_PRAGMA = {
|
|
32
32
|
react: {
|
|
@@ -49,7 +49,7 @@ const JSX_PRAGMA = {
|
|
|
49
49
|
pragmaFrag: undefined,
|
|
50
50
|
automatic: undefined,
|
|
51
51
|
},
|
|
52
|
-
};
|
|
52
|
+
} as const;
|
|
53
53
|
|
|
54
54
|
const BROWSER_MAPPING = {
|
|
55
55
|
and_chr: 'chrome',
|
|
@@ -63,7 +63,7 @@ const BROWSER_MAPPING = {
|
|
|
63
63
|
bb: null,
|
|
64
64
|
kaios: null,
|
|
65
65
|
op_mini: null,
|
|
66
|
-
};
|
|
66
|
+
} as const;
|
|
67
67
|
|
|
68
68
|
// List of browsers to exclude when the esmodule target is specified.
|
|
69
69
|
// Based on https://caniuse.com/#feat=es6-module
|
|
@@ -108,6 +108,9 @@ const CONFIG_SCHEMA: SchemaEntity = {
|
|
|
108
108
|
},
|
|
109
109
|
],
|
|
110
110
|
},
|
|
111
|
+
addReactDisplayName: {
|
|
112
|
+
type: 'boolean',
|
|
113
|
+
},
|
|
111
114
|
magicComments: {
|
|
112
115
|
type: 'boolean',
|
|
113
116
|
},
|
|
@@ -133,47 +136,45 @@ const SCRIPT_ERRORS = {
|
|
|
133
136
|
'Service workers cannot have imports or exports without the `type: "module"` option.',
|
|
134
137
|
hint: "Add {type: 'module'} as a second argument to the navigator.serviceWorker.register() call.",
|
|
135
138
|
},
|
|
136
|
-
};
|
|
139
|
+
} as const;
|
|
137
140
|
|
|
138
141
|
type TSConfig = {
|
|
139
142
|
compilerOptions?: {
|
|
140
143
|
// https://www.typescriptlang.org/tsconfig#jsx
|
|
141
|
-
jsx?: 'react' | 'react-jsx' | 'react-jsxdev' | 'preserve' | 'react-native'
|
|
144
|
+
jsx?: 'react' | 'react-jsx' | 'react-jsxdev' | 'preserve' | 'react-native';
|
|
142
145
|
// https://www.typescriptlang.org/tsconfig#jsxFactory
|
|
143
|
-
jsxFactory?: string
|
|
146
|
+
jsxFactory?: string;
|
|
144
147
|
// https://www.typescriptlang.org/tsconfig#jsxFragmentFactory
|
|
145
|
-
jsxFragmentFactory?: string
|
|
148
|
+
jsxFragmentFactory?: string;
|
|
146
149
|
// https://www.typescriptlang.org/tsconfig#jsxImportSource
|
|
147
|
-
jsxImportSource?: string
|
|
150
|
+
jsxImportSource?: string;
|
|
148
151
|
// https://www.typescriptlang.org/tsconfig#experimentalDecorators
|
|
149
|
-
experimentalDecorators?: boolean
|
|
152
|
+
experimentalDecorators?: boolean;
|
|
150
153
|
// https://www.typescriptlang.org/tsconfig#useDefineForClassFields
|
|
151
|
-
useDefineForClassFields?: boolean
|
|
154
|
+
useDefineForClassFields?: boolean;
|
|
152
155
|
// https://www.typescriptlang.org/tsconfig#target
|
|
153
|
-
target?: string
|
|
154
|
-
|
|
155
|
-
},
|
|
156
|
-
...
|
|
156
|
+
target?: string; // 'es3' | 'es5' | 'es6' | 'es2015' | ... |'es2022' | ... | 'esnext';
|
|
157
|
+
};
|
|
157
158
|
};
|
|
158
159
|
|
|
159
|
-
type MacroAsset = {
|
|
160
|
-
type: string
|
|
161
|
-
content: string
|
|
162
|
-
|
|
160
|
+
type MacroAsset = {
|
|
161
|
+
type: string;
|
|
162
|
+
content: string;
|
|
163
|
+
};
|
|
163
164
|
|
|
164
165
|
// NOTE: Make sure this is in sync with the TypeScript definition in the @atlaspack/macros package.
|
|
165
|
-
type MacroContext = {
|
|
166
|
-
addAsset(asset: MacroAsset): void
|
|
167
|
-
invalidateOnFileChange(FilePath): void
|
|
168
|
-
invalidateOnFileCreate(FileCreateInvalidation): void
|
|
169
|
-
invalidateOnEnvChange(string): void
|
|
170
|
-
invalidateOnStartup(): void
|
|
171
|
-
invalidateOnBuild(): void
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
export default
|
|
166
|
+
type MacroContext = {
|
|
167
|
+
addAsset(asset: MacroAsset): void;
|
|
168
|
+
invalidateOnFileChange(arg1: FilePath): void;
|
|
169
|
+
invalidateOnFileCreate(arg1: FileCreateInvalidation): void;
|
|
170
|
+
invalidateOnEnvChange(arg1: string): void;
|
|
171
|
+
invalidateOnStartup(): void;
|
|
172
|
+
invalidateOnBuild(): void;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export default new Transformer({
|
|
175
176
|
async loadConfig({config, options}) {
|
|
176
|
-
let
|
|
177
|
+
let packageJson = await config.getPackage();
|
|
177
178
|
let isJSX,
|
|
178
179
|
pragma,
|
|
179
180
|
pragmaFrag,
|
|
@@ -184,16 +185,16 @@ export default (new Transformer({
|
|
|
184
185
|
useDefineForClassFields;
|
|
185
186
|
if (config.isSource) {
|
|
186
187
|
let reactLib;
|
|
187
|
-
if (
|
|
188
|
+
if (packageJson?.alias && packageJson.alias['react']) {
|
|
188
189
|
// e.g.: `{ alias: { "react": "preact/compat" } }`
|
|
189
190
|
reactLib = 'react';
|
|
190
191
|
} else {
|
|
191
192
|
// Find a dependency that we can map to a JSX pragma
|
|
192
193
|
reactLib = Object.keys(JSX_PRAGMA).find(
|
|
193
194
|
(libName) =>
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
195
|
+
packageJson?.dependencies?.[libName] ||
|
|
196
|
+
packageJson?.devDependencies?.[libName] ||
|
|
197
|
+
packageJson?.peerDependencies?.[libName],
|
|
197
198
|
);
|
|
198
199
|
}
|
|
199
200
|
|
|
@@ -201,23 +202,29 @@ export default (new Transformer({
|
|
|
201
202
|
options.hmrOptions &&
|
|
202
203
|
options.mode === 'development' &&
|
|
203
204
|
Boolean(
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
205
|
+
packageJson?.dependencies?.react ||
|
|
206
|
+
packageJson?.devDependencies?.react ||
|
|
207
|
+
packageJson?.peerDependencies?.react,
|
|
207
208
|
);
|
|
208
209
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
210
|
+
const compilerOptions: TSConfig['compilerOptions'] = (
|
|
211
|
+
await config.getConfigFrom<TSConfig>(
|
|
212
|
+
options.projectRoot + '/index',
|
|
213
|
+
['tsconfig.json', 'jsconfig.json'],
|
|
214
|
+
{
|
|
215
|
+
readTracking: true,
|
|
216
|
+
},
|
|
217
|
+
)
|
|
218
|
+
)?.contents?.compilerOptions;
|
|
214
219
|
|
|
215
220
|
// Use explicitly defined JSX options in tsconfig.json over inferred values from dependencies.
|
|
216
221
|
pragma =
|
|
217
222
|
compilerOptions?.jsxFactory ||
|
|
223
|
+
// @ts-expect-error TS7053
|
|
218
224
|
(reactLib ? JSX_PRAGMA[reactLib].pragma : undefined);
|
|
219
225
|
pragmaFrag =
|
|
220
226
|
compilerOptions?.jsxFragmentFactory ||
|
|
227
|
+
// @ts-expect-error TS7053
|
|
221
228
|
(reactLib ? JSX_PRAGMA[reactLib].pragmaFrag : undefined);
|
|
222
229
|
|
|
223
230
|
if (
|
|
@@ -229,20 +236,23 @@ export default (new Transformer({
|
|
|
229
236
|
automaticJSXRuntime = true;
|
|
230
237
|
} else if (reactLib) {
|
|
231
238
|
let effectiveReactLib =
|
|
232
|
-
|
|
239
|
+
packageJson?.alias && packageJson.alias['react'] === 'preact/compat'
|
|
233
240
|
? 'preact'
|
|
234
241
|
: reactLib;
|
|
242
|
+
// @ts-expect-error TS7053
|
|
235
243
|
let automaticVersion = JSX_PRAGMA[effectiveReactLib]?.automatic;
|
|
236
244
|
let reactLibVersion =
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
245
|
+
packageJson?.dependencies?.[effectiveReactLib] ||
|
|
246
|
+
packageJson?.devDependencies?.[effectiveReactLib] ||
|
|
247
|
+
packageJson?.peerDependencies?.[effectiveReactLib];
|
|
248
|
+
// @ts-expect-error TS2322
|
|
240
249
|
reactLibVersion = reactLibVersion
|
|
241
250
|
? semver.validRange(reactLibVersion)
|
|
242
251
|
: null;
|
|
243
252
|
let minReactLibVersion =
|
|
244
253
|
reactLibVersion !== null && reactLibVersion !== '*'
|
|
245
|
-
?
|
|
254
|
+
? // @ts-expect-error TS2345
|
|
255
|
+
semver.minVersion(reactLibVersion)?.toString()
|
|
246
256
|
: null;
|
|
247
257
|
|
|
248
258
|
automaticJSXRuntime =
|
|
@@ -278,10 +288,10 @@ export default (new Transformer({
|
|
|
278
288
|
// Check if we should ignore fs calls
|
|
279
289
|
// See https://github.com/defunctzombie/node-browser-resolve#skip
|
|
280
290
|
let ignoreFS =
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
typeof
|
|
284
|
-
|
|
291
|
+
packageJson &&
|
|
292
|
+
packageJson.browser &&
|
|
293
|
+
typeof packageJson.browser === 'object' &&
|
|
294
|
+
packageJson.browser.fs === false;
|
|
285
295
|
|
|
286
296
|
let conf = await config.getConfigFrom(options.projectRoot + '/index', [], {
|
|
287
297
|
packageKey: '@atlaspack/transformer-js',
|
|
@@ -291,6 +301,7 @@ export default (new Transformer({
|
|
|
291
301
|
let inlineFS = !ignoreFS;
|
|
292
302
|
let inlineConstants = false;
|
|
293
303
|
let magicComments = false;
|
|
304
|
+
let addReactDisplayName = false;
|
|
294
305
|
|
|
295
306
|
if (conf && conf.contents) {
|
|
296
307
|
validateSchema.diagnostic(
|
|
@@ -307,10 +318,17 @@ export default (new Transformer({
|
|
|
307
318
|
'Invalid config for @atlaspack/transformer-js',
|
|
308
319
|
);
|
|
309
320
|
|
|
321
|
+
addReactDisplayName =
|
|
322
|
+
// @ts-expect-error TS2339
|
|
323
|
+
conf.contents?.addReactDisplayName ?? addReactDisplayName;
|
|
324
|
+
// @ts-expect-error TS2339
|
|
310
325
|
magicComments = conf.contents?.magicComments ?? magicComments;
|
|
326
|
+
// @ts-expect-error TS2339
|
|
311
327
|
inlineEnvironment = conf.contents?.inlineEnvironment ?? inlineEnvironment;
|
|
328
|
+
// @ts-expect-error TS2339
|
|
312
329
|
inlineFS = conf.contents?.inlineFS ?? inlineFS;
|
|
313
330
|
inlineConstants =
|
|
331
|
+
// @ts-expect-error TS2339
|
|
314
332
|
conf.contents?.unstable_inlineConstants ?? inlineConstants;
|
|
315
333
|
}
|
|
316
334
|
|
|
@@ -323,6 +341,7 @@ export default (new Transformer({
|
|
|
323
341
|
inlineEnvironment,
|
|
324
342
|
inlineFS,
|
|
325
343
|
inlineConstants,
|
|
344
|
+
addReactDisplayName,
|
|
326
345
|
reactRefresh,
|
|
327
346
|
decorators,
|
|
328
347
|
useDefineForClassFields,
|
|
@@ -357,6 +376,7 @@ export default (new Transformer({
|
|
|
357
376
|
for (let browser of browsers) {
|
|
358
377
|
let [name, version] = browser.split(' ');
|
|
359
378
|
if (BROWSER_MAPPING.hasOwnProperty(name)) {
|
|
379
|
+
// @ts-expect-error TS7053
|
|
360
380
|
name = BROWSER_MAPPING[name];
|
|
361
381
|
if (!name) {
|
|
362
382
|
continue;
|
|
@@ -366,12 +386,15 @@ export default (new Transformer({
|
|
|
366
386
|
let [major, minor = '0', patch = '0'] = version
|
|
367
387
|
.split('-')[0]
|
|
368
388
|
.split('.');
|
|
389
|
+
// @ts-expect-error TS2345
|
|
369
390
|
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
|
|
370
391
|
continue;
|
|
371
392
|
}
|
|
372
393
|
let semverVersion = `${major}.${minor}.${patch}`;
|
|
373
394
|
|
|
395
|
+
// @ts-expect-error TS2345
|
|
374
396
|
if (targets[name] == null || semver.gt(targets[name], semverVersion)) {
|
|
397
|
+
// @ts-expect-error TS7053
|
|
375
398
|
targets[name] = semverVersion;
|
|
376
399
|
}
|
|
377
400
|
}
|
|
@@ -411,24 +434,47 @@ export default (new Transformer({
|
|
|
411
434
|
if (asset.type === 'ts') {
|
|
412
435
|
isJSX = false;
|
|
413
436
|
} else if (!isJSX) {
|
|
437
|
+
// @ts-expect-error TS7053
|
|
414
438
|
isJSX = Boolean(JSX_EXTENSIONS[asset.type]);
|
|
415
439
|
}
|
|
416
440
|
}
|
|
417
441
|
|
|
418
|
-
let macroAssets
|
|
442
|
+
let macroAssets: Array<{
|
|
443
|
+
content: string;
|
|
444
|
+
// @ts-expect-error TS2552
|
|
445
|
+
map: undefined | NodeSourceMap;
|
|
446
|
+
type: string;
|
|
447
|
+
uniqueKey: string;
|
|
448
|
+
}> = [];
|
|
419
449
|
let {
|
|
450
|
+
// @ts-expect-error TS2339
|
|
420
451
|
dependencies,
|
|
452
|
+
// @ts-expect-error TS2339
|
|
421
453
|
code: compiledCode,
|
|
454
|
+
// @ts-expect-error TS2339
|
|
422
455
|
map,
|
|
456
|
+
// @ts-expect-error TS2339
|
|
423
457
|
shebang,
|
|
458
|
+
// @ts-expect-error TS2339
|
|
424
459
|
hoist_result,
|
|
460
|
+
// @ts-expect-error TS2339
|
|
425
461
|
symbol_result,
|
|
462
|
+
// @ts-expect-error TS2339
|
|
463
|
+
is_empty_or_empty_export,
|
|
464
|
+
// @ts-expect-error TS2339
|
|
426
465
|
needs_esm_helpers,
|
|
466
|
+
// @ts-expect-error TS2339
|
|
427
467
|
diagnostics,
|
|
468
|
+
// @ts-expect-error TS2339
|
|
428
469
|
used_env,
|
|
470
|
+
// @ts-expect-error TS2339
|
|
429
471
|
has_node_replacements,
|
|
472
|
+
// @ts-expect-error TS2339
|
|
430
473
|
is_constant_module,
|
|
474
|
+
// @ts-expect-error TS2339
|
|
431
475
|
conditions,
|
|
476
|
+
// @ts-expect-error TS2339
|
|
477
|
+
magic_comments,
|
|
432
478
|
} = await (transformAsync || transform)({
|
|
433
479
|
filename: asset.filePath,
|
|
434
480
|
code,
|
|
@@ -440,7 +486,7 @@ export default (new Transformer({
|
|
|
440
486
|
!asset.env.isNode() && asset.env.sourceType !== 'script',
|
|
441
487
|
node_replacer: asset.env.isNode(),
|
|
442
488
|
is_browser: asset.env.isBrowser(),
|
|
443
|
-
is_worker: asset.env.isWorker(),
|
|
489
|
+
is_worker: asset.env.isWorker() || asset.env.isTesseract(),
|
|
444
490
|
env,
|
|
445
491
|
is_type_script: asset.type === 'ts' || asset.type === 'tsx',
|
|
446
492
|
is_jsx: isJSX,
|
|
@@ -453,6 +499,7 @@ export default (new Transformer({
|
|
|
453
499
|
asset.env.isBrowser() &&
|
|
454
500
|
!asset.env.isLibrary &&
|
|
455
501
|
!asset.env.isWorker() &&
|
|
502
|
+
!asset.env.isTesseract() &&
|
|
456
503
|
!asset.env.isWorklet() &&
|
|
457
504
|
Boolean(config?.reactRefresh),
|
|
458
505
|
decorators: Boolean(config?.decorators),
|
|
@@ -470,9 +517,16 @@ export default (new Transformer({
|
|
|
470
517
|
standalone: asset.query.has('standalone'),
|
|
471
518
|
inline_constants: config.inlineConstants,
|
|
472
519
|
conditional_bundling: options.featureFlags.conditionalBundlingApi,
|
|
473
|
-
|
|
520
|
+
hmr_improvements: options.featureFlags.hmrImprovements,
|
|
521
|
+
computed_properties_fix: options.featureFlags.unusedComputedPropertyFix,
|
|
522
|
+
add_display_name: Boolean(config.addReactDisplayName),
|
|
523
|
+
exports_rebinding_optimisation:
|
|
524
|
+
options.featureFlags.exportsRebindingOptimisation,
|
|
525
|
+
magic_comments:
|
|
526
|
+
Boolean(config?.magicComments) ||
|
|
527
|
+
getFeatureFlag('supportWebpackChunkName'),
|
|
474
528
|
callMacro: asset.isSource
|
|
475
|
-
? async (err, src, exportName, args, loc) => {
|
|
529
|
+
? async (err: any, src: any, exportName: any, args: any, loc: any) => {
|
|
476
530
|
let mod;
|
|
477
531
|
try {
|
|
478
532
|
mod = await options.packageManager.require(src, asset.filePath);
|
|
@@ -481,7 +535,6 @@ export default (new Transformer({
|
|
|
481
535
|
if (
|
|
482
536
|
exportName === 'default' &&
|
|
483
537
|
!mod.__esModule &&
|
|
484
|
-
// $FlowFixMe
|
|
485
538
|
Object.prototype.toString.call(config) !== '[object Module]'
|
|
486
539
|
) {
|
|
487
540
|
mod = {default: mod};
|
|
@@ -490,7 +543,7 @@ export default (new Transformer({
|
|
|
490
543
|
if (!Object.hasOwnProperty.call(mod, exportName)) {
|
|
491
544
|
throw new Error(`"${src}" does not export "${exportName}".`);
|
|
492
545
|
}
|
|
493
|
-
} catch (err) {
|
|
546
|
+
} catch (err: any) {
|
|
494
547
|
throw {
|
|
495
548
|
kind: 1,
|
|
496
549
|
message: err.message,
|
|
@@ -507,7 +560,8 @@ export default (new Transformer({
|
|
|
507
560
|
if (asset.env.sourceMap) {
|
|
508
561
|
// Generate a source map that maps each line of the asset to the original macro call.
|
|
509
562
|
map = new SourceMap(options.projectRoot);
|
|
510
|
-
|
|
563
|
+
// @ts-expect-error TS2304
|
|
564
|
+
let mappings: Array<IndexedMapping<string>> = [];
|
|
511
565
|
let line = 1;
|
|
512
566
|
for (let i = 0; i <= a.content.length; i++) {
|
|
513
567
|
if (i === a.content.length || a.content[i] === '\n') {
|
|
@@ -528,6 +582,7 @@ export default (new Transformer({
|
|
|
528
582
|
|
|
529
583
|
map.addIndexedMappings(mappings);
|
|
530
584
|
if (originalMap) {
|
|
585
|
+
// @ts-expect-error TS2345
|
|
531
586
|
map.extends(originalMap);
|
|
532
587
|
} else {
|
|
533
588
|
map.setSourceContent(asset.filePath, code.toString());
|
|
@@ -546,13 +601,13 @@ export default (new Transformer({
|
|
|
546
601
|
specifierType: 'esm',
|
|
547
602
|
});
|
|
548
603
|
},
|
|
549
|
-
invalidateOnFileChange(filePath) {
|
|
604
|
+
invalidateOnFileChange(filePath: FilePath) {
|
|
550
605
|
asset.invalidateOnFileChange(filePath);
|
|
551
606
|
},
|
|
552
|
-
invalidateOnFileCreate(invalidation) {
|
|
607
|
+
invalidateOnFileCreate(invalidation: FileCreateInvalidation) {
|
|
553
608
|
asset.invalidateOnFileCreate(invalidation);
|
|
554
609
|
},
|
|
555
|
-
invalidateOnEnvChange(env) {
|
|
610
|
+
invalidateOnEnvChange(env: string) {
|
|
556
611
|
asset.invalidateOnEnvChange(env);
|
|
557
612
|
},
|
|
558
613
|
invalidateOnStartup() {
|
|
@@ -569,7 +624,7 @@ export default (new Transformer({
|
|
|
569
624
|
`"${exportName}" in "${src}" is not a function.`,
|
|
570
625
|
);
|
|
571
626
|
}
|
|
572
|
-
} catch (err) {
|
|
627
|
+
} catch (err: any) {
|
|
573
628
|
// Remove atlaspack core from stack and build string so Rust can process errors more easily.
|
|
574
629
|
let stack = (err.stack || '').split('\n').slice(1);
|
|
575
630
|
let message = err.message;
|
|
@@ -589,18 +644,21 @@ export default (new Transformer({
|
|
|
589
644
|
});
|
|
590
645
|
|
|
591
646
|
if (getFeatureFlag('conditionalBundlingApi')) {
|
|
592
|
-
asset.meta.conditions = conditions.map(
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
647
|
+
asset.meta.conditions = conditions.map(
|
|
648
|
+
// @ts-expect-error TS7006
|
|
649
|
+
(c): ConditionMeta => ({
|
|
650
|
+
key: c.key,
|
|
651
|
+
ifTruePlaceholder: c.if_true_placeholder,
|
|
652
|
+
ifFalsePlaceholder: c.if_false_placeholder,
|
|
653
|
+
}),
|
|
654
|
+
);
|
|
597
655
|
}
|
|
598
656
|
|
|
599
657
|
if (is_constant_module) {
|
|
600
658
|
asset.meta.isConstantModule = true;
|
|
601
659
|
}
|
|
602
660
|
|
|
603
|
-
let convertLoc = (loc): SourceLocation => {
|
|
661
|
+
let convertLoc = (loc: any): SourceLocation => {
|
|
604
662
|
let location = {
|
|
605
663
|
filePath: asset.filePath,
|
|
606
664
|
start: {
|
|
@@ -623,19 +681,22 @@ export default (new Transformer({
|
|
|
623
681
|
|
|
624
682
|
if (diagnostics) {
|
|
625
683
|
let errors = diagnostics.filter(
|
|
684
|
+
// @ts-expect-error TS7006
|
|
626
685
|
(d) =>
|
|
627
686
|
d.severity === 'Error' ||
|
|
628
687
|
(d.severity === 'SourceError' && asset.isSource),
|
|
629
688
|
);
|
|
630
689
|
let warnings = diagnostics.filter(
|
|
690
|
+
// @ts-expect-error TS7006
|
|
631
691
|
(d) =>
|
|
632
692
|
d.severity === 'Warning' ||
|
|
633
693
|
(d.severity === 'SourceError' && !asset.isSource),
|
|
634
694
|
);
|
|
635
|
-
let convertDiagnostic = (diagnostic) => {
|
|
695
|
+
let convertDiagnostic = (diagnostic: any) => {
|
|
636
696
|
let message = diagnostic.message;
|
|
637
697
|
if (message === 'SCRIPT_ERROR') {
|
|
638
|
-
|
|
698
|
+
// @ts-expect-error TS7053
|
|
699
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
639
700
|
message = err?.message || SCRIPT_ERRORS.browser.message;
|
|
640
701
|
}
|
|
641
702
|
|
|
@@ -644,11 +705,12 @@ export default (new Transformer({
|
|
|
644
705
|
codeFrames: [
|
|
645
706
|
{
|
|
646
707
|
filePath: asset.filePath,
|
|
647
|
-
codeHighlights: diagnostic.code_highlights?.map(
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
708
|
+
codeHighlights: diagnostic.code_highlights?.map(
|
|
709
|
+
(highlight: any) =>
|
|
710
|
+
convertSourceLocationToHighlight(
|
|
711
|
+
convertLoc(highlight.loc),
|
|
712
|
+
highlight.message ?? undefined,
|
|
713
|
+
),
|
|
652
714
|
),
|
|
653
715
|
},
|
|
654
716
|
],
|
|
@@ -672,7 +734,8 @@ export default (new Transformer({
|
|
|
672
734
|
});
|
|
673
735
|
}
|
|
674
736
|
|
|
675
|
-
|
|
737
|
+
// @ts-expect-error TS7053
|
|
738
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
676
739
|
if (err) {
|
|
677
740
|
if (!res.hints) {
|
|
678
741
|
res.hints = [err.hint];
|
|
@@ -729,6 +792,7 @@ export default (new Transformer({
|
|
|
729
792
|
env: {
|
|
730
793
|
context: 'web-worker',
|
|
731
794
|
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
|
|
795
|
+
// @ts-expect-error TS2322
|
|
732
796
|
outputFormat,
|
|
733
797
|
loc,
|
|
734
798
|
},
|
|
@@ -776,6 +840,13 @@ export default (new Transformer({
|
|
|
776
840
|
});
|
|
777
841
|
} else if (dep.kind === 'File') {
|
|
778
842
|
asset.invalidateOnFileChange(dep.specifier);
|
|
843
|
+
} else if (dep.kind === 'Id') {
|
|
844
|
+
// Record parcelRequire calls so that the dev packager can add them as dependencies.
|
|
845
|
+
// This allows the HMR runtime to collect parents across async boundaries (through runtimes).
|
|
846
|
+
// TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
|
|
847
|
+
asset.meta.hmrDeps ??= [];
|
|
848
|
+
invariant(Array.isArray(asset.meta.hmrDeps));
|
|
849
|
+
asset.meta.hmrDeps.push(dep.specifier);
|
|
779
850
|
} else {
|
|
780
851
|
let meta: JSONObject = {kind: dep.kind};
|
|
781
852
|
if (dep.attributes) {
|
|
@@ -839,6 +910,13 @@ export default (new Transformer({
|
|
|
839
910
|
outputFormat,
|
|
840
911
|
loc: convertLoc(dep.loc),
|
|
841
912
|
};
|
|
913
|
+
|
|
914
|
+
if (getFeatureFlag('supportWebpackChunkName')) {
|
|
915
|
+
let chunkName = magic_comments[dep.specifier];
|
|
916
|
+
if (chunkName) {
|
|
917
|
+
meta.chunkName = chunkName;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
842
920
|
}
|
|
843
921
|
|
|
844
922
|
// Always bundle helpers, even with includeNodeModules: false, except if this is a library.
|
|
@@ -863,6 +941,7 @@ export default (new Transformer({
|
|
|
863
941
|
idx = dep.specifier.indexOf('/', idx + 1);
|
|
864
942
|
}
|
|
865
943
|
let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
|
|
944
|
+
// @ts-expect-error TS7053
|
|
866
945
|
range = pkg.dependencies[module];
|
|
867
946
|
}
|
|
868
947
|
|
|
@@ -874,12 +953,13 @@ export default (new Transformer({
|
|
|
874
953
|
dep.kind === 'DynamicImport'
|
|
875
954
|
? 'lazy'
|
|
876
955
|
: dep.kind === 'ConditionalImport'
|
|
877
|
-
|
|
878
|
-
|
|
956
|
+
? 'conditional'
|
|
957
|
+
: 'sync',
|
|
879
958
|
isOptional: dep.is_optional,
|
|
880
959
|
meta,
|
|
881
960
|
resolveFrom: isHelper ? __filename : undefined,
|
|
882
961
|
range,
|
|
962
|
+
// @ts-expect-error TS2322
|
|
883
963
|
env,
|
|
884
964
|
});
|
|
885
965
|
}
|
|
@@ -893,8 +973,12 @@ export default (new Transformer({
|
|
|
893
973
|
local,
|
|
894
974
|
loc,
|
|
895
975
|
is_esm,
|
|
976
|
+
is_static_binding_safe,
|
|
896
977
|
} of hoist_result.exported_symbols) {
|
|
897
|
-
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
978
|
+
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
979
|
+
isEsm: is_esm,
|
|
980
|
+
isStaticBindingSafe: is_static_binding_safe,
|
|
981
|
+
});
|
|
898
982
|
}
|
|
899
983
|
|
|
900
984
|
// deps is a map of dependencies that are keyed by placeholder or specifier
|
|
@@ -991,6 +1075,9 @@ export default (new Transformer({
|
|
|
991
1075
|
Object.keys(hoist_result.exported_symbols).length === 0) ||
|
|
992
1076
|
(hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
|
|
993
1077
|
) {
|
|
1078
|
+
if (is_empty_or_empty_export) {
|
|
1079
|
+
asset.meta.emptyFileStarReexport = true;
|
|
1080
|
+
}
|
|
994
1081
|
asset.symbols.set('*', `$${asset.id}$exports`);
|
|
995
1082
|
}
|
|
996
1083
|
|
|
@@ -1086,6 +1173,7 @@ export default (new Transformer({
|
|
|
1086
1173
|
let sourceMap = new SourceMap(options.projectRoot);
|
|
1087
1174
|
sourceMap.addVLQMap(JSON.parse(map));
|
|
1088
1175
|
if (originalMap) {
|
|
1176
|
+
// @ts-expect-error TS2345
|
|
1089
1177
|
sourceMap.extends(originalMap);
|
|
1090
1178
|
}
|
|
1091
1179
|
asset.setMap(sourceMap);
|
|
@@ -1093,4 +1181,4 @@ export default (new Transformer({
|
|
|
1093
1181
|
|
|
1094
1182
|
return [asset, ...macroAssets];
|
|
1095
1183
|
},
|
|
1096
|
-
})
|
|
1184
|
+
}) as Transformer<unknown>;
|