@atlaspack/transformer-js 3.2.3-canary.29 → 3.2.3-canary.292
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} +186 -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: (0, _featureFlags().getFeatureFlag)('schemaValidationDeferSourceLoading') ? () => options.inputFS.readFileSync(conf.filePath, 'utf8') : await options.inputFS.readFile(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.292+98b80ebe1",
|
|
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.360+98b80ebe1",
|
|
28
|
+
"@atlaspack/feature-flags": "2.14.1-canary.360+98b80ebe1",
|
|
29
|
+
"@atlaspack/plugin": "2.14.5-canary.292+98b80ebe1",
|
|
30
|
+
"@atlaspack/rust": "3.2.1-canary.292+98b80ebe1",
|
|
31
|
+
"@atlaspack/source-map": "3.1.1-canary.4071+98b80ebe1",
|
|
32
|
+
"@atlaspack/utils": "2.14.5-canary.292+98b80ebe1",
|
|
33
|
+
"@atlaspack/workers": "2.14.5-canary.292+98b80ebe1",
|
|
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": "98b80ebe1e2b1114dd1d3f076dc28a70aaa86aaa"
|
|
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 = 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
|
-
|
|
317
|
+
source: getFeatureFlag('schemaValidationDeferSourceLoading')
|
|
318
|
+
? () => options.inputFS.readFileSync(conf.filePath, 'utf8')
|
|
319
|
+
: await options.inputFS.readFile(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,13 @@ 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,
|
|
330
358
|
};
|
|
331
359
|
},
|
|
332
360
|
async transform({asset, config, options, logger}) {
|
|
@@ -357,6 +385,7 @@ export default (new Transformer({
|
|
|
357
385
|
for (let browser of browsers) {
|
|
358
386
|
let [name, version] = browser.split(' ');
|
|
359
387
|
if (BROWSER_MAPPING.hasOwnProperty(name)) {
|
|
388
|
+
// @ts-expect-error TS7053
|
|
360
389
|
name = BROWSER_MAPPING[name];
|
|
361
390
|
if (!name) {
|
|
362
391
|
continue;
|
|
@@ -366,12 +395,15 @@ export default (new Transformer({
|
|
|
366
395
|
let [major, minor = '0', patch = '0'] = version
|
|
367
396
|
.split('-')[0]
|
|
368
397
|
.split('.');
|
|
398
|
+
// @ts-expect-error TS2345
|
|
369
399
|
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
|
|
370
400
|
continue;
|
|
371
401
|
}
|
|
372
402
|
let semverVersion = `${major}.${minor}.${patch}`;
|
|
373
403
|
|
|
404
|
+
// @ts-expect-error TS2345
|
|
374
405
|
if (targets[name] == null || semver.gt(targets[name], semverVersion)) {
|
|
406
|
+
// @ts-expect-error TS7053
|
|
375
407
|
targets[name] = semverVersion;
|
|
376
408
|
}
|
|
377
409
|
}
|
|
@@ -411,24 +443,47 @@ export default (new Transformer({
|
|
|
411
443
|
if (asset.type === 'ts') {
|
|
412
444
|
isJSX = false;
|
|
413
445
|
} else if (!isJSX) {
|
|
446
|
+
// @ts-expect-error TS7053
|
|
414
447
|
isJSX = Boolean(JSX_EXTENSIONS[asset.type]);
|
|
415
448
|
}
|
|
416
449
|
}
|
|
417
450
|
|
|
418
|
-
let macroAssets
|
|
451
|
+
let macroAssets: Array<{
|
|
452
|
+
content: string;
|
|
453
|
+
// @ts-expect-error TS2552
|
|
454
|
+
map: undefined | NodeSourceMap;
|
|
455
|
+
type: string;
|
|
456
|
+
uniqueKey: string;
|
|
457
|
+
}> = [];
|
|
419
458
|
let {
|
|
459
|
+
// @ts-expect-error TS2339
|
|
420
460
|
dependencies,
|
|
461
|
+
// @ts-expect-error TS2339
|
|
421
462
|
code: compiledCode,
|
|
463
|
+
// @ts-expect-error TS2339
|
|
422
464
|
map,
|
|
465
|
+
// @ts-expect-error TS2339
|
|
423
466
|
shebang,
|
|
467
|
+
// @ts-expect-error TS2339
|
|
424
468
|
hoist_result,
|
|
469
|
+
// @ts-expect-error TS2339
|
|
425
470
|
symbol_result,
|
|
471
|
+
// @ts-expect-error TS2339
|
|
472
|
+
is_empty_or_empty_export,
|
|
473
|
+
// @ts-expect-error TS2339
|
|
426
474
|
needs_esm_helpers,
|
|
475
|
+
// @ts-expect-error TS2339
|
|
427
476
|
diagnostics,
|
|
477
|
+
// @ts-expect-error TS2339
|
|
428
478
|
used_env,
|
|
479
|
+
// @ts-expect-error TS2339
|
|
429
480
|
has_node_replacements,
|
|
481
|
+
// @ts-expect-error TS2339
|
|
430
482
|
is_constant_module,
|
|
483
|
+
// @ts-expect-error TS2339
|
|
431
484
|
conditions,
|
|
485
|
+
// @ts-expect-error TS2339
|
|
486
|
+
magic_comments,
|
|
432
487
|
} = await (transformAsync || transform)({
|
|
433
488
|
filename: asset.filePath,
|
|
434
489
|
code,
|
|
@@ -440,7 +495,7 @@ export default (new Transformer({
|
|
|
440
495
|
!asset.env.isNode() && asset.env.sourceType !== 'script',
|
|
441
496
|
node_replacer: asset.env.isNode(),
|
|
442
497
|
is_browser: asset.env.isBrowser(),
|
|
443
|
-
is_worker: asset.env.isWorker(),
|
|
498
|
+
is_worker: asset.env.isWorker() || asset.env.isTesseract(),
|
|
444
499
|
env,
|
|
445
500
|
is_type_script: asset.type === 'ts' || asset.type === 'tsx',
|
|
446
501
|
is_jsx: isJSX,
|
|
@@ -453,6 +508,7 @@ export default (new Transformer({
|
|
|
453
508
|
asset.env.isBrowser() &&
|
|
454
509
|
!asset.env.isLibrary &&
|
|
455
510
|
!asset.env.isWorker() &&
|
|
511
|
+
!asset.env.isTesseract() &&
|
|
456
512
|
!asset.env.isWorklet() &&
|
|
457
513
|
Boolean(config?.reactRefresh),
|
|
458
514
|
decorators: Boolean(config?.decorators),
|
|
@@ -470,9 +526,21 @@ export default (new Transformer({
|
|
|
470
526
|
standalone: asset.query.has('standalone'),
|
|
471
527
|
inline_constants: config.inlineConstants,
|
|
472
528
|
conditional_bundling: options.featureFlags.conditionalBundlingApi,
|
|
473
|
-
|
|
529
|
+
hmr_improvements: options.featureFlags.hmrImprovements,
|
|
530
|
+
add_display_name: Boolean(config.addReactDisplayName),
|
|
531
|
+
exports_rebinding_optimisation:
|
|
532
|
+
options.featureFlags.exportsRebindingOptimisation,
|
|
533
|
+
magic_comments:
|
|
534
|
+
Boolean(config?.magicComments) ||
|
|
535
|
+
getFeatureFlag('supportWebpackChunkName'),
|
|
536
|
+
is_source: asset.isSource,
|
|
537
|
+
enable_global_this_aliaser: Boolean(config.enableGlobalThisAliaser),
|
|
538
|
+
enable_lazy_loading_transformer: Boolean(
|
|
539
|
+
config.enableLazyLoadingTransformer,
|
|
540
|
+
),
|
|
541
|
+
nested_promise_import_fix: options.featureFlags.nestedPromiseImportFix,
|
|
474
542
|
callMacro: asset.isSource
|
|
475
|
-
? async (err, src, exportName, args, loc) => {
|
|
543
|
+
? async (err: any, src: any, exportName: any, args: any, loc: any) => {
|
|
476
544
|
let mod;
|
|
477
545
|
try {
|
|
478
546
|
mod = await options.packageManager.require(src, asset.filePath);
|
|
@@ -481,7 +549,6 @@ export default (new Transformer({
|
|
|
481
549
|
if (
|
|
482
550
|
exportName === 'default' &&
|
|
483
551
|
!mod.__esModule &&
|
|
484
|
-
// $FlowFixMe
|
|
485
552
|
Object.prototype.toString.call(config) !== '[object Module]'
|
|
486
553
|
) {
|
|
487
554
|
mod = {default: mod};
|
|
@@ -490,7 +557,7 @@ export default (new Transformer({
|
|
|
490
557
|
if (!Object.hasOwnProperty.call(mod, exportName)) {
|
|
491
558
|
throw new Error(`"${src}" does not export "${exportName}".`);
|
|
492
559
|
}
|
|
493
|
-
} catch (err) {
|
|
560
|
+
} catch (err: any) {
|
|
494
561
|
throw {
|
|
495
562
|
kind: 1,
|
|
496
563
|
message: err.message,
|
|
@@ -507,7 +574,8 @@ export default (new Transformer({
|
|
|
507
574
|
if (asset.env.sourceMap) {
|
|
508
575
|
// Generate a source map that maps each line of the asset to the original macro call.
|
|
509
576
|
map = new SourceMap(options.projectRoot);
|
|
510
|
-
|
|
577
|
+
// @ts-expect-error TS2304
|
|
578
|
+
let mappings: Array<IndexedMapping<string>> = [];
|
|
511
579
|
let line = 1;
|
|
512
580
|
for (let i = 0; i <= a.content.length; i++) {
|
|
513
581
|
if (i === a.content.length || a.content[i] === '\n') {
|
|
@@ -530,7 +598,9 @@ export default (new Transformer({
|
|
|
530
598
|
if (originalMap) {
|
|
531
599
|
map.extends(originalMap);
|
|
532
600
|
} else {
|
|
533
|
-
|
|
601
|
+
if (!getFeatureFlag('omitSourcesContentInMemory')) {
|
|
602
|
+
map.setSourceContent(asset.filePath, code.toString());
|
|
603
|
+
}
|
|
534
604
|
}
|
|
535
605
|
}
|
|
536
606
|
|
|
@@ -546,13 +616,13 @@ export default (new Transformer({
|
|
|
546
616
|
specifierType: 'esm',
|
|
547
617
|
});
|
|
548
618
|
},
|
|
549
|
-
invalidateOnFileChange(filePath) {
|
|
619
|
+
invalidateOnFileChange(filePath: FilePath) {
|
|
550
620
|
asset.invalidateOnFileChange(filePath);
|
|
551
621
|
},
|
|
552
|
-
invalidateOnFileCreate(invalidation) {
|
|
622
|
+
invalidateOnFileCreate(invalidation: FileCreateInvalidation) {
|
|
553
623
|
asset.invalidateOnFileCreate(invalidation);
|
|
554
624
|
},
|
|
555
|
-
invalidateOnEnvChange(env) {
|
|
625
|
+
invalidateOnEnvChange(env: string) {
|
|
556
626
|
asset.invalidateOnEnvChange(env);
|
|
557
627
|
},
|
|
558
628
|
invalidateOnStartup() {
|
|
@@ -569,7 +639,7 @@ export default (new Transformer({
|
|
|
569
639
|
`"${exportName}" in "${src}" is not a function.`,
|
|
570
640
|
);
|
|
571
641
|
}
|
|
572
|
-
} catch (err) {
|
|
642
|
+
} catch (err: any) {
|
|
573
643
|
// Remove atlaspack core from stack and build string so Rust can process errors more easily.
|
|
574
644
|
let stack = (err.stack || '').split('\n').slice(1);
|
|
575
645
|
let message = err.message;
|
|
@@ -589,18 +659,14 @@ export default (new Transformer({
|
|
|
589
659
|
});
|
|
590
660
|
|
|
591
661
|
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
|
-
}));
|
|
662
|
+
asset.meta.conditions = conditions;
|
|
597
663
|
}
|
|
598
664
|
|
|
599
665
|
if (is_constant_module) {
|
|
600
666
|
asset.meta.isConstantModule = true;
|
|
601
667
|
}
|
|
602
668
|
|
|
603
|
-
let convertLoc = (loc): SourceLocation => {
|
|
669
|
+
let convertLoc = (loc: any): SourceLocation => {
|
|
604
670
|
let location = {
|
|
605
671
|
filePath: asset.filePath,
|
|
606
672
|
start: {
|
|
@@ -615,7 +681,11 @@ export default (new Transformer({
|
|
|
615
681
|
|
|
616
682
|
// If there is an original source map, use it to remap to the original source location.
|
|
617
683
|
if (originalMap) {
|
|
618
|
-
location = remapSourceLocation(
|
|
684
|
+
location = remapSourceLocation(
|
|
685
|
+
location,
|
|
686
|
+
originalMap,
|
|
687
|
+
options.projectRoot,
|
|
688
|
+
);
|
|
619
689
|
}
|
|
620
690
|
|
|
621
691
|
return location;
|
|
@@ -623,19 +693,22 @@ export default (new Transformer({
|
|
|
623
693
|
|
|
624
694
|
if (diagnostics) {
|
|
625
695
|
let errors = diagnostics.filter(
|
|
696
|
+
// @ts-expect-error TS7006
|
|
626
697
|
(d) =>
|
|
627
698
|
d.severity === 'Error' ||
|
|
628
699
|
(d.severity === 'SourceError' && asset.isSource),
|
|
629
700
|
);
|
|
630
701
|
let warnings = diagnostics.filter(
|
|
702
|
+
// @ts-expect-error TS7006
|
|
631
703
|
(d) =>
|
|
632
704
|
d.severity === 'Warning' ||
|
|
633
705
|
(d.severity === 'SourceError' && !asset.isSource),
|
|
634
706
|
);
|
|
635
|
-
let convertDiagnostic = (diagnostic) => {
|
|
707
|
+
let convertDiagnostic = (diagnostic: any) => {
|
|
636
708
|
let message = diagnostic.message;
|
|
637
709
|
if (message === 'SCRIPT_ERROR') {
|
|
638
|
-
|
|
710
|
+
// @ts-expect-error TS7053
|
|
711
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
639
712
|
message = err?.message || SCRIPT_ERRORS.browser.message;
|
|
640
713
|
}
|
|
641
714
|
|
|
@@ -644,11 +717,12 @@ export default (new Transformer({
|
|
|
644
717
|
codeFrames: [
|
|
645
718
|
{
|
|
646
719
|
filePath: asset.filePath,
|
|
647
|
-
codeHighlights: diagnostic.code_highlights?.map(
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
720
|
+
codeHighlights: diagnostic.code_highlights?.map(
|
|
721
|
+
(highlight: any) =>
|
|
722
|
+
convertSourceLocationToHighlight(
|
|
723
|
+
convertLoc(highlight.loc),
|
|
724
|
+
highlight.message ?? undefined,
|
|
725
|
+
),
|
|
652
726
|
),
|
|
653
727
|
},
|
|
654
728
|
],
|
|
@@ -672,7 +746,8 @@ export default (new Transformer({
|
|
|
672
746
|
});
|
|
673
747
|
}
|
|
674
748
|
|
|
675
|
-
|
|
749
|
+
// @ts-expect-error TS7053
|
|
750
|
+
let err = SCRIPT_ERRORS[asset.env.context as string];
|
|
676
751
|
if (err) {
|
|
677
752
|
if (!res.hints) {
|
|
678
753
|
res.hints = [err.hint];
|
|
@@ -729,6 +804,7 @@ export default (new Transformer({
|
|
|
729
804
|
env: {
|
|
730
805
|
context: 'web-worker',
|
|
731
806
|
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
|
|
807
|
+
// @ts-expect-error TS2322
|
|
732
808
|
outputFormat,
|
|
733
809
|
loc,
|
|
734
810
|
},
|
|
@@ -776,6 +852,13 @@ export default (new Transformer({
|
|
|
776
852
|
});
|
|
777
853
|
} else if (dep.kind === 'File') {
|
|
778
854
|
asset.invalidateOnFileChange(dep.specifier);
|
|
855
|
+
} else if (dep.kind === 'Id') {
|
|
856
|
+
// Record parcelRequire calls so that the dev packager can add them as dependencies.
|
|
857
|
+
// This allows the HMR runtime to collect parents across async boundaries (through runtimes).
|
|
858
|
+
// TODO: ideally this would result as an actual dep in the graph rather than asset.meta.
|
|
859
|
+
asset.meta.hmrDeps ??= [];
|
|
860
|
+
invariant(Array.isArray(asset.meta.hmrDeps));
|
|
861
|
+
asset.meta.hmrDeps.push(dep.specifier);
|
|
779
862
|
} else {
|
|
780
863
|
let meta: JSONObject = {kind: dep.kind};
|
|
781
864
|
if (dep.attributes) {
|
|
@@ -839,6 +922,13 @@ export default (new Transformer({
|
|
|
839
922
|
outputFormat,
|
|
840
923
|
loc: convertLoc(dep.loc),
|
|
841
924
|
};
|
|
925
|
+
|
|
926
|
+
if (getFeatureFlag('supportWebpackChunkName')) {
|
|
927
|
+
let chunkName = magic_comments[dep.specifier];
|
|
928
|
+
if (chunkName) {
|
|
929
|
+
meta.chunkName = chunkName;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
842
932
|
}
|
|
843
933
|
|
|
844
934
|
// Always bundle helpers, even with includeNodeModules: false, except if this is a library.
|
|
@@ -863,6 +953,7 @@ export default (new Transformer({
|
|
|
863
953
|
idx = dep.specifier.indexOf('/', idx + 1);
|
|
864
954
|
}
|
|
865
955
|
let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
|
|
956
|
+
// @ts-expect-error TS7053
|
|
866
957
|
range = pkg.dependencies[module];
|
|
867
958
|
}
|
|
868
959
|
|
|
@@ -874,12 +965,13 @@ export default (new Transformer({
|
|
|
874
965
|
dep.kind === 'DynamicImport'
|
|
875
966
|
? 'lazy'
|
|
876
967
|
: dep.kind === 'ConditionalImport'
|
|
877
|
-
|
|
878
|
-
|
|
968
|
+
? 'conditional'
|
|
969
|
+
: 'sync',
|
|
879
970
|
isOptional: dep.is_optional,
|
|
880
971
|
meta,
|
|
881
972
|
resolveFrom: isHelper ? __filename : undefined,
|
|
882
973
|
range,
|
|
974
|
+
// @ts-expect-error TS2322
|
|
883
975
|
env,
|
|
884
976
|
});
|
|
885
977
|
}
|
|
@@ -893,8 +985,12 @@ export default (new Transformer({
|
|
|
893
985
|
local,
|
|
894
986
|
loc,
|
|
895
987
|
is_esm,
|
|
988
|
+
is_static_binding_safe,
|
|
896
989
|
} of hoist_result.exported_symbols) {
|
|
897
|
-
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
990
|
+
asset.symbols.set(exported, local, convertLoc(loc), {
|
|
991
|
+
isEsm: is_esm,
|
|
992
|
+
isStaticBindingSafe: is_static_binding_safe,
|
|
993
|
+
});
|
|
898
994
|
}
|
|
899
995
|
|
|
900
996
|
// deps is a map of dependencies that are keyed by placeholder or specifier
|
|
@@ -991,6 +1087,9 @@ export default (new Transformer({
|
|
|
991
1087
|
Object.keys(hoist_result.exported_symbols).length === 0) ||
|
|
992
1088
|
(hoist_result.should_wrap && !asset.symbols.hasExportSymbol('*'))
|
|
993
1089
|
) {
|
|
1090
|
+
if (is_empty_or_empty_export) {
|
|
1091
|
+
asset.meta.emptyFileStarReexport = true;
|
|
1092
|
+
}
|
|
994
1093
|
asset.symbols.set('*', `$${asset.id}$exports`);
|
|
995
1094
|
}
|
|
996
1095
|
|
|
@@ -1093,4 +1192,4 @@ export default (new Transformer({
|
|
|
1093
1192
|
|
|
1094
1193
|
return [asset, ...macroAssets];
|
|
1095
1194
|
},
|
|
1096
|
-
})
|
|
1195
|
+
}) as Transformer<unknown>;
|