Skip to content

Commit

Permalink
Feature/add more test case (#40)
Browse files Browse the repository at this point in the history
* chore: commit js case

* refact: type and plugin

* feat: use config to control plugin

* chore: allow test for tailwindcss patch

* chore: add ci codecov
  • Loading branch information
sonofmagic authored Aug 9, 2023
1 parent b735483 commit 0f5ab74
Show file tree
Hide file tree
Showing 28 changed files with 261 additions and 240 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ jobs:
- name: Build
run: pnpm build

- name: Patch
working-directory: packages/tailwindcss-patch
run: pnpm run patch

- name: Test
run: pnpm test

- uses: codecov/codecov-action@v3
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ jobs:
# NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- uses: codecov/codecov-action@v3
5 changes: 3 additions & 2 deletions packages/config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tailwindcss-mangle/config",
"version": "1.0.1",
"version": "2.0.4",
"description": "The config and load function of tailwindcss-mangle",
"exports": {
".": {
Expand All @@ -25,7 +25,7 @@
],
"scripts": {
"build": "unbuild",
"test": "vitest run",
"test": "vitest run --coverage.enabled",
"test:dev": "vitest"
},
"keywords": [
Expand All @@ -44,6 +44,7 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"@tailwindcss-mangle/shared": "workspace:^",
"c12": "^1.4.2",
"dedent": "^1.5.1"
},
Expand Down
21 changes: 19 additions & 2 deletions packages/config/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PatchUserConfig, UserConfig } from './types'
import { defaultMangleClassFilter } from '@tailwindcss-mangle/shared'
import type { PatchUserConfig, UserConfig, MangleUserConfig } from './types'

export function getDefaultPatchConfig(): PatchUserConfig {
return {
Expand All @@ -11,8 +12,24 @@ export function getDefaultPatchConfig(): PatchUserConfig {
}
}

export function getDefaultMangleUserConfig(): MangleUserConfig {
return {
mangleClassFilter: defaultMangleClassFilter,
include: ['**/*.{js,jsx,ts,tsx,svelte,vue}'],
exclude: ['node_modules/**/*', '**/*.{css,scss,less,sass,postcss,html,htm}'],
disabled: process.env.NODE_ENV === 'development',
classListPath: '.tw-patch/tw-class-list.json',
classMapOutput: {
enable: false,
filename: '.tw-patch/tw-map-list.json',
loose: true
}
}
}

export function getDefaultUserConfig(): UserConfig {
return {
patch: getDefaultPatchConfig()
patch: getDefaultPatchConfig(),
mangle: getDefaultMangleUserConfig()
}
}
19 changes: 19 additions & 0 deletions packages/config/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import type { IClassGeneratorOptions } from '@tailwindcss-mangle/shared'

export interface ClassMapOutputOptions {
enable?: boolean
filename?: string
loose?: boolean
}

export interface MangleUserConfig {
mangleClassFilter?: (className: string) => boolean
classGenerator?: IClassGeneratorOptions
exclude?: string[]
include?: string[]
classListPath?: string
classMapOutput?: ClassMapOutputOptions
disabled?: boolean
}

export interface PatchUserConfig {
output?: {
filename?: string
Expand All @@ -16,4 +34,5 @@ export interface PatchUserConfig {

export interface UserConfig {
patch?: PatchUserConfig
mangle?: MangleUserConfig
}
17 changes: 17 additions & 0 deletions packages/config/test/__snapshots__/defaults.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ exports[`defaults > getDefaultPatchConfig 1`] = `

exports[`defaults > getDefaultUserConfig 1`] = `
{
"mangle": {
"classListPath": ".tw-patch/tw-class-list.json",
"classMapOutput": {
"enable": false,
"filename": ".tw-patch/tw-map-list.json",
"loose": true,
},
"disabled": false,
"exclude": [
"node_modules/**/*",
"**/*.{css,scss,less,sass,postcss,html,htm}",
],
"include": [
"**/*.{js,jsx,ts,tsx,svelte,vue}",
],
"mangleClassFilter": [Function],
},
"patch": {
"output": {
"filename": ".tw-patch/tw-class-list.json",
Expand Down
5 changes: 3 additions & 2 deletions packages/config/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync } from 'node:fs'
import { deleteAsync } from 'del'
import { fixturesRoot } from './utils'
import { initConfig, getConfig } from '@/index'
import { getDefaultUserConfig } from '@/defaults'
import { getDefaultUserConfig, getDefaultMangleUserConfig } from '@/defaults'
import { configName } from '@/constants'

describe('config', () => {
Expand Down Expand Up @@ -34,7 +34,8 @@ describe('config', () => {
tailwindcss: {
cwd: 'aaa/bbb/cc'
}
}
},
mangle: getDefaultMangleUserConfig()
})
})
})
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tailwindcss-mangle/core",
"version": "2.0.2",
"version": "2.0.4",
"description": "The core of tailwindcss-mangle",
"exports": {
".": {
Expand Down
20 changes: 1 addition & 19 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClassGenerator } from './shared'
import type { ClassGenerator } from './shared'

export interface IClassGeneratorContextItem {
name: string
Expand Down Expand Up @@ -32,21 +32,3 @@ export interface ICssHandlerOptions extends IHandlerOptions {
ignoreVueScoped?: boolean
file?: string
}

export interface ClassSetOutputOptions {
filename: string
dir?: string
type: 'all' | 'partial'
}

export interface ClassMapOutputOptions {
filename: string
dir?: string
}
export interface Options {
classGenerator?: IClassGeneratorOptions
exclude?: string[]
include?: string[]
classSetOutput?: boolean | ClassSetOutputOptions
classMapOutput?: boolean | ClassMapOutputOptions
}
11 changes: 11 additions & 0 deletions packages/core/test/fixtures/prod/0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Unterminated string constant
if (isSvg) {
let source = serializeString(svgNode);
// add xml declaration
source = '<?xml version="1.0" standalone="no"?>\r\n' + source;
// convert svg source to URI data scheme.
url = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source);
saveAs(url, "graph.svg");
onAlreadySerialized();
return;
}
11 changes: 11 additions & 0 deletions packages/core/test/fixtures/prod/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Missing semicolon.
if (typeof parent === 'string') {
parent = d.querySelectorAll(config.parent);
if (parent.length > 1) {
console.warn('WARNING: the given `parent` query(' + config.parent + ') matched more than one element, the first one will be used');
}
if (parent.length === 0) {
throw 'ERROR: the given `parent` doesn\'t exists!';
}
parent = parent[0];
}
4 changes: 2 additions & 2 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tailwindcss-mangle/shared",
"version": "2.0.0",
"version": "2.0.4",
"description": "The shared utils of tailwindcss-mangle",
"exports": {
".": {
Expand All @@ -25,7 +25,7 @@
],
"scripts": {
"build": "unbuild",
"test": "vitest run",
"test": "vitest run --coverage.enabled",
"test:dev": "vitest"
},
"keywords": [
Expand Down
6 changes: 3 additions & 3 deletions packages/tailwindcss-patch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tailwindcss-patch",
"version": "2.0.3",
"version": "2.0.4",
"description": "patch tailwindcss for exposing context and extract classes",
"exports": {
".": {
Expand Down Expand Up @@ -32,7 +32,7 @@
"build": "unbuild",
"test": "vitest run --coverage.enabled",
"test:dev": "vitest",
"cli": "node bin/tw-patch.js install"
"patch": "node bin/tw-patch.js install"
},
"keywords": [
"tailwindcss",
Expand All @@ -51,7 +51,7 @@
},
"peerDependenciesMeta": {
"tailwindcss": {
"optional": false
"optional": true
}
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/tailwindcss-patch/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from 'node:path'
import { existsSync } from 'node:fs'
import { deleteAsync } from 'del'
import { fixturesRoot } from './utils'
import { initConfig, getConfig, getDefaultUserConfig } from '@/core/config'
import { initConfig, getConfig, getDefaultUserConfig, getDefaultMangleUserConfig } from '@/core/config'

describe('config', () => {
it('0.default', async () => {
Expand Down Expand Up @@ -32,7 +32,8 @@ describe('config', () => {
tailwindcss: {
cwd: 'aaa/bbb/cc'
}
}
},
mangle: getDefaultMangleUserConfig()
})
})
})
4 changes: 0 additions & 4 deletions packages/tailwindcss-patch/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
// import 'tailwindcss-patch/cli'
import { TailwindcssPatcher } from './dist'
const twPatcher = new TailwindcssPatcher()
twPatcher.patch()
9 changes: 7 additions & 2 deletions packages/unplugin-tailwindcss-mangle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unplugin-tailwindcss-mangle",
"version": "2.0.2",
"version": "2.0.4",
"description": "mangle tailwindcss utilities class plugin. support vite and webpack!",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -36,6 +36,11 @@
"require": "./dist/nuxt.cjs",
"import": "./dist/nuxt.mjs"
},
"./utils": {
"types": "./dist/utils.d.ts",
"require": "./dist/utils.cjs",
"import": "./dist/utils.mjs"
},
"./*": "./*"
},
"typesVersions": {
Expand All @@ -48,7 +53,7 @@
},
"scripts": {
"build": "unbuild",
"test": "vitest run",
"test": "vitest run --coverage.enabled",
"test:dev": "vitest"
},
"files": [
Expand Down
2 changes: 0 additions & 2 deletions packages/unplugin-tailwindcss-mangle/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const pluginName = 'unplugin-tailwindcss-mangle'

export const webpackCacheKey = pluginName + '-sources-cache-key'
85 changes: 85 additions & 0 deletions packages/unplugin-tailwindcss-mangle/src/core/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import fs from 'node:fs'
import { resolve } from 'node:path'
import { ClassGenerator } from '@tailwindcss-mangle/shared'
import { getConfig, getDefaultMangleUserConfig } from '@tailwindcss-mangle/config'
import type { MangleUserConfig } from '@tailwindcss-mangle/config'
import { sort } from 'fast-sort'
import defu from 'defu'
import { createGlobMatcher, defaultMangleClassFilter } from '@/utils'

export class Context {
options: MangleUserConfig
includeMatcher: (file: string) => boolean
excludeMatcher: (file: string) => boolean
classSet: Set<string>
replaceMap: Map<string, string>
classGenerator: ClassGenerator
constructor(opts: MangleUserConfig) {
this.options = defu(opts, getDefaultMangleUserConfig())
this.classSet = new Set()
this.replaceMap = new Map()
this.includeMatcher = createGlobMatcher(this.options.include, true)
this.excludeMatcher = createGlobMatcher(this.options.exclude, false)
this.classGenerator = new ClassGenerator(this.options.classGenerator)
}

mergeOptions(opts?: MangleUserConfig) {
// 插件选项优先
this.options = defu(this.options, opts)
this.includeMatcher = createGlobMatcher(this.options.include, true)
this.excludeMatcher = createGlobMatcher(this.options.exclude, false)
this.classGenerator = new ClassGenerator(this.options.classGenerator)
}

isInclude(file: string) {
return this.includeMatcher(file) && !this.excludeMatcher(file)
}

currentMangleClassFilter(className: string) {
return (this.options.mangleClassFilter ?? defaultMangleClassFilter)(className)
}

getClassSet() {
return this.classSet
}

getReplaceMap() {
return this.replaceMap
}

addToUsedBy(key: string, file: string) {
const hit = this.classGenerator.newClassMap[key]
if (hit) {
hit.usedBy.add(file)
}
}

async initConfig() {
const { config } = await getConfig()
const mangleConfig = config?.mangle
this.mergeOptions(mangleConfig)
const jsonPath = this.options.classListPath ?? resolve(process.cwd(), config?.patch?.output?.filename as string)

if (jsonPath && fs.existsSync(jsonPath)) {
const rawClassList = fs.readFileSync(jsonPath, 'utf8')
const list = JSON.parse(rawClassList) as string[]
// why?
// case bg-red-500 and bg-red-500/50
// transform bg-red-500/50 first
const classList = sort(list).desc((c) => c.length)
for (const className of classList) {
if (this.currentMangleClassFilter(className)) {
this.classSet.add(className)
}
}
}
for (const cls of this.classSet) {
this.classGenerator.generateClassName(cls)
}

for (const x of Object.entries(this.classGenerator.newClassMap)) {
this.replaceMap.set(x[0], x[1].name)
}
return config
}
}
2 changes: 1 addition & 1 deletion packages/unplugin-tailwindcss-mangle/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './plugin'
export * from './options'
export * from './context'
Loading

0 comments on commit 0f5ab74

Please sign in to comment.