Skip to content

Commit 6139fc3

Browse files
committed
fix: support twind config in src and public folder
1 parent 34d07f1 commit 6139fc3

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ npm install --save-dev typescript @twind/typescript-plugin
4848

4949
This plugin requires TypeScript 4.1 or later. It can provide intellisense in both JavaScript and TypeScript files within any editor that uses TypeScript to power their language features. This includes [VS Code](https://code.visualstudio.com), [Sublime with the TypeScript plugin](https://github.com/Microsoft/TypeScript-Sublime-Plugin), [Atom with the TypeScript plugin](https://atom.io/packages/atom-typescript), [Visual Studio](https://www.visualstudio.com), and others.
5050

51-
If you have a custom twind configuration you need to extract that into an own file. Create a `twind.config.{ts,js,cjs,mjs}` in your root folder. Here is using a custom plugin:
51+
If you have a custom twind configuration you need to extract that into an own file. Create a `twind.config.{ts,js,cjs,mjs}` in the root , `src` or `public` folder. Then import it for use with setup. Here is example using a custom plugin:
5252

5353
```js
5454
import { forms, formInput } from '@twind/forms'
@@ -58,6 +58,7 @@ export default {
5858
plugins: { forms, 'form-input': formInput}
5959
}
6060

61+
// Augment the twind module to add addtional completions
6162
declare module 'twind' {
6263
interface Plugins {
6364
// forms should have been detected from setup – not need to add it

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"cssbeautify": "^0.3.1",
9999
"esbuild": "^0.9.1",
100100
"import-from": "^3.0.0",
101+
"locate-path": "^6.0.0",
101102
"match-sorter": "^6.3.0",
102103
"resolve-from": "^5.0.0",
103104
"twind": "^0.16.0",

src/load-twind-config.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import Module from 'module'
33

44
import { buildSync } from 'esbuild'
55
import findUp from 'find-up'
6+
import locatePath from 'locate-path'
67

78
import type { Configuration } from 'twind'
89

10+
const CONFIG_FILES = ['twind.config.ts', 'twind.config.mjs', 'twind.config.js', 'twind.config.cjs']
11+
912
export const findConfig = (cwd = process.cwd()): string | undefined =>
10-
findUp.sync(['twind.config.ts', 'twind.config.mjs', 'twind.config.js', 'twind.config.cjs'], {
11-
cwd,
12-
})
13+
locatePath.sync(CONFIG_FILES.map((file) => Path.resolve(cwd, 'config', file))) ||
14+
locatePath.sync(CONFIG_FILES.map((file) => Path.resolve(cwd, 'src', file))) ||
15+
locatePath.sync(CONFIG_FILES.map((file) => Path.resolve(cwd, 'public', file))) ||
16+
findUp.sync(CONFIG_FILES, { cwd })
1317

14-
export const loadConfig = (configFile: string): Configuration => {
18+
export const loadConfig = (configFile: string, cwd = process.cwd()): Configuration => {
1519
const result = buildSync({
1620
bundle: true,
1721
entryPoints: [configFile],
@@ -23,6 +27,7 @@ export const loadConfig = (configFile: string): Configuration => {
2327
minify: false,
2428
splitting: false,
2529
write: false,
30+
absWorkingDir: cwd,
2631
})
2732

2833
const module = { exports: {} as { default?: Configuration } & Configuration }
@@ -83,7 +88,7 @@ export const getConfig = (
8388
configFile ??= findConfig(cwd)
8489

8590
return {
86-
...(configFile ? loadConfig(Path.resolve(cwd, configFile)) : {}),
91+
...(configFile ? loadConfig(Path.resolve(cwd, configFile), cwd) : {}),
8792
configFile,
8893
}
8994
}

0 commit comments

Comments
 (0)