Skip to content

Commit 0078ca7

Browse files
committed
feat: remove dedent pkg
1 parent 086494b commit 0078ca7

File tree

15 files changed

+171
-575
lines changed

15 files changed

+171
-575
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@tailwindcss-mangle/core": "workspace:*",
3232
"@tailwindcss-mangle/shared": "workspace:*",
3333
"@tsconfig/recommended": "^1.0.6",
34+
"@types/fs-extra": "^11.0.4",
3435
"@types/lodash-es": "^4.17.12",
3536
"@types/node": "^20.14.8",
3637
"@vitest/coverage-v8": "^1.6.0",
@@ -40,6 +41,7 @@
4041
"defu": "^6.1.4",
4142
"del": "^7.1.0",
4243
"eslint": "^9.5.0",
44+
"fs-extra": "^11.2.0",
4345
"local-pkg": "^0.5.0",
4446
"lodash-es": "^4.17.21",
4547
"only-allow": "^1.2.1",

packages/config/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
},
5252
"dependencies": {
5353
"@tailwindcss-mangle/shared": "workspace:^",
54-
"c12": "^1.11.1",
55-
"dedent": "^1.5.3"
54+
"c12": "^1.11.1"
5655
}
5756
}

packages/config/src/config.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'node:path'
22
import fs from 'node:fs/promises'
33
import { createDefineConfig, loadConfig } from 'c12'
4-
import dedent from 'dedent'
54
import type { UserConfig } from './types'
65
import { getDefaultUserConfig } from './defaults'
76
import { configName } from './constants'
@@ -21,11 +20,10 @@ export const defineConfig = createDefineConfig<UserConfig>()
2120
export function initConfig(cwd: string) {
2221
return fs.writeFile(
2322
path.resolve(cwd, `${configName}.config.ts`),
24-
dedent`
25-
import { defineConfig } from 'tailwindcss-patch'
23+
`import { defineConfig } from 'tailwindcss-patch'
2624
27-
export default defineConfig({})
28-
`,
25+
export default defineConfig({})
26+
`,
2927
'utf8',
3028
)
3129
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`config > init config 1`] = `
4+
"import { defineConfig } from 'tailwindcss-patch'
5+
6+
export default defineConfig({})
7+
"
8+
`;

packages/config/test/config.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from 'fs-extra'
2+
import path from 'pathe'
3+
import { initConfig } from '@/config'
4+
5+
describe('config', () => {
6+
it('init config', async () => {
7+
const cwd = path.resolve(__dirname, './fixtures/config/initConfig')
8+
await initConfig(cwd)
9+
const dest = path.resolve(cwd, 'tailwindcss-mangle.config.ts')
10+
expect(await fs.readFile(dest, 'utf8')).toMatchSnapshot()
11+
})
12+
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { defineConfig } from 'tailwindcss-patch'
22

3-
export default defineConfig({})
3+
export default defineConfig({})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'tailwindcss-patch'
2+
3+
export default defineConfig({})

packages/core/src/css/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { transformSelectorPostcssPlugin } from './plugins'
44

55
export function cssHandler(rawSource: string, options: ICssHandlerOptions) {
66
const acceptedPlugins = [transformSelectorPostcssPlugin(options)]
7-
const { file } = options
7+
const { id } = options
88
return postcss(acceptedPlugins).process(rawSource, {
9-
from: file,
10-
to: file,
9+
from: id,
10+
to: id,
1111
})
1212
}

packages/core/src/ctx/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ export class Context {
6868
return this.replaceMap // map
6969
}
7070

71-
addToUsedBy(key: string, file: string) {
71+
addToUsedBy(key: string, file?: string) {
72+
if (!file) {
73+
return
74+
}
7275
const hit = this.classGenerator.newClassMap[key]
7376
if (hit) {
7477
hit.usedBy.add(file)

packages/core/src/js/index.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import type { IJsHandlerOptions } from '../types'
55
import { makeRegex, splitCode } from '../shared'
66
import { parse, traverse } from '@/babel'
77

8-
export { preProcessJs, preProcessRawCode } from './pre'
9-
108
export function handleValue(raw: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions, ms: MagicString, offset: number, escape: boolean) {
11-
const { ctx, splitQuote = true } = options
9+
const { ctx, splitQuote = true, id } = options
1210
const { replaceMap, classGenerator: clsGen } = ctx
1311

1412
const array = splitCode(raw, {
@@ -24,7 +22,9 @@ export function handleValue(raw: string, node: StringLiteral | TemplateElement,
2422
}
2523

2624
if (!ignoreFlag) {
27-
rawString = rawString.replace(makeRegex(v), clsGen.generateClassName(v).name)
25+
const gen = clsGen.generateClassName(v)
26+
rawString = rawString.replace(makeRegex(v), gen.name)
27+
ctx.addToUsedBy(gen.name, id)
2828
needUpdate = true
2929
}
3030
}
@@ -42,10 +42,18 @@ export function handleValue(raw: string, node: StringLiteral | TemplateElement,
4242

4343
export function jsHandler(rawSource: string | MagicString, options: IJsHandlerOptions) {
4444
const ms: MagicString = typeof rawSource === 'string' ? new MagicString(rawSource) : rawSource
45-
const ast = parse(ms.original, {
46-
sourceType: 'unambiguous',
47-
// plugins: ['typescript', 'jsx', 'decorators'],
48-
})
45+
let ast
46+
try {
47+
ast = parse(ms.original, {
48+
sourceType: 'unambiguous',
49+
})
50+
}
51+
catch (error) {
52+
return {
53+
code: ms.original,
54+
}
55+
}
56+
4957
traverse(ast, {
5058
StringLiteral: {
5159
enter(p) {
@@ -59,24 +67,6 @@ export function jsHandler(rawSource: string | MagicString, options: IJsHandlerOp
5967
handleValue(n.value.raw, n, options, ms, 0, false)
6068
},
6169
},
62-
// CallExpression: {
63-
// enter(p: NodePath<CallExpression>) {
64-
// const calleePath = p.get('callee')
65-
// if (calleePath.isIdentifier() && calleePath.node.name === 'eval') {
66-
// p.traverse({
67-
// StringLiteral: {
68-
// enter(s) {
69-
// // ___CSS_LOADER_EXPORT___
70-
// const res = jsHandler(s.node.value, options)
71-
// if (res.code) {
72-
// s.node.value = res.code
73-
// }
74-
// },
75-
// },
76-
// })
77-
// }
78-
// },
79-
// },
8070
})
8171

8272
return {

0 commit comments

Comments
 (0)