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