@tailwindcss-mangle/core 4.1.3 → 5.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/dist/index.cjs CHANGED
@@ -198,6 +198,7 @@ var Context = class {
198
198
  replaceMap;
199
199
  classSet;
200
200
  classGenerator;
201
+ configRoot;
201
202
  preserveFunctionSet;
202
203
  preserveClassNamesSet;
203
204
  preserveFunctionRegexs;
@@ -206,6 +207,7 @@ var Context = class {
206
207
  this.classSet = /* @__PURE__ */ new Set();
207
208
  this.replaceMap = /* @__PURE__ */ new Map();
208
209
  this.classGenerator = new shared_exports.ClassGenerator();
210
+ this.configRoot = import_node_process.default.cwd();
209
211
  this.preserveFunctionSet = /* @__PURE__ */ new Set();
210
212
  this.preserveClassNamesSet = /* @__PURE__ */ new Set();
211
213
  this.preserveFunctionRegexs = [];
@@ -221,14 +223,19 @@ var Context = class {
221
223
  }
222
224
  mergeOptions(...opts) {
223
225
  this.options = defu(this.options, ...opts);
224
- this.classGenerator = new shared_exports.ClassGenerator(this.options.classGenerator);
225
- this.preserveFunctionSet = new Set(this.options?.preserveFunction ?? []);
226
+ this.classGenerator = new shared_exports.ClassGenerator(this.options.generator);
227
+ const preserveOptions = this.options.preserve ?? {};
228
+ this.preserveFunctionSet = new Set(preserveOptions.functions ?? []);
229
+ this.preserveClassNamesSet = new Set(preserveOptions.classes ?? []);
226
230
  this.preserveFunctionRegexs = [...this.preserveFunctionSet.values()].map((x) => {
227
231
  return new RegExp(`${(0, shared_exports.escapeStringRegexp)(x)}\\(([^)]*)\\)`, "g");
228
232
  });
229
233
  }
230
234
  currentMangleClassFilter(className) {
231
- return (this.options.mangleClassFilter ?? shared_exports.defaultMangleClassFilter)(className);
235
+ if (this.preserveClassNamesSet.has(className)) {
236
+ return false;
237
+ }
238
+ return (this.options.filter ?? shared_exports.defaultMangleClassFilter)(className);
232
239
  }
233
240
  getClassSet() {
234
241
  return this.classSet;
@@ -254,26 +261,40 @@ var Context = class {
254
261
  }
255
262
  }
256
263
  async initConfig(opts = {}) {
257
- const { cwd, classList: _classList, mangleOptions } = opts;
264
+ const { cwd, classList: _classList, transformerOptions } = opts;
258
265
  const { config, cwd: configCwd } = await (0, import_config.getConfig)(cwd);
259
- if (mangleOptions?.classMapOutput === true) {
260
- mangleOptions.classMapOutput = config.mangle?.classMapOutput;
261
- if (typeof mangleOptions.classMapOutput === "object") {
262
- mangleOptions.classMapOutput.enable = true;
266
+ this.configRoot = configCwd ?? cwd ?? import_node_process.default.cwd();
267
+ let normalizedTransformer = transformerOptions ? { ...transformerOptions, registry: { ...transformerOptions.registry } } : void 0;
268
+ if (normalizedTransformer?.registry?.mapping === true) {
269
+ const fallback = config?.transformer?.registry?.mapping;
270
+ if (typeof fallback === "function") {
271
+ normalizedTransformer.registry.mapping = fallback;
272
+ } else if (fallback && typeof fallback === "object") {
273
+ normalizedTransformer.registry.mapping = {
274
+ ...fallback,
275
+ enabled: true
276
+ };
277
+ } else {
278
+ normalizedTransformer.registry.mapping = {
279
+ enabled: true
280
+ };
263
281
  }
264
282
  }
265
- this.mergeOptions(mangleOptions, config?.mangle);
283
+ this.mergeOptions(normalizedTransformer, config?.transformer);
266
284
  if (_classList) {
267
285
  this.loadClassSet(_classList);
268
286
  } else {
269
- let jsonPath = this.options.classListPath ?? (0, import_pathe.resolve)(import_node_process.default.cwd(), config?.patch?.output?.filename);
270
- if (!(0, import_pathe.isAbsolute)(jsonPath)) {
271
- jsonPath = (0, import_pathe.resolve)(configCwd ?? import_node_process.default.cwd(), jsonPath);
272
- }
273
- if (jsonPath && import_fs_extra.default.existsSync(jsonPath)) {
274
- const rawClassList = import_fs_extra.default.readFileSync(jsonPath, "utf8");
275
- const list = JSON.parse(rawClassList);
276
- this.loadClassSet(list);
287
+ const fallbackFile = config?.registry?.output?.file;
288
+ let jsonPath = this.options.registry?.file ?? fallbackFile;
289
+ if (jsonPath) {
290
+ if (!(0, import_pathe.isAbsolute)(jsonPath)) {
291
+ jsonPath = (0, import_pathe.resolve)(this.configRoot, jsonPath);
292
+ }
293
+ if (import_fs_extra.default.existsSync(jsonPath)) {
294
+ const rawClassList = import_fs_extra.default.readFileSync(jsonPath, "utf8");
295
+ const list = JSON.parse(rawClassList);
296
+ this.loadClassSet(list);
297
+ }
277
298
  }
278
299
  }
279
300
  for (const cls of this.classSet) {
@@ -288,16 +309,18 @@ var Context = class {
288
309
  try {
289
310
  const arr = Object.entries(this.classGenerator.newClassMap).map((x) => {
290
311
  return {
291
- before: x[0],
292
- after: x[1].name,
312
+ original: x[0],
313
+ mangled: x[1].name,
293
314
  usedBy: Array.from(x[1].usedBy)
294
315
  };
295
316
  });
296
- if (typeof this.options.classMapOutput === "function") {
297
- await this.options.classMapOutput(arr);
298
- } else if (typeof this.options.classMapOutput === "object" && this.options.classMapOutput.enable && this.options.classMapOutput.filename) {
299
- import_fs_extra.default.mkdirSync((0, import_pathe.dirname)(this.options.classMapOutput.filename), { recursive: true });
300
- import_fs_extra.default.writeFileSync(this.options.classMapOutput.filename, JSON.stringify(arr, null, 2));
317
+ const mappingOption = this.options.registry?.mapping;
318
+ if (typeof mappingOption === "function") {
319
+ await mappingOption(arr);
320
+ } else if (mappingOption && typeof mappingOption === "object" && mappingOption.enabled && mappingOption.file) {
321
+ const outputFile = (0, import_pathe.isAbsolute)(mappingOption.file) ? mappingOption.file : (0, import_pathe.resolve)(this.configRoot, mappingOption.file);
322
+ import_fs_extra.default.mkdirSync((0, import_pathe.dirname)(outputFile), { recursive: true });
323
+ import_fs_extra.default.writeFileSync(outputFile, JSON.stringify(arr, null, 2));
301
324
  }
302
325
  } catch (error) {
303
326
  console.error(`[tailwindcss-mangle]: ${error}`);
@@ -404,6 +427,9 @@ function jsHandler(rawSource, options) {
404
427
  StringLiteral: {
405
428
  enter(p) {
406
429
  const n = p.node;
430
+ if (typeof n.value === "string" && p.isDirectiveLiteral?.() && n.value.startsWith("use ")) {
431
+ return;
432
+ }
407
433
  handleValue(n.value, n, options, ms, 1, true);
408
434
  }
409
435
  },
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { TransformResult } from 'unplugin';
2
2
  import * as _tailwindcss_mangle_config from '@tailwindcss-mangle/config';
3
- import { MangleUserConfig } from '@tailwindcss-mangle/config';
3
+ import { TransformerOptions } from '@tailwindcss-mangle/config';
4
4
  import { ClassGenerator } from '@tailwindcss-mangle/shared';
5
5
  export { ClassGenerator } from '@tailwindcss-mangle/shared';
6
6
  import MagicString from 'magic-string';
@@ -9,13 +9,14 @@ import { StringLiteral, TemplateElement } from '@babel/types';
9
9
  interface InitConfigOptions {
10
10
  cwd?: string;
11
11
  classList?: string[];
12
- mangleOptions?: MangleUserConfig;
12
+ transformerOptions?: TransformerOptions;
13
13
  }
14
14
  declare class Context {
15
- options: MangleUserConfig;
15
+ options: TransformerOptions;
16
16
  replaceMap: Map<string, string>;
17
17
  classSet: Set<string>;
18
18
  classGenerator: ClassGenerator;
19
+ configRoot: string;
19
20
  preserveFunctionSet: Set<string>;
20
21
  preserveClassNamesSet: Set<string>;
21
22
  preserveFunctionRegexs: RegExp[];
@@ -29,7 +30,7 @@ declare class Context {
29
30
  getReplaceMap(): Map<string, string>;
30
31
  addToUsedBy(key: string, file?: string): void;
31
32
  loadClassSet(classList: string[]): void;
32
- initConfig(opts?: InitConfigOptions): Promise<_tailwindcss_mangle_config.UserConfig>;
33
+ initConfig(opts?: InitConfigOptions): Promise<_tailwindcss_mangle_config.TailwindcssMangleConfig>;
33
34
  dump(): Promise<void>;
34
35
  }
35
36
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { TransformResult } from 'unplugin';
2
2
  import * as _tailwindcss_mangle_config from '@tailwindcss-mangle/config';
3
- import { MangleUserConfig } from '@tailwindcss-mangle/config';
3
+ import { TransformerOptions } from '@tailwindcss-mangle/config';
4
4
  import { ClassGenerator } from '@tailwindcss-mangle/shared';
5
5
  export { ClassGenerator } from '@tailwindcss-mangle/shared';
6
6
  import MagicString from 'magic-string';
@@ -9,13 +9,14 @@ import { StringLiteral, TemplateElement } from '@babel/types';
9
9
  interface InitConfigOptions {
10
10
  cwd?: string;
11
11
  classList?: string[];
12
- mangleOptions?: MangleUserConfig;
12
+ transformerOptions?: TransformerOptions;
13
13
  }
14
14
  declare class Context {
15
- options: MangleUserConfig;
15
+ options: TransformerOptions;
16
16
  replaceMap: Map<string, string>;
17
17
  classSet: Set<string>;
18
18
  classGenerator: ClassGenerator;
19
+ configRoot: string;
19
20
  preserveFunctionSet: Set<string>;
20
21
  preserveClassNamesSet: Set<string>;
21
22
  preserveFunctionRegexs: RegExp[];
@@ -29,7 +30,7 @@ declare class Context {
29
30
  getReplaceMap(): Map<string, string>;
30
31
  addToUsedBy(key: string, file?: string): void;
31
32
  loadClassSet(classList: string[]): void;
32
- initConfig(opts?: InitConfigOptions): Promise<_tailwindcss_mangle_config.UserConfig>;
33
+ initConfig(opts?: InitConfigOptions): Promise<_tailwindcss_mangle_config.TailwindcssMangleConfig>;
33
34
  dump(): Promise<void>;
34
35
  }
35
36
 
package/dist/index.js CHANGED
@@ -171,6 +171,7 @@ var Context = class {
171
171
  replaceMap;
172
172
  classSet;
173
173
  classGenerator;
174
+ configRoot;
174
175
  preserveFunctionSet;
175
176
  preserveClassNamesSet;
176
177
  preserveFunctionRegexs;
@@ -179,6 +180,7 @@ var Context = class {
179
180
  this.classSet = /* @__PURE__ */ new Set();
180
181
  this.replaceMap = /* @__PURE__ */ new Map();
181
182
  this.classGenerator = new shared_exports.ClassGenerator();
183
+ this.configRoot = process.cwd();
182
184
  this.preserveFunctionSet = /* @__PURE__ */ new Set();
183
185
  this.preserveClassNamesSet = /* @__PURE__ */ new Set();
184
186
  this.preserveFunctionRegexs = [];
@@ -194,14 +196,19 @@ var Context = class {
194
196
  }
195
197
  mergeOptions(...opts) {
196
198
  this.options = defu(this.options, ...opts);
197
- this.classGenerator = new shared_exports.ClassGenerator(this.options.classGenerator);
198
- this.preserveFunctionSet = new Set(this.options?.preserveFunction ?? []);
199
+ this.classGenerator = new shared_exports.ClassGenerator(this.options.generator);
200
+ const preserveOptions = this.options.preserve ?? {};
201
+ this.preserveFunctionSet = new Set(preserveOptions.functions ?? []);
202
+ this.preserveClassNamesSet = new Set(preserveOptions.classes ?? []);
199
203
  this.preserveFunctionRegexs = [...this.preserveFunctionSet.values()].map((x) => {
200
204
  return new RegExp(`${(0, shared_exports.escapeStringRegexp)(x)}\\(([^)]*)\\)`, "g");
201
205
  });
202
206
  }
203
207
  currentMangleClassFilter(className) {
204
- return (this.options.mangleClassFilter ?? shared_exports.defaultMangleClassFilter)(className);
208
+ if (this.preserveClassNamesSet.has(className)) {
209
+ return false;
210
+ }
211
+ return (this.options.filter ?? shared_exports.defaultMangleClassFilter)(className);
205
212
  }
206
213
  getClassSet() {
207
214
  return this.classSet;
@@ -227,26 +234,40 @@ var Context = class {
227
234
  }
228
235
  }
229
236
  async initConfig(opts = {}) {
230
- const { cwd, classList: _classList, mangleOptions } = opts;
237
+ const { cwd, classList: _classList, transformerOptions } = opts;
231
238
  const { config, cwd: configCwd } = await getConfig(cwd);
232
- if (mangleOptions?.classMapOutput === true) {
233
- mangleOptions.classMapOutput = config.mangle?.classMapOutput;
234
- if (typeof mangleOptions.classMapOutput === "object") {
235
- mangleOptions.classMapOutput.enable = true;
239
+ this.configRoot = configCwd ?? cwd ?? process.cwd();
240
+ let normalizedTransformer = transformerOptions ? { ...transformerOptions, registry: { ...transformerOptions.registry } } : void 0;
241
+ if (normalizedTransformer?.registry?.mapping === true) {
242
+ const fallback = config?.transformer?.registry?.mapping;
243
+ if (typeof fallback === "function") {
244
+ normalizedTransformer.registry.mapping = fallback;
245
+ } else if (fallback && typeof fallback === "object") {
246
+ normalizedTransformer.registry.mapping = {
247
+ ...fallback,
248
+ enabled: true
249
+ };
250
+ } else {
251
+ normalizedTransformer.registry.mapping = {
252
+ enabled: true
253
+ };
236
254
  }
237
255
  }
238
- this.mergeOptions(mangleOptions, config?.mangle);
256
+ this.mergeOptions(normalizedTransformer, config?.transformer);
239
257
  if (_classList) {
240
258
  this.loadClassSet(_classList);
241
259
  } else {
242
- let jsonPath = this.options.classListPath ?? resolve(process.cwd(), config?.patch?.output?.filename);
243
- if (!isAbsolute(jsonPath)) {
244
- jsonPath = resolve(configCwd ?? process.cwd(), jsonPath);
245
- }
246
- if (jsonPath && fs.existsSync(jsonPath)) {
247
- const rawClassList = fs.readFileSync(jsonPath, "utf8");
248
- const list = JSON.parse(rawClassList);
249
- this.loadClassSet(list);
260
+ const fallbackFile = config?.registry?.output?.file;
261
+ let jsonPath = this.options.registry?.file ?? fallbackFile;
262
+ if (jsonPath) {
263
+ if (!isAbsolute(jsonPath)) {
264
+ jsonPath = resolve(this.configRoot, jsonPath);
265
+ }
266
+ if (fs.existsSync(jsonPath)) {
267
+ const rawClassList = fs.readFileSync(jsonPath, "utf8");
268
+ const list = JSON.parse(rawClassList);
269
+ this.loadClassSet(list);
270
+ }
250
271
  }
251
272
  }
252
273
  for (const cls of this.classSet) {
@@ -261,16 +282,18 @@ var Context = class {
261
282
  try {
262
283
  const arr = Object.entries(this.classGenerator.newClassMap).map((x) => {
263
284
  return {
264
- before: x[0],
265
- after: x[1].name,
285
+ original: x[0],
286
+ mangled: x[1].name,
266
287
  usedBy: Array.from(x[1].usedBy)
267
288
  };
268
289
  });
269
- if (typeof this.options.classMapOutput === "function") {
270
- await this.options.classMapOutput(arr);
271
- } else if (typeof this.options.classMapOutput === "object" && this.options.classMapOutput.enable && this.options.classMapOutput.filename) {
272
- fs.mkdirSync(dirname(this.options.classMapOutput.filename), { recursive: true });
273
- fs.writeFileSync(this.options.classMapOutput.filename, JSON.stringify(arr, null, 2));
290
+ const mappingOption = this.options.registry?.mapping;
291
+ if (typeof mappingOption === "function") {
292
+ await mappingOption(arr);
293
+ } else if (mappingOption && typeof mappingOption === "object" && mappingOption.enabled && mappingOption.file) {
294
+ const outputFile = isAbsolute(mappingOption.file) ? mappingOption.file : resolve(this.configRoot, mappingOption.file);
295
+ fs.mkdirSync(dirname(outputFile), { recursive: true });
296
+ fs.writeFileSync(outputFile, JSON.stringify(arr, null, 2));
274
297
  }
275
298
  } catch (error) {
276
299
  console.error(`[tailwindcss-mangle]: ${error}`);
@@ -377,6 +400,9 @@ function jsHandler(rawSource, options) {
377
400
  StringLiteral: {
378
401
  enter(p) {
379
402
  const n = p.node;
403
+ if (typeof n.value === "string" && p.isDirectiveLiteral?.() && n.value.startsWith("use ")) {
404
+ return;
405
+ }
380
406
  handleValue(n.value, n, options, ms, 1, true);
381
407
  }
382
408
  },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tailwindcss-mangle/core",
3
3
  "type": "module",
4
- "version": "4.1.3",
4
+ "version": "5.0.0",
5
5
  "description": "The core of tailwindcss-mangle",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -47,18 +47,18 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@ast-core/escape": "^1.0.1",
50
- "@babel/parser": "^7.28.4",
51
- "@babel/traverse": "^7.28.4",
52
- "@babel/types": "^7.28.4",
50
+ "@babel/parser": "^7.28.5",
51
+ "@babel/traverse": "^7.28.5",
52
+ "@babel/types": "^7.28.5",
53
53
  "fast-sort": "^3.4.1",
54
54
  "fs-extra": "^11.3.2",
55
55
  "htmlparser2": "10.0.0",
56
- "magic-string": "^0.30.19",
56
+ "magic-string": "^0.30.21",
57
57
  "pathe": "^2.0.3",
58
58
  "postcss": "^8.5.6",
59
59
  "postcss-selector-parser": "^7.1.0",
60
- "@tailwindcss-mangle/shared": "^4.1.1",
61
- "@tailwindcss-mangle/config": "^5.1.2"
60
+ "@tailwindcss-mangle/config": "^6.0.0",
61
+ "@tailwindcss-mangle/shared": "^4.1.1"
62
62
  },
63
63
  "scripts": {
64
64
  "dev": "tsup --watch --sourcemap",