@telegraph/style-engine 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/dist/cjs/postcss.js +1 -1
- package/dist/cjs/postcss.js.map +1 -1
- package/dist/esm/postcss.mjs +96 -63
- package/dist/esm/postcss.mjs.map +1 -1
- package/dist/types/plugins/postcss.d.ts.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @telegraph/style-engine
|
|
2
2
|
|
|
3
|
+
## 0.3.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#740](https://github.com/knocklabs/telegraph/pull/740) [`3de7713`](https://github.com/knocklabs/telegraph/commit/3de7713a8a4bba66ad07e8b3eb40acffcf79b5a4) Thanks [@kylemcd](https://github.com/kylemcd)! - Fix PostCSS plugin not discovering @telegraph/\* deps in monorepo builds
|
|
8
|
+
|
|
9
|
+
The plugin always read from `process.cwd()/package.json` to find which packages to inject CSS for. In a Turborepo monorepo, `process.cwd()` is the repo root — which has no `@telegraph/*` deps — so nothing was injected. This caused `@telegraph/toggle` CSS to be silently missing when using `@telegraph components`.
|
|
10
|
+
|
|
11
|
+
The fix uses `root.source?.input?.file` (the absolute path of the CSS file being processed) to walk up and find the nearest `package.json`, which is always the consumer package rather than the repo root. Also fixes `dep.path` resolution for both `workspace:` and semver deps that are hoisted to the monorepo root's `node_modules`.
|
|
12
|
+
|
|
13
|
+
## 0.3.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [[`d3b6fee`](https://github.com/knocklabs/telegraph/commit/d3b6fee0e7cd308151efdc5921164d324ccaf045)]:
|
|
18
|
+
- @telegraph/tokens@0.2.0
|
|
19
|
+
|
|
3
20
|
## 0.3.0
|
|
4
21
|
|
|
5
22
|
### Minor Changes
|
package/dist/cjs/postcss.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const i=require("node:path"),p=require("node:fs"),P=require("postcss");function j(t){let s=t;for(;;){const e=i.join(s,"package.json");if(p.existsSync(e))return s;const n=i.dirname(s);if(n===s)return;s=n}}function O(t=process.cwd()){let s=t,e=!0;for(;e;){const n=i.join(s,"package.json");if(p.existsSync(n))try{if(JSON.parse(p.readFileSync(n,"utf8")).workspaces)return s}catch{}const c=i.dirname(s);c===s&&(e=!1),s=c}}const m=(t,s)=>{var e;if(!(!t.dependencies||((e=Object.keys(t.dependencies))==null?void 0:e.length)===0))return Object.entries(t.dependencies).reduce((n,[c,o])=>(c.startsWith("@telegraph/")&&(n[c]={name:c,version:o,path:i.resolve(s,"node_modules",c)}),n),{})};function S(t){const s=t?i.dirname(t):process.cwd(),e=j(s)??process.cwd(),n=i.resolve(e,"package.json"),c=JSON.parse(p.readFileSync(n,"utf8")),o=m(c,e),l=O(e),k=u=>{if(!u||Object.keys(u).length===0)return u;const r={...u};for(const a of Object.values(u)){let d,f;if(a.version.includes("workspace:")&&l)d=i.resolve(l,"node_modules",a.name,"package.json"),f=l,r[a.name]={...a,path:i.resolve(l,"node_modules",a.name)};else{const h=i.resolve(e,"node_modules",a.name,"package.json"),g=l&&i.resolve(l,"node_modules",a.name,"package.json");p.existsSync(h)?(d=h,f=e):g&&p.existsSync(g)?(d=g,f=l,r[a.name]={...a,path:i.resolve(l,"node_modules",a.name)}):(d=h,f=e)}try{const h=JSON.parse(p.readFileSync(d,"utf8")),g=m(h,f);if(Object.assign(r,g),g){const y=k(g);Object.assign(r,y)}}catch{continue}}return r};return o?k(o):{}}function v({fileName:t="default.css",path:s}){try{const e=`${s}/dist/css/${t}`,n=i.resolve(e);return p.existsSync(n)?p.readFileSync(n,"utf8"):null}catch{return null}}const F=async({root:t,cssFilePath:s,config:e})=>{const n=S(s),c=e.components===!0?Object.values(n).filter(r=>!r.name.includes("@telegraph/tokens")):[],o=e.tokens.length>0?Object.values(n).filter(r=>r.name.includes("@telegraph/tokens")):[],l=c.map(r=>v({path:r.path})).filter(Boolean),k=e.tokens.map(r=>o.map(d=>v({path:d.path,fileName:`${r}.css`})).filter(Boolean)).filter(Boolean),u=[...l,...k];for(const r of u){const a=P.parse(r);t.append(a)}},J=()=>({postcssPlugin:"@telegraph/style-engine",plugins:[{postcssPlugin:"telegraph",AtRule:{telegraph(){}},async Once(t){var n,c;const s=(c=(n=t.source)==null?void 0:n.input)==null?void 0:c.file,e={tokens:[],components:!1};t.walkAtRules("telegraph",o=>{o.params==="components"&&(e.components=!0,o.remove()),o.params==="tokens-light"&&(e.tokens.push("light"),o.remove()),o.params==="tokens-dark"&&(e.tokens.push("dark"),o.remove()),o.params==="tokens"&&(e.tokens.push("default"),o.remove())}),await F({root:t,cssFilePath:s,config:{tokens:e.tokens,components:e.components}})}}]}),b=Object.assign(J,{postcss:!0});module.exports=b;
|
|
2
2
|
//# sourceMappingURL=postcss.js.map
|
package/dist/cjs/postcss.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postcss.js","sources":["../../src/plugins/postcss.ts"],"sourcesContent":["import { type AcceptedPlugin, type PluginCreator, type Root } from \"postcss\";\n\n// Using require() instead of import to prevent ESM-related bugs in PostCSS.\n// ESM = ECMAScript Modules (the import/export syntax)\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodePath = require(\"node:path\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodeFs = require(\"node:fs\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst postcss = require(\"postcss\");\n\ntype PkgJson = {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n};\n\ntype DepObject = Record<\n string,\n {\n name: string;\n version: string;\n path: string;\n }\n>;\n\n/**\n * Traverses up the directory tree to find the root of the monorepo by looking for a package.json with workspaces.\n * This is needed to properly resolve workspace dependencies in a monorepo setup.\n */\nfunction findMonorepoRoot(start = process.cwd()): string | undefined {\n let current = start;\n let run = true;\n\n while (run) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n try {\n const pkg = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n if (pkg.workspaces) {\n return current;\n }\n } catch {\n // malformed package.json, skip\n }\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) run = false;\n current = parent;\n }\n\n return undefined;\n}\n\n/**\n * Extracts all @telegraph/* dependencies from a package.json file and returns them with their paths.\n * This is used to find all Telegraph packages that might contain CSS we need to include.\n */\nconst getTelegraphDepsFromPackageJson = (\n pkg: PkgJson,\n pkgPath: string,\n): DepObject | undefined => {\n if (!pkg.dependencies || Object.keys(pkg.dependencies)?.length === 0) return;\n\n return Object.entries(pkg.dependencies).reduce((acc, [dep, version]) => {\n if (dep.startsWith(\"@telegraph/\")) {\n acc[dep] = {\n name: dep,\n version,\n path: nodePath.resolve(pkgPath, \"node_modules\", dep),\n };\n }\n return acc;\n }, {} as DepObject);\n};\n\n/**\n * Gets all Telegraph dependencies recursively, including dependencies of dependencies.\n * Handles both normal npm dependencies and workspace dependencies in a monorepo.\n */\nfunction getTelegraphDeps(): DepObject {\n const pkgPath = nodePath.resolve(process.cwd(), \"package.json\");\n const pkg = JSON.parse(nodeFs.readFileSync(pkgPath, \"utf8\"));\n const topLevelDeps = getTelegraphDepsFromPackageJson(pkg, process.cwd());\n const monorepoRoot = findMonorepoRoot();\n\n const recursivelyGetTelegraphDeps = (deps: DepObject): DepObject => {\n if (!deps || Object.keys(deps).length === 0) {\n return deps;\n }\n\n const allDeps = { ...deps };\n\n for (const dep of Object.values(deps)) {\n let pkgJsonPath: string;\n let searchPath: string;\n\n if (dep.version.includes(\"workspace:\") && monorepoRoot) {\n pkgJsonPath = nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = monorepoRoot;\n } else {\n pkgJsonPath = nodePath.resolve(\n process.cwd(),\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = process.cwd();\n }\n\n try {\n const pkgJson = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n const deps = getTelegraphDepsFromPackageJson(pkgJson, searchPath);\n\n // Merge new dependencies into allDeps\n Object.assign(allDeps, deps);\n\n if (deps) {\n // Recursively get dependencies of dependencies\n const nestedDeps = recursivelyGetTelegraphDeps(deps);\n Object.assign(allDeps, nestedDeps);\n }\n } catch (_err) {\n // Skip if package.json cannot be read\n continue;\n }\n }\n\n return allDeps;\n };\n\n if (topLevelDeps) {\n return recursivelyGetTelegraphDeps(topLevelDeps);\n }\n\n return {};\n}\n\ntype GetCssStylesParams = {\n fileName?: string;\n path: string;\n};\n\n/**\n * Reads CSS file content from a Telegraph package's dist/css directory.\n * Returns null if the file doesn't exist or can't be read.\n */\nfunction getCssStyles({\n fileName = \"default.css\",\n path,\n}: GetCssStylesParams): string | null {\n try {\n const cssPath = `${path}/dist/css/${fileName}`;\n const pkgPath = nodePath.resolve(cssPath);\n\n if (nodeFs.existsSync(pkgPath)) {\n return nodeFs.readFileSync(pkgPath, \"utf8\");\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\ntype BuildTelegraphCssParams = {\n root: Root;\n config: {\n tokens: Array<\"light\" | \"dark\" | \"default\">;\n components: boolean;\n };\n};\n\n/**\n * Main function that builds the final CSS by:\n * 1. Getting all Telegraph dependencies\n * 2. Filtering for either token packages or component packages based on config\n * 3. Reading their CSS files\n * 4. Appending all CSS to the PostCSS Root node\n */\nconst buildTelegraphCss = async ({ root, config }: BuildTelegraphCssParams) => {\n const deps = getTelegraphDeps();\n\n const depsWithoutTokens =\n config.components === true\n ? Object.values(deps).filter(\n (dep) => !dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const tokensDeps =\n config.tokens.length > 0\n ? Object.values(deps).filter((dep) =>\n dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const cssFiles = depsWithoutTokens\n .map((dep) => getCssStyles({ path: dep.path }))\n .filter(Boolean) as Array<string>;\n\n const tokensCssFiles = config.tokens\n .map((token) => {\n const cssFile = tokensDeps.map((dep) =>\n getCssStyles({ path: dep.path, fileName: `${token}.css` }),\n );\n\n return cssFile.filter(Boolean) as Array<string>;\n })\n .filter(Boolean);\n\n const allCssFiles = [...cssFiles, ...tokensCssFiles];\n\n for (const content of allCssFiles) {\n const parsed = postcss.parse(content);\n root.append(parsed);\n }\n};\n\n/**\n * PostCSS plugin that processes @telegraph rules in CSS files.\n * It looks for @telegraph tokens, @telegraph tokens-light, @telegraph tokens-dark,\n * and @telegraph components rules and includes the appropriate CSS from Telegraph packages.\n */\nconst styleEnginePostCssPlugin = (): AcceptedPlugin => {\n return {\n postcssPlugin: \"@telegraph/style-engine\",\n plugins: [\n {\n postcssPlugin: \"telegraph\",\n AtRule: {\n telegraph() {},\n },\n async Once(root) {\n const run = {\n tokens: [] as BuildTelegraphCssParams[\"config\"][\"tokens\"],\n components: false,\n };\n root.walkAtRules(\"telegraph\", (atRule) => {\n if (atRule.params === \"components\") {\n run.components = true;\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-light\") {\n run.tokens.push(\"light\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-dark\") {\n run.tokens.push(\"dark\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens\") {\n run.tokens.push(\"default\");\n atRule.remove();\n }\n });\n\n await buildTelegraphCss({\n root,\n config: {\n tokens: run.tokens,\n components: run.components,\n },\n });\n },\n },\n ],\n };\n};\n\nexport default Object.assign(styleEnginePostCssPlugin, {\n postcss: true,\n}) as PluginCreator<Record<string, never>>;\n"],"names":["nodePath","nodeFs","postcss","findMonorepoRoot","start","current","run","pkgJsonPath","parent","getTelegraphDepsFromPackageJson","pkg","pkgPath","_a","acc","dep","version","getTelegraphDeps","topLevelDeps","monorepoRoot","recursivelyGetTelegraphDeps","deps","allDeps","searchPath","pkgJson","nestedDeps","getCssStyles","fileName","path","cssPath","buildTelegraphCss","root","config","depsWithoutTokens","tokensDeps","cssFiles","tokensCssFiles","token","allCssFiles","content","parsed","styleEnginePostCssPlugin","atRule","postcss$1"],"mappings":"aAKA,MAAMA,EAAW,QAAQ,WAAW,EAE9BC,EAAS,QAAQ,SAAS,EAE1BC,EAAU,QAAQ,SAAS,EAoBjC,SAASC,EAAiBC,EAAQ,QAAQ,MAA2B,CACnE,IAAIC,EAAUD,EACVE,EAAM,GAEV,KAAOA,GAAK,CACV,MAAMC,EAAcP,EAAS,KAAKK,EAAS,cAAc,EACzD,GAAIJ,EAAO,WAAWM,CAAW,EAC/B,GAAI,CAEF,GADY,KAAK,MAAMN,EAAO,aAAaM,EAAa,MAAM,CAAC,EACvD,WACN,OAAOF,CAEX,MAAQ,CAER,CAGF,MAAMG,EAASR,EAAS,QAAQK,CAAO,EACnCG,IAAWH,IAASC,EAAM,IAC9BD,EAAUG,CACZ,CAGF,CAMA,MAAMC,EAAkC,CACtCC,EACAC,IAC0B,OAC1B,GAAI,GAACD,EAAI,gBAAgBE,EAAA,OAAO,KAAKF,EAAI,YAAY,IAA5B,YAAAE,EAA+B,UAAW,GAEnE,OAAO,OAAO,QAAQF,EAAI,YAAY,EAAE,OAAO,CAACG,EAAK,CAACC,EAAKC,CAAO,KAC5DD,EAAI,WAAW,aAAa,IAC9BD,EAAIC,CAAG,EAAI,CACT,KAAMA,EACN,QAAAC,EACA,KAAMf,EAAS,QAAQW,EAAS,eAAgBG,CAAG,CAAA,GAGhDD,GACN,CAAA,CAAe,CACpB,EAMA,SAASG,GAA8B,CACrC,MAAML,EAAUX,EAAS,QAAQ,QAAQ,IAAA,EAAO,cAAc,EACxDU,EAAM,KAAK,MAAMT,EAAO,aAAaU,EAAS,MAAM,CAAC,EACrDM,EAAeR,EAAgCC,EAAK,QAAQ,KAAK,EACjEQ,EAAef,EAAA,EAEfgB,EAA+BC,GAA+B,CAClE,GAAI,CAACA,GAAQ,OAAO,KAAKA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type AcceptedPlugin, type PluginCreator, type Root } from \"postcss\";\n\n// Using require() instead of import to prevent ESM-related bugs in PostCSS.\n// ESM = ECMAScript Modules (the import/export syntax)\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodePath = require(\"node:path\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodeFs = require(\"node:fs\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst postcss = require(\"postcss\");\n\ntype PkgJson = {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n};\n\ntype DepObject = Record<\n string,\n {\n name: string;\n version: string;\n path: string;\n }\n>;\n\n/**\n * Traverses up the directory tree from a given start path to find the nearest package.json.\n * Returns the directory containing that package.json, or undefined if none is found.\n * Used to resolve the correct consumer package when process.cwd() may be the monorepo root.\n */\nfunction findNearestPackageJsonDir(start: string): string | undefined {\n let current = start;\n\n while (true) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n return current;\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) return undefined;\n current = parent;\n }\n}\n\n/**\n * Traverses up the directory tree to find the root of the monorepo by looking for a package.json with workspaces.\n * This is needed to properly resolve workspace dependencies in a monorepo setup.\n */\nfunction findMonorepoRoot(start = process.cwd()): string | undefined {\n let current = start;\n let run = true;\n\n while (run) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n try {\n const pkg = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n if (pkg.workspaces) {\n return current;\n }\n } catch {\n // malformed package.json, skip\n }\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) run = false;\n current = parent;\n }\n\n return undefined;\n}\n\n/**\n * Extracts all @telegraph/* dependencies from a package.json file and returns them with their paths.\n * This is used to find all Telegraph packages that might contain CSS we need to include.\n */\nconst getTelegraphDepsFromPackageJson = (\n pkg: PkgJson,\n pkgPath: string,\n): DepObject | undefined => {\n if (!pkg.dependencies || Object.keys(pkg.dependencies)?.length === 0) return;\n\n return Object.entries(pkg.dependencies).reduce((acc, [dep, version]) => {\n if (dep.startsWith(\"@telegraph/\")) {\n acc[dep] = {\n name: dep,\n version,\n path: nodePath.resolve(pkgPath, \"node_modules\", dep),\n };\n }\n return acc;\n }, {} as DepObject);\n};\n\n/**\n * Gets all Telegraph dependencies recursively, including dependencies of dependencies.\n * Handles both normal npm dependencies and workspace dependencies in a monorepo.\n *\n * @param cssFilePath - The absolute path of the CSS file being processed by PostCSS.\n * When provided, we walk up from the CSS file's directory to find the nearest\n * package.json. This ensures we read the *consumer's* package.json rather than\n * the monorepo root's package.json (which has no @telegraph/* deps), fixing the\n * Turborepo issue where process.cwd() is always the repo root during builds.\n */\nfunction getTelegraphDeps(cssFilePath?: string): DepObject {\n // Prefer to start from the CSS file's directory so we find the consumer's\n // package.json. Fall back to process.cwd() for in-memory PostCSS usage.\n const startDir = cssFilePath ? nodePath.dirname(cssFilePath) : process.cwd();\n const consumerDir = findNearestPackageJsonDir(startDir) ?? process.cwd();\n\n const pkgPath = nodePath.resolve(consumerDir, \"package.json\");\n const pkg = JSON.parse(nodeFs.readFileSync(pkgPath, \"utf8\"));\n const topLevelDeps = getTelegraphDepsFromPackageJson(pkg, consumerDir);\n const monorepoRoot = findMonorepoRoot(consumerDir);\n\n const recursivelyGetTelegraphDeps = (deps: DepObject): DepObject => {\n if (!deps || Object.keys(deps).length === 0) {\n return deps;\n }\n\n const allDeps = { ...deps };\n\n for (const dep of Object.values(deps)) {\n let pkgJsonPath: string;\n let searchPath: string;\n\n if (dep.version.includes(\"workspace:\") && monorepoRoot) {\n pkgJsonPath = nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = monorepoRoot;\n // Workspace deps are hoisted to the monorepo root's node_modules.\n // The path stored in DepObject was set relative to consumerDir when the\n // dep was first recorded, so correct it to the monorepo root here so\n // that getCssStyles() can find the actual dist/css/default.css file.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot, \"node_modules\", dep.name),\n };\n } else {\n // Non-workspace dep: try consumerDir/node_modules first, then fall\n // back to monorepoRoot/node_modules. Yarn (and other package managers)\n // hoist all deps to the root node_modules regardless of whether they\n // use workspace: protocol, so a pinned semver dep like \"0.1.1\" will\n // typically live at the monorepo root, not the consumer package dir.\n const localPkgJsonPath = nodePath.resolve(\n consumerDir,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n const rootPkgJsonPath =\n monorepoRoot &&\n nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n\n if (nodeFs.existsSync(localPkgJsonPath)) {\n pkgJsonPath = localPkgJsonPath;\n searchPath = consumerDir;\n } else if (rootPkgJsonPath && nodeFs.existsSync(rootPkgJsonPath)) {\n pkgJsonPath = rootPkgJsonPath;\n searchPath = monorepoRoot!;\n // Correct the stored path so getCssStyles() finds the right dist/css/ dir.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot!, \"node_modules\", dep.name),\n };\n } else {\n pkgJsonPath = localPkgJsonPath; // will throw on readFileSync, caught below\n searchPath = consumerDir;\n }\n }\n\n try {\n const pkgJson = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n const deps = getTelegraphDepsFromPackageJson(pkgJson, searchPath);\n\n // Merge new dependencies into allDeps\n Object.assign(allDeps, deps);\n\n if (deps) {\n // Recursively get dependencies of dependencies\n const nestedDeps = recursivelyGetTelegraphDeps(deps);\n Object.assign(allDeps, nestedDeps);\n }\n } catch (_err) {\n // Skip if package.json cannot be read\n continue;\n }\n }\n\n return allDeps;\n };\n\n if (topLevelDeps) {\n return recursivelyGetTelegraphDeps(topLevelDeps);\n }\n\n return {};\n}\n\ntype GetCssStylesParams = {\n fileName?: string;\n path: string;\n};\n\n/**\n * Reads CSS file content from a Telegraph package's dist/css directory.\n * Returns null if the file doesn't exist or can't be read.\n */\nfunction getCssStyles({\n fileName = \"default.css\",\n path,\n}: GetCssStylesParams): string | null {\n try {\n const cssPath = `${path}/dist/css/${fileName}`;\n const pkgPath = nodePath.resolve(cssPath);\n\n if (nodeFs.existsSync(pkgPath)) {\n return nodeFs.readFileSync(pkgPath, \"utf8\");\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\ntype BuildTelegraphCssParams = {\n root: Root;\n cssFilePath?: string;\n config: {\n tokens: Array<\"light\" | \"dark\" | \"default\">;\n components: boolean;\n };\n};\n\n/**\n * Main function that builds the final CSS by:\n * 1. Getting all Telegraph dependencies\n * 2. Filtering for either token packages or component packages based on config\n * 3. Reading their CSS files\n * 4. Appending all CSS to the PostCSS Root node\n */\nconst buildTelegraphCss = async ({\n root,\n cssFilePath,\n config,\n}: BuildTelegraphCssParams) => {\n const deps = getTelegraphDeps(cssFilePath);\n\n const depsWithoutTokens =\n config.components === true\n ? Object.values(deps).filter(\n (dep) => !dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const tokensDeps =\n config.tokens.length > 0\n ? Object.values(deps).filter((dep) =>\n dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const cssFiles = depsWithoutTokens\n .map((dep) => getCssStyles({ path: dep.path }))\n .filter(Boolean) as Array<string>;\n\n const tokensCssFiles = config.tokens\n .map((token) => {\n const cssFile = tokensDeps.map((dep) =>\n getCssStyles({ path: dep.path, fileName: `${token}.css` }),\n );\n\n return cssFile.filter(Boolean) as Array<string>;\n })\n .filter(Boolean);\n\n const allCssFiles = [...cssFiles, ...tokensCssFiles];\n\n for (const content of allCssFiles) {\n const parsed = postcss.parse(content);\n root.append(parsed);\n }\n};\n\n/**\n * PostCSS plugin that processes @telegraph rules in CSS files.\n * It looks for @telegraph tokens, @telegraph tokens-light, @telegraph tokens-dark,\n * and @telegraph components rules and includes the appropriate CSS from Telegraph packages.\n */\nconst styleEnginePostCssPlugin = (): AcceptedPlugin => {\n return {\n postcssPlugin: \"@telegraph/style-engine\",\n plugins: [\n {\n postcssPlugin: \"telegraph\",\n AtRule: {\n telegraph() {},\n },\n async Once(root) {\n // Use the CSS file's path so getTelegraphDeps walks up from the\n // consumer package rather than from process.cwd() (which may be the\n // monorepo root when Turborepo is running the build).\n const cssFilePath = root.source?.input?.file;\n\n const run = {\n tokens: [] as BuildTelegraphCssParams[\"config\"][\"tokens\"],\n components: false,\n };\n root.walkAtRules(\"telegraph\", (atRule) => {\n if (atRule.params === \"components\") {\n run.components = true;\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-light\") {\n run.tokens.push(\"light\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-dark\") {\n run.tokens.push(\"dark\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens\") {\n run.tokens.push(\"default\");\n atRule.remove();\n }\n });\n\n await buildTelegraphCss({\n root,\n cssFilePath,\n config: {\n tokens: run.tokens,\n components: run.components,\n },\n });\n },\n },\n ],\n };\n};\n\nexport default Object.assign(styleEnginePostCssPlugin, {\n postcss: true,\n}) as PluginCreator<Record<string, never>>;\n"],"names":["nodePath","nodeFs","postcss","findNearestPackageJsonDir","start","current","pkgJsonPath","parent","findMonorepoRoot","run","getTelegraphDepsFromPackageJson","pkg","pkgPath","_a","acc","dep","version","getTelegraphDeps","cssFilePath","startDir","consumerDir","topLevelDeps","monorepoRoot","recursivelyGetTelegraphDeps","deps","allDeps","searchPath","localPkgJsonPath","rootPkgJsonPath","pkgJson","nestedDeps","getCssStyles","fileName","path","cssPath","buildTelegraphCss","root","config","depsWithoutTokens","tokensDeps","cssFiles","tokensCssFiles","token","allCssFiles","content","parsed","styleEnginePostCssPlugin","_b","atRule","postcss$1"],"mappings":"aAKA,MAAMA,EAAW,QAAQ,WAAW,EAE9BC,EAAS,QAAQ,SAAS,EAE1BC,EAAU,QAAQ,SAAS,EAqBjC,SAASC,EAA0BC,EAAmC,CACpE,IAAIC,EAAUD,EAEd,OAAa,CACX,MAAME,EAAcN,EAAS,KAAKK,EAAS,cAAc,EAC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AK,YAAY,YAAcY,GAAW,CACpCA,EAAO,SAAW,eACpBvC,EAAI,WAAa,GACjBuC,EAAO,OAAA,GAGLA,EAAO,SAAW,iBACpBvC,EAAI,OAAO,KAAK,OAAO,EACvBuC,EAAO,OAAA,GAGLA,EAAO,SAAW,gBACpBvC,EAAI,OAAO,KAAK,MAAM,EACtBuC,EAAO,OAAA,GAGLA,EAAO,SAAW,WACpBvC,EAAI,OAAO,KAAK,SAAS,EACzBuC,EAAO,OAAA,EAEX,CAAC,EAED,MAAMb,EAAkB,CACtB,KAAAC,EACA,YAAAlB,EACA,OAAQ,CACN,OAAQT,EAAI,OACZ,WAAYA,EAAI,UAAA,CAClB,CACD,CACH,CAAA,CACF,CACF,GAIJwC,EAAe,OAAO,OAAOH,EAA0B,CACrD,QAAS,EACX,CAAC"}
|
package/dist/esm/postcss.mjs
CHANGED
|
@@ -1,83 +1,114 @@
|
|
|
1
|
-
const
|
|
2
|
-
function
|
|
3
|
-
let
|
|
4
|
-
for (;
|
|
5
|
-
const
|
|
6
|
-
if (
|
|
1
|
+
const i = require("node:path"), p = require("node:fs"), P = require("postcss");
|
|
2
|
+
function j(t) {
|
|
3
|
+
let s = t;
|
|
4
|
+
for (; ; ) {
|
|
5
|
+
const e = i.join(s, "package.json");
|
|
6
|
+
if (p.existsSync(e))
|
|
7
|
+
return s;
|
|
8
|
+
const n = i.dirname(s);
|
|
9
|
+
if (n === s) return;
|
|
10
|
+
s = n;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function O(t = process.cwd()) {
|
|
14
|
+
let s = t, e = !0;
|
|
15
|
+
for (; e; ) {
|
|
16
|
+
const n = i.join(s, "package.json");
|
|
17
|
+
if (p.existsSync(n))
|
|
7
18
|
try {
|
|
8
|
-
if (JSON.parse(
|
|
9
|
-
return
|
|
19
|
+
if (JSON.parse(p.readFileSync(n, "utf8")).workspaces)
|
|
20
|
+
return s;
|
|
10
21
|
} catch {
|
|
11
22
|
}
|
|
12
|
-
const
|
|
13
|
-
|
|
23
|
+
const c = i.dirname(s);
|
|
24
|
+
c === s && (e = !1), s = c;
|
|
14
25
|
}
|
|
15
26
|
}
|
|
16
|
-
const
|
|
17
|
-
var
|
|
18
|
-
if (!(!t.dependencies || ((
|
|
19
|
-
return Object.entries(t.dependencies).reduce((n, [
|
|
20
|
-
name:
|
|
21
|
-
version:
|
|
22
|
-
path:
|
|
27
|
+
const m = (t, s) => {
|
|
28
|
+
var e;
|
|
29
|
+
if (!(!t.dependencies || ((e = Object.keys(t.dependencies)) == null ? void 0 : e.length) === 0))
|
|
30
|
+
return Object.entries(t.dependencies).reduce((n, [c, o]) => (c.startsWith("@telegraph/") && (n[c] = {
|
|
31
|
+
name: c,
|
|
32
|
+
version: o,
|
|
33
|
+
path: i.resolve(s, "node_modules", c)
|
|
23
34
|
}), n), {});
|
|
24
35
|
};
|
|
25
|
-
function
|
|
26
|
-
const
|
|
27
|
-
if (!
|
|
28
|
-
return
|
|
29
|
-
const
|
|
30
|
-
for (const
|
|
31
|
-
let
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
function S(t) {
|
|
37
|
+
const s = t ? i.dirname(t) : process.cwd(), e = j(s) ?? process.cwd(), n = i.resolve(e, "package.json"), c = JSON.parse(p.readFileSync(n, "utf8")), o = m(c, e), l = O(e), k = (u) => {
|
|
38
|
+
if (!u || Object.keys(u).length === 0)
|
|
39
|
+
return u;
|
|
40
|
+
const r = { ...u };
|
|
41
|
+
for (const a of Object.values(u)) {
|
|
42
|
+
let d, f;
|
|
43
|
+
if (a.version.includes("workspace:") && l)
|
|
44
|
+
d = i.resolve(
|
|
45
|
+
l,
|
|
46
|
+
"node_modules",
|
|
47
|
+
a.name,
|
|
48
|
+
"package.json"
|
|
49
|
+
), f = l, r[a.name] = {
|
|
50
|
+
...a,
|
|
51
|
+
path: i.resolve(l, "node_modules", a.name)
|
|
52
|
+
};
|
|
53
|
+
else {
|
|
54
|
+
const h = i.resolve(
|
|
55
|
+
e,
|
|
56
|
+
"node_modules",
|
|
57
|
+
a.name,
|
|
58
|
+
"package.json"
|
|
59
|
+
), g = l && i.resolve(
|
|
60
|
+
l,
|
|
61
|
+
"node_modules",
|
|
62
|
+
a.name,
|
|
63
|
+
"package.json"
|
|
64
|
+
);
|
|
65
|
+
p.existsSync(h) ? (d = h, f = e) : g && p.existsSync(g) ? (d = g, f = l, r[a.name] = {
|
|
66
|
+
...a,
|
|
67
|
+
path: i.resolve(l, "node_modules", a.name)
|
|
68
|
+
}) : (d = h, f = e);
|
|
69
|
+
}
|
|
43
70
|
try {
|
|
44
|
-
const
|
|
45
|
-
if (Object.assign(
|
|
46
|
-
const
|
|
47
|
-
Object.assign(
|
|
71
|
+
const h = JSON.parse(p.readFileSync(d, "utf8")), g = m(h, f);
|
|
72
|
+
if (Object.assign(r, g), g) {
|
|
73
|
+
const y = k(g);
|
|
74
|
+
Object.assign(r, y);
|
|
48
75
|
}
|
|
49
76
|
} catch {
|
|
50
77
|
continue;
|
|
51
78
|
}
|
|
52
79
|
}
|
|
53
|
-
return
|
|
80
|
+
return r;
|
|
54
81
|
};
|
|
55
|
-
return
|
|
82
|
+
return o ? k(o) : {};
|
|
56
83
|
}
|
|
57
|
-
function
|
|
84
|
+
function v({
|
|
58
85
|
fileName: t = "default.css",
|
|
59
|
-
path:
|
|
86
|
+
path: s
|
|
60
87
|
}) {
|
|
61
88
|
try {
|
|
62
|
-
const
|
|
63
|
-
return
|
|
89
|
+
const e = `${s}/dist/css/${t}`, n = i.resolve(e);
|
|
90
|
+
return p.existsSync(n) ? p.readFileSync(n, "utf8") : null;
|
|
64
91
|
} catch {
|
|
65
92
|
return null;
|
|
66
93
|
}
|
|
67
94
|
}
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
(
|
|
75
|
-
).
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
95
|
+
const F = async ({
|
|
96
|
+
root: t,
|
|
97
|
+
cssFilePath: s,
|
|
98
|
+
config: e
|
|
99
|
+
}) => {
|
|
100
|
+
const n = S(s), c = e.components === !0 ? Object.values(n).filter(
|
|
101
|
+
(r) => !r.name.includes("@telegraph/tokens")
|
|
102
|
+
) : [], o = e.tokens.length > 0 ? Object.values(n).filter(
|
|
103
|
+
(r) => r.name.includes("@telegraph/tokens")
|
|
104
|
+
) : [], l = c.map((r) => v({ path: r.path })).filter(Boolean), k = e.tokens.map((r) => o.map(
|
|
105
|
+
(d) => v({ path: d.path, fileName: `${r}.css` })
|
|
106
|
+
).filter(Boolean)).filter(Boolean), u = [...l, ...k];
|
|
107
|
+
for (const r of u) {
|
|
108
|
+
const a = P.parse(r);
|
|
109
|
+
t.append(a);
|
|
79
110
|
}
|
|
80
|
-
},
|
|
111
|
+
}, J = () => ({
|
|
81
112
|
postcssPlugin: "@telegraph/style-engine",
|
|
82
113
|
plugins: [
|
|
83
114
|
{
|
|
@@ -87,14 +118,16 @@ const j = async ({ root: t, config: e }) => {
|
|
|
87
118
|
}
|
|
88
119
|
},
|
|
89
120
|
async Once(t) {
|
|
90
|
-
|
|
121
|
+
var n, c;
|
|
122
|
+
const s = (c = (n = t.source) == null ? void 0 : n.input) == null ? void 0 : c.file, e = {
|
|
91
123
|
tokens: [],
|
|
92
124
|
components: !1
|
|
93
125
|
};
|
|
94
|
-
t.walkAtRules("telegraph", (
|
|
95
|
-
|
|
96
|
-
}), await
|
|
126
|
+
t.walkAtRules("telegraph", (o) => {
|
|
127
|
+
o.params === "components" && (e.components = !0, o.remove()), o.params === "tokens-light" && (e.tokens.push("light"), o.remove()), o.params === "tokens-dark" && (e.tokens.push("dark"), o.remove()), o.params === "tokens" && (e.tokens.push("default"), o.remove());
|
|
128
|
+
}), await F({
|
|
97
129
|
root: t,
|
|
130
|
+
cssFilePath: s,
|
|
98
131
|
config: {
|
|
99
132
|
tokens: e.tokens,
|
|
100
133
|
components: e.components
|
|
@@ -103,10 +136,10 @@ const j = async ({ root: t, config: e }) => {
|
|
|
103
136
|
}
|
|
104
137
|
}
|
|
105
138
|
]
|
|
106
|
-
}),
|
|
139
|
+
}), b = Object.assign(J, {
|
|
107
140
|
postcss: !0
|
|
108
141
|
});
|
|
109
142
|
export {
|
|
110
|
-
|
|
143
|
+
b as default
|
|
111
144
|
};
|
|
112
145
|
//# sourceMappingURL=postcss.mjs.map
|
package/dist/esm/postcss.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postcss.mjs","sources":["../../src/plugins/postcss.ts"],"sourcesContent":["import { type AcceptedPlugin, type PluginCreator, type Root } from \"postcss\";\n\n// Using require() instead of import to prevent ESM-related bugs in PostCSS.\n// ESM = ECMAScript Modules (the import/export syntax)\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodePath = require(\"node:path\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodeFs = require(\"node:fs\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst postcss = require(\"postcss\");\n\ntype PkgJson = {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n};\n\ntype DepObject = Record<\n string,\n {\n name: string;\n version: string;\n path: string;\n }\n>;\n\n/**\n * Traverses up the directory tree to find the root of the monorepo by looking for a package.json with workspaces.\n * This is needed to properly resolve workspace dependencies in a monorepo setup.\n */\nfunction findMonorepoRoot(start = process.cwd()): string | undefined {\n let current = start;\n let run = true;\n\n while (run) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n try {\n const pkg = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n if (pkg.workspaces) {\n return current;\n }\n } catch {\n // malformed package.json, skip\n }\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) run = false;\n current = parent;\n }\n\n return undefined;\n}\n\n/**\n * Extracts all @telegraph/* dependencies from a package.json file and returns them with their paths.\n * This is used to find all Telegraph packages that might contain CSS we need to include.\n */\nconst getTelegraphDepsFromPackageJson = (\n pkg: PkgJson,\n pkgPath: string,\n): DepObject | undefined => {\n if (!pkg.dependencies || Object.keys(pkg.dependencies)?.length === 0) return;\n\n return Object.entries(pkg.dependencies).reduce((acc, [dep, version]) => {\n if (dep.startsWith(\"@telegraph/\")) {\n acc[dep] = {\n name: dep,\n version,\n path: nodePath.resolve(pkgPath, \"node_modules\", dep),\n };\n }\n return acc;\n }, {} as DepObject);\n};\n\n/**\n * Gets all Telegraph dependencies recursively, including dependencies of dependencies.\n * Handles both normal npm dependencies and workspace dependencies in a monorepo.\n */\nfunction getTelegraphDeps(): DepObject {\n const pkgPath = nodePath.resolve(process.cwd(), \"package.json\");\n const pkg = JSON.parse(nodeFs.readFileSync(pkgPath, \"utf8\"));\n const topLevelDeps = getTelegraphDepsFromPackageJson(pkg, process.cwd());\n const monorepoRoot = findMonorepoRoot();\n\n const recursivelyGetTelegraphDeps = (deps: DepObject): DepObject => {\n if (!deps || Object.keys(deps).length === 0) {\n return deps;\n }\n\n const allDeps = { ...deps };\n\n for (const dep of Object.values(deps)) {\n let pkgJsonPath: string;\n let searchPath: string;\n\n if (dep.version.includes(\"workspace:\") && monorepoRoot) {\n pkgJsonPath = nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = monorepoRoot;\n } else {\n pkgJsonPath = nodePath.resolve(\n process.cwd(),\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = process.cwd();\n }\n\n try {\n const pkgJson = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n const deps = getTelegraphDepsFromPackageJson(pkgJson, searchPath);\n\n // Merge new dependencies into allDeps\n Object.assign(allDeps, deps);\n\n if (deps) {\n // Recursively get dependencies of dependencies\n const nestedDeps = recursivelyGetTelegraphDeps(deps);\n Object.assign(allDeps, nestedDeps);\n }\n } catch (_err) {\n // Skip if package.json cannot be read\n continue;\n }\n }\n\n return allDeps;\n };\n\n if (topLevelDeps) {\n return recursivelyGetTelegraphDeps(topLevelDeps);\n }\n\n return {};\n}\n\ntype GetCssStylesParams = {\n fileName?: string;\n path: string;\n};\n\n/**\n * Reads CSS file content from a Telegraph package's dist/css directory.\n * Returns null if the file doesn't exist or can't be read.\n */\nfunction getCssStyles({\n fileName = \"default.css\",\n path,\n}: GetCssStylesParams): string | null {\n try {\n const cssPath = `${path}/dist/css/${fileName}`;\n const pkgPath = nodePath.resolve(cssPath);\n\n if (nodeFs.existsSync(pkgPath)) {\n return nodeFs.readFileSync(pkgPath, \"utf8\");\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\ntype BuildTelegraphCssParams = {\n root: Root;\n config: {\n tokens: Array<\"light\" | \"dark\" | \"default\">;\n components: boolean;\n };\n};\n\n/**\n * Main function that builds the final CSS by:\n * 1. Getting all Telegraph dependencies\n * 2. Filtering for either token packages or component packages based on config\n * 3. Reading their CSS files\n * 4. Appending all CSS to the PostCSS Root node\n */\nconst buildTelegraphCss = async ({ root, config }: BuildTelegraphCssParams) => {\n const deps = getTelegraphDeps();\n\n const depsWithoutTokens =\n config.components === true\n ? Object.values(deps).filter(\n (dep) => !dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const tokensDeps =\n config.tokens.length > 0\n ? Object.values(deps).filter((dep) =>\n dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const cssFiles = depsWithoutTokens\n .map((dep) => getCssStyles({ path: dep.path }))\n .filter(Boolean) as Array<string>;\n\n const tokensCssFiles = config.tokens\n .map((token) => {\n const cssFile = tokensDeps.map((dep) =>\n getCssStyles({ path: dep.path, fileName: `${token}.css` }),\n );\n\n return cssFile.filter(Boolean) as Array<string>;\n })\n .filter(Boolean);\n\n const allCssFiles = [...cssFiles, ...tokensCssFiles];\n\n for (const content of allCssFiles) {\n const parsed = postcss.parse(content);\n root.append(parsed);\n }\n};\n\n/**\n * PostCSS plugin that processes @telegraph rules in CSS files.\n * It looks for @telegraph tokens, @telegraph tokens-light, @telegraph tokens-dark,\n * and @telegraph components rules and includes the appropriate CSS from Telegraph packages.\n */\nconst styleEnginePostCssPlugin = (): AcceptedPlugin => {\n return {\n postcssPlugin: \"@telegraph/style-engine\",\n plugins: [\n {\n postcssPlugin: \"telegraph\",\n AtRule: {\n telegraph() {},\n },\n async Once(root) {\n const run = {\n tokens: [] as BuildTelegraphCssParams[\"config\"][\"tokens\"],\n components: false,\n };\n root.walkAtRules(\"telegraph\", (atRule) => {\n if (atRule.params === \"components\") {\n run.components = true;\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-light\") {\n run.tokens.push(\"light\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-dark\") {\n run.tokens.push(\"dark\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens\") {\n run.tokens.push(\"default\");\n atRule.remove();\n }\n });\n\n await buildTelegraphCss({\n root,\n config: {\n tokens: run.tokens,\n components: run.components,\n },\n });\n },\n },\n ],\n };\n};\n\nexport default Object.assign(styleEnginePostCssPlugin, {\n postcss: true,\n}) as PluginCreator<Record<string, never>>;\n"],"names":["nodePath","nodeFs","postcss","findMonorepoRoot","start","current","run","pkgJsonPath","parent","getTelegraphDepsFromPackageJson","pkg","pkgPath","_a","acc","dep","version","getTelegraphDeps","topLevelDeps","monorepoRoot","recursivelyGetTelegraphDeps","deps","allDeps","searchPath","pkgJson","nestedDeps","getCssStyles","fileName","path","cssPath","buildTelegraphCss","root","config","depsWithoutTokens","tokensDeps","cssFiles","tokensCssFiles","token","allCssFiles","content","parsed","styleEnginePostCssPlugin","atRule","postcss$1"],"mappings":"AAKA,MAAMA,IAAW,QAAQ,WAAW,GAE9BC,IAAS,QAAQ,SAAS,GAE1BC,IAAU,QAAQ,SAAS;AAoBjC,SAASC,EAAiBC,IAAQ,QAAQ,OAA2B;AACnE,MAAIC,IAAUD,GACVE,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C,IAAe,OAAO,OAAOF,GAA0B;AAAA,EACrD,SAAS;AACX,CAAC;"}
|
|
1
|
+
{"version":3,"file":"postcss.mjs","sources":["../../src/plugins/postcss.ts"],"sourcesContent":["import { type AcceptedPlugin, type PluginCreator, type Root } from \"postcss\";\n\n// Using require() instead of import to prevent ESM-related bugs in PostCSS.\n// ESM = ECMAScript Modules (the import/export syntax)\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodePath = require(\"node:path\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst nodeFs = require(\"node:fs\");\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst postcss = require(\"postcss\");\n\ntype PkgJson = {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n};\n\ntype DepObject = Record<\n string,\n {\n name: string;\n version: string;\n path: string;\n }\n>;\n\n/**\n * Traverses up the directory tree from a given start path to find the nearest package.json.\n * Returns the directory containing that package.json, or undefined if none is found.\n * Used to resolve the correct consumer package when process.cwd() may be the monorepo root.\n */\nfunction findNearestPackageJsonDir(start: string): string | undefined {\n let current = start;\n\n while (true) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n return current;\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) return undefined;\n current = parent;\n }\n}\n\n/**\n * Traverses up the directory tree to find the root of the monorepo by looking for a package.json with workspaces.\n * This is needed to properly resolve workspace dependencies in a monorepo setup.\n */\nfunction findMonorepoRoot(start = process.cwd()): string | undefined {\n let current = start;\n let run = true;\n\n while (run) {\n const pkgJsonPath = nodePath.join(current, \"package.json\");\n if (nodeFs.existsSync(pkgJsonPath)) {\n try {\n const pkg = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n if (pkg.workspaces) {\n return current;\n }\n } catch {\n // malformed package.json, skip\n }\n }\n\n const parent = nodePath.dirname(current);\n if (parent === current) run = false;\n current = parent;\n }\n\n return undefined;\n}\n\n/**\n * Extracts all @telegraph/* dependencies from a package.json file and returns them with their paths.\n * This is used to find all Telegraph packages that might contain CSS we need to include.\n */\nconst getTelegraphDepsFromPackageJson = (\n pkg: PkgJson,\n pkgPath: string,\n): DepObject | undefined => {\n if (!pkg.dependencies || Object.keys(pkg.dependencies)?.length === 0) return;\n\n return Object.entries(pkg.dependencies).reduce((acc, [dep, version]) => {\n if (dep.startsWith(\"@telegraph/\")) {\n acc[dep] = {\n name: dep,\n version,\n path: nodePath.resolve(pkgPath, \"node_modules\", dep),\n };\n }\n return acc;\n }, {} as DepObject);\n};\n\n/**\n * Gets all Telegraph dependencies recursively, including dependencies of dependencies.\n * Handles both normal npm dependencies and workspace dependencies in a monorepo.\n *\n * @param cssFilePath - The absolute path of the CSS file being processed by PostCSS.\n * When provided, we walk up from the CSS file's directory to find the nearest\n * package.json. This ensures we read the *consumer's* package.json rather than\n * the monorepo root's package.json (which has no @telegraph/* deps), fixing the\n * Turborepo issue where process.cwd() is always the repo root during builds.\n */\nfunction getTelegraphDeps(cssFilePath?: string): DepObject {\n // Prefer to start from the CSS file's directory so we find the consumer's\n // package.json. Fall back to process.cwd() for in-memory PostCSS usage.\n const startDir = cssFilePath ? nodePath.dirname(cssFilePath) : process.cwd();\n const consumerDir = findNearestPackageJsonDir(startDir) ?? process.cwd();\n\n const pkgPath = nodePath.resolve(consumerDir, \"package.json\");\n const pkg = JSON.parse(nodeFs.readFileSync(pkgPath, \"utf8\"));\n const topLevelDeps = getTelegraphDepsFromPackageJson(pkg, consumerDir);\n const monorepoRoot = findMonorepoRoot(consumerDir);\n\n const recursivelyGetTelegraphDeps = (deps: DepObject): DepObject => {\n if (!deps || Object.keys(deps).length === 0) {\n return deps;\n }\n\n const allDeps = { ...deps };\n\n for (const dep of Object.values(deps)) {\n let pkgJsonPath: string;\n let searchPath: string;\n\n if (dep.version.includes(\"workspace:\") && monorepoRoot) {\n pkgJsonPath = nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n searchPath = monorepoRoot;\n // Workspace deps are hoisted to the monorepo root's node_modules.\n // The path stored in DepObject was set relative to consumerDir when the\n // dep was first recorded, so correct it to the monorepo root here so\n // that getCssStyles() can find the actual dist/css/default.css file.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot, \"node_modules\", dep.name),\n };\n } else {\n // Non-workspace dep: try consumerDir/node_modules first, then fall\n // back to monorepoRoot/node_modules. Yarn (and other package managers)\n // hoist all deps to the root node_modules regardless of whether they\n // use workspace: protocol, so a pinned semver dep like \"0.1.1\" will\n // typically live at the monorepo root, not the consumer package dir.\n const localPkgJsonPath = nodePath.resolve(\n consumerDir,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n const rootPkgJsonPath =\n monorepoRoot &&\n nodePath.resolve(\n monorepoRoot,\n \"node_modules\",\n dep.name,\n \"package.json\",\n );\n\n if (nodeFs.existsSync(localPkgJsonPath)) {\n pkgJsonPath = localPkgJsonPath;\n searchPath = consumerDir;\n } else if (rootPkgJsonPath && nodeFs.existsSync(rootPkgJsonPath)) {\n pkgJsonPath = rootPkgJsonPath;\n searchPath = monorepoRoot!;\n // Correct the stored path so getCssStyles() finds the right dist/css/ dir.\n allDeps[dep.name] = {\n ...dep,\n path: nodePath.resolve(monorepoRoot!, \"node_modules\", dep.name),\n };\n } else {\n pkgJsonPath = localPkgJsonPath; // will throw on readFileSync, caught below\n searchPath = consumerDir;\n }\n }\n\n try {\n const pkgJson = JSON.parse(nodeFs.readFileSync(pkgJsonPath, \"utf8\"));\n const deps = getTelegraphDepsFromPackageJson(pkgJson, searchPath);\n\n // Merge new dependencies into allDeps\n Object.assign(allDeps, deps);\n\n if (deps) {\n // Recursively get dependencies of dependencies\n const nestedDeps = recursivelyGetTelegraphDeps(deps);\n Object.assign(allDeps, nestedDeps);\n }\n } catch (_err) {\n // Skip if package.json cannot be read\n continue;\n }\n }\n\n return allDeps;\n };\n\n if (topLevelDeps) {\n return recursivelyGetTelegraphDeps(topLevelDeps);\n }\n\n return {};\n}\n\ntype GetCssStylesParams = {\n fileName?: string;\n path: string;\n};\n\n/**\n * Reads CSS file content from a Telegraph package's dist/css directory.\n * Returns null if the file doesn't exist or can't be read.\n */\nfunction getCssStyles({\n fileName = \"default.css\",\n path,\n}: GetCssStylesParams): string | null {\n try {\n const cssPath = `${path}/dist/css/${fileName}`;\n const pkgPath = nodePath.resolve(cssPath);\n\n if (nodeFs.existsSync(pkgPath)) {\n return nodeFs.readFileSync(pkgPath, \"utf8\");\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\ntype BuildTelegraphCssParams = {\n root: Root;\n cssFilePath?: string;\n config: {\n tokens: Array<\"light\" | \"dark\" | \"default\">;\n components: boolean;\n };\n};\n\n/**\n * Main function that builds the final CSS by:\n * 1. Getting all Telegraph dependencies\n * 2. Filtering for either token packages or component packages based on config\n * 3. Reading their CSS files\n * 4. Appending all CSS to the PostCSS Root node\n */\nconst buildTelegraphCss = async ({\n root,\n cssFilePath,\n config,\n}: BuildTelegraphCssParams) => {\n const deps = getTelegraphDeps(cssFilePath);\n\n const depsWithoutTokens =\n config.components === true\n ? Object.values(deps).filter(\n (dep) => !dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const tokensDeps =\n config.tokens.length > 0\n ? Object.values(deps).filter((dep) =>\n dep.name.includes(\"@telegraph/tokens\"),\n )\n : [];\n\n const cssFiles = depsWithoutTokens\n .map((dep) => getCssStyles({ path: dep.path }))\n .filter(Boolean) as Array<string>;\n\n const tokensCssFiles = config.tokens\n .map((token) => {\n const cssFile = tokensDeps.map((dep) =>\n getCssStyles({ path: dep.path, fileName: `${token}.css` }),\n );\n\n return cssFile.filter(Boolean) as Array<string>;\n })\n .filter(Boolean);\n\n const allCssFiles = [...cssFiles, ...tokensCssFiles];\n\n for (const content of allCssFiles) {\n const parsed = postcss.parse(content);\n root.append(parsed);\n }\n};\n\n/**\n * PostCSS plugin that processes @telegraph rules in CSS files.\n * It looks for @telegraph tokens, @telegraph tokens-light, @telegraph tokens-dark,\n * and @telegraph components rules and includes the appropriate CSS from Telegraph packages.\n */\nconst styleEnginePostCssPlugin = (): AcceptedPlugin => {\n return {\n postcssPlugin: \"@telegraph/style-engine\",\n plugins: [\n {\n postcssPlugin: \"telegraph\",\n AtRule: {\n telegraph() {},\n },\n async Once(root) {\n // Use the CSS file's path so getTelegraphDeps walks up from the\n // consumer package rather than from process.cwd() (which may be the\n // monorepo root when Turborepo is running the build).\n const cssFilePath = root.source?.input?.file;\n\n const run = {\n tokens: [] as BuildTelegraphCssParams[\"config\"][\"tokens\"],\n components: false,\n };\n root.walkAtRules(\"telegraph\", (atRule) => {\n if (atRule.params === \"components\") {\n run.components = true;\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-light\") {\n run.tokens.push(\"light\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens-dark\") {\n run.tokens.push(\"dark\");\n atRule.remove();\n }\n\n if (atRule.params === \"tokens\") {\n run.tokens.push(\"default\");\n atRule.remove();\n }\n });\n\n await buildTelegraphCss({\n root,\n cssFilePath,\n config: {\n tokens: run.tokens,\n components: run.components,\n },\n });\n },\n },\n ],\n };\n};\n\nexport default Object.assign(styleEnginePostCssPlugin, {\n postcss: true,\n}) as PluginCreator<Record<string, never>>;\n"],"names":["nodePath","nodeFs","postcss","findNearestPackageJsonDir","start","current","pkgJsonPath","parent","findMonorepoRoot","run","getTelegraphDepsFromPackageJson","pkg","pkgPath","_a","acc","dep","version","getTelegraphDeps","cssFilePath","startDir","consumerDir","topLevelDeps","monorepoRoot","recursivelyGetTelegraphDeps","deps","allDeps","searchPath","localPkgJsonPath","rootPkgJsonPath","pkgJson","nestedDeps","getCssStyles","fileName","path","cssPath","buildTelegraphCss","root","config","depsWithoutTokens","tokensDeps","cssFiles","tokensCssFiles","token","allCssFiles","content","parsed","styleEnginePostCssPlugin","_b","atRule","postcss$1"],"mappings":"AAKA,MAAMA,IAAW,QAAQ,WAAW,GAE9BC,IAAS,QAAQ,SAAS,GAE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@
|
|
|
1
|
-
{"version":3,"file":"postcss.d.ts","sourceRoot":"","sources":["../../../src/plugins/postcss.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,aAAa,EAAa,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"postcss.d.ts","sourceRoot":"","sources":["../../../src/plugins/postcss.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,aAAa,EAAa,MAAM,SAAS,CAAC;wBAsWvE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAF1C,wBAE2C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telegraph/style-engine",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "A wrappar around vanilla extract to style telegraph",
|
|
5
5
|
"repository": "https://github.com/knocklabs/telegraph/tree/main/packages/style-engine",
|
|
6
6
|
"author": "@knocklabs",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"preview": "vite preview"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@telegraph/tokens": "^0.
|
|
40
|
+
"@telegraph/tokens": "^0.2.0",
|
|
41
41
|
"postcss": "^8.5.6"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
@@ -45,12 +45,12 @@
|
|
|
45
45
|
"@knocklabs/typescript-config": "^0.0.2",
|
|
46
46
|
"@telegraph/prettier-config": "^0.0.7",
|
|
47
47
|
"@telegraph/vite-config": "^0.0.15",
|
|
48
|
-
"@types/node": "^25.
|
|
48
|
+
"@types/node": "^25.5.0",
|
|
49
49
|
"@types/postcss-import": "^14.0.3",
|
|
50
50
|
"@types/react": "^19.2.9",
|
|
51
|
-
"eslint": "^10.0
|
|
51
|
+
"eslint": "^10.2.0",
|
|
52
52
|
"globby": "^16.1.1",
|
|
53
|
-
"lightningcss": "^1.
|
|
53
|
+
"lightningcss": "^1.32.0",
|
|
54
54
|
"react": "^19.2.4",
|
|
55
55
|
"react-dom": "^19.2.4",
|
|
56
56
|
"typescript": "^5.9.3",
|