@tailwindcss-mangle/core 2.2.0 → 2.2.1

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/dist/index.cjs CHANGED
@@ -13,8 +13,6 @@ const babel = require('@babel/core');
13
13
  const helperPluginUtils = require('@babel/helper-plugin-utils');
14
14
  const MagicString = require('magic-string');
15
15
  const escape = require('@ast-core/escape');
16
- const parser$1 = require('@babel/parser');
17
- const traverse$1 = require('@babel/traverse');
18
16
 
19
17
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
20
18
 
@@ -24,7 +22,6 @@ const postcss__default = /*#__PURE__*/_interopDefaultCompat(postcss);
24
22
  const parser__default = /*#__PURE__*/_interopDefaultCompat(parser);
25
23
  const babel__default = /*#__PURE__*/_interopDefaultCompat(babel);
26
24
  const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
27
- const traverse__default = /*#__PURE__*/_interopDefaultCompat(traverse$1);
28
25
 
29
26
  function isObject(value) {
30
27
  return value !== null && typeof value === "object";
@@ -360,46 +357,32 @@ function htmlHandler(rawSource, options) {
360
357
 
361
358
  const isProd = () => process.env.NODE_ENV === "production";
362
359
 
363
- function getStringLiteralCalleeName(path) {
364
- if (path.parentPath.isCallExpression()) {
365
- const callee = path.parentPath.get("callee");
366
- if (callee.isIdentifier()) {
367
- return callee.node.name;
368
- }
369
- }
370
- }
371
- function getTemplateElementCalleeName(path) {
372
- if (path.parentPath.isTemplateLiteral()) {
373
- const pp = path.parentPath;
374
- if (pp.parentPath.isCallExpression()) {
375
- const callee = pp.parentPath.get("callee");
376
- if (callee.isIdentifier()) {
377
- return callee.node.name;
378
- }
379
- }
360
+ function between(x, min, max, included = false) {
361
+ if (typeof x !== "number") {
362
+ return false;
380
363
  }
364
+ return included ? x >= min && x <= max : x > min && x < max;
381
365
  }
382
366
 
383
367
  function handleValue$1(options) {
384
- const { ctx, id, path, magicString, raw, replaceMap, offset = 0, escape: escape$1 = false, preserve = false } = options;
368
+ const { ctx, id, path, magicString, raw, replaceMap, offset = 0, escape: escape$1 = false, markedArray } = options;
385
369
  const node = path.node;
386
370
  let value = raw;
371
+ for (const [s, e] of markedArray) {
372
+ if (between(node.start, s, e) || between(node.end, s, e)) {
373
+ return;
374
+ }
375
+ }
387
376
  const arr = fastSort.sort(shared.splitCode(value)).desc((x) => x.length);
388
377
  for (const str of arr) {
389
378
  if (replaceMap.has(str)) {
390
379
  ctx.addToUsedBy(str, id);
391
- if (preserve) {
392
- ctx.addPreserveClass(str);
393
- }
394
380
  const v = replaceMap.get(str);
395
381
  if (v) {
396
382
  value = value.replaceAll(str, v);
397
383
  }
398
384
  }
399
385
  }
400
- if (preserve) {
401
- return;
402
- }
403
386
  if (typeof node.start === "number" && typeof node.end === "number" && value) {
404
387
  const start = node.start + offset;
405
388
  const end = node.end - offset;
@@ -410,11 +393,11 @@ function handleValue$1(options) {
410
393
  }
411
394
  const JsPlugin = helperPluginUtils.declare((api, options) => {
412
395
  api.assertVersion(7);
413
- const { magicString, replaceMap, id, ctx } = options;
396
+ const { magicString, replaceMap, id, ctx, markedArray } = options;
414
397
  return {
415
398
  visitor: {
416
399
  StringLiteral: {
417
- enter(p) {
400
+ exit(p) {
418
401
  const opts = {
419
402
  ctx,
420
403
  id,
@@ -424,17 +407,13 @@ const JsPlugin = helperPluginUtils.declare((api, options) => {
424
407
  replaceMap,
425
408
  offset: 1,
426
409
  escape: true,
427
- preserve: false
410
+ markedArray
428
411
  };
429
- const calleeName = getStringLiteralCalleeName(p);
430
- if (calleeName && ctx.isPreserveFunction(calleeName)) {
431
- opts.preserve = true;
432
- }
433
412
  handleValue$1(opts);
434
413
  }
435
414
  },
436
415
  TemplateElement: {
437
- enter(p) {
416
+ exit(p) {
438
417
  const opts = {
439
418
  ctx,
440
419
  id,
@@ -444,38 +423,90 @@ const JsPlugin = helperPluginUtils.declare((api, options) => {
444
423
  replaceMap,
445
424
  offset: 0,
446
425
  escape: false,
447
- preserve: false
426
+ markedArray
448
427
  };
449
- const calleeName = getTemplateElementCalleeName(p);
450
- if (calleeName && ctx.isPreserveFunction(calleeName)) {
451
- opts.preserve = true;
452
- }
453
428
  handleValue$1(opts);
454
429
  }
455
430
  }
456
431
  }
457
432
  };
458
433
  });
459
- function transformSync(code, plugins, filename) {
460
- babel__default.transformSync(code, {
461
- presets: [
462
- // ['@babel/preset-react', {}],
463
- [
464
- require("@babel/preset-typescript"),
465
- {
466
- allExtensions: true,
467
- isTSX: true
468
- }
469
- ]
470
- ],
434
+ function transformSync(ast, code, plugins, filename) {
435
+ babel__default.transformFromAstSync(ast, code, {
436
+ presets: loadPresets(),
471
437
  plugins,
472
438
  filename
473
439
  });
474
440
  }
441
+ function loadPresets() {
442
+ return [
443
+ [
444
+ require("@babel/preset-typescript"),
445
+ {
446
+ allExtensions: true,
447
+ isTSX: true
448
+ }
449
+ ]
450
+ ];
451
+ }
475
452
  function preProcessJs(options) {
476
453
  const { code, replaceMap, id, ctx } = options;
477
454
  const magicString = typeof code === "string" ? new MagicString__default(code) : code;
455
+ let ast;
456
+ try {
457
+ const file = babel__default.parseSync(magicString.original, {
458
+ sourceType: "unambiguous",
459
+ presets: loadPresets()
460
+ });
461
+ if (file) {
462
+ ast = file;
463
+ } else {
464
+ return code.toString();
465
+ }
466
+ } catch {
467
+ return code.toString();
468
+ }
469
+ const markedArray = [];
470
+ babel__default.traverse(ast, {
471
+ CallExpression: {
472
+ enter(p) {
473
+ const callee = p.get("callee");
474
+ if (callee.isIdentifier() && ctx.isPreserveFunction(callee.node.name)) {
475
+ if (p.node.start && p.node.end) {
476
+ markedArray.push([p.node.start, p.node.end]);
477
+ }
478
+ p.traverse({
479
+ StringLiteral: {
480
+ enter(path) {
481
+ const node = path.node;
482
+ const value = node.value;
483
+ const arr = fastSort.sort(shared.splitCode(value)).desc((x) => x.length);
484
+ for (const str of arr) {
485
+ if (replaceMap.has(str)) {
486
+ ctx.addPreserveClass(str);
487
+ }
488
+ }
489
+ }
490
+ },
491
+ TemplateElement: {
492
+ enter(path) {
493
+ const node = path.node;
494
+ const value = node.value.raw;
495
+ const arr = fastSort.sort(shared.splitCode(value)).desc((x) => x.length);
496
+ for (const str of arr) {
497
+ if (replaceMap.has(str)) {
498
+ ctx.addPreserveClass(str);
499
+ }
500
+ }
501
+ }
502
+ }
503
+ });
504
+ }
505
+ }
506
+ }
507
+ });
478
508
  transformSync(
509
+ ast,
479
510
  magicString.original,
480
511
  [
481
512
  [
@@ -484,7 +515,8 @@ function preProcessJs(options) {
484
515
  magicString,
485
516
  replaceMap,
486
517
  id,
487
- ctx
518
+ ctx,
519
+ markedArray
488
520
  }
489
521
  ]
490
522
  ],
@@ -506,10 +538,10 @@ function preProcessRawCode(options) {
506
538
  for (const regExpMatch of allArr) {
507
539
  let ast;
508
540
  try {
509
- ast = parser$1.parse(regExpMatch[0], {
541
+ ast = babel__default.parseSync(regExpMatch[0], {
510
542
  sourceType: "unambiguous"
511
543
  });
512
- traverse__default(ast, {
544
+ ast && babel__default.traverse(ast, {
513
545
  StringLiteral: {
514
546
  enter(p) {
515
547
  const arr2 = fastSort.sort(shared.splitCode(p.node.value)).desc((x) => x.length);
@@ -544,7 +576,7 @@ function preProcessRawCode(options) {
544
576
  const end = arr.index + arr[0].length;
545
577
  let shouldUpdate = true;
546
578
  for (const [ps, pe] of markArr) {
547
- if (start > ps && start < pe || end < pe && end > ps) {
579
+ if (between(start, ps, pe) || between(end, ps, pe)) {
548
580
  shouldUpdate = false;
549
581
  break;
550
582
  }
package/dist/index.mjs CHANGED
@@ -12,8 +12,6 @@ import babel, { transformSync as transformSync$1 } from '@babel/core';
12
12
  import { declare } from '@babel/helper-plugin-utils';
13
13
  import MagicString from 'magic-string';
14
14
  import { jsStringEscape } from '@ast-core/escape';
15
- import { parse as parse$1 } from '@babel/parser';
16
- import traverse$1 from '@babel/traverse';
17
15
 
18
16
 
19
17
 
@@ -358,46 +356,32 @@ function htmlHandler(rawSource, options) {
358
356
 
359
357
  const isProd = () => process.env.NODE_ENV === "production";
360
358
 
361
- function getStringLiteralCalleeName(path) {
362
- if (path.parentPath.isCallExpression()) {
363
- const callee = path.parentPath.get("callee");
364
- if (callee.isIdentifier()) {
365
- return callee.node.name;
366
- }
367
- }
368
- }
369
- function getTemplateElementCalleeName(path) {
370
- if (path.parentPath.isTemplateLiteral()) {
371
- const pp = path.parentPath;
372
- if (pp.parentPath.isCallExpression()) {
373
- const callee = pp.parentPath.get("callee");
374
- if (callee.isIdentifier()) {
375
- return callee.node.name;
376
- }
377
- }
359
+ function between(x, min, max, included = false) {
360
+ if (typeof x !== "number") {
361
+ return false;
378
362
  }
363
+ return included ? x >= min && x <= max : x > min && x < max;
379
364
  }
380
365
 
381
366
  function handleValue$1(options) {
382
- const { ctx, id, path, magicString, raw, replaceMap, offset = 0, escape = false, preserve = false } = options;
367
+ const { ctx, id, path, magicString, raw, replaceMap, offset = 0, escape = false, markedArray } = options;
383
368
  const node = path.node;
384
369
  let value = raw;
370
+ for (const [s, e] of markedArray) {
371
+ if (between(node.start, s, e) || between(node.end, s, e)) {
372
+ return;
373
+ }
374
+ }
385
375
  const arr = sort(splitCode(value)).desc((x) => x.length);
386
376
  for (const str of arr) {
387
377
  if (replaceMap.has(str)) {
388
378
  ctx.addToUsedBy(str, id);
389
- if (preserve) {
390
- ctx.addPreserveClass(str);
391
- }
392
379
  const v = replaceMap.get(str);
393
380
  if (v) {
394
381
  value = value.replaceAll(str, v);
395
382
  }
396
383
  }
397
384
  }
398
- if (preserve) {
399
- return;
400
- }
401
385
  if (typeof node.start === "number" && typeof node.end === "number" && value) {
402
386
  const start = node.start + offset;
403
387
  const end = node.end - offset;
@@ -408,11 +392,11 @@ function handleValue$1(options) {
408
392
  }
409
393
  const JsPlugin = declare((api, options) => {
410
394
  api.assertVersion(7);
411
- const { magicString, replaceMap, id, ctx } = options;
395
+ const { magicString, replaceMap, id, ctx, markedArray } = options;
412
396
  return {
413
397
  visitor: {
414
398
  StringLiteral: {
415
- enter(p) {
399
+ exit(p) {
416
400
  const opts = {
417
401
  ctx,
418
402
  id,
@@ -422,17 +406,13 @@ const JsPlugin = declare((api, options) => {
422
406
  replaceMap,
423
407
  offset: 1,
424
408
  escape: true,
425
- preserve: false
409
+ markedArray
426
410
  };
427
- const calleeName = getStringLiteralCalleeName(p);
428
- if (calleeName && ctx.isPreserveFunction(calleeName)) {
429
- opts.preserve = true;
430
- }
431
411
  handleValue$1(opts);
432
412
  }
433
413
  },
434
414
  TemplateElement: {
435
- enter(p) {
415
+ exit(p) {
436
416
  const opts = {
437
417
  ctx,
438
418
  id,
@@ -442,38 +422,90 @@ const JsPlugin = declare((api, options) => {
442
422
  replaceMap,
443
423
  offset: 0,
444
424
  escape: false,
445
- preserve: false
425
+ markedArray
446
426
  };
447
- const calleeName = getTemplateElementCalleeName(p);
448
- if (calleeName && ctx.isPreserveFunction(calleeName)) {
449
- opts.preserve = true;
450
- }
451
427
  handleValue$1(opts);
452
428
  }
453
429
  }
454
430
  }
455
431
  };
456
432
  });
457
- function transformSync(code, plugins, filename) {
458
- babel.transformSync(code, {
459
- presets: [
460
- // ['@babel/preset-react', {}],
461
- [
462
- require("@babel/preset-typescript"),
463
- {
464
- allExtensions: true,
465
- isTSX: true
466
- }
467
- ]
468
- ],
433
+ function transformSync(ast, code, plugins, filename) {
434
+ babel.transformFromAstSync(ast, code, {
435
+ presets: loadPresets(),
469
436
  plugins,
470
437
  filename
471
438
  });
472
439
  }
440
+ function loadPresets() {
441
+ return [
442
+ [
443
+ require("@babel/preset-typescript"),
444
+ {
445
+ allExtensions: true,
446
+ isTSX: true
447
+ }
448
+ ]
449
+ ];
450
+ }
473
451
  function preProcessJs(options) {
474
452
  const { code, replaceMap, id, ctx } = options;
475
453
  const magicString = typeof code === "string" ? new MagicString(code) : code;
454
+ let ast;
455
+ try {
456
+ const file = babel.parseSync(magicString.original, {
457
+ sourceType: "unambiguous",
458
+ presets: loadPresets()
459
+ });
460
+ if (file) {
461
+ ast = file;
462
+ } else {
463
+ return code.toString();
464
+ }
465
+ } catch {
466
+ return code.toString();
467
+ }
468
+ const markedArray = [];
469
+ babel.traverse(ast, {
470
+ CallExpression: {
471
+ enter(p) {
472
+ const callee = p.get("callee");
473
+ if (callee.isIdentifier() && ctx.isPreserveFunction(callee.node.name)) {
474
+ if (p.node.start && p.node.end) {
475
+ markedArray.push([p.node.start, p.node.end]);
476
+ }
477
+ p.traverse({
478
+ StringLiteral: {
479
+ enter(path) {
480
+ const node = path.node;
481
+ const value = node.value;
482
+ const arr = sort(splitCode(value)).desc((x) => x.length);
483
+ for (const str of arr) {
484
+ if (replaceMap.has(str)) {
485
+ ctx.addPreserveClass(str);
486
+ }
487
+ }
488
+ }
489
+ },
490
+ TemplateElement: {
491
+ enter(path) {
492
+ const node = path.node;
493
+ const value = node.value.raw;
494
+ const arr = sort(splitCode(value)).desc((x) => x.length);
495
+ for (const str of arr) {
496
+ if (replaceMap.has(str)) {
497
+ ctx.addPreserveClass(str);
498
+ }
499
+ }
500
+ }
501
+ }
502
+ });
503
+ }
504
+ }
505
+ }
506
+ });
476
507
  transformSync(
508
+ ast,
477
509
  magicString.original,
478
510
  [
479
511
  [
@@ -482,7 +514,8 @@ function preProcessJs(options) {
482
514
  magicString,
483
515
  replaceMap,
484
516
  id,
485
- ctx
517
+ ctx,
518
+ markedArray
486
519
  }
487
520
  ]
488
521
  ],
@@ -504,10 +537,10 @@ function preProcessRawCode(options) {
504
537
  for (const regExpMatch of allArr) {
505
538
  let ast;
506
539
  try {
507
- ast = parse$1(regExpMatch[0], {
540
+ ast = babel.parseSync(regExpMatch[0], {
508
541
  sourceType: "unambiguous"
509
542
  });
510
- traverse$1(ast, {
543
+ ast && babel.traverse(ast, {
511
544
  StringLiteral: {
512
545
  enter(p) {
513
546
  const arr2 = sort(splitCode(p.node.value)).desc((x) => x.length);
@@ -542,7 +575,7 @@ function preProcessRawCode(options) {
542
575
  const end = arr.index + arr[0].length;
543
576
  let shouldUpdate = true;
544
577
  for (const [ps, pe] of markArr) {
545
- if (start > ps && start < pe || end < pe && end > ps) {
578
+ if (between(start, ps, pe) || between(end, ps, pe)) {
546
579
  shouldUpdate = false;
547
580
  break;
548
581
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tailwindcss-mangle/core",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "The core of tailwindcss-mangle",
5
5
  "exports": {
6
6
  ".": {
@@ -39,9 +39,7 @@
39
39
  "@ast-core/escape": "^1.0.1",
40
40
  "@babel/core": "^7.22.19",
41
41
  "@babel/helper-plugin-utils": "^7.22.5",
42
- "@babel/parser": "^7.22.16",
43
42
  "@babel/preset-typescript": "^7.22.15",
44
- "@babel/traverse": "^7.22.19",
45
43
  "@babel/types": "^7.22.19",
46
44
  "fast-sort": "^3.4.0",
47
45
  "magic-string": "^0.30.3",
@@ -49,13 +47,12 @@
49
47
  "parse5": "^7.1.2",
50
48
  "postcss": "^8.4.29",
51
49
  "postcss-selector-parser": "^6.0.13",
52
- "@tailwindcss-mangle/config": "^2.2.0",
53
- "@tailwindcss-mangle/shared": "^2.2.0"
50
+ "@tailwindcss-mangle/config": "^2.2.1",
51
+ "@tailwindcss-mangle/shared": "^2.2.1"
54
52
  },
55
53
  "devDependencies": {
56
54
  "@parse5/tools": "^0.3.0",
57
55
  "@types/babel__core": "^7.20.1",
58
- "@types/babel__traverse": "^7.20.1",
59
56
  "@types/micromatch": "^4.0.2",
60
57
  "@vue/compiler-core": "^3.3.4",
61
58
  "@vue/compiler-sfc": "^3.3.4"