@atlaspack/transformer-js 3.2.3-canary.30 → 3.2.3-canary.301
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 +141 -45
- package/lib/types/JSTransformer.d.ts +3 -0
- package/package.json +16 -14
- package/src/{JSTransformer.js → JSTransformer.ts} +192 -87
package/lib/JSTransformer.js
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
function _sourceMap() {
|
|
8
|
-
const data = _interopRequireDefault(require("@
|
|
8
|
+
const data = _interopRequireDefault(require("@atlaspack/source-map"));
|
|
9
9
|
_sourceMap = function () {
|
|
10
10
|
return data;
|
|
11
11
|
};
|
|
@@ -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,21 +242,33 @@ 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 = options.env.NATIVE_GLOBAL_THIS_ALIASER === 'true';
|
|
247
|
+
let enableLazyLoadingTransformer = options.env.NATIVE_LAZY_LOADING_TRANSFORMER === 'true';
|
|
248
|
+
let enableDeadReturnsRemover = options.env.NATIVE_DEAD_RETURNS_REMOVER === 'true';
|
|
249
|
+
let enableUnusedBindingsRemover = options.env.NATIVE_UNUSED_BINDINGS_REMOVER === 'true';
|
|
226
250
|
if (conf && conf.contents) {
|
|
227
|
-
var _conf$contents, _conf$contents2, _conf$contents3, _conf$contents4;
|
|
251
|
+
var _conf$contents, _conf$contents2, _conf$contents3, _conf$contents4, _conf$contents5;
|
|
228
252
|
_utils().validateSchema.diagnostic(CONFIG_SCHEMA, {
|
|
229
253
|
data: conf.contents,
|
|
230
|
-
|
|
231
|
-
source: await options.inputFS.readFile(conf.filePath, 'utf8'),
|
|
254
|
+
source: () => options.inputFS.readFileSync(conf.filePath, 'utf8'),
|
|
232
255
|
filePath: conf.filePath,
|
|
233
256
|
prependKey: `/${(0, _diagnostic().encodeJSONKeyComponent)('@atlaspack/transformer-js')}`
|
|
234
257
|
},
|
|
235
258
|
// FIXME
|
|
236
259
|
'@atlaspack/transformer-js', 'Invalid config for @atlaspack/transformer-js');
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
260
|
+
addReactDisplayName =
|
|
261
|
+
// @ts-expect-error TS2339
|
|
262
|
+
((_conf$contents = conf.contents) === null || _conf$contents === void 0 ? void 0 : _conf$contents.addReactDisplayName) ?? addReactDisplayName;
|
|
263
|
+
// @ts-expect-error TS2339
|
|
264
|
+
magicComments = ((_conf$contents2 = conf.contents) === null || _conf$contents2 === void 0 ? void 0 : _conf$contents2.magicComments) ?? magicComments;
|
|
265
|
+
// @ts-expect-error TS2339
|
|
266
|
+
inlineEnvironment = ((_conf$contents3 = conf.contents) === null || _conf$contents3 === void 0 ? void 0 : _conf$contents3.inlineEnvironment) ?? inlineEnvironment;
|
|
267
|
+
// @ts-expect-error TS2339
|
|
268
|
+
inlineFS = ((_conf$contents4 = conf.contents) === null || _conf$contents4 === void 0 ? void 0 : _conf$contents4.inlineFS) ?? inlineFS;
|
|
269
|
+
inlineConstants =
|
|
270
|
+
// @ts-expect-error TS2339
|
|
271
|
+
((_conf$contents5 = conf.contents) === null || _conf$contents5 === void 0 ? void 0 : _conf$contents5.unstable_inlineConstants) ?? inlineConstants;
|
|
241
272
|
}
|
|
242
273
|
return {
|
|
243
274
|
isJSX,
|
|
@@ -248,10 +279,15 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
248
279
|
inlineEnvironment,
|
|
249
280
|
inlineFS,
|
|
250
281
|
inlineConstants,
|
|
282
|
+
addReactDisplayName,
|
|
251
283
|
reactRefresh,
|
|
252
284
|
decorators,
|
|
253
285
|
useDefineForClassFields,
|
|
254
|
-
magicComments
|
|
286
|
+
magicComments,
|
|
287
|
+
enableGlobalThisAliaser,
|
|
288
|
+
enableLazyLoadingTransformer,
|
|
289
|
+
enableDeadReturnsRemover,
|
|
290
|
+
enableUnusedBindingsRemover
|
|
255
291
|
};
|
|
256
292
|
},
|
|
257
293
|
async transform({
|
|
@@ -280,17 +316,22 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
280
316
|
for (let browser of browsers) {
|
|
281
317
|
let [name, version] = browser.split(' ');
|
|
282
318
|
if (BROWSER_MAPPING.hasOwnProperty(name)) {
|
|
319
|
+
// @ts-expect-error TS7053
|
|
283
320
|
name = BROWSER_MAPPING[name];
|
|
284
321
|
if (!name) {
|
|
285
322
|
continue;
|
|
286
323
|
}
|
|
287
324
|
}
|
|
288
325
|
let [major, minor = '0', patch = '0'] = version.split('-')[0].split('.');
|
|
326
|
+
// @ts-expect-error TS2345
|
|
289
327
|
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
|
|
290
328
|
continue;
|
|
291
329
|
}
|
|
292
330
|
let semverVersion = `${major}.${minor}.${patch}`;
|
|
331
|
+
|
|
332
|
+
// @ts-expect-error TS2345
|
|
293
333
|
if (targets[name] == null || _semver().default.gt(targets[name], semverVersion)) {
|
|
334
|
+
// @ts-expect-error TS7053
|
|
294
335
|
targets[name] = semverVersion;
|
|
295
336
|
}
|
|
296
337
|
}
|
|
@@ -305,6 +346,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
305
346
|
if (options.env.NODE_ENV != null) {
|
|
306
347
|
env.NODE_ENV = options.env.NODE_ENV;
|
|
307
348
|
}
|
|
349
|
+
if (process.env.ATLASPACK_BUILD_ENV === 'test') {
|
|
350
|
+
env.ATLASPACK_BUILD_ENV = 'test';
|
|
351
|
+
}
|
|
308
352
|
} else if (Array.isArray(config === null || config === void 0 ? void 0 : config.inlineEnvironment)) {
|
|
309
353
|
for (let match of (0, _utils().globMatch)(Object.keys(options.env), config.inlineEnvironment)) {
|
|
310
354
|
env[match] = String(options.env[match]);
|
|
@@ -322,23 +366,40 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
322
366
|
if (asset.type === 'ts') {
|
|
323
367
|
isJSX = false;
|
|
324
368
|
} else if (!isJSX) {
|
|
369
|
+
// @ts-expect-error TS7053
|
|
325
370
|
isJSX = Boolean(JSX_EXTENSIONS[asset.type]);
|
|
326
371
|
}
|
|
327
372
|
}
|
|
328
373
|
let macroAssets = [];
|
|
329
374
|
let {
|
|
375
|
+
// @ts-expect-error TS2339
|
|
330
376
|
dependencies,
|
|
377
|
+
// @ts-expect-error TS2339
|
|
331
378
|
code: compiledCode,
|
|
379
|
+
// @ts-expect-error TS2339
|
|
332
380
|
map,
|
|
381
|
+
// @ts-expect-error TS2339
|
|
333
382
|
shebang,
|
|
383
|
+
// @ts-expect-error TS2339
|
|
334
384
|
hoist_result,
|
|
385
|
+
// @ts-expect-error TS2339
|
|
335
386
|
symbol_result,
|
|
387
|
+
// @ts-expect-error TS2339
|
|
388
|
+
is_empty_or_empty_export,
|
|
389
|
+
// @ts-expect-error TS2339
|
|
336
390
|
needs_esm_helpers,
|
|
391
|
+
// @ts-expect-error TS2339
|
|
337
392
|
diagnostics,
|
|
393
|
+
// @ts-expect-error TS2339
|
|
338
394
|
used_env,
|
|
395
|
+
// @ts-expect-error TS2339
|
|
339
396
|
has_node_replacements,
|
|
397
|
+
// @ts-expect-error TS2339
|
|
340
398
|
is_constant_module,
|
|
341
|
-
|
|
399
|
+
// @ts-expect-error TS2339
|
|
400
|
+
conditions,
|
|
401
|
+
// @ts-expect-error TS2339
|
|
402
|
+
magic_comments
|
|
342
403
|
} = await (_rust().transformAsync || _rust().transform)({
|
|
343
404
|
filename: asset.filePath,
|
|
344
405
|
code,
|
|
@@ -349,7 +410,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
349
410
|
insert_node_globals: !asset.env.isNode() && asset.env.sourceType !== 'script',
|
|
350
411
|
node_replacer: asset.env.isNode(),
|
|
351
412
|
is_browser: asset.env.isBrowser(),
|
|
352
|
-
is_worker: asset.env.isWorker(),
|
|
413
|
+
is_worker: asset.env.isWorker() || asset.env.isTesseract(),
|
|
353
414
|
env,
|
|
354
415
|
is_type_script: asset.type === 'ts' || asset.type === 'tsx',
|
|
355
416
|
is_jsx: isJSX,
|
|
@@ -358,7 +419,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
358
419
|
automatic_jsx_runtime: Boolean(config === null || config === void 0 ? void 0 : config.automaticJSXRuntime),
|
|
359
420
|
jsx_import_source: config === null || config === void 0 ? void 0 : config.jsxImportSource,
|
|
360
421
|
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),
|
|
422
|
+
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
423
|
decorators: Boolean(config === null || config === void 0 ? void 0 : config.decorators),
|
|
363
424
|
use_define_for_class_fields: Boolean(config === null || config === void 0 ? void 0 : config.useDefineForClassFields),
|
|
364
425
|
targets,
|
|
@@ -373,16 +434,23 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
373
434
|
standalone: asset.query.has('standalone'),
|
|
374
435
|
inline_constants: config.inlineConstants,
|
|
375
436
|
conditional_bundling: options.featureFlags.conditionalBundlingApi,
|
|
376
|
-
|
|
437
|
+
hmr_improvements: options.featureFlags.hmrImprovements,
|
|
438
|
+
add_display_name: Boolean(config.addReactDisplayName),
|
|
439
|
+
exports_rebinding_optimisation: options.featureFlags.exportsRebindingOptimisation,
|
|
440
|
+
magic_comments: Boolean(config === null || config === void 0 ? void 0 : config.magicComments) || (0, _featureFlags().getFeatureFlag)('supportWebpackChunkName'),
|
|
441
|
+
is_source: asset.isSource,
|
|
442
|
+
enable_global_this_aliaser: Boolean(config.enableGlobalThisAliaser),
|
|
443
|
+
enable_lazy_loading_transformer: Boolean(config.enableLazyLoadingTransformer),
|
|
444
|
+
nested_promise_import_fix: options.featureFlags.nestedPromiseImportFix,
|
|
445
|
+
enable_dead_returns_remover: Boolean(config.enableDeadReturnsRemover),
|
|
446
|
+
enable_unused_bindings_remover: Boolean(config.enableUnusedBindingsRemover),
|
|
377
447
|
callMacro: asset.isSource ? async (err, src, exportName, args, loc) => {
|
|
378
448
|
let mod;
|
|
379
449
|
try {
|
|
380
450
|
mod = await options.packageManager.require(src, asset.filePath);
|
|
381
451
|
|
|
382
452
|
// Default interop for CommonJS modules.
|
|
383
|
-
if (exportName === 'default' && !mod.__esModule &&
|
|
384
|
-
// $FlowFixMe
|
|
385
|
-
Object.prototype.toString.call(config) !== '[object Module]') {
|
|
453
|
+
if (exportName === 'default' && !mod.__esModule && Object.prototype.toString.call(config) !== '[object Module]') {
|
|
386
454
|
mod = {
|
|
387
455
|
default: mod
|
|
388
456
|
};
|
|
@@ -398,7 +466,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
398
466
|
}
|
|
399
467
|
try {
|
|
400
468
|
if (typeof mod[exportName] === 'function') {
|
|
401
|
-
|
|
469
|
+
let ctx = {
|
|
402
470
|
// Allows macros to emit additional assets to add as dependencies (e.g. css).
|
|
403
471
|
addAsset(a) {
|
|
404
472
|
let k = String(macroAssets.length);
|
|
@@ -406,6 +474,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
406
474
|
if (asset.env.sourceMap) {
|
|
407
475
|
// Generate a source map that maps each line of the asset to the original macro call.
|
|
408
476
|
map = new (_sourceMap().default)(options.projectRoot);
|
|
477
|
+
// @ts-expect-error TS2304
|
|
409
478
|
let mappings = [];
|
|
410
479
|
let line = 1;
|
|
411
480
|
for (let i = 0; i <= a.content.length; i++) {
|
|
@@ -428,7 +497,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
428
497
|
if (originalMap) {
|
|
429
498
|
map.extends(originalMap);
|
|
430
499
|
} else {
|
|
431
|
-
|
|
500
|
+
if (!(0, _featureFlags().getFeatureFlag)('omitSourcesContentInMemory')) {
|
|
501
|
+
map.setSourceContent(asset.filePath, code.toString());
|
|
502
|
+
}
|
|
432
503
|
}
|
|
433
504
|
}
|
|
434
505
|
macroAssets.push({
|
|
@@ -457,7 +528,8 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
457
528
|
invalidateOnBuild() {
|
|
458
529
|
asset.invalidateOnBuild();
|
|
459
530
|
}
|
|
460
|
-
}
|
|
531
|
+
};
|
|
532
|
+
return mod[exportName].apply(ctx, args);
|
|
461
533
|
} else {
|
|
462
534
|
throw new Error(`"${exportName}" in "${src}" is not a function.`);
|
|
463
535
|
}
|
|
@@ -479,11 +551,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
479
551
|
} : null
|
|
480
552
|
});
|
|
481
553
|
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
|
-
}));
|
|
554
|
+
asset.meta.conditions = conditions;
|
|
487
555
|
}
|
|
488
556
|
if (is_constant_module) {
|
|
489
557
|
asset.meta.isConstantModule = true;
|
|
@@ -503,17 +571,22 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
503
571
|
|
|
504
572
|
// If there is an original source map, use it to remap to the original source location.
|
|
505
573
|
if (originalMap) {
|
|
506
|
-
location = (0, _utils().remapSourceLocation)(location, originalMap);
|
|
574
|
+
location = (0, _utils().remapSourceLocation)(location, originalMap, options.projectRoot);
|
|
507
575
|
}
|
|
508
576
|
return location;
|
|
509
577
|
};
|
|
510
578
|
if (diagnostics) {
|
|
511
|
-
let errors = diagnostics.filter(
|
|
512
|
-
|
|
579
|
+
let errors = diagnostics.filter(
|
|
580
|
+
// @ts-expect-error TS7006
|
|
581
|
+
d => d.severity === 'Error' || d.severity === 'SourceError' && asset.isSource);
|
|
582
|
+
let warnings = diagnostics.filter(
|
|
583
|
+
// @ts-expect-error TS7006
|
|
584
|
+
d => d.severity === 'Warning' || d.severity === 'SourceError' && !asset.isSource);
|
|
513
585
|
let convertDiagnostic = diagnostic => {
|
|
514
586
|
var _diagnostic$code_high;
|
|
515
587
|
let message = diagnostic.message;
|
|
516
588
|
if (message === 'SCRIPT_ERROR') {
|
|
589
|
+
// @ts-expect-error TS7053
|
|
517
590
|
let err = SCRIPT_ERRORS[asset.env.context];
|
|
518
591
|
message = (err === null || err === void 0 ? void 0 : err.message) || SCRIPT_ERRORS.browser.message;
|
|
519
592
|
}
|
|
@@ -536,6 +609,8 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
536
609
|
codeHighlights: [(0, _diagnostic().convertSourceLocationToHighlight)(asset.env.loc, 'The environment was originally created here')]
|
|
537
610
|
});
|
|
538
611
|
}
|
|
612
|
+
|
|
613
|
+
// @ts-expect-error TS7053
|
|
539
614
|
let err = SCRIPT_ERRORS[asset.env.context];
|
|
540
615
|
if (err) {
|
|
541
616
|
if (!res.hints) {
|
|
@@ -580,6 +655,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
580
655
|
env: {
|
|
581
656
|
context: 'web-worker',
|
|
582
657
|
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
|
|
658
|
+
// @ts-expect-error TS2322
|
|
583
659
|
outputFormat,
|
|
584
660
|
loc
|
|
585
661
|
},
|
|
@@ -629,6 +705,13 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
629
705
|
});
|
|
630
706
|
} else if (dep.kind === 'File') {
|
|
631
707
|
asset.invalidateOnFileChange(dep.specifier);
|
|
708
|
+
} else if (dep.kind === 'Id') {
|
|
709
|
+
// Record parcelRequire calls so that the dev packager can add them as dependencies.
|
|
710
|
+
// This allows the HMR runtime to collect parents across async boundaries (through runtimes).
|
|
711
|
+
// TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
|
|
712
|
+
asset.meta.hmrDeps ??= [];
|
|
713
|
+
(0, _assert().default)(Array.isArray(asset.meta.hmrDeps));
|
|
714
|
+
asset.meta.hmrDeps.push(dep.specifier);
|
|
632
715
|
} else {
|
|
633
716
|
let meta = {
|
|
634
717
|
kind: dep.kind
|
|
@@ -676,6 +759,12 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
676
759
|
outputFormat,
|
|
677
760
|
loc: convertLoc(dep.loc)
|
|
678
761
|
};
|
|
762
|
+
if ((0, _featureFlags().getFeatureFlag)('supportWebpackChunkName')) {
|
|
763
|
+
let chunkName = magic_comments[dep.specifier];
|
|
764
|
+
if (chunkName) {
|
|
765
|
+
meta.chunkName = chunkName;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
679
768
|
}
|
|
680
769
|
|
|
681
770
|
// Always bundle helpers, even with includeNodeModules: false, except if this is a library.
|
|
@@ -695,6 +784,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
695
784
|
idx = dep.specifier.indexOf('/', idx + 1);
|
|
696
785
|
}
|
|
697
786
|
let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
|
|
787
|
+
// @ts-expect-error TS7053
|
|
698
788
|
range = _package.default.dependencies[module];
|
|
699
789
|
}
|
|
700
790
|
asset.addDependency({
|
|
@@ -706,6 +796,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
706
796
|
meta,
|
|
707
797
|
resolveFrom: isHelper ? __filename : undefined,
|
|
708
798
|
range,
|
|
799
|
+
// @ts-expect-error TS2322
|
|
709
800
|
env
|
|
710
801
|
});
|
|
711
802
|
}
|
|
@@ -717,10 +808,12 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
717
808
|
exported,
|
|
718
809
|
local,
|
|
719
810
|
loc,
|
|
720
|
-
is_esm
|
|
811
|
+
is_esm,
|
|
812
|
+
is_static_binding_safe
|
|
721
813
|
} of hoist_result.exported_symbols) {
|
|
722
814
|
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
723
|
-
isEsm: is_esm
|
|
815
|
+
isEsm: is_esm,
|
|
816
|
+
isStaticBindingSafe: is_static_binding_safe
|
|
724
817
|
});
|
|
725
818
|
}
|
|
726
819
|
|
|
@@ -802,6 +895,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
802
895
|
// (and the asset has side effects), or the asset is wrapped.
|
|
803
896
|
// This allows accessing symbols that don't exist without errors in symbol propagation.
|
|
804
897
|
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('*')) {
|
|
898
|
+
if (is_empty_or_empty_export) {
|
|
899
|
+
asset.meta.emptyFileStarReexport = true;
|
|
900
|
+
}
|
|
805
901
|
asset.symbols.set('*', `$${asset.id}$exports`);
|
|
806
902
|
}
|
|
807
903
|
asset.meta.hasCJSExports = hoist_result.has_cjs_exports;
|
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.301+25fee723d",
|
|
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,13 +24,13 @@
|
|
|
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/
|
|
30
|
-
"@atlaspack/
|
|
31
|
-
"@
|
|
27
|
+
"@atlaspack/diagnostic": "2.14.1-canary.369+25fee723d",
|
|
28
|
+
"@atlaspack/feature-flags": "2.14.1-canary.369+25fee723d",
|
|
29
|
+
"@atlaspack/plugin": "2.14.5-canary.301+25fee723d",
|
|
30
|
+
"@atlaspack/rust": "3.2.1-canary.301+25fee723d",
|
|
31
|
+
"@atlaspack/source-map": "3.1.1-canary.4080+25fee723d",
|
|
32
|
+
"@atlaspack/utils": "2.14.5-canary.301+25fee723d",
|
|
33
|
+
"@atlaspack/workers": "2.14.5-canary.301+25fee723d",
|
|
32
34
|
"@swc/helpers": "^0.5.15",
|
|
33
35
|
"browserslist": "^4.6.6",
|
|
34
36
|
"nullthrows": "^1.1.1",
|
|
@@ -36,8 +38,8 @@
|
|
|
36
38
|
"semver": "^7.5.2"
|
|
37
39
|
},
|
|
38
40
|
"peerDependencies": {
|
|
39
|
-
"@atlaspack/core": "
|
|
41
|
+
"@atlaspack/core": "2.28.0"
|
|
40
42
|
},
|
|
41
43
|
"type": "commonjs",
|
|
42
|
-
"gitHead": "
|
|
43
|
-
}
|
|
44
|
+
"gitHead": "25fee723d2228d0482ec8a8f1b8379ccd2977ce7"
|
|
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
|
-
import SourceMap from '@
|
|
10
|
+
import SourceMap from '@atlaspack/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,14 +300,23 @@ export default (new Transformer({
|
|
|
291
300
|
let inlineFS = !ignoreFS;
|
|
292
301
|
let inlineConstants = false;
|
|
293
302
|
let magicComments = false;
|
|
303
|
+
let addReactDisplayName = false;
|
|
304
|
+
|
|
305
|
+
let enableGlobalThisAliaser =
|
|
306
|
+
options.env.NATIVE_GLOBAL_THIS_ALIASER === 'true';
|
|
307
|
+
let enableLazyLoadingTransformer =
|
|
308
|
+
options.env.NATIVE_LAZY_LOADING_TRANSFORMER === 'true';
|
|
309
|
+
let enableDeadReturnsRemover =
|
|
310
|
+
options.env.NATIVE_DEAD_RETURNS_REMOVER === 'true';
|
|
311
|
+
let enableUnusedBindingsRemover =
|
|
312
|
+
options.env.NATIVE_UNUSED_BINDINGS_REMOVER === 'true';
|
|
294
313
|
|
|
295
314
|
if (conf && conf.contents) {
|
|
296
315
|
validateSchema.diagnostic(
|
|
297
316
|
CONFIG_SCHEMA,
|
|
298
317
|
{
|
|
299
318
|
data: conf.contents,
|
|
300
|
-
|
|
301
|
-
source: await options.inputFS.readFile(conf.filePath, 'utf8'),
|
|
319
|
+
source: () => options.inputFS.readFileSync(conf.filePath, 'utf8'),
|
|
302
320
|
filePath: conf.filePath,
|
|
303
321
|
prependKey: `/${encodeJSONKeyComponent('@atlaspack/transformer-js')}`,
|
|
304
322
|
},
|
|
@@ -307,10 +325,17 @@ export default (new Transformer({
|
|
|
307
325
|
'Invalid config for @atlaspack/transformer-js',
|
|
308
326
|
);
|
|
309
327
|
|
|
328
|
+
addReactDisplayName =
|
|
329
|
+
// @ts-expect-error TS2339
|
|
330
|
+
conf.contents?.addReactDisplayName ?? addReactDisplayName;
|
|
331
|
+
// @ts-expect-error TS2339
|
|
310
332
|
magicComments = conf.contents?.magicComments ?? magicComments;
|
|
333
|
+
// @ts-expect-error TS2339
|
|
311
334
|
inlineEnvironment = conf.contents?.inlineEnvironment ?? inlineEnvironment;
|
|
335
|
+
// @ts-expect-error TS2339
|
|
312
336
|
inlineFS = conf.contents?.inlineFS ?? inlineFS;
|
|
313
337
|
inlineConstants =
|
|
338
|
+
// @ts-expect-error TS2339
|
|
314
339
|
conf.contents?.unstable_inlineConstants ?? inlineConstants;
|
|
315
340
|
}
|
|
316
341
|
|
|
@@ -323,10 +348,15 @@ export default (new Transformer({
|
|
|
323
348
|
inlineEnvironment,
|
|
324
349
|
inlineFS,
|
|
325
350
|
inlineConstants,
|
|
351
|
+
addReactDisplayName,
|
|
326
352
|
reactRefresh,
|
|
327
353
|
decorators,
|
|
328
354
|
useDefineForClassFields,
|
|
329
355
|
magicComments,
|
|
356
|
+
enableGlobalThisAliaser,
|
|
357
|
+
enableLazyLoadingTransformer,
|
|
358
|
+
enableDeadReturnsRemover,
|
|
359
|
+
enableUnusedBindingsRemover,
|
|
330
360
|
};
|
|
331
361
|
},
|
|
332
362
|
async transform({asset, config, options, logger}) {
|
|
@@ -357,6 +387,7 @@ export default (new Transformer({
|
|
|
357
387
|
for (let browser of browsers) {
|
|
358
388
|
let [name, version] = browser.split(' ');
|
|
359
389
|
if (BROWSER_MAPPING.hasOwnProperty(name)) {
|
|
390
|
+
// @ts-expect-error TS7053
|
|
360
391
|
name = BROWSER_MAPPING[name];
|
|
361
392
|
if (!name) {
|
|
362
393
|
continue;
|
|
@@ -366,12 +397,15 @@ export default (new Transformer({
|
|
|
366
397
|
let [major, minor = '0', patch = '0'] = version
|
|
367
398
|
.split('-')[0]
|
|
368
399
|
.split('.');
|
|
400
|
+
// @ts-expect-error TS2345
|
|
369
401
|
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
|
|
370
402
|
continue;
|
|
371
403
|
}
|
|
372
404
|
let semverVersion = `${major}.${minor}.${patch}`;
|
|
373
405
|
|
|
406
|
+
// @ts-expect-error TS2345
|
|
374
407
|
if (targets[name] == null || semver.gt(targets[name], semverVersion)) {
|
|
408
|
+
// @ts-expect-error TS7053
|
|
375
409
|
targets[name] = semverVersion;
|
|
376
410
|
}
|
|
377
411
|
}
|
|
@@ -411,24 +445,47 @@ export default (new Transformer({
|
|
|
411
445
|
if (asset.type === 'ts') {
|
|
412
446
|
isJSX = false;
|
|
413
447
|
} else if (!isJSX) {
|
|
448
|
+
// @ts-expect-error TS7053
|
|
414
449
|
isJSX = Boolean(JSX_EXTENSIONS[asset.type]);
|
|
415
450
|
}
|
|
416
451
|
}
|
|
417
452
|
|
|
418
|
-
let macroAssets
|
|
453
|
+
let macroAssets: Array<{
|
|
454
|
+
content: string;
|
|
455
|
+
// @ts-expect-error TS2552
|
|
456
|
+
map: undefined | NodeSourceMap;
|
|
457
|
+
type: string;
|
|
458
|
+
uniqueKey: string;
|
|
459
|
+
}> = [];
|
|
419
460
|
let {
|
|
461
|
+
// @ts-expect-error TS2339
|
|
420
462
|
dependencies,
|
|
463
|
+
// @ts-expect-error TS2339
|
|
421
464
|
code: compiledCode,
|
|
465
|
+
// @ts-expect-error TS2339
|
|
422
466
|
map,
|
|
467
|
+
// @ts-expect-error TS2339
|
|
423
468
|
shebang,
|
|
469
|
+
// @ts-expect-error TS2339
|
|
424
470
|
hoist_result,
|
|
471
|
+
// @ts-expect-error TS2339
|
|
425
472
|
symbol_result,
|
|
473
|
+
// @ts-expect-error TS2339
|
|
474
|
+
is_empty_or_empty_export,
|
|
475
|
+
// @ts-expect-error TS2339
|
|
426
476
|
needs_esm_helpers,
|
|
477
|
+
// @ts-expect-error TS2339
|
|
427
478
|
diagnostics,
|
|
479
|
+
// @ts-expect-error TS2339
|
|
428
480
|
used_env,
|
|
481
|
+
// @ts-expect-error TS2339
|
|
429
482
|
has_node_replacements,
|
|
483
|
+
// @ts-expect-error TS2339
|
|
430
484
|
is_constant_module,
|
|
485
|
+
// @ts-expect-error TS2339
|
|
431
486
|
conditions,
|
|
487
|
+
// @ts-expect-error TS2339
|
|
488
|
+
magic_comments,
|
|
432
489
|
} = await (transformAsync || transform)({
|
|
433
490
|
filename: asset.filePath,
|
|
434
491
|
code,
|
|
@@ -440,7 +497,7 @@ export default (new Transformer({
|
|
|
440
497
|
!asset.env.isNode() && asset.env.sourceType !== 'script',
|
|
441
498
|
node_replacer: asset.env.isNode(),
|
|
442
499
|
is_browser: asset.env.isBrowser(),
|
|
443
|
-
is_worker: asset.env.isWorker(),
|
|
500
|
+
is_worker: asset.env.isWorker() || asset.env.isTesseract(),
|
|
444
501
|
env,
|
|
445
502
|
is_type_script: asset.type === 'ts' || asset.type === 'tsx',
|
|
446
503
|
is_jsx: isJSX,
|
|
@@ -453,6 +510,7 @@ export default (new Transformer({
|
|
|
453
510
|
asset.env.isBrowser() &&
|
|
454
511
|
!asset.env.isLibrary &&
|
|
455
512
|
!asset.env.isWorker() &&
|
|
513
|
+
!asset.env.isTesseract() &&
|
|
456
514
|
!asset.env.isWorklet() &&
|
|
457
515
|
Boolean(config?.reactRefresh),
|
|
458
516
|
decorators: Boolean(config?.decorators),
|
|
@@ -470,9 +528,25 @@ export default (new Transformer({
|
|
|
470
528
|
standalone: asset.query.has('standalone'),
|
|
471
529
|
inline_constants: config.inlineConstants,
|
|
472
530
|
conditional_bundling: options.featureFlags.conditionalBundlingApi,
|
|
473
|
-
|
|
531
|
+
hmr_improvements: options.featureFlags.hmrImprovements,
|
|
532
|
+
add_display_name: Boolean(config.addReactDisplayName),
|
|
533
|
+
exports_rebinding_optimisation:
|
|
534
|
+
options.featureFlags.exportsRebindingOptimisation,
|
|
535
|
+
magic_comments:
|
|
536
|
+
Boolean(config?.magicComments) ||
|
|
537
|
+
getFeatureFlag('supportWebpackChunkName'),
|
|
538
|
+
is_source: asset.isSource,
|
|
539
|
+
enable_global_this_aliaser: Boolean(config.enableGlobalThisAliaser),
|
|
540
|
+
enable_lazy_loading_transformer: Boolean(
|
|
541
|
+
config.enableLazyLoadingTransformer,
|
|
542
|
+
),
|
|
543
|
+
nested_promise_import_fix: options.featureFlags.nestedPromiseImportFix,
|
|
544
|
+
enable_dead_returns_remover: Boolean(config.enableDeadReturnsRemover),
|
|
545
|
+
enable_unused_bindings_remover: Boolean(
|
|
546
|
+
config.enableUnusedBindingsRemover,
|
|
547
|
+
),
|
|
474
548
|
callMacro: asset.isSource
|
|
475
|
-
? async (err, src, exportName, args, loc) => {
|
|
549
|
+
? async (err: any, src: any, exportName: any, args: any, loc: any) => {
|
|
476
550
|
let mod;
|
|
477
551
|
try {
|
|
478
552
|
mod = await options.packageManager.require(src, asset.filePath);
|
|
@@ -481,7 +555,6 @@ export default (new Transformer({
|
|
|
481
555
|
if (
|
|
482
556
|
exportName === 'default' &&
|
|
483
557
|
!mod.__esModule &&
|
|
484
|
-
// $FlowFixMe
|
|
485
558
|
Object.prototype.toString.call(config) !== '[object Module]'
|
|
486
559
|
) {
|
|
487
560
|
mod = {default: mod};
|
|
@@ -490,7 +563,7 @@ export default (new Transformer({
|
|
|
490
563
|
if (!Object.hasOwnProperty.call(mod, exportName)) {
|
|
491
564
|
throw new Error(`"${src}" does not export "${exportName}".`);
|
|
492
565
|
}
|
|
493
|
-
} catch (err) {
|
|
566
|
+
} catch (err: any) {
|
|
494
567
|
throw {
|
|
495
568
|
kind: 1,
|
|
496
569
|
message: err.message,
|
|
@@ -507,7 +580,8 @@ export default (new Transformer({
|
|
|
507
580
|
if (asset.env.sourceMap) {
|
|
508
581
|
// Generate a source map that maps each line of the asset to the original macro call.
|
|
509
582
|
map = new SourceMap(options.projectRoot);
|
|
510
|
-
|
|
583
|
+
// @ts-expect-error TS2304
|
|
584
|
+
let mappings: Array<IndexedMapping<string>> = [];
|
|
511
585
|
let line = 1;
|
|
512
586
|
for (let i = 0; i <= a.content.length; i++) {
|
|
513
587
|
if (i === a.content.length || a.content[i] === '\n') {
|
|
@@ -530,7 +604,9 @@ export default (new Transformer({
|
|
|
530
604
|
if (originalMap) {
|
|
531
605
|
map.extends(originalMap);
|
|
532
606
|
} else {
|
|
533
|
-
|
|
607
|
+
if (!getFeatureFlag('omitSourcesContentInMemory')) {
|
|
608
|
+
map.setSourceContent(asset.filePath, code.toString());
|
|
609
|
+
}
|
|
534
610
|
}
|
|
535
611
|
}
|
|
536
612
|
|
|
@@ -546,13 +622,13 @@ export default (new Transformer({
|
|
|
546
622
|
specifierType: 'esm',
|
|
547
623
|
});
|
|
548
624
|
},
|
|
549
|
-
invalidateOnFileChange(filePath) {
|
|
625
|
+
invalidateOnFileChange(filePath: FilePath) {
|
|
550
626
|
asset.invalidateOnFileChange(filePath);
|
|
551
627
|
},
|
|
552
|
-
invalidateOnFileCreate(invalidation) {
|
|
628
|
+
invalidateOnFileCreate(invalidation: FileCreateInvalidation) {
|
|
553
629
|
asset.invalidateOnFileCreate(invalidation);
|
|
554
630
|
},
|
|
555
|
-
invalidateOnEnvChange(env) {
|
|
631
|
+
invalidateOnEnvChange(env: string) {
|
|
556
632
|
asset.invalidateOnEnvChange(env);
|
|
557
633
|
},
|
|
558
634
|
invalidateOnStartup() {
|
|
@@ -569,7 +645,7 @@ export default (new Transformer({
|
|
|
569
645
|
`"${exportName}" in "${src}" is not a function.`,
|
|
570
646
|
);
|
|
571
647
|
}
|
|
572
|
-
} catch (err) {
|
|
648
|
+
} catch (err: any) {
|
|
573
649
|
// Remove atlaspack core from stack and build string so Rust can process errors more easily.
|
|
574
650
|
let stack = (err.stack || '').split('\n').slice(1);
|
|
575
651
|
let message = err.message;
|
|
@@ -589,18 +665,14 @@ export default (new Transformer({
|
|
|
589
665
|
});
|
|
590
666
|
|
|
591
667
|
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
|
-
}));
|
|
668
|
+
asset.meta.conditions = conditions;
|
|
597
669
|
}
|
|
598
670
|
|
|
599
671
|
if (is_constant_module) {
|
|
600
672
|
asset.meta.isConstantModule = true;
|
|
601
673
|
}
|
|
602
674
|
|
|
603
|
-
let convertLoc = (loc): SourceLocation => {
|
|
675
|
+
let convertLoc = (loc: any): SourceLocation => {
|
|
604
676
|
let location = {
|
|
605
677
|
filePath: asset.filePath,
|
|
606
678
|
start: {
|
|
@@ -615,7 +687,11 @@ export default (new Transformer({
|
|
|
615
687
|
|
|
616
688
|
// If there is an original source map, use it to remap to the original source location.
|
|
617
689
|
if (originalMap) {
|
|
618
|
-
location = remapSourceLocation(
|
|
690
|
+
location = remapSourceLocation(
|
|
691
|
+
location,
|
|
692
|
+
originalMap,
|
|
693
|
+
options.projectRoot,
|
|
694
|
+
);
|
|
619
695
|
}
|
|
620
696
|
|
|
621
697
|
return location;
|
|
@@ -623,19 +699,22 @@ export default (new Transformer({
|
|
|
623
699
|
|
|
624
700
|
if (diagnostics) {
|
|
625
701
|
let errors = diagnostics.filter(
|
|
702
|
+
// @ts-expect-error TS7006
|
|
626
703
|
(d) =>
|
|
627
704
|
d.severity === 'Error' ||
|
|
628
705
|
(d.severity === 'SourceError' && asset.isSource),
|
|
629
706
|
);
|
|
630
707
|
let warnings = diagnostics.filter(
|
|
708
|
+
// @ts-expect-error TS7006
|
|
631
709
|
(d) =>
|
|
632
710
|
d.severity === 'Warning' ||
|
|
633
711
|
(d.severity === 'SourceError' && !asset.isSource),
|
|
634
712
|
);
|
|
635
|
-
let convertDiagnostic = (diagnostic) => {
|
|
713
|
+
let convertDiagnostic = (diagnostic: any) => {
|
|
636
714
|
let message = diagnostic.message;
|
|
637
715
|
if (message === 'SCRIPT_ERROR') {
|
|
638
|
-
|
|
716
|
+
// @ts-expect-error TS7053
|
|
717
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
639
718
|
message = err?.message || SCRIPT_ERRORS.browser.message;
|
|
640
719
|
}
|
|
641
720
|
|
|
@@ -644,11 +723,12 @@ export default (new Transformer({
|
|
|
644
723
|
codeFrames: [
|
|
645
724
|
{
|
|
646
725
|
filePath: asset.filePath,
|
|
647
|
-
codeHighlights: diagnostic.code_highlights?.map(
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
726
|
+
codeHighlights: diagnostic.code_highlights?.map(
|
|
727
|
+
(highlight: any) =>
|
|
728
|
+
convertSourceLocationToHighlight(
|
|
729
|
+
convertLoc(highlight.loc),
|
|
730
|
+
highlight.message ?? undefined,
|
|
731
|
+
),
|
|
652
732
|
),
|
|
653
733
|
},
|
|
654
734
|
],
|
|
@@ -672,7 +752,8 @@ export default (new Transformer({
|
|
|
672
752
|
});
|
|
673
753
|
}
|
|
674
754
|
|
|
675
|
-
|
|
755
|
+
// @ts-expect-error TS7053
|
|
756
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
676
757
|
if (err) {
|
|
677
758
|
if (!res.hints) {
|
|
678
759
|
res.hints = [err.hint];
|
|
@@ -729,6 +810,7 @@ export default (new Transformer({
|
|
|
729
810
|
env: {
|
|
730
811
|
context: 'web-worker',
|
|
731
812
|
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
|
|
813
|
+
// @ts-expect-error TS2322
|
|
732
814
|
outputFormat,
|
|
733
815
|
loc,
|
|
734
816
|
},
|
|
@@ -776,6 +858,13 @@ export default (new Transformer({
|
|
|
776
858
|
});
|
|
777
859
|
} else if (dep.kind === 'File') {
|
|
778
860
|
asset.invalidateOnFileChange(dep.specifier);
|
|
861
|
+
} else if (dep.kind === 'Id') {
|
|
862
|
+
// Record parcelRequire calls so that the dev packager can add them as dependencies.
|
|
863
|
+
// This allows the HMR runtime to collect parents across async boundaries (through runtimes).
|
|
864
|
+
// TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
|
|
865
|
+
asset.meta.hmrDeps ??= [];
|
|
866
|
+
invariant(Array.isArray(asset.meta.hmrDeps));
|
|
867
|
+
asset.meta.hmrDeps.push(dep.specifier);
|
|
779
868
|
} else {
|
|
780
869
|
let meta: JSONObject = {kind: dep.kind};
|
|
781
870
|
if (dep.attributes) {
|
|
@@ -839,6 +928,13 @@ export default (new Transformer({
|
|
|
839
928
|
outputFormat,
|
|
840
929
|
loc: convertLoc(dep.loc),
|
|
841
930
|
};
|
|
931
|
+
|
|
932
|
+
if (getFeatureFlag('supportWebpackChunkName')) {
|
|
933
|
+
let chunkName = magic_comments[dep.specifier];
|
|
934
|
+
if (chunkName) {
|
|
935
|
+
meta.chunkName = chunkName;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
842
938
|
}
|
|
843
939
|
|
|
844
940
|
// Always bundle helpers, even with includeNodeModules: false, except if this is a library.
|
|
@@ -863,6 +959,7 @@ export default (new Transformer({
|
|
|
863
959
|
idx = dep.specifier.indexOf('/', idx + 1);
|
|
864
960
|
}
|
|
865
961
|
let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
|
|
962
|
+
// @ts-expect-error TS7053
|
|
866
963
|
range = pkg.dependencies[module];
|
|
867
964
|
}
|
|
868
965
|
|
|
@@ -874,12 +971,13 @@ export default (new Transformer({
|
|
|
874
971
|
dep.kind === 'DynamicImport'
|
|
875
972
|
? 'lazy'
|
|
876
973
|
: dep.kind === 'ConditionalImport'
|
|
877
|
-
|
|
878
|
-
|
|
974
|
+
? 'conditional'
|
|
975
|
+
: 'sync',
|
|
879
976
|
isOptional: dep.is_optional,
|
|
880
977
|
meta,
|
|
881
978
|
resolveFrom: isHelper ? __filename : undefined,
|
|
882
979
|
range,
|
|
980
|
+
// @ts-expect-error TS2322
|
|
883
981
|
env,
|
|
884
982
|
});
|
|
885
983
|
}
|
|
@@ -893,8 +991,12 @@ export default (new Transformer({
|
|
|
893
991
|
local,
|
|
894
992
|
loc,
|
|
895
993
|
is_esm,
|
|
994
|
+
is_static_binding_safe,
|
|
896
995
|
} of hoist_result.exported_symbols) {
|
|
897
|
-
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
996
|
+
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
997
|
+
isEsm: is_esm,
|
|
998
|
+
isStaticBindingSafe: is_static_binding_safe,
|
|
999
|
+
});
|
|
898
1000
|
}
|
|
899
1001
|
|
|
900
1002
|
// deps is a map of dependencies that are keyed by placeholder or specifier
|
|
@@ -991,6 +1093,9 @@ export default (new Transformer({
|
|
|
991
1093
|
Object.keys(hoist_result.exported_symbols).length === 0) ||
|
|
992
1094
|
(hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
|
|
993
1095
|
) {
|
|
1096
|
+
if (is_empty_or_empty_export) {
|
|
1097
|
+
asset.meta.emptyFileStarReexport = true;
|
|
1098
|
+
}
|
|
994
1099
|
asset.symbols.set('*', `$${asset.id}$exports`);
|
|
995
1100
|
}
|
|
996
1101
|
|
|
@@ -1093,4 +1198,4 @@ export default (new Transformer({
|
|
|
1093
1198
|
|
|
1094
1199
|
return [asset, ...macroAssets];
|
|
1095
1200
|
},
|
|
1096
|
-
})
|
|
1201
|
+
}) as Transformer<unknown>;
|