-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for TypeScript config path mapping in CSS files (#1106)
This is a work in progress with a handful of things that need improvements regarding stylesheet loading / editing when in a v4 project. Fixes #1103 Fixes #1100 - [x] Recover from missing stylesheet imports - [ ] Recover from unparsable stylesheet imports (not sure if possible) - [x] Read path aliases from tsconfig.json - [x] Log errors from analyzing CSS files during the resolve imports stage (the cause of #1100) - [x] Watch for tsconfig.json file changes and reload when they change (or maybe only when the list of seen `paths` do) - [x] Consider path aliases when doing project discovery - [x] Consider path aliases when loading the design system - [x] Allow in `@import` - [x] Allow in `@reference` - [x] Allow in `@config` - [x] Allow in `@plugin` - [ ] Consider path aliases when producing diagnostics - [ ] Allow in `@import` - [ ] Allow in `@reference` - [x] Allow in `@config` (nothing to do here) - [x] Allow in `@plugin` (nothing to do here) - [ ] Consider path aliases when generating document links - [ ] Allow in `@import` (no upstream support; non-trivial) - [ ] Allow in `@reference` (no upstream support in `@import`; non-trivial) - [x] Allow in `@config` - [x] Allow in `@plugin` - [ ] Consider path aliases when offering completions - [ ] Allow in `@import` (no upstream support; non-trivial) - [ ] Allow in `@reference` (no upstream support in `@import`; non-trivial) - [x] Allow in `@config` - [x] Allow in `@plugin`
- Loading branch information
1 parent
9dfa540
commit e871fc9
Showing
44 changed files
with
1,341 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/tailwindcss-language-server/src/css/fix-relative-paths.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 56 additions & 19 deletions
75
packages/tailwindcss-language-server/src/css/resolve-css-imports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,62 @@ | ||
import * as fs from 'node:fs/promises' | ||
import postcss from 'postcss' | ||
import postcssImport from 'postcss-import' | ||
import { createResolver } from '../util/resolve' | ||
import { fixRelativePaths } from './fix-relative-paths' | ||
import { Resolver } from '../resolver' | ||
|
||
const resolver = createResolver({ | ||
extensions: ['.css'], | ||
mainFields: ['style'], | ||
conditionNames: ['style'], | ||
}) | ||
|
||
const resolveImports = postcss([ | ||
postcssImport({ | ||
resolve: (id, base) => resolveCssFrom(base, id), | ||
}), | ||
fixRelativePaths(), | ||
]) | ||
|
||
export function resolveCssImports() { | ||
return resolveImports | ||
} | ||
export function resolveCssImports({ | ||
resolver, | ||
loose = false, | ||
}: { | ||
resolver: Resolver | ||
loose?: boolean | ||
}) { | ||
return postcss([ | ||
// Hoist imports to the top of the file | ||
{ | ||
postcssPlugin: 'hoist-at-import', | ||
Once(root, { result }) { | ||
if (!loose) return | ||
|
||
let hoist: postcss.AtRule[] = [] | ||
let seenImportsAfterOtherNodes = false | ||
|
||
for (let node of root.nodes) { | ||
if (node.type === 'atrule' && (node.name === 'import' || node.name === 'charset')) { | ||
hoist.push(node) | ||
} else if (hoist.length > 0 && (node.type === 'atrule' || node.type === 'rule')) { | ||
seenImportsAfterOtherNodes = true | ||
} | ||
} | ||
|
||
root.prepend(hoist) | ||
|
||
if (!seenImportsAfterOtherNodes) return | ||
|
||
console.log( | ||
`hoist-at-import: The file '${result.opts.from}' contains @import rules after other at rules. This is invalid CSS and may cause problems with your build.`, | ||
) | ||
}, | ||
}, | ||
|
||
postcssImport({ | ||
async resolve(id, base) { | ||
try { | ||
return await resolver.resolveCssId(id, base) | ||
} catch (e) { | ||
// TODO: Need to test this on windows | ||
return `/virtual:missing/${id}` | ||
} | ||
}, | ||
|
||
load(filepath) { | ||
if (filepath.startsWith('/virtual:missing/')) { | ||
return Promise.resolve('') | ||
} | ||
|
||
export function resolveCssFrom(base: string, id: string) { | ||
return resolver.resolveSync({}, base, id) || id | ||
return fs.readFile(filepath, 'utf-8') | ||
}, | ||
}), | ||
fixRelativePaths(), | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.