@atlaspack/transformer-js 3.2.3-canary.17 → 3.2.3-canary.170

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
+ computed_properties_fix: options.featureFlags.unusedComputedPropertyFix,
431
+ add_display_name: Boolean(config.addReactDisplayName),
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
@@ -508,12 +567,17 @@ var _default = exports.default = new (_plugin().Transformer)({
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
  }
@@ -752,11 +834,14 @@ var _default = exports.default = new (_plugin().Transformer)({
752
834
  let dep = deps.get(source);
753
835
  if (!dep) continue;
754
836
  if (local === '*' && imported === '*') {
837
+ // @ts-expect-error TS2345
755
838
  dep.symbols.set('*', '*', convertLoc(loc), true);
756
839
  } else {
757
840
  var _dep$symbols$get;
758
841
  let reExportName = ((_dep$symbols$get = dep.symbols.get(imported)) === null || _dep$symbols$get === void 0 ? void 0 : _dep$symbols$get.local) ?? `$${asset.id}$re_export$${local}`;
842
+ // @ts-expect-error TS2345
759
843
  asset.symbols.set(local, reExportName);
844
+ // @ts-expect-error TS2345
760
845
  dep.symbols.set(imported, reExportName, convertLoc(loc), true);
761
846
  }
762
847
  }
@@ -774,7 +859,9 @@ var _default = exports.default = new (_plugin().Transformer)({
774
859
  let symbols = new Map();
775
860
  for (let name of hoist_result.self_references) {
776
861
  // Do not create a self-reference for the `default` symbol unless we have seen an __esModule flag.
777
- if (name === 'default' && !asset.symbols.hasExportSymbol('__esModule')) {
862
+ if (name === 'default' &&
863
+ // @ts-expect-error TS2345
864
+ !asset.symbols.hasExportSymbol('__esModule')) {
778
865
  continue;
779
866
  }
780
867
  let local = (0, _nullthrows().default)(asset.symbols.get(name)).local;
@@ -801,7 +888,13 @@ var _default = exports.default = new (_plugin().Transformer)({
801
888
  // Add * symbol if there are CJS exports, no imports/exports at all
802
889
  // (and the asset has side effects), or the asset is wrapped.
803
890
  // This allows accessing symbols that don't exist without errors in symbol propagation.
804
- 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('*')) {
891
+ if (hoist_result.has_cjs_exports || !hoist_result.is_esm && asset.sideEffects && deps.size === 0 && Object.keys(hoist_result.exported_symbols).length === 0 ||
892
+ // @ts-expect-error TS2345
893
+ hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*')) {
894
+ if (is_empty_or_empty_export) {
895
+ asset.meta.emptyFileStarReexport = true;
896
+ }
897
+ // @ts-expect-error TS2345
805
898
  asset.symbols.set('*', `$${asset.id}$exports`);
806
899
  }
807
900
  asset.meta.hasCJSExports = hoist_result.has_cjs_exports;
@@ -818,10 +911,14 @@ var _default = exports.default = new (_plugin().Transformer)({
818
911
  source
819
912
  } of symbol_result.exports) {
820
913
  let dep = source ? deps.get(source) : undefined;
821
- asset.symbols.set(exported, `${(dep === null || dep === void 0 ? void 0 : dep.id) ?? ''}$${local}`, convertLoc(loc));
914
+ asset.symbols.set(exported,
915
+ // @ts-expect-error TS2345
916
+ `${(dep === null || dep === void 0 ? void 0 : dep.id) ?? ''}$${local}`, convertLoc(loc));
822
917
  if (dep != null) {
823
918
  dep.symbols.ensure();
824
- dep.symbols.set(local, `${(dep === null || dep === void 0 ? void 0 : dep.id) ?? ''}$${local}`, convertLoc(loc), true);
919
+ dep.symbols.set(local,
920
+ // @ts-expect-error TS2345
921
+ `${(dep === null || dep === void 0 ? void 0 : dep.id) ?? ''}$${local}`, convertLoc(loc), true);
825
922
  }
826
923
  }
827
924
  for (let {
@@ -842,18 +939,23 @@ var _default = exports.default = new (_plugin().Transformer)({
842
939
  let dep = deps.get(source);
843
940
  if (!dep) continue;
844
941
  dep.symbols.ensure();
942
+ // @ts-expect-error TS2345
845
943
  dep.symbols.set('*', '*', convertLoc(loc), true);
846
944
  }
847
945
 
848
946
  // Add * symbol if there are CJS exports, no imports/exports at all, or the asset is wrapped.
849
947
  // This allows accessing symbols that don't exist without errors in symbol propagation.
850
- if (symbol_result.has_cjs_exports || !symbol_result.is_esm && deps.size === 0 && symbol_result.exports.length === 0 || symbol_result.should_wrap && !asset.symbols.hasExportSymbol('*')) {
948
+ if (symbol_result.has_cjs_exports || !symbol_result.is_esm && deps.size === 0 && symbol_result.exports.length === 0 ||
949
+ // @ts-expect-error TS2345
950
+ symbol_result.should_wrap && !asset.symbols.hasExportSymbol('*')) {
851
951
  asset.symbols.ensure();
952
+ // @ts-expect-error TS2345
852
953
  asset.symbols.set('*', `$${asset.id}$exports`);
853
954
  }
854
955
  } else {
855
956
  // If the asset is wrapped, add * as a fallback
856
957
  asset.symbols.ensure();
958
+ // @ts-expect-error TS2345
857
959
  asset.symbols.set('*', `$${asset.id}$exports`);
858
960
  }
859
961
 
@@ -862,6 +964,7 @@ var _default = exports.default = new (_plugin().Transformer)({
862
964
  for (let dep of asset.getDependencies()) {
863
965
  if (dep.symbols.isCleared) {
864
966
  dep.symbols.ensure();
967
+ // @ts-expect-error TS2345
865
968
  dep.symbols.set('*', `${dep.id}$`);
866
969
  }
867
970
  }
@@ -884,6 +987,7 @@ var _default = exports.default = new (_plugin().Transformer)({
884
987
  let sourceMap = new (_sourceMap().default)(options.projectRoot);
885
988
  sourceMap.addVLQMap(JSON.parse(map));
886
989
  if (originalMap) {
990
+ // @ts-expect-error TS2345
887
991
  sourceMap.extends(originalMap);
888
992
  }
889
993
  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.17+a1773d2a6",
3
+ "version": "3.2.3-canary.170+c940954d2",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -9,10 +9,13 @@
9
9
  "type": "git",
10
10
  "url": "https://github.com/atlassian-labs/atlaspack.git"
11
11
  },
12
- "main": "lib/JSTransformer.js",
13
- "source": "src/JSTransformer.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
+ "check-ts": "tsc --emitDeclarationOnly --rootDir src",
18
+ "build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
16
19
  },
17
20
  "engines": {
18
21
  "node": ">= 16.0.0"
@@ -22,12 +25,12 @@
22
25
  "src"
23
26
  ],
24
27
  "dependencies": {
25
- "@atlaspack/diagnostic": "2.14.1-canary.85+a1773d2a6",
26
- "@atlaspack/feature-flags": "2.14.1-canary.85+a1773d2a6",
27
- "@atlaspack/plugin": "2.14.5-canary.17+a1773d2a6",
28
- "@atlaspack/rust": "3.2.1-canary.17+a1773d2a6",
29
- "@atlaspack/utils": "2.14.5-canary.17+a1773d2a6",
30
- "@atlaspack/workers": "2.14.5-canary.17+a1773d2a6",
28
+ "@atlaspack/diagnostic": "2.14.1-canary.238+c940954d2",
29
+ "@atlaspack/feature-flags": "2.14.1-canary.238+c940954d2",
30
+ "@atlaspack/plugin": "2.14.5-canary.170+c940954d2",
31
+ "@atlaspack/rust": "3.2.1-canary.170+c940954d2",
32
+ "@atlaspack/utils": "2.14.5-canary.170+c940954d2",
33
+ "@atlaspack/workers": "2.14.5-canary.170+c940954d2",
31
34
  "@parcel/source-map": "^2.1.1",
32
35
  "@swc/helpers": "^0.5.15",
33
36
  "browserslist": "^4.6.6",
@@ -36,8 +39,8 @@
36
39
  "semver": "^7.5.2"
37
40
  },
38
41
  "peerDependencies": {
39
- "@atlaspack/core": "^2.13.1"
42
+ "@atlaspack/core": "2.23.3"
40
43
  },
41
44
  "type": "commonjs",
42
- "gitHead": "a1773d2a62d0ef7805ac7524621dcabcc1afe929"
45
+ "gitHead": "c940954d27d657c3c93cd328dfb394778da46eab"
43
46
  }
@@ -1,4 +1,3 @@
1
- // @flow
2
1
  import type {
3
2
  JSONObject,
4
3
  EnvMap,
@@ -12,6 +11,7 @@ import type {Diagnostic} from '@atlaspack/diagnostic';
12
11
  import SourceMap from '@parcel/source-map';
13
12
  import {Transformer} from '@atlaspack/plugin';
14
13
  import {transform, transformAsync} from '@atlaspack/rust';
14
+ import invariant from 'assert';
15
15
  import browserslist from 'browserslist';
16
16
  import semver from 'semver';
17
17
  import nullthrows from 'nullthrows';
@@ -26,7 +26,7 @@ import {getFeatureFlag} from '@atlaspack/feature-flags';
26
26
  const JSX_EXTENSIONS = {
27
27
  jsx: true,
28
28
  tsx: true,
29
- };
29
+ } as const;
30
30
 
31
31
  const JSX_PRAGMA = {
32
32
  react: {
@@ -49,7 +49,7 @@ const JSX_PRAGMA = {
49
49
  pragmaFrag: undefined,
50
50
  automatic: undefined,
51
51
  },
52
- };
52
+ } as const;
53
53
 
54
54
  const BROWSER_MAPPING = {
55
55
  and_chr: 'chrome',
@@ -63,7 +63,7 @@ const BROWSER_MAPPING = {
63
63
  bb: null,
64
64
  kaios: null,
65
65
  op_mini: null,
66
- };
66
+ } as const;
67
67
 
68
68
  // List of browsers to exclude when the esmodule target is specified.
69
69
  // Based on https://caniuse.com/#feat=es6-module
@@ -108,6 +108,9 @@ const CONFIG_SCHEMA: SchemaEntity = {
108
108
  },
109
109
  ],
110
110
  },
111
+ addReactDisplayName: {
112
+ type: 'boolean',
113
+ },
111
114
  magicComments: {
112
115
  type: 'boolean',
113
116
  },
@@ -133,47 +136,45 @@ const SCRIPT_ERRORS = {
133
136
  'Service workers cannot have imports or exports without the `type: "module"` option.',
134
137
  hint: "Add {type: 'module'} as a second argument to the navigator.serviceWorker.register() call.",
135
138
  },
136
- };
139
+ } as const;
137
140
 
138
141
  type TSConfig = {
139
142
  compilerOptions?: {
140
143
  // https://www.typescriptlang.org/tsconfig#jsx
141
- jsx?: 'react' | 'react-jsx' | 'react-jsxdev' | 'preserve' | 'react-native',
144
+ jsx?: 'react' | 'react-jsx' | 'react-jsxdev' | 'preserve' | 'react-native';
142
145
  // https://www.typescriptlang.org/tsconfig#jsxFactory
143
- jsxFactory?: string,
146
+ jsxFactory?: string;
144
147
  // https://www.typescriptlang.org/tsconfig#jsxFragmentFactory
145
- jsxFragmentFactory?: string,
148
+ jsxFragmentFactory?: string;
146
149
  // https://www.typescriptlang.org/tsconfig#jsxImportSource
147
- jsxImportSource?: string,
150
+ jsxImportSource?: string;
148
151
  // https://www.typescriptlang.org/tsconfig#experimentalDecorators
149
- experimentalDecorators?: boolean,
152
+ experimentalDecorators?: boolean;
150
153
  // https://www.typescriptlang.org/tsconfig#useDefineForClassFields
151
- useDefineForClassFields?: boolean,
154
+ useDefineForClassFields?: boolean;
152
155
  // https://www.typescriptlang.org/tsconfig#target
153
- target?: string, // '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,14 @@ 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
+ computed_properties_fix: options.featureFlags.unusedComputedPropertyFix,
522
+ add_display_name: Boolean(config.addReactDisplayName),
523
+ magic_comments:
524
+ Boolean(config?.magicComments) ||
525
+ getFeatureFlag('supportWebpackChunkName'),
474
526
  callMacro: asset.isSource
475
- ? async (err, src, exportName, args, loc) => {
527
+ ? async (err: any, src: any, exportName: any, args: any, loc: any) => {
476
528
  let mod;
477
529
  try {
478
530
  mod = await options.packageManager.require(src, asset.filePath);
@@ -481,7 +533,6 @@ export default (new Transformer({
481
533
  if (
482
534
  exportName === 'default' &&
483
535
  !mod.__esModule &&
484
- // $FlowFixMe
485
536
  Object.prototype.toString.call(config) !== '[object Module]'
486
537
  ) {
487
538
  mod = {default: mod};
@@ -490,7 +541,7 @@ export default (new Transformer({
490
541
  if (!Object.hasOwnProperty.call(mod, exportName)) {
491
542
  throw new Error(`"${src}" does not export "${exportName}".`);
492
543
  }
493
- } catch (err) {
544
+ } catch (err: any) {
494
545
  throw {
495
546
  kind: 1,
496
547
  message: err.message,
@@ -507,7 +558,8 @@ export default (new Transformer({
507
558
  if (asset.env.sourceMap) {
508
559
  // Generate a source map that maps each line of the asset to the original macro call.
509
560
  map = new SourceMap(options.projectRoot);
510
- let mappings = [];
561
+ // @ts-expect-error TS2304
562
+ let mappings: Array<IndexedMapping<string>> = [];
511
563
  let line = 1;
512
564
  for (let i = 0; i <= a.content.length; i++) {
513
565
  if (i === a.content.length || a.content[i] === '\n') {
@@ -528,6 +580,7 @@ export default (new Transformer({
528
580
 
529
581
  map.addIndexedMappings(mappings);
530
582
  if (originalMap) {
583
+ // @ts-expect-error TS2345
531
584
  map.extends(originalMap);
532
585
  } else {
533
586
  map.setSourceContent(asset.filePath, code.toString());
@@ -546,13 +599,13 @@ export default (new Transformer({
546
599
  specifierType: 'esm',
547
600
  });
548
601
  },
549
- invalidateOnFileChange(filePath) {
602
+ invalidateOnFileChange(filePath: FilePath) {
550
603
  asset.invalidateOnFileChange(filePath);
551
604
  },
552
- invalidateOnFileCreate(invalidation) {
605
+ invalidateOnFileCreate(invalidation: FileCreateInvalidation) {
553
606
  asset.invalidateOnFileCreate(invalidation);
554
607
  },
555
- invalidateOnEnvChange(env) {
608
+ invalidateOnEnvChange(env: string) {
556
609
  asset.invalidateOnEnvChange(env);
557
610
  },
558
611
  invalidateOnStartup() {
@@ -569,7 +622,7 @@ export default (new Transformer({
569
622
  `"${exportName}" in "${src}" is not a function.`,
570
623
  );
571
624
  }
572
- } catch (err) {
625
+ } catch (err: any) {
573
626
  // Remove atlaspack core from stack and build string so Rust can process errors more easily.
574
627
  let stack = (err.stack || '').split('\n').slice(1);
575
628
  let message = err.message;
@@ -589,18 +642,21 @@ export default (new Transformer({
589
642
  });
590
643
 
591
644
  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
- }));
645
+ asset.meta.conditions = conditions.map(
646
+ // @ts-expect-error TS7006
647
+ (c): ConditionMeta => ({
648
+ key: c.key,
649
+ ifTruePlaceholder: c.if_true_placeholder,
650
+ ifFalsePlaceholder: c.if_false_placeholder,
651
+ }),
652
+ );
597
653
  }
598
654
 
599
655
  if (is_constant_module) {
600
656
  asset.meta.isConstantModule = true;
601
657
  }
602
658
 
603
- let convertLoc = (loc): SourceLocation => {
659
+ let convertLoc = (loc: any): SourceLocation => {
604
660
  let location = {
605
661
  filePath: asset.filePath,
606
662
  start: {
@@ -623,19 +679,22 @@ export default (new Transformer({
623
679
 
624
680
  if (diagnostics) {
625
681
  let errors = diagnostics.filter(
682
+ // @ts-expect-error TS7006
626
683
  (d) =>
627
684
  d.severity === 'Error' ||
628
685
  (d.severity === 'SourceError' && asset.isSource),
629
686
  );
630
687
  let warnings = diagnostics.filter(
688
+ // @ts-expect-error TS7006
631
689
  (d) =>
632
690
  d.severity === 'Warning' ||
633
691
  (d.severity === 'SourceError' && !asset.isSource),
634
692
  );
635
- let convertDiagnostic = (diagnostic) => {
693
+ let convertDiagnostic = (diagnostic: any) => {
636
694
  let message = diagnostic.message;
637
695
  if (message === 'SCRIPT_ERROR') {
638
- let err = SCRIPT_ERRORS[(asset.env.context: string)];
696
+ // @ts-expect-error TS7053
697
+ let err = SCRIPT_ERRORS[asset.env.context as string];
639
698
  message = err?.message || SCRIPT_ERRORS.browser.message;
640
699
  }
641
700
 
@@ -644,11 +703,12 @@ export default (new Transformer({
644
703
  codeFrames: [
645
704
  {
646
705
  filePath: asset.filePath,
647
- codeHighlights: diagnostic.code_highlights?.map((highlight) =>
648
- convertSourceLocationToHighlight(
649
- convertLoc(highlight.loc),
650
- highlight.message ?? undefined,
651
- ),
706
+ codeHighlights: diagnostic.code_highlights?.map(
707
+ (highlight: any) =>
708
+ convertSourceLocationToHighlight(
709
+ convertLoc(highlight.loc),
710
+ highlight.message ?? undefined,
711
+ ),
652
712
  ),
653
713
  },
654
714
  ],
@@ -672,7 +732,8 @@ export default (new Transformer({
672
732
  });
673
733
  }
674
734
 
675
- let err = SCRIPT_ERRORS[(asset.env.context: string)];
735
+ // @ts-expect-error TS7053
736
+ let err = SCRIPT_ERRORS[asset.env.context as string];
676
737
  if (err) {
677
738
  if (!res.hints) {
678
739
  res.hints = [err.hint];
@@ -729,6 +790,7 @@ export default (new Transformer({
729
790
  env: {
730
791
  context: 'web-worker',
731
792
  sourceType: dep.source_type === 'Module' ? 'module' : 'script',
793
+ // @ts-expect-error TS2322
732
794
  outputFormat,
733
795
  loc,
734
796
  },
@@ -776,6 +838,13 @@ export default (new Transformer({
776
838
  });
777
839
  } else if (dep.kind === 'File') {
778
840
  asset.invalidateOnFileChange(dep.specifier);
841
+ } else if (dep.kind === 'Id') {
842
+ // Record parcelRequire calls so that the dev packager can add them as dependencies.
843
+ // This allows the HMR runtime to collect parents across async boundaries (through runtimes).
844
+ // TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
845
+ asset.meta.hmrDeps ??= [];
846
+ invariant(Array.isArray(asset.meta.hmrDeps));
847
+ asset.meta.hmrDeps.push(dep.specifier);
779
848
  } else {
780
849
  let meta: JSONObject = {kind: dep.kind};
781
850
  if (dep.attributes) {
@@ -839,6 +908,13 @@ export default (new Transformer({
839
908
  outputFormat,
840
909
  loc: convertLoc(dep.loc),
841
910
  };
911
+
912
+ if (getFeatureFlag('supportWebpackChunkName')) {
913
+ let chunkName = magic_comments[dep.specifier];
914
+ if (chunkName) {
915
+ meta.chunkName = chunkName;
916
+ }
917
+ }
842
918
  }
843
919
 
844
920
  // Always bundle helpers, even with includeNodeModules: false, except if this is a library.
@@ -863,6 +939,7 @@ export default (new Transformer({
863
939
  idx = dep.specifier.indexOf('/', idx + 1);
864
940
  }
865
941
  let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
942
+ // @ts-expect-error TS7053
866
943
  range = pkg.dependencies[module];
867
944
  }
868
945
 
@@ -874,12 +951,13 @@ export default (new Transformer({
874
951
  dep.kind === 'DynamicImport'
875
952
  ? 'lazy'
876
953
  : dep.kind === 'ConditionalImport'
877
- ? 'conditional'
878
- : 'sync',
954
+ ? 'conditional'
955
+ : 'sync',
879
956
  isOptional: dep.is_optional,
880
957
  meta,
881
958
  resolveFrom: isHelper ? __filename : undefined,
882
959
  range,
960
+ // @ts-expect-error TS2322
883
961
  env,
884
962
  });
885
963
  }
@@ -926,12 +1004,15 @@ export default (new Transformer({
926
1004
  let dep = deps.get(source);
927
1005
  if (!dep) continue;
928
1006
  if (local === '*' && imported === '*') {
1007
+ // @ts-expect-error TS2345
929
1008
  dep.symbols.set('*', '*', convertLoc(loc), true);
930
1009
  } else {
931
1010
  let reExportName =
932
1011
  dep.symbols.get(imported)?.local ??
933
1012
  `$${asset.id}$re_export$${local}`;
1013
+ // @ts-expect-error TS2345
934
1014
  asset.symbols.set(local, reExportName);
1015
+ // @ts-expect-error TS2345
935
1016
  dep.symbols.set(imported, reExportName, convertLoc(loc), true);
936
1017
  }
937
1018
  }
@@ -954,6 +1035,7 @@ export default (new Transformer({
954
1035
  // Do not create a self-reference for the `default` symbol unless we have seen an __esModule flag.
955
1036
  if (
956
1037
  name === 'default' &&
1038
+ // @ts-expect-error TS2345
957
1039
  !asset.symbols.hasExportSymbol('__esModule')
958
1040
  ) {
959
1041
  continue;
@@ -989,8 +1071,13 @@ export default (new Transformer({
989
1071
  asset.sideEffects &&
990
1072
  deps.size === 0 &&
991
1073
  Object.keys(hoist_result.exported_symbols).length === 0) ||
1074
+ // @ts-expect-error TS2345
992
1075
  (hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
993
1076
  ) {
1077
+ if (is_empty_or_empty_export) {
1078
+ asset.meta.emptyFileStarReexport = true;
1079
+ }
1080
+ // @ts-expect-error TS2345
994
1081
  asset.symbols.set('*', `$${asset.id}$exports`);
995
1082
  }
996
1083
 
@@ -1010,6 +1097,7 @@ export default (new Transformer({
1010
1097
  let dep = source ? deps.get(source) : undefined;
1011
1098
  asset.symbols.set(
1012
1099
  exported,
1100
+ // @ts-expect-error TS2345
1013
1101
  `${dep?.id ?? ''}$${local}`,
1014
1102
  convertLoc(loc),
1015
1103
  );
@@ -1017,6 +1105,7 @@ export default (new Transformer({
1017
1105
  dep.symbols.ensure();
1018
1106
  dep.symbols.set(
1019
1107
  local,
1108
+ // @ts-expect-error TS2345
1020
1109
  `${dep?.id ?? ''}$${local}`,
1021
1110
  convertLoc(loc),
1022
1111
  true,
@@ -1035,6 +1124,7 @@ export default (new Transformer({
1035
1124
  let dep = deps.get(source);
1036
1125
  if (!dep) continue;
1037
1126
  dep.symbols.ensure();
1127
+ // @ts-expect-error TS2345
1038
1128
  dep.symbols.set('*', '*', convertLoc(loc), true);
1039
1129
  }
1040
1130
 
@@ -1045,14 +1135,17 @@ export default (new Transformer({
1045
1135
  (!symbol_result.is_esm &&
1046
1136
  deps.size === 0 &&
1047
1137
  symbol_result.exports.length === 0) ||
1138
+ // @ts-expect-error TS2345
1048
1139
  (symbol_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
1049
1140
  ) {
1050
1141
  asset.symbols.ensure();
1142
+ // @ts-expect-error TS2345
1051
1143
  asset.symbols.set('*', `$${asset.id}$exports`);
1052
1144
  }
1053
1145
  } else {
1054
1146
  // If the asset is wrapped, add * as a fallback
1055
1147
  asset.symbols.ensure();
1148
+ // @ts-expect-error TS2345
1056
1149
  asset.symbols.set('*', `$${asset.id}$exports`);
1057
1150
  }
1058
1151
 
@@ -1061,6 +1154,7 @@ export default (new Transformer({
1061
1154
  for (let dep of asset.getDependencies()) {
1062
1155
  if (dep.symbols.isCleared) {
1063
1156
  dep.symbols.ensure();
1157
+ // @ts-expect-error TS2345
1064
1158
  dep.symbols.set('*', `${dep.id}$`);
1065
1159
  }
1066
1160
  }
@@ -1086,6 +1180,7 @@ export default (new Transformer({
1086
1180
  let sourceMap = new SourceMap(options.projectRoot);
1087
1181
  sourceMap.addVLQMap(JSON.parse(map));
1088
1182
  if (originalMap) {
1183
+ // @ts-expect-error TS2345
1089
1184
  sourceMap.extends(originalMap);
1090
1185
  }
1091
1186
  asset.setMap(sourceMap);
@@ -1093,4 +1188,4 @@ export default (new Transformer({
1093
1188
 
1094
1189
  return [asset, ...macroAssets];
1095
1190
  },
1096
- }): Transformer);
1191
+ }) as Transformer<unknown>;