@atlaspack/transformer-js 3.2.3-canary.3 → 3.2.3-canary.300
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 +135 -45
- package/lib/types/JSTransformer.d.ts +3 -0
- package/package.json +16 -14
- package/src/{JSTransformer.js → JSTransformer.ts} +184 -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,31 @@ 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
|
-
|
|
231
|
-
source: await options.inputFS.readFile(conf.filePath, 'utf8'),
|
|
252
|
+
source: () => options.inputFS.readFileSync(conf.filePath, 'utf8'),
|
|
232
253
|
filePath: conf.filePath,
|
|
233
254
|
prependKey: `/${(0, _diagnostic().encodeJSONKeyComponent)('@atlaspack/transformer-js')}`
|
|
234
255
|
},
|
|
235
256
|
// FIXME
|
|
236
257
|
'@atlaspack/transformer-js', 'Invalid config for @atlaspack/transformer-js');
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
258
|
+
addReactDisplayName =
|
|
259
|
+
// @ts-expect-error TS2339
|
|
260
|
+
((_conf$contents = conf.contents) === null || _conf$contents === void 0 ? void 0 : _conf$contents.addReactDisplayName) ?? addReactDisplayName;
|
|
261
|
+
// @ts-expect-error TS2339
|
|
262
|
+
magicComments = ((_conf$contents2 = conf.contents) === null || _conf$contents2 === void 0 ? void 0 : _conf$contents2.magicComments) ?? magicComments;
|
|
263
|
+
// @ts-expect-error TS2339
|
|
264
|
+
inlineEnvironment = ((_conf$contents3 = conf.contents) === null || _conf$contents3 === void 0 ? void 0 : _conf$contents3.inlineEnvironment) ?? inlineEnvironment;
|
|
265
|
+
// @ts-expect-error TS2339
|
|
266
|
+
inlineFS = ((_conf$contents4 = conf.contents) === null || _conf$contents4 === void 0 ? void 0 : _conf$contents4.inlineFS) ?? inlineFS;
|
|
267
|
+
inlineConstants =
|
|
268
|
+
// @ts-expect-error TS2339
|
|
269
|
+
((_conf$contents5 = conf.contents) === null || _conf$contents5 === void 0 ? void 0 : _conf$contents5.unstable_inlineConstants) ?? inlineConstants;
|
|
241
270
|
}
|
|
242
271
|
return {
|
|
243
272
|
isJSX,
|
|
@@ -248,10 +277,13 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
248
277
|
inlineEnvironment,
|
|
249
278
|
inlineFS,
|
|
250
279
|
inlineConstants,
|
|
280
|
+
addReactDisplayName,
|
|
251
281
|
reactRefresh,
|
|
252
282
|
decorators,
|
|
253
283
|
useDefineForClassFields,
|
|
254
|
-
magicComments
|
|
284
|
+
magicComments,
|
|
285
|
+
enableGlobalThisAliaser,
|
|
286
|
+
enableLazyLoadingTransformer
|
|
255
287
|
};
|
|
256
288
|
},
|
|
257
289
|
async transform({
|
|
@@ -280,17 +312,22 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
280
312
|
for (let browser of browsers) {
|
|
281
313
|
let [name, version] = browser.split(' ');
|
|
282
314
|
if (BROWSER_MAPPING.hasOwnProperty(name)) {
|
|
315
|
+
// @ts-expect-error TS7053
|
|
283
316
|
name = BROWSER_MAPPING[name];
|
|
284
317
|
if (!name) {
|
|
285
318
|
continue;
|
|
286
319
|
}
|
|
287
320
|
}
|
|
288
321
|
let [major, minor = '0', patch = '0'] = version.split('-')[0].split('.');
|
|
322
|
+
// @ts-expect-error TS2345
|
|
289
323
|
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
|
|
290
324
|
continue;
|
|
291
325
|
}
|
|
292
326
|
let semverVersion = `${major}.${minor}.${patch}`;
|
|
327
|
+
|
|
328
|
+
// @ts-expect-error TS2345
|
|
293
329
|
if (targets[name] == null || _semver().default.gt(targets[name], semverVersion)) {
|
|
330
|
+
// @ts-expect-error TS7053
|
|
294
331
|
targets[name] = semverVersion;
|
|
295
332
|
}
|
|
296
333
|
}
|
|
@@ -305,6 +342,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
305
342
|
if (options.env.NODE_ENV != null) {
|
|
306
343
|
env.NODE_ENV = options.env.NODE_ENV;
|
|
307
344
|
}
|
|
345
|
+
if (process.env.ATLASPACK_BUILD_ENV === 'test') {
|
|
346
|
+
env.ATLASPACK_BUILD_ENV = 'test';
|
|
347
|
+
}
|
|
308
348
|
} else if (Array.isArray(config === null || config === void 0 ? void 0 : config.inlineEnvironment)) {
|
|
309
349
|
for (let match of (0, _utils().globMatch)(Object.keys(options.env), config.inlineEnvironment)) {
|
|
310
350
|
env[match] = String(options.env[match]);
|
|
@@ -322,23 +362,40 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
322
362
|
if (asset.type === 'ts') {
|
|
323
363
|
isJSX = false;
|
|
324
364
|
} else if (!isJSX) {
|
|
365
|
+
// @ts-expect-error TS7053
|
|
325
366
|
isJSX = Boolean(JSX_EXTENSIONS[asset.type]);
|
|
326
367
|
}
|
|
327
368
|
}
|
|
328
369
|
let macroAssets = [];
|
|
329
370
|
let {
|
|
371
|
+
// @ts-expect-error TS2339
|
|
330
372
|
dependencies,
|
|
373
|
+
// @ts-expect-error TS2339
|
|
331
374
|
code: compiledCode,
|
|
375
|
+
// @ts-expect-error TS2339
|
|
332
376
|
map,
|
|
377
|
+
// @ts-expect-error TS2339
|
|
333
378
|
shebang,
|
|
379
|
+
// @ts-expect-error TS2339
|
|
334
380
|
hoist_result,
|
|
381
|
+
// @ts-expect-error TS2339
|
|
335
382
|
symbol_result,
|
|
383
|
+
// @ts-expect-error TS2339
|
|
384
|
+
is_empty_or_empty_export,
|
|
385
|
+
// @ts-expect-error TS2339
|
|
336
386
|
needs_esm_helpers,
|
|
387
|
+
// @ts-expect-error TS2339
|
|
337
388
|
diagnostics,
|
|
389
|
+
// @ts-expect-error TS2339
|
|
338
390
|
used_env,
|
|
391
|
+
// @ts-expect-error TS2339
|
|
339
392
|
has_node_replacements,
|
|
393
|
+
// @ts-expect-error TS2339
|
|
340
394
|
is_constant_module,
|
|
341
|
-
|
|
395
|
+
// @ts-expect-error TS2339
|
|
396
|
+
conditions,
|
|
397
|
+
// @ts-expect-error TS2339
|
|
398
|
+
magic_comments
|
|
342
399
|
} = await (_rust().transformAsync || _rust().transform)({
|
|
343
400
|
filename: asset.filePath,
|
|
344
401
|
code,
|
|
@@ -349,7 +406,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
349
406
|
insert_node_globals: !asset.env.isNode() && asset.env.sourceType !== 'script',
|
|
350
407
|
node_replacer: asset.env.isNode(),
|
|
351
408
|
is_browser: asset.env.isBrowser(),
|
|
352
|
-
is_worker: asset.env.isWorker(),
|
|
409
|
+
is_worker: asset.env.isWorker() || asset.env.isTesseract(),
|
|
353
410
|
env,
|
|
354
411
|
is_type_script: asset.type === 'ts' || asset.type === 'tsx',
|
|
355
412
|
is_jsx: isJSX,
|
|
@@ -358,7 +415,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
358
415
|
automatic_jsx_runtime: Boolean(config === null || config === void 0 ? void 0 : config.automaticJSXRuntime),
|
|
359
416
|
jsx_import_source: config === null || config === void 0 ? void 0 : config.jsxImportSource,
|
|
360
417
|
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),
|
|
418
|
+
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
419
|
decorators: Boolean(config === null || config === void 0 ? void 0 : config.decorators),
|
|
363
420
|
use_define_for_class_fields: Boolean(config === null || config === void 0 ? void 0 : config.useDefineForClassFields),
|
|
364
421
|
targets,
|
|
@@ -373,16 +430,21 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
373
430
|
standalone: asset.query.has('standalone'),
|
|
374
431
|
inline_constants: config.inlineConstants,
|
|
375
432
|
conditional_bundling: options.featureFlags.conditionalBundlingApi,
|
|
376
|
-
|
|
433
|
+
hmr_improvements: options.featureFlags.hmrImprovements,
|
|
434
|
+
add_display_name: Boolean(config.addReactDisplayName),
|
|
435
|
+
exports_rebinding_optimisation: options.featureFlags.exportsRebindingOptimisation,
|
|
436
|
+
magic_comments: Boolean(config === null || config === void 0 ? void 0 : config.magicComments) || (0, _featureFlags().getFeatureFlag)('supportWebpackChunkName'),
|
|
437
|
+
is_source: asset.isSource,
|
|
438
|
+
enable_global_this_aliaser: Boolean(config.enableGlobalThisAliaser),
|
|
439
|
+
enable_lazy_loading_transformer: Boolean(config.enableLazyLoadingTransformer),
|
|
440
|
+
nested_promise_import_fix: options.featureFlags.nestedPromiseImportFix,
|
|
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++) {
|
|
@@ -428,7 +491,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
428
491
|
if (originalMap) {
|
|
429
492
|
map.extends(originalMap);
|
|
430
493
|
} else {
|
|
431
|
-
|
|
494
|
+
if (!(0, _featureFlags().getFeatureFlag)('omitSourcesContentInMemory')) {
|
|
495
|
+
map.setSourceContent(asset.filePath, code.toString());
|
|
496
|
+
}
|
|
432
497
|
}
|
|
433
498
|
}
|
|
434
499
|
macroAssets.push({
|
|
@@ -457,7 +522,8 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
457
522
|
invalidateOnBuild() {
|
|
458
523
|
asset.invalidateOnBuild();
|
|
459
524
|
}
|
|
460
|
-
}
|
|
525
|
+
};
|
|
526
|
+
return mod[exportName].apply(ctx, args);
|
|
461
527
|
} else {
|
|
462
528
|
throw new Error(`"${exportName}" in "${src}" is not a function.`);
|
|
463
529
|
}
|
|
@@ -479,11 +545,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
479
545
|
} : null
|
|
480
546
|
});
|
|
481
547
|
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
|
-
}));
|
|
548
|
+
asset.meta.conditions = conditions;
|
|
487
549
|
}
|
|
488
550
|
if (is_constant_module) {
|
|
489
551
|
asset.meta.isConstantModule = true;
|
|
@@ -503,17 +565,22 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
503
565
|
|
|
504
566
|
// If there is an original source map, use it to remap to the original source location.
|
|
505
567
|
if (originalMap) {
|
|
506
|
-
location = (0, _utils().remapSourceLocation)(location, originalMap);
|
|
568
|
+
location = (0, _utils().remapSourceLocation)(location, originalMap, options.projectRoot);
|
|
507
569
|
}
|
|
508
570
|
return location;
|
|
509
571
|
};
|
|
510
572
|
if (diagnostics) {
|
|
511
|
-
let errors = diagnostics.filter(
|
|
512
|
-
|
|
573
|
+
let errors = diagnostics.filter(
|
|
574
|
+
// @ts-expect-error TS7006
|
|
575
|
+
d => d.severity === 'Error' || d.severity === 'SourceError' && asset.isSource);
|
|
576
|
+
let warnings = diagnostics.filter(
|
|
577
|
+
// @ts-expect-error TS7006
|
|
578
|
+
d => d.severity === 'Warning' || d.severity === 'SourceError' && !asset.isSource);
|
|
513
579
|
let convertDiagnostic = diagnostic => {
|
|
514
580
|
var _diagnostic$code_high;
|
|
515
581
|
let message = diagnostic.message;
|
|
516
582
|
if (message === 'SCRIPT_ERROR') {
|
|
583
|
+
// @ts-expect-error TS7053
|
|
517
584
|
let err = SCRIPT_ERRORS[asset.env.context];
|
|
518
585
|
message = (err === null || err === void 0 ? void 0 : err.message) || SCRIPT_ERRORS.browser.message;
|
|
519
586
|
}
|
|
@@ -536,6 +603,8 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
536
603
|
codeHighlights: [(0, _diagnostic().convertSourceLocationToHighlight)(asset.env.loc, 'The environment was originally created here')]
|
|
537
604
|
});
|
|
538
605
|
}
|
|
606
|
+
|
|
607
|
+
// @ts-expect-error TS7053
|
|
539
608
|
let err = SCRIPT_ERRORS[asset.env.context];
|
|
540
609
|
if (err) {
|
|
541
610
|
if (!res.hints) {
|
|
@@ -580,6 +649,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
580
649
|
env: {
|
|
581
650
|
context: 'web-worker',
|
|
582
651
|
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
|
|
652
|
+
// @ts-expect-error TS2322
|
|
583
653
|
outputFormat,
|
|
584
654
|
loc
|
|
585
655
|
},
|
|
@@ -629,6 +699,13 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
629
699
|
});
|
|
630
700
|
} else if (dep.kind === 'File') {
|
|
631
701
|
asset.invalidateOnFileChange(dep.specifier);
|
|
702
|
+
} else if (dep.kind === 'Id') {
|
|
703
|
+
// Record parcelRequire calls so that the dev packager can add them as dependencies.
|
|
704
|
+
// This allows the HMR runtime to collect parents across async boundaries (through runtimes).
|
|
705
|
+
// TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
|
|
706
|
+
asset.meta.hmrDeps ??= [];
|
|
707
|
+
(0, _assert().default)(Array.isArray(asset.meta.hmrDeps));
|
|
708
|
+
asset.meta.hmrDeps.push(dep.specifier);
|
|
632
709
|
} else {
|
|
633
710
|
let meta = {
|
|
634
711
|
kind: dep.kind
|
|
@@ -676,6 +753,12 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
676
753
|
outputFormat,
|
|
677
754
|
loc: convertLoc(dep.loc)
|
|
678
755
|
};
|
|
756
|
+
if ((0, _featureFlags().getFeatureFlag)('supportWebpackChunkName')) {
|
|
757
|
+
let chunkName = magic_comments[dep.specifier];
|
|
758
|
+
if (chunkName) {
|
|
759
|
+
meta.chunkName = chunkName;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
679
762
|
}
|
|
680
763
|
|
|
681
764
|
// Always bundle helpers, even with includeNodeModules: false, except if this is a library.
|
|
@@ -695,6 +778,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
695
778
|
idx = dep.specifier.indexOf('/', idx + 1);
|
|
696
779
|
}
|
|
697
780
|
let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
|
|
781
|
+
// @ts-expect-error TS7053
|
|
698
782
|
range = _package.default.dependencies[module];
|
|
699
783
|
}
|
|
700
784
|
asset.addDependency({
|
|
@@ -706,6 +790,7 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
706
790
|
meta,
|
|
707
791
|
resolveFrom: isHelper ? __filename : undefined,
|
|
708
792
|
range,
|
|
793
|
+
// @ts-expect-error TS2322
|
|
709
794
|
env
|
|
710
795
|
});
|
|
711
796
|
}
|
|
@@ -717,10 +802,12 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
717
802
|
exported,
|
|
718
803
|
local,
|
|
719
804
|
loc,
|
|
720
|
-
is_esm
|
|
805
|
+
is_esm,
|
|
806
|
+
is_static_binding_safe
|
|
721
807
|
} of hoist_result.exported_symbols) {
|
|
722
808
|
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
723
|
-
isEsm: is_esm
|
|
809
|
+
isEsm: is_esm,
|
|
810
|
+
isStaticBindingSafe: is_static_binding_safe
|
|
724
811
|
});
|
|
725
812
|
}
|
|
726
813
|
|
|
@@ -802,6 +889,9 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
802
889
|
// (and the asset has side effects), or the asset is wrapped.
|
|
803
890
|
// This allows accessing symbols that don't exist without errors in symbol propagation.
|
|
804
891
|
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('*')) {
|
|
892
|
+
if (is_empty_or_empty_export) {
|
|
893
|
+
asset.meta.emptyFileStarReexport = true;
|
|
894
|
+
}
|
|
805
895
|
asset.symbols.set('*', `$${asset.id}$exports`);
|
|
806
896
|
}
|
|
807
897
|
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.300+2f3af9374",
|
|
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.368+2f3af9374",
|
|
28
|
+
"@atlaspack/feature-flags": "2.14.1-canary.368+2f3af9374",
|
|
29
|
+
"@atlaspack/plugin": "2.14.5-canary.300+2f3af9374",
|
|
30
|
+
"@atlaspack/rust": "3.2.1-canary.300+2f3af9374",
|
|
31
|
+
"@atlaspack/source-map": "3.1.1-canary.4079+2f3af9374",
|
|
32
|
+
"@atlaspack/utils": "2.14.5-canary.300+2f3af9374",
|
|
33
|
+
"@atlaspack/workers": "2.14.5-canary.300+2f3af9374",
|
|
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": "2f3af9374bcc082e6f4835f6f5a2b98e32ea45b4"
|
|
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,21 @@ 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(
|
|
297
314
|
CONFIG_SCHEMA,
|
|
298
315
|
{
|
|
299
316
|
data: conf.contents,
|
|
300
|
-
|
|
301
|
-
source: await options.inputFS.readFile(conf.filePath, 'utf8'),
|
|
317
|
+
source: () => options.inputFS.readFileSync(conf.filePath, 'utf8'),
|
|
302
318
|
filePath: conf.filePath,
|
|
303
319
|
prependKey: `/${encodeJSONKeyComponent('@atlaspack/transformer-js')}`,
|
|
304
320
|
},
|
|
@@ -307,10 +323,17 @@ export default (new Transformer({
|
|
|
307
323
|
'Invalid config for @atlaspack/transformer-js',
|
|
308
324
|
);
|
|
309
325
|
|
|
326
|
+
addReactDisplayName =
|
|
327
|
+
// @ts-expect-error TS2339
|
|
328
|
+
conf.contents?.addReactDisplayName ?? addReactDisplayName;
|
|
329
|
+
// @ts-expect-error TS2339
|
|
310
330
|
magicComments = conf.contents?.magicComments ?? magicComments;
|
|
331
|
+
// @ts-expect-error TS2339
|
|
311
332
|
inlineEnvironment = conf.contents?.inlineEnvironment ?? inlineEnvironment;
|
|
333
|
+
// @ts-expect-error TS2339
|
|
312
334
|
inlineFS = conf.contents?.inlineFS ?? inlineFS;
|
|
313
335
|
inlineConstants =
|
|
336
|
+
// @ts-expect-error TS2339
|
|
314
337
|
conf.contents?.unstable_inlineConstants ?? inlineConstants;
|
|
315
338
|
}
|
|
316
339
|
|
|
@@ -323,10 +346,13 @@ export default (new Transformer({
|
|
|
323
346
|
inlineEnvironment,
|
|
324
347
|
inlineFS,
|
|
325
348
|
inlineConstants,
|
|
349
|
+
addReactDisplayName,
|
|
326
350
|
reactRefresh,
|
|
327
351
|
decorators,
|
|
328
352
|
useDefineForClassFields,
|
|
329
353
|
magicComments,
|
|
354
|
+
enableGlobalThisAliaser,
|
|
355
|
+
enableLazyLoadingTransformer,
|
|
330
356
|
};
|
|
331
357
|
},
|
|
332
358
|
async transform({asset, config, options, logger}) {
|
|
@@ -357,6 +383,7 @@ export default (new Transformer({
|
|
|
357
383
|
for (let browser of browsers) {
|
|
358
384
|
let [name, version] = browser.split(' ');
|
|
359
385
|
if (BROWSER_MAPPING.hasOwnProperty(name)) {
|
|
386
|
+
// @ts-expect-error TS7053
|
|
360
387
|
name = BROWSER_MAPPING[name];
|
|
361
388
|
if (!name) {
|
|
362
389
|
continue;
|
|
@@ -366,12 +393,15 @@ export default (new Transformer({
|
|
|
366
393
|
let [major, minor = '0', patch = '0'] = version
|
|
367
394
|
.split('-')[0]
|
|
368
395
|
.split('.');
|
|
396
|
+
// @ts-expect-error TS2345
|
|
369
397
|
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
|
|
370
398
|
continue;
|
|
371
399
|
}
|
|
372
400
|
let semverVersion = `${major}.${minor}.${patch}`;
|
|
373
401
|
|
|
402
|
+
// @ts-expect-error TS2345
|
|
374
403
|
if (targets[name] == null || semver.gt(targets[name], semverVersion)) {
|
|
404
|
+
// @ts-expect-error TS7053
|
|
375
405
|
targets[name] = semverVersion;
|
|
376
406
|
}
|
|
377
407
|
}
|
|
@@ -411,24 +441,47 @@ export default (new Transformer({
|
|
|
411
441
|
if (asset.type === 'ts') {
|
|
412
442
|
isJSX = false;
|
|
413
443
|
} else if (!isJSX) {
|
|
444
|
+
// @ts-expect-error TS7053
|
|
414
445
|
isJSX = Boolean(JSX_EXTENSIONS[asset.type]);
|
|
415
446
|
}
|
|
416
447
|
}
|
|
417
448
|
|
|
418
|
-
let macroAssets
|
|
449
|
+
let macroAssets: Array<{
|
|
450
|
+
content: string;
|
|
451
|
+
// @ts-expect-error TS2552
|
|
452
|
+
map: undefined | NodeSourceMap;
|
|
453
|
+
type: string;
|
|
454
|
+
uniqueKey: string;
|
|
455
|
+
}> = [];
|
|
419
456
|
let {
|
|
457
|
+
// @ts-expect-error TS2339
|
|
420
458
|
dependencies,
|
|
459
|
+
// @ts-expect-error TS2339
|
|
421
460
|
code: compiledCode,
|
|
461
|
+
// @ts-expect-error TS2339
|
|
422
462
|
map,
|
|
463
|
+
// @ts-expect-error TS2339
|
|
423
464
|
shebang,
|
|
465
|
+
// @ts-expect-error TS2339
|
|
424
466
|
hoist_result,
|
|
467
|
+
// @ts-expect-error TS2339
|
|
425
468
|
symbol_result,
|
|
469
|
+
// @ts-expect-error TS2339
|
|
470
|
+
is_empty_or_empty_export,
|
|
471
|
+
// @ts-expect-error TS2339
|
|
426
472
|
needs_esm_helpers,
|
|
473
|
+
// @ts-expect-error TS2339
|
|
427
474
|
diagnostics,
|
|
475
|
+
// @ts-expect-error TS2339
|
|
428
476
|
used_env,
|
|
477
|
+
// @ts-expect-error TS2339
|
|
429
478
|
has_node_replacements,
|
|
479
|
+
// @ts-expect-error TS2339
|
|
430
480
|
is_constant_module,
|
|
481
|
+
// @ts-expect-error TS2339
|
|
431
482
|
conditions,
|
|
483
|
+
// @ts-expect-error TS2339
|
|
484
|
+
magic_comments,
|
|
432
485
|
} = await (transformAsync || transform)({
|
|
433
486
|
filename: asset.filePath,
|
|
434
487
|
code,
|
|
@@ -440,7 +493,7 @@ export default (new Transformer({
|
|
|
440
493
|
!asset.env.isNode() && asset.env.sourceType !== 'script',
|
|
441
494
|
node_replacer: asset.env.isNode(),
|
|
442
495
|
is_browser: asset.env.isBrowser(),
|
|
443
|
-
is_worker: asset.env.isWorker(),
|
|
496
|
+
is_worker: asset.env.isWorker() || asset.env.isTesseract(),
|
|
444
497
|
env,
|
|
445
498
|
is_type_script: asset.type === 'ts' || asset.type === 'tsx',
|
|
446
499
|
is_jsx: isJSX,
|
|
@@ -453,6 +506,7 @@ export default (new Transformer({
|
|
|
453
506
|
asset.env.isBrowser() &&
|
|
454
507
|
!asset.env.isLibrary &&
|
|
455
508
|
!asset.env.isWorker() &&
|
|
509
|
+
!asset.env.isTesseract() &&
|
|
456
510
|
!asset.env.isWorklet() &&
|
|
457
511
|
Boolean(config?.reactRefresh),
|
|
458
512
|
decorators: Boolean(config?.decorators),
|
|
@@ -470,9 +524,21 @@ export default (new Transformer({
|
|
|
470
524
|
standalone: asset.query.has('standalone'),
|
|
471
525
|
inline_constants: config.inlineConstants,
|
|
472
526
|
conditional_bundling: options.featureFlags.conditionalBundlingApi,
|
|
473
|
-
|
|
527
|
+
hmr_improvements: options.featureFlags.hmrImprovements,
|
|
528
|
+
add_display_name: Boolean(config.addReactDisplayName),
|
|
529
|
+
exports_rebinding_optimisation:
|
|
530
|
+
options.featureFlags.exportsRebindingOptimisation,
|
|
531
|
+
magic_comments:
|
|
532
|
+
Boolean(config?.magicComments) ||
|
|
533
|
+
getFeatureFlag('supportWebpackChunkName'),
|
|
534
|
+
is_source: asset.isSource,
|
|
535
|
+
enable_global_this_aliaser: Boolean(config.enableGlobalThisAliaser),
|
|
536
|
+
enable_lazy_loading_transformer: Boolean(
|
|
537
|
+
config.enableLazyLoadingTransformer,
|
|
538
|
+
),
|
|
539
|
+
nested_promise_import_fix: options.featureFlags.nestedPromiseImportFix,
|
|
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') {
|
|
@@ -530,7 +596,9 @@ export default (new Transformer({
|
|
|
530
596
|
if (originalMap) {
|
|
531
597
|
map.extends(originalMap);
|
|
532
598
|
} else {
|
|
533
|
-
|
|
599
|
+
if (!getFeatureFlag('omitSourcesContentInMemory')) {
|
|
600
|
+
map.setSourceContent(asset.filePath, code.toString());
|
|
601
|
+
}
|
|
534
602
|
}
|
|
535
603
|
}
|
|
536
604
|
|
|
@@ -546,13 +614,13 @@ export default (new Transformer({
|
|
|
546
614
|
specifierType: 'esm',
|
|
547
615
|
});
|
|
548
616
|
},
|
|
549
|
-
invalidateOnFileChange(filePath) {
|
|
617
|
+
invalidateOnFileChange(filePath: FilePath) {
|
|
550
618
|
asset.invalidateOnFileChange(filePath);
|
|
551
619
|
},
|
|
552
|
-
invalidateOnFileCreate(invalidation) {
|
|
620
|
+
invalidateOnFileCreate(invalidation: FileCreateInvalidation) {
|
|
553
621
|
asset.invalidateOnFileCreate(invalidation);
|
|
554
622
|
},
|
|
555
|
-
invalidateOnEnvChange(env) {
|
|
623
|
+
invalidateOnEnvChange(env: string) {
|
|
556
624
|
asset.invalidateOnEnvChange(env);
|
|
557
625
|
},
|
|
558
626
|
invalidateOnStartup() {
|
|
@@ -569,7 +637,7 @@ export default (new Transformer({
|
|
|
569
637
|
`"${exportName}" in "${src}" is not a function.`,
|
|
570
638
|
);
|
|
571
639
|
}
|
|
572
|
-
} catch (err) {
|
|
640
|
+
} catch (err: any) {
|
|
573
641
|
// Remove atlaspack core from stack and build string so Rust can process errors more easily.
|
|
574
642
|
let stack = (err.stack || '').split('\n').slice(1);
|
|
575
643
|
let message = err.message;
|
|
@@ -589,18 +657,14 @@ export default (new Transformer({
|
|
|
589
657
|
});
|
|
590
658
|
|
|
591
659
|
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
|
-
}));
|
|
660
|
+
asset.meta.conditions = conditions;
|
|
597
661
|
}
|
|
598
662
|
|
|
599
663
|
if (is_constant_module) {
|
|
600
664
|
asset.meta.isConstantModule = true;
|
|
601
665
|
}
|
|
602
666
|
|
|
603
|
-
let convertLoc = (loc): SourceLocation => {
|
|
667
|
+
let convertLoc = (loc: any): SourceLocation => {
|
|
604
668
|
let location = {
|
|
605
669
|
filePath: asset.filePath,
|
|
606
670
|
start: {
|
|
@@ -615,7 +679,11 @@ export default (new Transformer({
|
|
|
615
679
|
|
|
616
680
|
// If there is an original source map, use it to remap to the original source location.
|
|
617
681
|
if (originalMap) {
|
|
618
|
-
location = remapSourceLocation(
|
|
682
|
+
location = remapSourceLocation(
|
|
683
|
+
location,
|
|
684
|
+
originalMap,
|
|
685
|
+
options.projectRoot,
|
|
686
|
+
);
|
|
619
687
|
}
|
|
620
688
|
|
|
621
689
|
return location;
|
|
@@ -623,19 +691,22 @@ export default (new Transformer({
|
|
|
623
691
|
|
|
624
692
|
if (diagnostics) {
|
|
625
693
|
let errors = diagnostics.filter(
|
|
694
|
+
// @ts-expect-error TS7006
|
|
626
695
|
(d) =>
|
|
627
696
|
d.severity === 'Error' ||
|
|
628
697
|
(d.severity === 'SourceError' && asset.isSource),
|
|
629
698
|
);
|
|
630
699
|
let warnings = diagnostics.filter(
|
|
700
|
+
// @ts-expect-error TS7006
|
|
631
701
|
(d) =>
|
|
632
702
|
d.severity === 'Warning' ||
|
|
633
703
|
(d.severity === 'SourceError' && !asset.isSource),
|
|
634
704
|
);
|
|
635
|
-
let convertDiagnostic = (diagnostic) => {
|
|
705
|
+
let convertDiagnostic = (diagnostic: any) => {
|
|
636
706
|
let message = diagnostic.message;
|
|
637
707
|
if (message === 'SCRIPT_ERROR') {
|
|
638
|
-
|
|
708
|
+
// @ts-expect-error TS7053
|
|
709
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
639
710
|
message = err?.message || SCRIPT_ERRORS.browser.message;
|
|
640
711
|
}
|
|
641
712
|
|
|
@@ -644,11 +715,12 @@ export default (new Transformer({
|
|
|
644
715
|
codeFrames: [
|
|
645
716
|
{
|
|
646
717
|
filePath: asset.filePath,
|
|
647
|
-
codeHighlights: diagnostic.code_highlights?.map(
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
718
|
+
codeHighlights: diagnostic.code_highlights?.map(
|
|
719
|
+
(highlight: any) =>
|
|
720
|
+
convertSourceLocationToHighlight(
|
|
721
|
+
convertLoc(highlight.loc),
|
|
722
|
+
highlight.message ?? undefined,
|
|
723
|
+
),
|
|
652
724
|
),
|
|
653
725
|
},
|
|
654
726
|
],
|
|
@@ -672,7 +744,8 @@ export default (new Transformer({
|
|
|
672
744
|
});
|
|
673
745
|
}
|
|
674
746
|
|
|
675
|
-
|
|
747
|
+
// @ts-expect-error TS7053
|
|
748
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
676
749
|
if (err) {
|
|
677
750
|
if (!res.hints) {
|
|
678
751
|
res.hints = [err.hint];
|
|
@@ -729,6 +802,7 @@ export default (new Transformer({
|
|
|
729
802
|
env: {
|
|
730
803
|
context: 'web-worker',
|
|
731
804
|
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
|
|
805
|
+
// @ts-expect-error TS2322
|
|
732
806
|
outputFormat,
|
|
733
807
|
loc,
|
|
734
808
|
},
|
|
@@ -776,6 +850,13 @@ export default (new Transformer({
|
|
|
776
850
|
});
|
|
777
851
|
} else if (dep.kind === 'File') {
|
|
778
852
|
asset.invalidateOnFileChange(dep.specifier);
|
|
853
|
+
} else if (dep.kind === 'Id') {
|
|
854
|
+
// Record parcelRequire calls so that the dev packager can add them as dependencies.
|
|
855
|
+
// This allows the HMR runtime to collect parents across async boundaries (through runtimes).
|
|
856
|
+
// TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
|
|
857
|
+
asset.meta.hmrDeps ??= [];
|
|
858
|
+
invariant(Array.isArray(asset.meta.hmrDeps));
|
|
859
|
+
asset.meta.hmrDeps.push(dep.specifier);
|
|
779
860
|
} else {
|
|
780
861
|
let meta: JSONObject = {kind: dep.kind};
|
|
781
862
|
if (dep.attributes) {
|
|
@@ -839,6 +920,13 @@ export default (new Transformer({
|
|
|
839
920
|
outputFormat,
|
|
840
921
|
loc: convertLoc(dep.loc),
|
|
841
922
|
};
|
|
923
|
+
|
|
924
|
+
if (getFeatureFlag('supportWebpackChunkName')) {
|
|
925
|
+
let chunkName = magic_comments[dep.specifier];
|
|
926
|
+
if (chunkName) {
|
|
927
|
+
meta.chunkName = chunkName;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
842
930
|
}
|
|
843
931
|
|
|
844
932
|
// Always bundle helpers, even with includeNodeModules: false, except if this is a library.
|
|
@@ -863,6 +951,7 @@ export default (new Transformer({
|
|
|
863
951
|
idx = dep.specifier.indexOf('/', idx + 1);
|
|
864
952
|
}
|
|
865
953
|
let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
|
|
954
|
+
// @ts-expect-error TS7053
|
|
866
955
|
range = pkg.dependencies[module];
|
|
867
956
|
}
|
|
868
957
|
|
|
@@ -874,12 +963,13 @@ export default (new Transformer({
|
|
|
874
963
|
dep.kind === 'DynamicImport'
|
|
875
964
|
? 'lazy'
|
|
876
965
|
: dep.kind === 'ConditionalImport'
|
|
877
|
-
|
|
878
|
-
|
|
966
|
+
? 'conditional'
|
|
967
|
+
: 'sync',
|
|
879
968
|
isOptional: dep.is_optional,
|
|
880
969
|
meta,
|
|
881
970
|
resolveFrom: isHelper ? __filename : undefined,
|
|
882
971
|
range,
|
|
972
|
+
// @ts-expect-error TS2322
|
|
883
973
|
env,
|
|
884
974
|
});
|
|
885
975
|
}
|
|
@@ -893,8 +983,12 @@ export default (new Transformer({
|
|
|
893
983
|
local,
|
|
894
984
|
loc,
|
|
895
985
|
is_esm,
|
|
986
|
+
is_static_binding_safe,
|
|
896
987
|
} of hoist_result.exported_symbols) {
|
|
897
|
-
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
988
|
+
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
989
|
+
isEsm: is_esm,
|
|
990
|
+
isStaticBindingSafe: is_static_binding_safe,
|
|
991
|
+
});
|
|
898
992
|
}
|
|
899
993
|
|
|
900
994
|
// deps is a map of dependencies that are keyed by placeholder or specifier
|
|
@@ -991,6 +1085,9 @@ export default (new Transformer({
|
|
|
991
1085
|
Object.keys(hoist_result.exported_symbols).length === 0) ||
|
|
992
1086
|
(hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
|
|
993
1087
|
) {
|
|
1088
|
+
if (is_empty_or_empty_export) {
|
|
1089
|
+
asset.meta.emptyFileStarReexport = true;
|
|
1090
|
+
}
|
|
994
1091
|
asset.symbols.set('*', `$${asset.id}$exports`);
|
|
995
1092
|
}
|
|
996
1093
|
|
|
@@ -1093,4 +1190,4 @@ export default (new Transformer({
|
|
|
1093
1190
|
|
|
1094
1191
|
return [asset, ...macroAssets];
|
|
1095
1192
|
},
|
|
1096
|
-
})
|
|
1193
|
+
}) as Transformer<unknown>;
|