@atlaspack/transformer-js 3.2.3-canary.27 → 3.2.3-canary.271

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