Skip to content

Commit

Permalink
chore: update exports field and separate responsibilities in core
Browse files Browse the repository at this point in the history
- Create `merge.ts` and `tsup.config.base.ts` files to better separate
- Update `exports` field in `package.json` adding new entry points for
  `merge`, css` and `tsup.config.base`

- Update imports in packages to use the new `tsup.config.base` entry
  point
  • Loading branch information
halvaradop committed Oct 24, 2024
1 parent d2fd224 commit eb4a943
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/tailwindcss-animations/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from "vitest"
import { extractClasses } from "@halvaradop/tailwindcss-core/utils"
import { extractClasses } from "@halvaradop/tailwindcss-core/css"
import { plugin } from "../src/index.js"
import { theme } from "../src/theme.js"

Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-animations/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "tsup"
import { tsupConfig } from "@halvaradop/tailwindcss-core"
import { tsupConfig } from "@halvaradop/tailwindcss-core/tsup.config.base"

export default defineConfig(tsupConfig)
18 changes: 12 additions & 6 deletions packages/tailwindcss-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@
},
"homepage": "https://github.com/halvaradop/tailwindcss-utilities#readme",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"./merge": {
"types": "./dist/merge.d.ts",
"import": "./dist/merge.js",
"require": "./dist/merge.cjs"
},
"./utils": {
"./css": {
"types": "./dist/generate-classes.d.ts",
"import": "./dist/generate-classes.js"
"import": "./dist/generate-classes.js",
"require": "./dist/generate-classes.cjs"
},
"./tsup.config.base": {
"types": "./dist/tsup.config.base.d.ts",
"import": "./dist/tsup.config.base.js",
"require": "./dist/tsup.config.base.cjs"
},
"./tsconfig.base.json": "./tsconfig.base.json"
},
Expand Down
29 changes: 0 additions & 29 deletions packages/tailwindcss-core/src/generate-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import tailwindcss from "tailwindcss"
import minify from "@csstools/postcss-minify"
import { PluginsConfig } from "tailwindcss/types/config.js"
import { FlattenArrayType } from "@halvaradop/ts-utility-types"
import { isObject, isPrimitive } from "@halvaradop/ts-utility-types/utils"

const TAILWIND_UTILITIES_DIRECTIVE = "@tailwind utilities;"

Expand Down Expand Up @@ -33,31 +32,3 @@ export const extractClasses = (plugin: FlattenArrayType<PluginsConfig>) => {
}
return generateCSS
}

/**
* Create a new object by merging two objects which prioritize the object types
*
* @param source first object to merge
* @param target second object to merge
* @param priority true if the object type should be prioritized, false otherwise
* @returns merged two objects
*/
export const merge = <S extends Record<string, unknown>, T extends Record<string, unknown>>(
source: S,
target: T,
priority: boolean = true
) => {
if (priority && isPrimitive(source) && isObject(target)) return target
if (priority && isPrimitive(target) && isObject(source)) return source
const merged: Record<string, unknown> = { ...source }
for (const key in target) {
if (!isObject(target[key])) {
if ((priority && !isObject(source[key])) || !priority) {
merged[key] = target[key]
}
} else {
merged[key] = merge(source[key] as S, target[key] as T, priority)
}
}
return merged
}
29 changes: 29 additions & 0 deletions packages/tailwindcss-core/src/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isObject, isPrimitive } from "@halvaradop/ts-utility-types/utils"

/**
* Create a new object by merging two objects which prioritize the object types
*
* @param source first object to merge
* @param target second object to merge
* @param priority true if the object type should be prioritized, false otherwise
* @returns merged two objects
*/
export const merge = <S extends Record<string, unknown>, T extends Record<string, unknown>>(
source: S,
target: T,
priority: boolean = true
) => {
if (priority && isPrimitive(source) && isObject(target)) return target
if (priority && isPrimitive(target) && isObject(source)) return source
const merged: Record<string, unknown> = { ...source }
for (const key in target) {
if (!isObject(target[key])) {
if ((priority && !isObject(source[key])) || !priority) {
merged[key] = target[key]
}
} else {
merged[key] = merge(source[key] as S, target[key] as T, priority)
}
}
return merged
}
File renamed without changes.
1 change: 0 additions & 1 deletion packages/tailwindcss-core/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,4 @@ describe("Merge objects", () => {
const target = { b: { d: 4 }, c: 5 }
expect(merge(source, target, false)).toEqual({ a: 1, b: { c: 2, d: 4 }, c: 5 })
})

})
2 changes: 1 addition & 1 deletion packages/tailwindcss-core/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "tsup"
import { tsupConfig } from "./src/index.js"
import { tsupConfig } from "./src/tsup.config.base.js"

export default defineConfig(tsupConfig)
4 changes: 2 additions & 2 deletions packages/tailwindcss-utilities/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, test } from "vitest"
import { extractClasses } from "@halvaradop/tailwindcss-core/utils"
import plugin from "../src"
import { extractClasses } from "@halvaradop/tailwindcss-core/css"
import plugin from "../src/index.js"

const generateClasses = extractClasses(plugin)

Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-utilities/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "tsup"
import { tsupConfig } from "@halvaradop/tailwindcss-core"
import { tsupConfig } from "@halvaradop/tailwindcss-core/tsup.config.base"

export default defineConfig(tsupConfig)

0 comments on commit eb4a943

Please sign in to comment.