@atlaspack/transformer-js 8.0.0 → 10.0.0
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 +47 -7
- package/package.json +9 -8
- package/src/JSTransformer.ts +63 -12
package/lib/JSTransformer.js
CHANGED
|
@@ -4,8 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
+
function _buildCache() {
|
|
8
|
+
const data = require("@atlaspack/build-cache");
|
|
9
|
+
_buildCache = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
7
14
|
function _sourceMap() {
|
|
8
|
-
const data = _interopRequireDefault(require("@
|
|
15
|
+
const data = _interopRequireDefault(require("@atlaspack/source-map"));
|
|
9
16
|
_sourceMap = function () {
|
|
10
17
|
return data;
|
|
11
18
|
};
|
|
@@ -149,6 +156,7 @@ const CONFIG_SCHEMA = {
|
|
|
149
156
|
},
|
|
150
157
|
additionalProperties: false
|
|
151
158
|
};
|
|
159
|
+
const configCache = (0, _buildCache().createBuildCache)();
|
|
152
160
|
const SCRIPT_ERRORS = {
|
|
153
161
|
browser: {
|
|
154
162
|
message: 'Browser scripts cannot have imports or exports.',
|
|
@@ -243,13 +251,41 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
243
251
|
let inlineConstants = false;
|
|
244
252
|
let magicComments = false;
|
|
245
253
|
let addReactDisplayName = false;
|
|
246
|
-
let
|
|
247
|
-
|
|
254
|
+
let syncDynamicImportConfig;
|
|
255
|
+
if (config.env.isTesseract() && options.env.SYNC_DYNAMIC_IMPORT_CONFIG) {
|
|
256
|
+
try {
|
|
257
|
+
let config = configCache.get('SYNC_DYNAMIC_IMPORT_CONFIG');
|
|
258
|
+
if (!config) {
|
|
259
|
+
var _config;
|
|
260
|
+
config = JSON.parse(options.env.SYNC_DYNAMIC_IMPORT_CONFIG);
|
|
261
|
+
(0, _assert().default)(typeof ((_config = config) === null || _config === void 0 ? void 0 : _config.entrypoint_filepath_suffix) === 'string');
|
|
262
|
+
(0, _assert().default)(Array.isArray(config.actual_require_paths));
|
|
263
|
+
configCache.set('SYNC_DYNAMIC_IMPORT_CONFIG', config);
|
|
264
|
+
}
|
|
265
|
+
syncDynamicImportConfig = config;
|
|
266
|
+
} catch {
|
|
267
|
+
// eslint-disable-next-line no-console
|
|
268
|
+
console.warn('Failed to parse SYNC_DYNAMIC_IMPORT_CONFIG to JSON or config shape did not match. Config will not be applied.');
|
|
269
|
+
const fallback = {
|
|
270
|
+
entrypoint_filepath_suffix: '__NO_MATCH__',
|
|
271
|
+
actual_require_paths: []
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
// Set cache to fallback so we don't keep trying to parse.
|
|
275
|
+
configCache.set('SYNC_DYNAMIC_IMPORT_CONFIG', fallback);
|
|
276
|
+
syncDynamicImportConfig = fallback;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
config.invalidateOnEnvChange('SYNC_DYNAMIC_IMPORT_CONFIG');
|
|
280
|
+
let enableGlobalThisAliaser = options.env.NATIVE_GLOBAL_THIS_ALIASER === 'true';
|
|
281
|
+
let enableLazyLoadingTransformer = options.env.NATIVE_LAZY_LOADING_TRANSFORMER === 'true';
|
|
282
|
+
let enableDeadReturnsRemover = options.env.NATIVE_DEAD_RETURNS_REMOVER === 'true';
|
|
283
|
+
let enableUnusedBindingsRemover = options.env.NATIVE_UNUSED_BINDINGS_REMOVER === 'true';
|
|
248
284
|
if (conf && conf.contents) {
|
|
249
285
|
var _conf$contents, _conf$contents2, _conf$contents3, _conf$contents4, _conf$contents5;
|
|
250
286
|
_utils().validateSchema.diagnostic(CONFIG_SCHEMA, {
|
|
251
287
|
data: conf.contents,
|
|
252
|
-
source: (
|
|
288
|
+
source: () => options.inputFS.readFileSync(conf.filePath, 'utf8'),
|
|
253
289
|
filePath: conf.filePath,
|
|
254
290
|
prependKey: `/${(0, _diagnostic().encodeJSONKeyComponent)('@atlaspack/transformer-js')}`
|
|
255
291
|
},
|
|
@@ -283,7 +319,10 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
283
319
|
useDefineForClassFields,
|
|
284
320
|
magicComments,
|
|
285
321
|
enableGlobalThisAliaser,
|
|
286
|
-
enableLazyLoadingTransformer
|
|
322
|
+
enableLazyLoadingTransformer,
|
|
323
|
+
enableDeadReturnsRemover,
|
|
324
|
+
enableUnusedBindingsRemover,
|
|
325
|
+
syncDynamicImportConfig
|
|
287
326
|
};
|
|
288
327
|
},
|
|
289
328
|
async transform({
|
|
@@ -437,7 +476,10 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
437
476
|
is_source: asset.isSource,
|
|
438
477
|
enable_global_this_aliaser: Boolean(config.enableGlobalThisAliaser),
|
|
439
478
|
enable_lazy_loading_transformer: Boolean(config.enableLazyLoadingTransformer),
|
|
479
|
+
sync_dynamic_import_config: config.syncDynamicImportConfig,
|
|
440
480
|
nested_promise_import_fix: options.featureFlags.nestedPromiseImportFix,
|
|
481
|
+
enable_dead_returns_remover: Boolean(config.enableDeadReturnsRemover),
|
|
482
|
+
enable_unused_bindings_remover: Boolean(config.enableUnusedBindingsRemover),
|
|
441
483
|
callMacro: asset.isSource ? async (err, src, exportName, args, loc) => {
|
|
442
484
|
let mod;
|
|
443
485
|
try {
|
|
@@ -489,7 +531,6 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
489
531
|
}
|
|
490
532
|
map.addIndexedMappings(mappings);
|
|
491
533
|
if (originalMap) {
|
|
492
|
-
// @ts-expect-error TS2345
|
|
493
534
|
map.extends(originalMap);
|
|
494
535
|
} else {
|
|
495
536
|
if (!(0, _featureFlags().getFeatureFlag)('omitSourcesContentInMemory')) {
|
|
@@ -975,7 +1016,6 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
975
1016
|
let sourceMap = new (_sourceMap().default)(options.projectRoot);
|
|
976
1017
|
sourceMap.addVLQMap(JSON.parse(map));
|
|
977
1018
|
if (originalMap) {
|
|
978
|
-
// @ts-expect-error TS2345
|
|
979
1019
|
sourceMap.extends(originalMap);
|
|
980
1020
|
}
|
|
981
1021
|
asset.setMap(sourceMap);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/transformer-js",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -24,13 +24,14 @@
|
|
|
24
24
|
"src"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
+
"@atlaspack/build-cache": "2.13.6",
|
|
27
28
|
"@atlaspack/diagnostic": "2.14.4",
|
|
28
|
-
"@atlaspack/feature-flags": "2.
|
|
29
|
-
"@atlaspack/plugin": "2.14.
|
|
30
|
-
"@atlaspack/rust": "3.
|
|
31
|
-
"@
|
|
32
|
-
"@atlaspack/utils": "3.1
|
|
33
|
-
"@atlaspack/workers": "2.14.
|
|
29
|
+
"@atlaspack/feature-flags": "2.27.1",
|
|
30
|
+
"@atlaspack/plugin": "2.14.40",
|
|
31
|
+
"@atlaspack/rust": "3.12.0",
|
|
32
|
+
"@atlaspack/source-map": "3.1.1",
|
|
33
|
+
"@atlaspack/utils": "3.2.1",
|
|
34
|
+
"@atlaspack/workers": "2.14.40",
|
|
34
35
|
"@swc/helpers": "^0.5.15",
|
|
35
36
|
"browserslist": "^4.6.6",
|
|
36
37
|
"nullthrows": "^1.1.1",
|
|
@@ -38,7 +39,7 @@
|
|
|
38
39
|
"semver": "^7.5.2"
|
|
39
40
|
},
|
|
40
41
|
"peerDependencies": {
|
|
41
|
-
"@atlaspack/core": "2.
|
|
42
|
+
"@atlaspack/core": "2.29.0"
|
|
42
43
|
},
|
|
43
44
|
"type": "commonjs"
|
|
44
45
|
}
|
package/src/JSTransformer.ts
CHANGED
|
@@ -5,9 +5,10 @@ import type {
|
|
|
5
5
|
FilePath,
|
|
6
6
|
FileCreateInvalidation,
|
|
7
7
|
} from '@atlaspack/types';
|
|
8
|
+
import {createBuildCache} from '@atlaspack/build-cache';
|
|
8
9
|
import type {SchemaEntity} from '@atlaspack/utils';
|
|
9
10
|
import type {Diagnostic} from '@atlaspack/diagnostic';
|
|
10
|
-
import SourceMap from '@
|
|
11
|
+
import SourceMap from '@atlaspack/source-map';
|
|
11
12
|
import {Transformer} from '@atlaspack/plugin';
|
|
12
13
|
import {transform, transformAsync} from '@atlaspack/rust';
|
|
13
14
|
import invariant from 'assert';
|
|
@@ -120,6 +121,8 @@ const CONFIG_SCHEMA: SchemaEntity = {
|
|
|
120
121
|
additionalProperties: false,
|
|
121
122
|
};
|
|
122
123
|
|
|
124
|
+
const configCache = createBuildCache();
|
|
125
|
+
|
|
123
126
|
const SCRIPT_ERRORS = {
|
|
124
127
|
browser: {
|
|
125
128
|
message: 'Browser scripts cannot have imports or exports.',
|
|
@@ -302,21 +305,63 @@ export default new Transformer({
|
|
|
302
305
|
let magicComments = false;
|
|
303
306
|
let addReactDisplayName = false;
|
|
304
307
|
|
|
305
|
-
let
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
308
|
+
let syncDynamicImportConfig:
|
|
309
|
+
| {
|
|
310
|
+
entrypoint_filepath_suffix: string;
|
|
311
|
+
actual_require_paths: string[];
|
|
312
|
+
}
|
|
313
|
+
| undefined;
|
|
314
|
+
|
|
315
|
+
if (config.env.isTesseract() && options.env.SYNC_DYNAMIC_IMPORT_CONFIG) {
|
|
316
|
+
try {
|
|
317
|
+
let config = configCache.get(
|
|
318
|
+
'SYNC_DYNAMIC_IMPORT_CONFIG',
|
|
319
|
+
) as typeof syncDynamicImportConfig;
|
|
320
|
+
|
|
321
|
+
if (!config) {
|
|
322
|
+
config = JSON.parse(options.env.SYNC_DYNAMIC_IMPORT_CONFIG);
|
|
323
|
+
|
|
324
|
+
invariant(typeof config?.entrypoint_filepath_suffix === 'string');
|
|
325
|
+
invariant(Array.isArray(config.actual_require_paths));
|
|
326
|
+
|
|
327
|
+
configCache.set('SYNC_DYNAMIC_IMPORT_CONFIG', config);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
syncDynamicImportConfig = config;
|
|
331
|
+
} catch {
|
|
332
|
+
// eslint-disable-next-line no-console
|
|
333
|
+
console.warn(
|
|
334
|
+
'Failed to parse SYNC_DYNAMIC_IMPORT_CONFIG to JSON or config shape did not match. Config will not be applied.',
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
const fallback = {
|
|
338
|
+
entrypoint_filepath_suffix: '__NO_MATCH__',
|
|
339
|
+
actual_require_paths: [],
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
// Set cache to fallback so we don't keep trying to parse.
|
|
343
|
+
configCache.set('SYNC_DYNAMIC_IMPORT_CONFIG', fallback);
|
|
344
|
+
syncDynamicImportConfig = fallback;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
config.invalidateOnEnvChange('SYNC_DYNAMIC_IMPORT_CONFIG');
|
|
349
|
+
|
|
350
|
+
let enableGlobalThisAliaser =
|
|
351
|
+
options.env.NATIVE_GLOBAL_THIS_ALIASER === 'true';
|
|
352
|
+
let enableLazyLoadingTransformer =
|
|
353
|
+
options.env.NATIVE_LAZY_LOADING_TRANSFORMER === 'true';
|
|
354
|
+
let enableDeadReturnsRemover =
|
|
355
|
+
options.env.NATIVE_DEAD_RETURNS_REMOVER === 'true';
|
|
356
|
+
let enableUnusedBindingsRemover =
|
|
357
|
+
options.env.NATIVE_UNUSED_BINDINGS_REMOVER === 'true';
|
|
311
358
|
|
|
312
359
|
if (conf && conf.contents) {
|
|
313
360
|
validateSchema.diagnostic(
|
|
314
361
|
CONFIG_SCHEMA,
|
|
315
362
|
{
|
|
316
363
|
data: conf.contents,
|
|
317
|
-
source:
|
|
318
|
-
? () => options.inputFS.readFileSync(conf.filePath, 'utf8')
|
|
319
|
-
: await options.inputFS.readFile(conf.filePath, 'utf8'),
|
|
364
|
+
source: () => options.inputFS.readFileSync(conf.filePath, 'utf8'),
|
|
320
365
|
filePath: conf.filePath,
|
|
321
366
|
prependKey: `/${encodeJSONKeyComponent('@atlaspack/transformer-js')}`,
|
|
322
367
|
},
|
|
@@ -355,6 +400,9 @@ export default new Transformer({
|
|
|
355
400
|
magicComments,
|
|
356
401
|
enableGlobalThisAliaser,
|
|
357
402
|
enableLazyLoadingTransformer,
|
|
403
|
+
enableDeadReturnsRemover,
|
|
404
|
+
enableUnusedBindingsRemover,
|
|
405
|
+
syncDynamicImportConfig,
|
|
358
406
|
};
|
|
359
407
|
},
|
|
360
408
|
async transform({asset, config, options, logger}) {
|
|
@@ -538,7 +586,12 @@ export default new Transformer({
|
|
|
538
586
|
enable_lazy_loading_transformer: Boolean(
|
|
539
587
|
config.enableLazyLoadingTransformer,
|
|
540
588
|
),
|
|
589
|
+
sync_dynamic_import_config: config.syncDynamicImportConfig,
|
|
541
590
|
nested_promise_import_fix: options.featureFlags.nestedPromiseImportFix,
|
|
591
|
+
enable_dead_returns_remover: Boolean(config.enableDeadReturnsRemover),
|
|
592
|
+
enable_unused_bindings_remover: Boolean(
|
|
593
|
+
config.enableUnusedBindingsRemover,
|
|
594
|
+
),
|
|
542
595
|
callMacro: asset.isSource
|
|
543
596
|
? async (err: any, src: any, exportName: any, args: any, loc: any) => {
|
|
544
597
|
let mod;
|
|
@@ -596,7 +649,6 @@ export default new Transformer({
|
|
|
596
649
|
|
|
597
650
|
map.addIndexedMappings(mappings);
|
|
598
651
|
if (originalMap) {
|
|
599
|
-
// @ts-expect-error TS2345
|
|
600
652
|
map.extends(originalMap);
|
|
601
653
|
} else {
|
|
602
654
|
if (!getFeatureFlag('omitSourcesContentInMemory')) {
|
|
@@ -1186,7 +1238,6 @@ export default new Transformer({
|
|
|
1186
1238
|
let sourceMap = new SourceMap(options.projectRoot);
|
|
1187
1239
|
sourceMap.addVLQMap(JSON.parse(map));
|
|
1188
1240
|
if (originalMap) {
|
|
1189
|
-
// @ts-expect-error TS2345
|
|
1190
1241
|
sourceMap.extends(originalMap);
|
|
1191
1242
|
}
|
|
1192
1243
|
asset.setMap(sourceMap);
|