@atlaspack/transformer-js 3.2.3-canary.21 → 3.2.3-canary.211

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
  },
@@ -160,38 +170,46 @@ var _default = exports.default = new (_plugin().Transformer)({
160
170
  config,
161
171
  options
162
172
  }) {
163
- let pkg = await config.getPackage();
173
+ let packageJson = await config.getPackage();
164
174
  let isJSX, pragma, pragmaFrag, jsxImportSource, automaticJSXRuntime, reactRefresh, decorators, useDefineForClassFields;
165
175
  if (config.isSource) {
166
- var _pkg$dependencies2, _pkg$devDependencies2, _pkg$peerDependencies2, _tsconfig$contents;
176
+ var _packageJson$dependen2, _packageJson$devDepen2, _packageJson$peerDepe2, _await$config$getConf;
167
177
  let reactLib;
168
- if (pkg !== null && pkg !== void 0 && pkg.alias && pkg.alias['react']) {
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 _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]);
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((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;
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) || (reactLib ? JSX_PRAGMA[reactLib].pragma : undefined);
184
- pragmaFrag = (compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.jsxFragmentFactory) || (reactLib ? JSX_PRAGMA[reactLib].pragmaFrag : undefined);
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, _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;
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 = (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]);
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 !== '*' ? (_semver$minVersion = _semver().default.minVersion(reactLibVersion)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.toString() : null;
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 = pkg && pkg.browser && typeof pkg.browser === 'object' && pkg.browser.fs === false;
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
- 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;
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
- conditions
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,17 @@ 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
- magic_comments: Boolean(config === null || config === void 0 ? void 0 : config.magicComments),
429
+ hmr_improvements: options.featureFlags.hmrImprovements,
430
+ add_display_name: Boolean(config.addReactDisplayName),
431
+ exports_rebinding_optimisation: options.featureFlags.exportsRebindingOptimisation,
432
+ magic_comments: Boolean(config === null || config === void 0 ? void 0 : config.magicComments) || (0, _featureFlags().getFeatureFlag)('supportWebpackChunkName'),
377
433
  callMacro: asset.isSource ? async (err, src, exportName, args, loc) => {
378
434
  let mod;
379
435
  try {
380
436
  mod = await options.packageManager.require(src, asset.filePath);
381
437
 
382
438
  // Default interop for CommonJS modules.
383
- if (exportName === 'default' && !mod.__esModule &&
384
- // $FlowFixMe
385
- Object.prototype.toString.call(config) !== '[object Module]') {
439
+ if (exportName === 'default' && !mod.__esModule && Object.prototype.toString.call(config) !== '[object Module]') {
386
440
  mod = {
387
441
  default: mod
388
442
  };
@@ -398,7 +452,7 @@ var _default = exports.default = new (_plugin().Transformer)({
398
452
  }
399
453
  try {
400
454
  if (typeof mod[exportName] === 'function') {
401
- return mod[exportName].apply({
455
+ let ctx = {
402
456
  // Allows macros to emit additional assets to add as dependencies (e.g. css).
403
457
  addAsset(a) {
404
458
  let k = String(macroAssets.length);
@@ -406,6 +460,7 @@ var _default = exports.default = new (_plugin().Transformer)({
406
460
  if (asset.env.sourceMap) {
407
461
  // Generate a source map that maps each line of the asset to the original macro call.
408
462
  map = new (_sourceMap().default)(options.projectRoot);
463
+ // @ts-expect-error TS2304
409
464
  let mappings = [];
410
465
  let line = 1;
411
466
  for (let i = 0; i <= a.content.length; i++) {
@@ -426,6 +481,7 @@ var _default = exports.default = new (_plugin().Transformer)({
426
481
  }
427
482
  map.addIndexedMappings(mappings);
428
483
  if (originalMap) {
484
+ // @ts-expect-error TS2345
429
485
  map.extends(originalMap);
430
486
  } else {
431
487
  map.setSourceContent(asset.filePath, code.toString());
@@ -457,7 +513,8 @@ var _default = exports.default = new (_plugin().Transformer)({
457
513
  invalidateOnBuild() {
458
514
  asset.invalidateOnBuild();
459
515
  }
460
- }, args);
516
+ };
517
+ return mod[exportName].apply(ctx, args);
461
518
  } else {
462
519
  throw new Error(`"${exportName}" in "${src}" is not a function.`);
463
520
  }
@@ -479,7 +536,9 @@ var _default = exports.default = new (_plugin().Transformer)({
479
536
  } : null
480
537
  });
481
538
  if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
482
- asset.meta.conditions = conditions.map(c => ({
539
+ asset.meta.conditions = conditions.map(
540
+ // @ts-expect-error TS7006
541
+ c => ({
483
542
  key: c.key,
484
543
  ifTruePlaceholder: c.if_true_placeholder,
485
544
  ifFalsePlaceholder: c.if_false_placeholder
@@ -503,17 +562,22 @@ var _default = exports.default = new (_plugin().Transformer)({
503
562
 
504
563
  // If there is an original source map, use it to remap to the original source location.
505
564
  if (originalMap) {
506
- location = (0, _utils().remapSourceLocation)(location, originalMap);
565
+ location = (0, _utils().remapSourceLocation)(location, originalMap, options.projectRoot);
507
566
  }
508
567
  return location;
509
568
  };
510
569
  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);
570
+ let errors = diagnostics.filter(
571
+ // @ts-expect-error TS7006
572
+ d => d.severity === 'Error' || d.severity === 'SourceError' && asset.isSource);
573
+ let warnings = diagnostics.filter(
574
+ // @ts-expect-error TS7006
575
+ d => d.severity === 'Warning' || d.severity === 'SourceError' && !asset.isSource);
513
576
  let convertDiagnostic = diagnostic => {
514
577
  var _diagnostic$code_high;
515
578
  let message = diagnostic.message;
516
579
  if (message === 'SCRIPT_ERROR') {
580
+ // @ts-expect-error TS7053
517
581
  let err = SCRIPT_ERRORS[asset.env.context];
518
582
  message = (err === null || err === void 0 ? void 0 : err.message) || SCRIPT_ERRORS.browser.message;
519
583
  }
@@ -536,6 +600,8 @@ var _default = exports.default = new (_plugin().Transformer)({
536
600
  codeHighlights: [(0, _diagnostic().convertSourceLocationToHighlight)(asset.env.loc, 'The environment was originally created here')]
537
601
  });
538
602
  }
603
+
604
+ // @ts-expect-error TS7053
539
605
  let err = SCRIPT_ERRORS[asset.env.context];
540
606
  if (err) {
541
607
  if (!res.hints) {
@@ -580,6 +646,7 @@ var _default = exports.default = new (_plugin().Transformer)({
580
646
  env: {
581
647
  context: 'web-worker',
582
648
  sourceType: dep.source_type === 'Module' ? 'module' : 'script',
649
+ // @ts-expect-error TS2322
583
650
  outputFormat,
584
651
  loc
585
652
  },
@@ -629,6 +696,13 @@ var _default = exports.default = new (_plugin().Transformer)({
629
696
  });
630
697
  } else if (dep.kind === 'File') {
631
698
  asset.invalidateOnFileChange(dep.specifier);
699
+ } else if (dep.kind === 'Id') {
700
+ // Record parcelRequire calls so that the dev packager can add them as dependencies.
701
+ // This allows the HMR runtime to collect parents across async boundaries (through runtimes).
702
+ // TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
703
+ asset.meta.hmrDeps ??= [];
704
+ (0, _assert().default)(Array.isArray(asset.meta.hmrDeps));
705
+ asset.meta.hmrDeps.push(dep.specifier);
632
706
  } else {
633
707
  let meta = {
634
708
  kind: dep.kind
@@ -676,6 +750,12 @@ var _default = exports.default = new (_plugin().Transformer)({
676
750
  outputFormat,
677
751
  loc: convertLoc(dep.loc)
678
752
  };
753
+ if ((0, _featureFlags().getFeatureFlag)('supportWebpackChunkName')) {
754
+ let chunkName = magic_comments[dep.specifier];
755
+ if (chunkName) {
756
+ meta.chunkName = chunkName;
757
+ }
758
+ }
679
759
  }
680
760
 
681
761
  // Always bundle helpers, even with includeNodeModules: false, except if this is a library.
@@ -695,6 +775,7 @@ var _default = exports.default = new (_plugin().Transformer)({
695
775
  idx = dep.specifier.indexOf('/', idx + 1);
696
776
  }
697
777
  let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
778
+ // @ts-expect-error TS7053
698
779
  range = _package.default.dependencies[module];
699
780
  }
700
781
  asset.addDependency({
@@ -706,6 +787,7 @@ var _default = exports.default = new (_plugin().Transformer)({
706
787
  meta,
707
788
  resolveFrom: isHelper ? __filename : undefined,
708
789
  range,
790
+ // @ts-expect-error TS2322
709
791
  env
710
792
  });
711
793
  }
@@ -717,10 +799,12 @@ var _default = exports.default = new (_plugin().Transformer)({
717
799
  exported,
718
800
  local,
719
801
  loc,
720
- is_esm
802
+ is_esm,
803
+ is_static_binding_safe
721
804
  } of hoist_result.exported_symbols) {
722
805
  asset.symbols.set(exported, local, convertLoc(loc), {
723
- isEsm: is_esm
806
+ isEsm: is_esm,
807
+ isStaticBindingSafe: is_static_binding_safe
724
808
  });
725
809
  }
726
810
 
@@ -802,6 +886,9 @@ var _default = exports.default = new (_plugin().Transformer)({
802
886
  // (and the asset has side effects), or the asset is wrapped.
803
887
  // This allows accessing symbols that don't exist without errors in symbol propagation.
804
888
  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('*')) {
889
+ if (is_empty_or_empty_export) {
890
+ asset.meta.emptyFileStarReexport = true;
891
+ }
805
892
  asset.symbols.set('*', `$${asset.id}$exports`);
806
893
  }
807
894
  asset.meta.hasCJSExports = hoist_result.has_cjs_exports;
@@ -884,6 +971,7 @@ var _default = exports.default = new (_plugin().Transformer)({
884
971
  let sourceMap = new (_sourceMap().default)(options.projectRoot);
885
972
  sourceMap.addVLQMap(JSON.parse(map));
886
973
  if (originalMap) {
974
+ // @ts-expect-error TS2345
887
975
  sourceMap.extends(originalMap);
888
976
  }
889
977
  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.21+3a3e8e7be",
3
+ "version": "3.2.3-canary.211+236e54658",
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.89+3a3e8e7be",
26
- "@atlaspack/feature-flags": "2.14.1-canary.89+3a3e8e7be",
27
- "@atlaspack/plugin": "2.14.5-canary.21+3a3e8e7be",
28
- "@atlaspack/rust": "3.2.1-canary.21+3a3e8e7be",
29
- "@atlaspack/utils": "2.14.5-canary.21+3a3e8e7be",
30
- "@atlaspack/workers": "2.14.5-canary.21+3a3e8e7be",
27
+ "@atlaspack/diagnostic": "2.14.1-canary.279+236e54658",
28
+ "@atlaspack/feature-flags": "2.14.1-canary.279+236e54658",
29
+ "@atlaspack/plugin": "2.14.5-canary.211+236e54658",
30
+ "@atlaspack/rust": "3.2.1-canary.211+236e54658",
31
+ "@atlaspack/utils": "2.14.5-canary.211+236e54658",
32
+ "@atlaspack/workers": "2.14.5-canary.211+236e54658",
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.24.2"
40
42
  },
41
43
  "type": "commonjs",
42
- "gitHead": "3a3e8e7be9e2dffd7304436d792f0f595d59665a"
43
- }
44
+ "gitHead": "236e5465863dca6044a7191e05260a5b924c342e"
45
+ }
@@ -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, // 'es3' | 'es5' | 'es6' | 'es2015' | ... |'es2022' | ... | 'esnext'
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 (new Transformer({
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 pkg = await config.getPackage();
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 (pkg?.alias && pkg.alias['react']) {
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
- pkg?.dependencies?.[libName] ||
195
- pkg?.devDependencies?.[libName] ||
196
- pkg?.peerDependencies?.[libName],
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
- pkg?.dependencies?.react ||
205
- pkg?.devDependencies?.react ||
206
- pkg?.peerDependencies?.react,
205
+ packageJson?.dependencies?.react ||
206
+ packageJson?.devDependencies?.react ||
207
+ packageJson?.peerDependencies?.react,
207
208
  );
208
209
 
209
- let tsconfig = await config.getConfigFrom<TSConfig>(
210
- options.projectRoot + '/index',
211
- ['tsconfig.json', 'jsconfig.json'],
212
- );
213
- let compilerOptions = tsconfig?.contents?.compilerOptions;
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
- pkg?.alias && pkg.alias['react'] === 'preact/compat'
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
- pkg?.dependencies?.[effectiveReactLib] ||
238
- pkg?.devDependencies?.[effectiveReactLib] ||
239
- pkg?.peerDependencies?.[effectiveReactLib];
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
- ? semver.minVersion(reactLibVersion)?.toString()
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
- pkg &&
282
- pkg.browser &&
283
- typeof pkg.browser === 'object' &&
284
- pkg.browser.fs === false;
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,15 @@ 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
- magic_comments: Boolean(config?.magicComments),
520
+ hmr_improvements: options.featureFlags.hmrImprovements,
521
+ add_display_name: Boolean(config.addReactDisplayName),
522
+ exports_rebinding_optimisation:
523
+ options.featureFlags.exportsRebindingOptimisation,
524
+ magic_comments:
525
+ Boolean(config?.magicComments) ||
526
+ getFeatureFlag('supportWebpackChunkName'),
474
527
  callMacro: asset.isSource
475
- ? async (err, src, exportName, args, loc) => {
528
+ ? async (err: any, src: any, exportName: any, args: any, loc: any) => {
476
529
  let mod;
477
530
  try {
478
531
  mod = await options.packageManager.require(src, asset.filePath);
@@ -481,7 +534,6 @@ export default (new Transformer({
481
534
  if (
482
535
  exportName === 'default' &&
483
536
  !mod.__esModule &&
484
- // $FlowFixMe
485
537
  Object.prototype.toString.call(config) !== '[object Module]'
486
538
  ) {
487
539
  mod = {default: mod};
@@ -490,7 +542,7 @@ export default (new Transformer({
490
542
  if (!Object.hasOwnProperty.call(mod, exportName)) {
491
543
  throw new Error(`"${src}" does not export "${exportName}".`);
492
544
  }
493
- } catch (err) {
545
+ } catch (err: any) {
494
546
  throw {
495
547
  kind: 1,
496
548
  message: err.message,
@@ -507,7 +559,8 @@ export default (new Transformer({
507
559
  if (asset.env.sourceMap) {
508
560
  // Generate a source map that maps each line of the asset to the original macro call.
509
561
  map = new SourceMap(options.projectRoot);
510
- let mappings = [];
562
+ // @ts-expect-error TS2304
563
+ let mappings: Array<IndexedMapping<string>> = [];
511
564
  let line = 1;
512
565
  for (let i = 0; i <= a.content.length; i++) {
513
566
  if (i === a.content.length || a.content[i] === '\n') {
@@ -528,6 +581,7 @@ export default (new Transformer({
528
581
 
529
582
  map.addIndexedMappings(mappings);
530
583
  if (originalMap) {
584
+ // @ts-expect-error TS2345
531
585
  map.extends(originalMap);
532
586
  } else {
533
587
  map.setSourceContent(asset.filePath, code.toString());
@@ -546,13 +600,13 @@ export default (new Transformer({
546
600
  specifierType: 'esm',
547
601
  });
548
602
  },
549
- invalidateOnFileChange(filePath) {
603
+ invalidateOnFileChange(filePath: FilePath) {
550
604
  asset.invalidateOnFileChange(filePath);
551
605
  },
552
- invalidateOnFileCreate(invalidation) {
606
+ invalidateOnFileCreate(invalidation: FileCreateInvalidation) {
553
607
  asset.invalidateOnFileCreate(invalidation);
554
608
  },
555
- invalidateOnEnvChange(env) {
609
+ invalidateOnEnvChange(env: string) {
556
610
  asset.invalidateOnEnvChange(env);
557
611
  },
558
612
  invalidateOnStartup() {
@@ -569,7 +623,7 @@ export default (new Transformer({
569
623
  `"${exportName}" in "${src}" is not a function.`,
570
624
  );
571
625
  }
572
- } catch (err) {
626
+ } catch (err: any) {
573
627
  // Remove atlaspack core from stack and build string so Rust can process errors more easily.
574
628
  let stack = (err.stack || '').split('\n').slice(1);
575
629
  let message = err.message;
@@ -589,18 +643,21 @@ export default (new Transformer({
589
643
  });
590
644
 
591
645
  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
- }));
646
+ asset.meta.conditions = conditions.map(
647
+ // @ts-expect-error TS7006
648
+ (c): ConditionMeta => ({
649
+ key: c.key,
650
+ ifTruePlaceholder: c.if_true_placeholder,
651
+ ifFalsePlaceholder: c.if_false_placeholder,
652
+ }),
653
+ );
597
654
  }
598
655
 
599
656
  if (is_constant_module) {
600
657
  asset.meta.isConstantModule = true;
601
658
  }
602
659
 
603
- let convertLoc = (loc): SourceLocation => {
660
+ let convertLoc = (loc: any): SourceLocation => {
604
661
  let location = {
605
662
  filePath: asset.filePath,
606
663
  start: {
@@ -615,7 +672,11 @@ export default (new Transformer({
615
672
 
616
673
  // If there is an original source map, use it to remap to the original source location.
617
674
  if (originalMap) {
618
- location = remapSourceLocation(location, originalMap);
675
+ location = remapSourceLocation(
676
+ location,
677
+ originalMap,
678
+ options.projectRoot,
679
+ );
619
680
  }
620
681
 
621
682
  return location;
@@ -623,19 +684,22 @@ export default (new Transformer({
623
684
 
624
685
  if (diagnostics) {
625
686
  let errors = diagnostics.filter(
687
+ // @ts-expect-error TS7006
626
688
  (d) =>
627
689
  d.severity === 'Error' ||
628
690
  (d.severity === 'SourceError' && asset.isSource),
629
691
  );
630
692
  let warnings = diagnostics.filter(
693
+ // @ts-expect-error TS7006
631
694
  (d) =>
632
695
  d.severity === 'Warning' ||
633
696
  (d.severity === 'SourceError' && !asset.isSource),
634
697
  );
635
- let convertDiagnostic = (diagnostic) => {
698
+ let convertDiagnostic = (diagnostic: any) => {
636
699
  let message = diagnostic.message;
637
700
  if (message === 'SCRIPT_ERROR') {
638
- let err = SCRIPT_ERRORS[(asset.env.context: string)];
701
+ // @ts-expect-error TS7053
702
+ let err = SCRIPT_ERRORS[asset.env.context as string];
639
703
  message = err?.message || SCRIPT_ERRORS.browser.message;
640
704
  }
641
705
 
@@ -644,11 +708,12 @@ export default (new Transformer({
644
708
  codeFrames: [
645
709
  {
646
710
  filePath: asset.filePath,
647
- codeHighlights: diagnostic.code_highlights?.map((highlight) =>
648
- convertSourceLocationToHighlight(
649
- convertLoc(highlight.loc),
650
- highlight.message ?? undefined,
651
- ),
711
+ codeHighlights: diagnostic.code_highlights?.map(
712
+ (highlight: any) =>
713
+ convertSourceLocationToHighlight(
714
+ convertLoc(highlight.loc),
715
+ highlight.message ?? undefined,
716
+ ),
652
717
  ),
653
718
  },
654
719
  ],
@@ -672,7 +737,8 @@ export default (new Transformer({
672
737
  });
673
738
  }
674
739
 
675
- let err = SCRIPT_ERRORS[(asset.env.context: string)];
740
+ // @ts-expect-error TS7053
741
+ let err = SCRIPT_ERRORS[asset.env.context as string];
676
742
  if (err) {
677
743
  if (!res.hints) {
678
744
  res.hints = [err.hint];
@@ -729,6 +795,7 @@ export default (new Transformer({
729
795
  env: {
730
796
  context: 'web-worker',
731
797
  sourceType: dep.source_type === 'Module' ? 'module' : 'script',
798
+ // @ts-expect-error TS2322
732
799
  outputFormat,
733
800
  loc,
734
801
  },
@@ -776,6 +843,13 @@ export default (new Transformer({
776
843
  });
777
844
  } else if (dep.kind === 'File') {
778
845
  asset.invalidateOnFileChange(dep.specifier);
846
+ } else if (dep.kind === 'Id') {
847
+ // Record parcelRequire calls so that the dev packager can add them as dependencies.
848
+ // This allows the HMR runtime to collect parents across async boundaries (through runtimes).
849
+ // TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
850
+ asset.meta.hmrDeps ??= [];
851
+ invariant(Array.isArray(asset.meta.hmrDeps));
852
+ asset.meta.hmrDeps.push(dep.specifier);
779
853
  } else {
780
854
  let meta: JSONObject = {kind: dep.kind};
781
855
  if (dep.attributes) {
@@ -839,6 +913,13 @@ export default (new Transformer({
839
913
  outputFormat,
840
914
  loc: convertLoc(dep.loc),
841
915
  };
916
+
917
+ if (getFeatureFlag('supportWebpackChunkName')) {
918
+ let chunkName = magic_comments[dep.specifier];
919
+ if (chunkName) {
920
+ meta.chunkName = chunkName;
921
+ }
922
+ }
842
923
  }
843
924
 
844
925
  // Always bundle helpers, even with includeNodeModules: false, except if this is a library.
@@ -863,6 +944,7 @@ export default (new Transformer({
863
944
  idx = dep.specifier.indexOf('/', idx + 1);
864
945
  }
865
946
  let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
947
+ // @ts-expect-error TS7053
866
948
  range = pkg.dependencies[module];
867
949
  }
868
950
 
@@ -874,12 +956,13 @@ export default (new Transformer({
874
956
  dep.kind === 'DynamicImport'
875
957
  ? 'lazy'
876
958
  : dep.kind === 'ConditionalImport'
877
- ? 'conditional'
878
- : 'sync',
959
+ ? 'conditional'
960
+ : 'sync',
879
961
  isOptional: dep.is_optional,
880
962
  meta,
881
963
  resolveFrom: isHelper ? __filename : undefined,
882
964
  range,
965
+ // @ts-expect-error TS2322
883
966
  env,
884
967
  });
885
968
  }
@@ -893,8 +976,12 @@ export default (new Transformer({
893
976
  local,
894
977
  loc,
895
978
  is_esm,
979
+ is_static_binding_safe,
896
980
  } of hoist_result.exported_symbols) {
897
- asset.symbols.set(exported, local, convertLoc(loc), {isEsm: is_esm});
981
+ asset.symbols.set(exported, local, convertLoc(loc), {
982
+ isEsm: is_esm,
983
+ isStaticBindingSafe: is_static_binding_safe,
984
+ });
898
985
  }
899
986
 
900
987
  // deps is a map of dependencies that are keyed by placeholder or specifier
@@ -991,6 +1078,9 @@ export default (new Transformer({
991
1078
  Object.keys(hoist_result.exported_symbols).length === 0) ||
992
1079
  (hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
993
1080
  ) {
1081
+ if (is_empty_or_empty_export) {
1082
+ asset.meta.emptyFileStarReexport = true;
1083
+ }
994
1084
  asset.symbols.set('*', `$${asset.id}$exports`);
995
1085
  }
996
1086
 
@@ -1086,6 +1176,7 @@ export default (new Transformer({
1086
1176
  let sourceMap = new SourceMap(options.projectRoot);
1087
1177
  sourceMap.addVLQMap(JSON.parse(map));
1088
1178
  if (originalMap) {
1179
+ // @ts-expect-error TS2345
1089
1180
  sourceMap.extends(originalMap);
1090
1181
  }
1091
1182
  asset.setMap(sourceMap);
@@ -1093,4 +1184,4 @@ export default (new Transformer({
1093
1184
 
1094
1185
  return [asset, ...macroAssets];
1095
1186
  },
1096
- }): Transformer);
1187
+ }) as Transformer<unknown>;