Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inject tailwind and global styles #4641

Closed
Show file tree
Hide file tree
Changes from all commits
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
61 changes: 61 additions & 0 deletions packages/components/bin/addTailwindStylesImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from "fs"
import path from "path"

const DIST_STYLES_PATH = path.resolve(__dirname, "../dist/tailwind.css")

const addGlobalStylesImport = async (dirOrFile: string): Promise<void> => {
if (fs.statSync(dirOrFile).isDirectory()) {
const dirContent = await fs.promises.readdir(dirOrFile)

dirContent.forEach(content => {
const contentPath = path.join(dirOrFile, content)
addGlobalStylesImport(contentPath)
})
return
}

const filePath = path.resolve(dirOrFile)
if (fs.statSync(filePath).isFile()) {
const sourceFile = dirOrFile
.replace(/dist\/(esm|cjs)/, "src")
.replace(/(c|m)js$/, "tsx")
if (!fs.existsSync(sourceFile)) {
return
}

const stylesPath = path.relative(path.dirname(filePath), DIST_STYLES_PATH)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Use scoped path @kaizen/components to stop agadoo failing

const fileContent = fs.readFileSync(filePath).toString()

const isESM = dirOrFile.includes("dist/esm")
if (isESM) {
fs.writeFile(
filePath,
`import "${stylesPath}"\n${fileContent}`,
err => err
)
return
}

const isCJS = dirOrFile.includes("dist/cjs")
if (isCJS) {
const fileSplit = fileContent.split("\n")
fileSplit.splice(1, 0, `require("${stylesPath}")`)
const newContent = fileSplit.join("\n")
fs.writeFile(filePath, newContent, err => err)
return
}
}
}

const DIST_DIRS = ["dist/cjs", "dist/esm"]

DIST_DIRS.forEach(dir => {
;(async () => {
try {
addGlobalStylesImport(dir)
} catch (e) {
// eslint-disable-next-line no-console
console.error("Directory not found", e)
}
})()
})
8 changes: 6 additions & 2 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
"module": "dist/esm/index.mjs",
"types": "dist/types/index.d.ts",
"sideEffects": [
"tailwind.css",
gyfchong marked this conversation as resolved.
Show resolved Hide resolved
"styles.css"
],
"scripts": {
"prepublishOnly": "pnpm build:components && pnpm build:styles && pnpm postBuild && pnpm dist:clean",
"prepublishOnly": "pnpm build:components && pnpm build:styles && pnpm build:inject-global-styles",
"build": "pnpm clean && pnpm prepublishOnly",
"test": "FORCE_COLOR=1 jest",
"test:ci": "pnpm test -- --ci",
"test:treeshake": "agadoo ./dist/esm/index.mjs",
"clean": "rimraf dist",
"build:components": "rollup -c && pnpm build:types && pnpm build:purify-style-inject",
"build:inject-global-styles": "pnpm ts-node ./bin/addTailwindStylesImport.ts",
"build:purify-style-inject": "pnpm ts-node ./bin/markStyleInjectAsPure.ts",
"build:styles": "postcss styles/global.css --output dist/global.css",
"build:styles": "pnpm build:styles:global && pnpm build:styles:tailwind",
"build:styles:global": "postcss styles/global.css --output dist/styles.css",
"build:styles:tailwind": "postcss styles/tailwind.css --output dist/tailwind.css",
"build:types": "tspc --project tsconfig.types.json",
"compile": "tsc",
"dist:clean": "rm ./dist/global.css ./dist/raw-styles.css",
Expand Down
4 changes: 2 additions & 2 deletions packages/components/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const cjsConfig = {
compilerOptions: {
esModuleInterop: false,
allowSyntheticDefaultImports: true
}
},
}),
commonjs(),
],
Expand All @@ -73,7 +73,7 @@ const esmConfig = {
...sharedConfig,
plugins: [
...sharedConfig.plugins,
typescript({ tsconfig: "./tsconfig.dist.json" })
typescript({ tsconfig: "./tsconfig.dist.json" }),
],
output: {
dir: "dist/esm",
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/Label/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const Label = ({
styles[labelType],
reversed && styles.reversed,
variant === "prominent" && styles.prominent,
disabled && styles.disabled
disabled && styles.disabled,
"!kz-text-blue-400 kz-bg-yellow-400"
)}
{...restProps}
>
Expand Down
6 changes: 0 additions & 6 deletions packages/components/styles/foundation/styles.css

This file was deleted.

7 changes: 6 additions & 1 deletion packages/components/styles/global.css
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
@import "./foundation/styles.css";
@layer tokens, normalize, reset;

@import "@kaizen/design-tokens/css/variables.css" layer(tokens);
@import "./foundation/_normalize.css" layer(normalize);
@import "./foundation/_fonts.css" layer(reset);
@import "./foundation/_reset.css" layer(reset);
3 changes: 3 additions & 0 deletions packages/components/styles/tailwind.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
17 changes: 17 additions & 0 deletions packages/components/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ts-check
/** @type {import('tailwindcss').Config} */

const { Preset } = require("@kaizen/tailwind")

module.exports = {
content: {
relative: true,
files: ["./src/**/*.tsx"],
},
presets: [Preset],
corePlugins: {
preflight: false,
},
plugins: [],
prefix: "kz-",
}
8 changes: 7 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
},
"@kaizen/components#build": {
"dependsOn": ["@kaizen/design-tokens#build"],
"inputs": ["src/**", "rollup.config.mjs", "tsconfig*.json"],
"inputs": [
"src/**",
"rollup.config.mjs",
"tsconfig*.json",
"tailwind.config.js",
"bin/**"
],
"outputs": ["dist/**"]
},
"@kaizen/components#test:treeshake": {
Expand Down
Loading