Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: commit config pkg
  • Loading branch information
sonofmagic committed Aug 8, 2023
commit b6504c3aef90ae49a10e70cbee52965d43df1121
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@
"@icebreakers/eslint-config-ts": "^1.2.2",
"@icebreakers/rollup": "^0.4.0",
"@icebreakers/tsconfig": "^0.0.7",
"@tailwindcss-mangle/core": "workspace:*",
"@tailwindcss-mangle/shared": "workspace:*",
"@tsconfig/recommended": "^1.0.2",
"@types/lodash-es": "^4.17.8",
"@types/node": "^20.4.8",
"@vitest/coverage-v8": "^0.34.1",
"cross-env": "^7.0.3",
"dedent": "^1.5.1",
"defu": "^6.1.2",
"del": "^7.0.0",
"eslint": "^8.46.0",
"eslint-plugin-unicorn": "^48.0.1",
"lodash-es": "^4.17.21",
"only-allow": "^1.1.1",
"prettier": "^3.0.1",
"rollup": "^3.27.2",
"@tailwindcss-mangle/core": "workspace:*",
"@tailwindcss-mangle/shared": "workspace:*",
"tailwindcss-patch": "workspace:*",
"ts-node": "^10.9.1",
"tslib": "^2.6.1",
"typescript": "^5.1.6",
"unbuild": "^1.2.1",
"unplugin-tailwindcss-mangle": "workspace:*",
"vitest": "^0.34.1",
"dedent": "^1.5.1",
"defu": "^6.1.2",
"del": "^7.0.0",
"tslib": "^2.6.1"
"vitest": "^0.34.1"
},
"engines": {
"node": ">=16.6.0"
Expand Down
31 changes: 31 additions & 0 deletions packages/config/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { loadConfig, createDefineConfig } from 'c12'
import type { UserConfig } from './types'
import { getDefaultUserConfig } from './defaults'
import dedent from 'dedent'
import path from 'node:path'
import fs from 'node:fs/promises'
import { configName } from './constants'

export function getConfig(cwd?: string) {
return loadConfig<UserConfig>({
name: configName,
defaults: {
...getDefaultUserConfig()
},
cwd
})
}

export const defineConfig = createDefineConfig<UserConfig>()

export function initConfig(cwd: string) {
return fs.writeFile(
path.resolve(cwd, `${configName}.config.ts`),
dedent`
import { defineConfig } from 'tailwindcss-patch'

export default defineConfig({})
`,
'utf8'
)
}
6 changes: 3 additions & 3 deletions packages/config/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PatchUserConfig, UserConfig } from './types'

