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