@tailwindcss-mangle/core 2.3.0 → 4.0.0-alpha.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/LICENSE +21 -0
- package/package.json +17 -25
- package/dist/index.cjs +0 -589
- package/dist/index.d.cts +0 -88
- package/dist/index.d.mts +0 -88
- package/dist/index.d.ts +0 -88
- package/dist/index.mjs +0 -570
package/dist/index.mjs
DELETED
|
@@ -1,570 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import { resolve, isAbsolute } from 'node:path';
|
|
3
|
-
import process from 'node:process';
|
|
4
|
-
import { ClassGenerator, defaultMangleClassFilter, splitCode, makeRegex } from '@tailwindcss-mangle/shared';
|
|
5
|
-
export { ClassGenerator } from '@tailwindcss-mangle/shared';
|
|
6
|
-
import { getConfig } from '@tailwindcss-mangle/config';
|
|
7
|
-
import { sort } from 'fast-sort';
|
|
8
|
-
import micromatch from 'micromatch';
|
|
9
|
-
import postcss from 'postcss';
|
|
10
|
-
import parser from 'postcss-selector-parser';
|
|
11
|
-
import { Parser } from 'htmlparser2';
|
|
12
|
-
import MagicString from 'magic-string';
|
|
13
|
-
import { jsStringEscape } from '@ast-core/escape';
|
|
14
|
-
import _babelTraverse from '@babel/traverse';
|
|
15
|
-
import { parse } from '@babel/parser';
|
|
16
|
-
import { parse as parse$1 } from '@vue/compiler-sfc';
|
|
17
|
-
|
|
18
|
-
function isPlainObject(value) {
|
|
19
|
-
if (value === null || typeof value !== "object") {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
const prototype = Object.getPrototypeOf(value);
|
|
23
|
-
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
if (Symbol.iterator in value) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
if (Symbol.toStringTag in value) {
|
|
30
|
-
return Object.prototype.toString.call(value) === "[object Module]";
|
|
31
|
-
}
|
|
32
|
-
return true;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function _defu(baseObject, defaults, namespace = ".", merger) {
|
|
36
|
-
if (!isPlainObject(defaults)) {
|
|
37
|
-
return _defu(baseObject, {}, namespace, merger);
|
|
38
|
-
}
|
|
39
|
-
const object = Object.assign({}, defaults);
|
|
40
|
-
for (const key in baseObject) {
|
|
41
|
-
if (key === "__proto__" || key === "constructor") {
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
const value = baseObject[key];
|
|
45
|
-
if (value === null || value === void 0) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
if (merger && merger(object, key, value, namespace)) {
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
if (Array.isArray(value) && Array.isArray(object[key])) {
|
|
52
|
-
object[key] = [...value, ...object[key]];
|
|
53
|
-
} else if (isPlainObject(value) && isPlainObject(object[key])) {
|
|
54
|
-
object[key] = _defu(
|
|
55
|
-
value,
|
|
56
|
-
object[key],
|
|
57
|
-
(namespace ? `${namespace}.` : "") + key.toString(),
|
|
58
|
-
merger
|
|
59
|
-
);
|
|
60
|
-
} else {
|
|
61
|
-
object[key] = value;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return object;
|
|
65
|
-
}
|
|
66
|
-
function createDefu(merger) {
|
|
67
|
-
return (...arguments_) => (
|
|
68
|
-
// eslint-disable-next-line unicorn/no-array-reduce
|
|
69
|
-
arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
const defu = createDefu();
|
|
73
|
-
|
|
74
|
-
const { isMatch } = micromatch;
|
|
75
|
-
function escapeStringRegexp(str) {
|
|
76
|
-
if (typeof str !== "string") {
|
|
77
|
-
throw new TypeError("Expected a string");
|
|
78
|
-
}
|
|
79
|
-
return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, "\\$&").replaceAll("-", "\\x2d");
|
|
80
|
-
}
|
|
81
|
-
function createGlobMatcher(pattern, fallbackValue = false) {
|
|
82
|
-
if (pattern === void 0) {
|
|
83
|
-
return function() {
|
|
84
|
-
return fallbackValue;
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
return function(file) {
|
|
88
|
-
return isMatch(file, pattern);
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
var __defProp = Object.defineProperty;
|
|
93
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
94
|
-
var __publicField = (obj, key, value) => {
|
|
95
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
96
|
-
return value;
|
|
97
|
-
};
|
|
98
|
-
class Context {
|
|
99
|
-
constructor() {
|
|
100
|
-
__publicField(this, "options");
|
|
101
|
-
__publicField(this, "includeMatcher");
|
|
102
|
-
__publicField(this, "excludeMatcher");
|
|
103
|
-
__publicField(this, "replaceMap");
|
|
104
|
-
__publicField(this, "classSet");
|
|
105
|
-
__publicField(this, "classGenerator");
|
|
106
|
-
__publicField(this, "preserveFunctionSet");
|
|
107
|
-
__publicField(this, "preserveClassNamesSet");
|
|
108
|
-
__publicField(this, "preserveFunctionRegexs");
|
|
109
|
-
this.options = {};
|
|
110
|
-
this.classSet = /* @__PURE__ */ new Set();
|
|
111
|
-
this.replaceMap = /* @__PURE__ */ new Map();
|
|
112
|
-
this.includeMatcher = () => true;
|
|
113
|
-
this.excludeMatcher = () => false;
|
|
114
|
-
this.classGenerator = new ClassGenerator();
|
|
115
|
-
this.preserveFunctionSet = /* @__PURE__ */ new Set();
|
|
116
|
-
this.preserveClassNamesSet = /* @__PURE__ */ new Set();
|
|
117
|
-
this.preserveFunctionRegexs = [];
|
|
118
|
-
}
|
|
119
|
-
isPreserveClass(className) {
|
|
120
|
-
return this.preserveClassNamesSet.has(className);
|
|
121
|
-
}
|
|
122
|
-
addPreserveClass(className) {
|
|
123
|
-
return this.preserveClassNamesSet.add(className);
|
|
124
|
-
}
|
|
125
|
-
isPreserveFunction(calleeName) {
|
|
126
|
-
return this.preserveFunctionSet.has(calleeName);
|
|
127
|
-
}
|
|
128
|
-
mergeOptions(...opts) {
|
|
129
|
-
this.options = defu(this.options, ...opts);
|
|
130
|
-
this.includeMatcher = createGlobMatcher(this.options.include, true);
|
|
131
|
-
this.excludeMatcher = createGlobMatcher(this.options.exclude, false);
|
|
132
|
-
this.classGenerator = new ClassGenerator(this.options.classGenerator);
|
|
133
|
-
this.preserveFunctionSet = new Set(this.options?.preserveFunction ?? []);
|
|
134
|
-
this.preserveFunctionRegexs = [...this.preserveFunctionSet.values()].map((x) => {
|
|
135
|
-
return new RegExp(`${escapeStringRegexp(x)}\\(([^)]*)\\)`, "g");
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
isInclude(file) {
|
|
139
|
-
return this.includeMatcher(file) && !this.excludeMatcher(file);
|
|
140
|
-
}
|
|
141
|
-
currentMangleClassFilter(className) {
|
|
142
|
-
return (this.options.mangleClassFilter ?? defaultMangleClassFilter)(className);
|
|
143
|
-
}
|
|
144
|
-
getClassSet() {
|
|
145
|
-
return this.classSet;
|
|
146
|
-
}
|
|
147
|
-
getReplaceMap() {
|
|
148
|
-
return this.replaceMap;
|
|
149
|
-
}
|
|
150
|
-
addToUsedBy(key, file) {
|
|
151
|
-
const hit = this.classGenerator.newClassMap[key];
|
|
152
|
-
if (hit) {
|
|
153
|
-
hit.usedBy.add(file);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
loadClassSet(classList) {
|
|
157
|
-
const list = sort(classList).desc((c) => c.length);
|
|
158
|
-
for (const className of list) {
|
|
159
|
-
if (this.currentMangleClassFilter(className)) {
|
|
160
|
-
this.classSet.add(className);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
async initConfig(opts = {}) {
|
|
165
|
-
const { cwd, classList: _classList, mangleOptions } = opts;
|
|
166
|
-
const { config, cwd: configCwd } = await getConfig(cwd);
|
|
167
|
-
this.mergeOptions(mangleOptions, config?.mangle);
|
|
168
|
-
if (_classList) {
|
|
169
|
-
this.loadClassSet(_classList);
|
|
170
|
-
} else {
|
|
171
|
-
let jsonPath = this.options.classListPath ?? resolve(process.cwd(), config?.patch?.output?.filename);
|
|
172
|
-
if (!isAbsolute(jsonPath)) {
|
|
173
|
-
jsonPath = resolve(configCwd ?? process.cwd(), jsonPath);
|
|
174
|
-
}
|
|
175
|
-
if (jsonPath && fs.existsSync(jsonPath)) {
|
|
176
|
-
const rawClassList = fs.readFileSync(jsonPath, "utf8");
|
|
177
|
-
const list = JSON.parse(rawClassList);
|
|
178
|
-
this.loadClassSet(list);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
for (const cls of this.classSet) {
|
|
182
|
-
this.classGenerator.generateClassName(cls);
|
|
183
|
-
}
|
|
184
|
-
for (const x of Object.entries(this.classGenerator.newClassMap)) {
|
|
185
|
-
this.replaceMap.set(x[0], x[1].name);
|
|
186
|
-
}
|
|
187
|
-
return config;
|
|
188
|
-
}
|
|
189
|
-
// ["clsx\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const postcssPlugin = "postcss-mangle-tailwindcss-plugin";
|
|
193
|
-
function isVueScoped(s) {
|
|
194
|
-
if (s.parent) {
|
|
195
|
-
const index = s.parent.nodes.indexOf(s);
|
|
196
|
-
if (index > -1) {
|
|
197
|
-
const nextNode = s.parent.nodes[index + 1];
|
|
198
|
-
if (nextNode && nextNode.type === "attribute" && nextNode.attribute.includes("data-v-")) {
|
|
199
|
-
return true;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
const transformSelectorPostcssPlugin = function(options) {
|
|
206
|
-
const { ignoreVueScoped, ctx } = defu(options, {
|
|
207
|
-
ignoreVueScoped: true
|
|
208
|
-
});
|
|
209
|
-
const replaceMap = ctx.replaceMap;
|
|
210
|
-
return {
|
|
211
|
-
postcssPlugin,
|
|
212
|
-
Once(root) {
|
|
213
|
-
root.walkRules((rule) => {
|
|
214
|
-
parser((selectors) => {
|
|
215
|
-
selectors.walkClasses((s) => {
|
|
216
|
-
if (s.value && replaceMap && replaceMap.has(s.value)) {
|
|
217
|
-
if (ignoreVueScoped && isVueScoped(s)) {
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
const v = replaceMap.get(s.value);
|
|
221
|
-
if (v) {
|
|
222
|
-
if (ctx.isPreserveClass(s.value)) {
|
|
223
|
-
rule.cloneBefore();
|
|
224
|
-
}
|
|
225
|
-
s.value = v;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
}).transformSync(rule, {
|
|
230
|
-
lossless: false,
|
|
231
|
-
updateSelector: true
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
};
|
|
237
|
-
transformSelectorPostcssPlugin.postcss = true;
|
|
238
|
-
|
|
239
|
-
function cssHandler(rawSource, options) {
|
|
240
|
-
const acceptedPlugins = [transformSelectorPostcssPlugin(options)];
|
|
241
|
-
const { file } = options;
|
|
242
|
-
return postcss(acceptedPlugins).process(rawSource, {
|
|
243
|
-
from: file,
|
|
244
|
-
to: file
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function _interopDefaultCompat(e) {
|
|
249
|
-
return e && typeof e === "object" && "default" in e ? e.default : e;
|
|
250
|
-
}
|
|
251
|
-
const traverse = _interopDefaultCompat(_babelTraverse);
|
|
252
|
-
|
|
253
|
-
function between(x, min, max, included = false) {
|
|
254
|
-
if (typeof x !== "number") {
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
return included ? x >= min && x <= max : x > min && x < max;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function handleValue$1(options) {
|
|
261
|
-
const { ctx, id, path, magicString, raw, offset = 0, escape = false, markedArray } = options;
|
|
262
|
-
const { replaceMap } = ctx;
|
|
263
|
-
const node = path.node;
|
|
264
|
-
let value = raw;
|
|
265
|
-
for (const [s, e] of markedArray) {
|
|
266
|
-
if (between(node.start, s, e) || between(node.end, s, e)) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
const arr = sort(splitCode(value)).desc((x) => x.length);
|
|
271
|
-
for (const str of arr) {
|
|
272
|
-
if (replaceMap.has(str)) {
|
|
273
|
-
ctx.addToUsedBy(str, id);
|
|
274
|
-
const v = replaceMap.get(str);
|
|
275
|
-
if (v && typeof v === "string") {
|
|
276
|
-
value = value.replaceAll(str, v);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
if (typeof node.start === "number" && typeof node.end === "number" && value) {
|
|
281
|
-
const start = node.start + offset;
|
|
282
|
-
const end = node.end - offset;
|
|
283
|
-
if (start < end) {
|
|
284
|
-
magicString.update(start, end, escape ? jsStringEscape(value) : value);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
function preProcessJs(options) {
|
|
289
|
-
const { code, id, ctx } = options;
|
|
290
|
-
const { replaceMap } = ctx;
|
|
291
|
-
const magicString = typeof code === "string" ? new MagicString(code) : code;
|
|
292
|
-
let ast;
|
|
293
|
-
try {
|
|
294
|
-
const file = parse(magicString.original, {
|
|
295
|
-
sourceType: "unambiguous",
|
|
296
|
-
plugins: ["typescript", "jsx"]
|
|
297
|
-
});
|
|
298
|
-
if (file) {
|
|
299
|
-
ast = file;
|
|
300
|
-
} else {
|
|
301
|
-
return code.toString();
|
|
302
|
-
}
|
|
303
|
-
} catch {
|
|
304
|
-
return code.toString();
|
|
305
|
-
}
|
|
306
|
-
const markedArray = [];
|
|
307
|
-
traverse(ast, {
|
|
308
|
-
CallExpression: {
|
|
309
|
-
enter(p) {
|
|
310
|
-
const callee = p.get("callee");
|
|
311
|
-
if (callee.isIdentifier() && ctx.isPreserveFunction(callee.node.name)) {
|
|
312
|
-
if (p.node.start && p.node.end) {
|
|
313
|
-
markedArray.push([p.node.start, p.node.end]);
|
|
314
|
-
}
|
|
315
|
-
p.traverse({
|
|
316
|
-
StringLiteral: {
|
|
317
|
-
enter(path) {
|
|
318
|
-
const node = path.node;
|
|
319
|
-
const value = node.value;
|
|
320
|
-
const arr = sort(splitCode(value)).desc((x) => x.length);
|
|
321
|
-
for (const str of arr) {
|
|
322
|
-
if (replaceMap.has(str)) {
|
|
323
|
-
ctx.addPreserveClass(str);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
},
|
|
328
|
-
TemplateElement: {
|
|
329
|
-
enter(path) {
|
|
330
|
-
const node = path.node;
|
|
331
|
-
const value = node.value.raw;
|
|
332
|
-
const arr = sort(splitCode(value)).desc((x) => x.length);
|
|
333
|
-
for (const str of arr) {
|
|
334
|
-
if (replaceMap.has(str)) {
|
|
335
|
-
ctx.addPreserveClass(str);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
},
|
|
344
|
-
StringLiteral: {
|
|
345
|
-
exit(p) {
|
|
346
|
-
handleValue$1({
|
|
347
|
-
ctx,
|
|
348
|
-
id,
|
|
349
|
-
magicString,
|
|
350
|
-
path: p,
|
|
351
|
-
raw: p.node.value,
|
|
352
|
-
offset: 1,
|
|
353
|
-
escape: true,
|
|
354
|
-
markedArray
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
},
|
|
358
|
-
TemplateElement: {
|
|
359
|
-
exit(p) {
|
|
360
|
-
handleValue$1({
|
|
361
|
-
ctx,
|
|
362
|
-
id,
|
|
363
|
-
magicString,
|
|
364
|
-
path: p,
|
|
365
|
-
raw: p.node.value.raw,
|
|
366
|
-
offset: 0,
|
|
367
|
-
escape: false,
|
|
368
|
-
markedArray
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
return magicString.toString();
|
|
374
|
-
}
|
|
375
|
-
function preProcessRawCode(options) {
|
|
376
|
-
const { code, ctx } = options;
|
|
377
|
-
const { replaceMap } = ctx;
|
|
378
|
-
const magicString = typeof code === "string" ? new MagicString(code) : code;
|
|
379
|
-
const markArr = [];
|
|
380
|
-
for (const regex of ctx.preserveFunctionRegexs) {
|
|
381
|
-
const allArr = [];
|
|
382
|
-
let arr = null;
|
|
383
|
-
while ((arr = regex.exec(magicString.original)) !== null) {
|
|
384
|
-
allArr.push(arr);
|
|
385
|
-
markArr.push([arr.index, arr.index + arr[0].length]);
|
|
386
|
-
}
|
|
387
|
-
for (const regExpMatch of allArr) {
|
|
388
|
-
let ast;
|
|
389
|
-
try {
|
|
390
|
-
ast = parse(regExpMatch[0], {
|
|
391
|
-
sourceType: "unambiguous"
|
|
392
|
-
});
|
|
393
|
-
ast && traverse(ast, {
|
|
394
|
-
StringLiteral: {
|
|
395
|
-
enter(p) {
|
|
396
|
-
const arr2 = sort(splitCode(p.node.value)).desc((x) => x.length);
|
|
397
|
-
for (const v of arr2) {
|
|
398
|
-
if (replaceMap.has(v)) {
|
|
399
|
-
ctx.addPreserveClass(v);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
},
|
|
404
|
-
TemplateElement: {
|
|
405
|
-
enter(p) {
|
|
406
|
-
const arr2 = sort(splitCode(p.node.value.raw)).desc((x) => x.length);
|
|
407
|
-
for (const v of arr2) {
|
|
408
|
-
if (replaceMap.has(v)) {
|
|
409
|
-
ctx.addPreserveClass(v);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
} catch {
|
|
416
|
-
continue;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
for (const [key, value] of replaceMap) {
|
|
421
|
-
const regex = new RegExp(escapeStringRegexp(key), "g");
|
|
422
|
-
let arr = null;
|
|
423
|
-
while ((arr = regex.exec(magicString.original)) !== null) {
|
|
424
|
-
const start = arr.index;
|
|
425
|
-
const end = arr.index + arr[0].length;
|
|
426
|
-
let shouldUpdate = true;
|
|
427
|
-
for (const [ps, pe] of markArr) {
|
|
428
|
-
if (between(start, ps, pe) || between(end, ps, pe)) {
|
|
429
|
-
shouldUpdate = false;
|
|
430
|
-
break;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
if (shouldUpdate && typeof value === "string") {
|
|
434
|
-
magicString.update(start, end, value);
|
|
435
|
-
markArr.push([start, end]);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
return magicString.toString();
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
function handleValue(raw, node, options, ms, offset, escape) {
|
|
443
|
-
const { ctx, splitQuote = true } = options;
|
|
444
|
-
const { replaceMap, classGenerator: clsGen } = ctx;
|
|
445
|
-
const array = splitCode(raw, {
|
|
446
|
-
splitQuote
|
|
447
|
-
});
|
|
448
|
-
let rawString = raw;
|
|
449
|
-
let needUpdate = false;
|
|
450
|
-
for (const v of array) {
|
|
451
|
-
if (replaceMap.has(v)) {
|
|
452
|
-
let ignoreFlag = false;
|
|
453
|
-
if (Array.isArray(node.leadingComments)) {
|
|
454
|
-
ignoreFlag = node.leadingComments.findIndex((x) => x.value.includes("tw-mangle") && x.value.includes("ignore")) > -1;
|
|
455
|
-
}
|
|
456
|
-
if (!ignoreFlag) {
|
|
457
|
-
rawString = rawString.replace(makeRegex(v), clsGen.generateClassName(v).name);
|
|
458
|
-
needUpdate = true;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
if (needUpdate && typeof node.start === "number" && typeof node.end === "number") {
|
|
463
|
-
const start = node.start + offset;
|
|
464
|
-
const end = node.end - offset;
|
|
465
|
-
if (start < end && raw !== rawString) {
|
|
466
|
-
ms.update(start, end, escape ? jsStringEscape(rawString) : rawString);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return rawString;
|
|
470
|
-
}
|
|
471
|
-
function jsHandler(rawSource, options) {
|
|
472
|
-
const ms = typeof rawSource === "string" ? new MagicString(rawSource) : rawSource;
|
|
473
|
-
const ast = parse(ms.original, {
|
|
474
|
-
sourceType: "unambiguous",
|
|
475
|
-
plugins: ["typescript", "jsx"]
|
|
476
|
-
});
|
|
477
|
-
traverse(ast, {
|
|
478
|
-
StringLiteral: {
|
|
479
|
-
enter(p) {
|
|
480
|
-
const n = p.node;
|
|
481
|
-
handleValue(n.value, n, options, ms, 1, true);
|
|
482
|
-
}
|
|
483
|
-
},
|
|
484
|
-
TemplateElement: {
|
|
485
|
-
enter(p) {
|
|
486
|
-
const n = p.node;
|
|
487
|
-
handleValue(n.value.raw, n, options, ms, 0, false);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
// CallExpression: {
|
|
491
|
-
// enter(p: NodePath<CallExpression>) {
|
|
492
|
-
// const calleePath = p.get('callee')
|
|
493
|
-
// if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
|
|
494
|
-
// p.traverse({
|
|
495
|
-
// StringLiteral: {
|
|
496
|
-
// enter(s) {
|
|
497
|
-
// // ___CSS_LOADER_EXPORT___
|
|
498
|
-
// const res = jsHandler(s.node.value, options)
|
|
499
|
-
// if (res.code) {
|
|
500
|
-
// s.node.value = res.code
|
|
501
|
-
// }
|
|
502
|
-
// },
|
|
503
|
-
// },
|
|
504
|
-
// })
|
|
505
|
-
// }
|
|
506
|
-
// },
|
|
507
|
-
// },
|
|
508
|
-
});
|
|
509
|
-
return {
|
|
510
|
-
code: ms.toString(),
|
|
511
|
-
get map() {
|
|
512
|
-
return ms.generateMap();
|
|
513
|
-
}
|
|
514
|
-
};
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function htmlHandler(raw, options) {
|
|
518
|
-
const { ctx, isVue } = options;
|
|
519
|
-
const { replaceMap } = ctx;
|
|
520
|
-
const ms = typeof raw === "string" ? new MagicString(raw) : raw;
|
|
521
|
-
const parser = new Parser({
|
|
522
|
-
onattribute(name, value) {
|
|
523
|
-
if (name === "class") {
|
|
524
|
-
const arr = splitCode(value, {
|
|
525
|
-
splitQuote: false
|
|
526
|
-
});
|
|
527
|
-
let rawValue = value;
|
|
528
|
-
for (const v of arr) {
|
|
529
|
-
if (replaceMap.has(v)) {
|
|
530
|
-
rawValue = rawValue.replace(makeRegex(v), ctx.classGenerator.generateClassName(v).name);
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
ms.update(parser.startIndex + name.length + 2, parser.endIndex - 1, rawValue);
|
|
534
|
-
}
|
|
535
|
-
if (isVue) {
|
|
536
|
-
if (name === ":class") {
|
|
537
|
-
const { code } = jsHandler(value, {
|
|
538
|
-
ctx
|
|
539
|
-
});
|
|
540
|
-
ms.update(parser.startIndex + name.length + 2, parser.endIndex - 1, code);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
parser.write(ms.original);
|
|
546
|
-
parser.end();
|
|
547
|
-
return ms.toString();
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
function vueHandler(raw, options) {
|
|
551
|
-
const { ctx } = options;
|
|
552
|
-
const ms = typeof raw === "string" ? new MagicString(raw) : raw;
|
|
553
|
-
const { descriptor } = parse$1(ms.original);
|
|
554
|
-
const { template, scriptSetup, script } = descriptor;
|
|
555
|
-
if (template) {
|
|
556
|
-
const code = htmlHandler(template.content, { ctx, isVue: true });
|
|
557
|
-
ms.update(template.loc.start.offset, template.loc.end.offset, code);
|
|
558
|
-
}
|
|
559
|
-
if (script) {
|
|
560
|
-
const x = jsHandler(script.content, { ctx });
|
|
561
|
-
ms.update(script.loc.start.offset, script.loc.end.offset, x.code);
|
|
562
|
-
}
|
|
563
|
-
if (scriptSetup) {
|
|
564
|
-
const x = jsHandler(scriptSetup.content, { ctx });
|
|
565
|
-
ms.update(scriptSetup.loc.start.offset, scriptSetup.loc.end.offset, x.code);
|
|
566
|
-
}
|
|
567
|
-
return ms.toString();
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
export { Context, cssHandler, handleValue, htmlHandler, jsHandler, preProcessJs, preProcessRawCode, vueHandler };
|