export function getPatchConfig(): PatchUserConfig {
export function getDefaultPatchConfig(): PatchUserConfig {
return {
output: {
filename: '.tw-patch/tw-class-list.json',
Expand All @@ -13,8 +13,8 @@ export function getPatchConfig(): PatchUserConfig {
}
}

export function getUserConfig(): UserConfig {
export function getDefaultUserConfig(): UserConfig {
return {
patch: getPatchConfig()
patch: getDefaultPatchConfig()
}
}
35 changes: 4 additions & 31 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
import { loadConfig, createDefineConfig } from 'c12'
import type { UserConfig } from './types'
import { getUserConfig } from './defaults'
import dedent from 'dedent'
import path from 'node:path'
import fs from 'node:fs/promises'
import { configName } from './constants'

export function getConfig(cwd?: string) {
return loadConfig<UserConfig>({
name: configName,
defaults: {
...getUserConfig()
},
cwd
})
}

export const defineConfig = createDefineConfig<UserConfig>()

export function initConfig(cwd: string) {
return fs.writeFile(
path.resolve(cwd, `${configName}.config.ts`),
dedent`
import { defineConfig } from 'tailwindcss-patch'

export default defineConfig({})
`,
'utf8'
)
}
export * from './config'
export * from './defaults'
export * from './constants'
export * from './types'
25 changes: 25 additions & 0 deletions packages/config/test/__snapshots__/defaults.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`defaults > getDefaultPatchConfig 1`] = `
{
"output": {
"filename": ".tw-patch/tw-class-list.json",
"loose": true,
"removeUniversalSelector": true,
},
"tailwindcss": {},
}
`;

exports[`defaults > getDefaultUserConfig 1`] = `
{
"patch": {
"output": {
"filename": ".tw-patch/tw-class-list.json",
"loose": true,
"removeUniversalSelector": true,
},
"tailwindcss": {},
},
}
`;
16 changes: 16 additions & 0 deletions packages/config/test/defaults.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getDefaultPatchConfig, getDefaultUserConfig } from '@/defaults'
import { omit } from 'lodash-es'

function omitCwdPath(o: any) {
return omit(o, ['tailwindcss.cwd', 'patch.tailwindcss.cwd'])
}

describe('defaults', () => {
it('getDefaultPatchConfig', () => {
expect(omitCwdPath(getDefaultPatchConfig())).toMatchSnapshot()
})

it('getDefaultUserConfig', () => {
expect(omitCwdPath(getDefaultUserConfig())).toMatchSnapshot()
})
})
4 changes: 2 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 { fixturesRoot } from './utils'
import { resolve } from 'node:path'
import { existsSync } from 'node:fs'
import { deleteAsync } from 'del'
import { getUserConfig } from '@/defaults'
import { getDefaultUserConfig } from '@/defaults'
import { configName } from '@/constants'

describe('config', () => {
Expand All @@ -18,7 +18,7 @@ describe('config', () => {
expect(existsSync(configPath)).toBe(true)

const { config } = await getConfig(cwd)
expect(config).toEqual(getUserConfig())
expect(config).toEqual(getDefaultUserConfig())
})

it('1.change-options', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/tailwindcss-patch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@
"@babel/parser": "^7.22.10",
"@babel/traverse": "^7.22.10",
"@babel/types": "^7.22.10",
"c12": "^1.4.2",
"cac": "^6.7.14",
"postcss": "^8.4.27",
"resolve": "^1.22.4",
"semver": "^7.5.4"
"semver": "^7.5.4",
"@tailwindcss-mangle/config": "workspace:^"
},
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
"repository": {
Expand Down
31 changes: 1 addition & 30 deletions packages/tailwindcss-patch/src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
import { loadConfig, createDefineConfig } from 'c12'
import { UserConfig } from '@/types'
import { getDefaultUserConfig } from '@/defaults'
import dedent from 'dedent'
import path from 'node:path'
import fs from 'node:fs/promises'

export function getConfig(cwd?: string) {
return loadConfig<UserConfig>({
name: 'tailwindcss-patch',
defaults: {
...getDefaultUserConfig()
},
cwd
})
}

export const defineConfig = createDefineConfig<UserConfig>()

export function initConfig(cwd: string) {
return fs.writeFile(
path.resolve(cwd, 'tailwindcss-patch.config.ts'),
dedent`
import { defineConfig } from 'tailwindcss-patch'

export default defineConfig({})
`,
'utf8'
)
}
export * from '@tailwindcss-mangle/config'
15 changes: 1 addition & 14 deletions packages/tailwindcss-patch/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import type { PatchOptions, UserConfig, DeepRequired } from './types'
import type { PatchOptions, DeepRequired } from './types'

export function getDefaultPatchOptions(): DeepRequired<PatchOptions> {
return {
overwrite: true
}
}

export function getDefaultUserConfig(): UserConfig {
return {
output: {
filename: '.tw-patch/tw-class-list.json',
removeUniversalSelector: true,
loose: true
},
tailwindcss: {
cwd: process.cwd()
}
}
}
15 changes: 0 additions & 15 deletions packages/tailwindcss-patch/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,6 @@ export type TailwindcssRuntimeContext = {
variantOptions: Map<string, object>
}

export interface UserConfig {
output?: {
filename?: string

loose?: boolean
/**
* @description remove * in output json
*/
removeUniversalSelector?: boolean
}
tailwindcss?: {
cwd?: string
config?: string
}
}
// Custom utility type:
export type DeepRequired<T> = {
[K in keyof T]: Required<DeepRequired<T[K]>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,3 @@ exports[`defaults > getDefaultPatchOptions 1`] = `
"overwrite": true,
}
`;

exports[`defaults > getDefaultUserConfig 1`] = `
{
"filename": ".tw-patch/tw-class-list.json",
"loose": true,
"removeUniversalSelector": true,
}
`;
19 changes: 10 additions & 9 deletions packages/tailwindcss-patch/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { initConfig, getConfig } from '@/core/config'
import { initConfig, getConfig, getDefaultUserConfig } from '@/core/config'
import { fixturesRoot } from './utils'
import { resolve } from 'node:path'
import { existsSync } from 'node:fs'
import { deleteAsync } from 'del'
import { getDefaultUserConfig } from '@/defaults'

describe('config', () => {
it('0.default', async () => {
Expand All @@ -24,13 +23,15 @@ describe('config', () => {
const cwd = resolve(fixturesRoot, './config/1.change-options')
const { config } = await getConfig(cwd)
expect(config).toEqual({
output: {
filename: 'xxx/yyy/zzz.json',
loose: false,
removeUniversalSelector: false
},
tailwindcss: {
cwd: 'aaa/bbb/cc'
patch: {
output: {
filename: 'xxx/yyy/zzz.json',
loose: false,
removeUniversalSelector: false
},
tailwindcss: {
cwd: 'aaa/bbb/cc'
}
}
})
})
Expand Down
6 changes: 1 addition & 5 deletions packages/tailwindcss-patch/test/defaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { getDefaultPatchOptions, getDefaultUserConfig } from '@/defaults'
import { getDefaultPatchOptions } from '@/defaults'

describe('defaults', () => {
it('getDefaultPatchOptions', () => {
expect(getDefaultPatchOptions()).toMatchSnapshot()
})

it('getDefaultUserConfig', () => {
expect(getDefaultUserConfig().output).toMatchSnapshot()
})
})
26 changes: 23 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.