@@ -69,12 +69,15 @@ import {
69
69
} from './lsp/diagnosticsProvider'
70
70
import { doCodeActions } from 'tailwindcss-language-service/src/codeActions/codeActionProvider'
71
71
import { getDocumentColors } from 'tailwindcss-language-service/src/documentColorProvider'
72
- import { fromRatio , names as namedColors } from '@ctrl/tinycolor'
73
72
import { debounce } from 'debounce'
74
73
import { getModuleDependencies } from './util/getModuleDependencies'
75
74
import assert from 'assert'
76
75
// import postcssLoadConfig from 'postcss-load-config'
77
76
import * as parcel from './watcher/index.js'
77
+ import { generateRules } from 'tailwindcss-language-service/src/util/jit'
78
+ import { getColor } from 'tailwindcss-language-service/src/util/color'
79
+ import * as culori from 'culori'
80
+ import namedColors from 'color-name'
78
81
79
82
const CONFIG_FILE_GLOB = '{tailwind,tailwind.config}.{js,cjs}'
80
83
const TRIGGER_CHARACTERS = [
@@ -101,7 +104,7 @@ declare var __non_webpack_require__: typeof require
101
104
const connection =
102
105
process . argv . length <= 2 ? createConnection ( process . stdin , process . stdout ) : createConnection ( )
103
106
104
- console . log = connection . console . log . bind ( connection . console )
107
+ // console.log = connection.console.log.bind(connection.console)
105
108
console . error = connection . console . error . bind ( connection . console )
106
109
107
110
process . on ( 'unhandledRejection' , ( e : any ) => {
@@ -757,6 +760,9 @@ async function createProjectService(
757
760
let presetVariants : any [ ] = [ ]
758
761
let originalConfig : any
759
762
763
+ // TODO
764
+ let isV3 = semver . gte ( tailwindcss . version , '2.99.0' ) || tailwindcss . version . includes ( 'insiders' )
765
+
760
766
let hook = new Hook ( fs . realpathSync ( state . configPath ) , ( exports ) => {
761
767
originalConfig = klona ( exports )
762
768
@@ -783,7 +789,9 @@ async function createProjectService(
783
789
delete exports . mode
784
790
785
791
// TODO
786
- if ( false || ( state . modules . jit && mode === 'jit' ) ) {
792
+ let isJit = isV3 || ( state . modules . jit && mode === 'jit' )
793
+
794
+ if ( isJit ) {
787
795
state . jit = true
788
796
exports . variants = [ ]
789
797
@@ -858,32 +866,43 @@ async function createProjectService(
858
866
if ( state . jit ) {
859
867
state . jitContext = state . modules . jit . createContext . module ( state )
860
868
state . jitContext . tailwindConfig . separator = state . config . separator
869
+ if ( state . jitContext . completions ) {
870
+ state . coreUtilities = state . jitContext . completions ( ) . map ( ( item ) => {
871
+ let className = Array . isArray ( item ) ? item [ 0 ] : item
872
+ return [ className , { color : getColor ( state , className ) } ]
873
+ } )
874
+ }
861
875
}
862
876
863
877
let postcssResult : Result
864
- try {
865
- postcssResult = await postcss
866
- . module ( [
867
- // ...state.postcssPlugins.before.map((x) => x()),
868
- tailwindcss . module ( state . configPath ) ,
869
- // ...state.postcssPlugins.after.map((x) => x()),
870
- ] )
871
- . process (
872
- [
873
- semver . gte ( tailwindcss . version , '0.99.0' ) ? 'base' : 'preflight' ,
874
- 'components' ,
875
- 'utilities' ,
876
- ]
877
- . map ( ( x ) => `/*__tw_intellisense_layer_${ x } __*/\n@tailwind ${ x } ;` )
878
- . join ( '\n' ) ,
879
- {
880
- from : undefined ,
881
- }
882
- )
883
- } catch ( error ) {
884
- throw error
885
- } finally {
878
+
879
+ if ( state . coreUtilities ) {
886
880
hook . unhook ( )
881
+ } else {
882
+ try {
883
+ postcssResult = await postcss
884
+ . module ( [
885
+ // ...state.postcssPlugins.before.map((x) => x()),
886
+ tailwindcss . module ( state . configPath ) ,
887
+ // ...state.postcssPlugins.after.map((x) => x()),
888
+ ] )
889
+ . process (
890
+ [
891
+ semver . gte ( tailwindcss . version , '0.99.0' ) ? 'base' : 'preflight' ,
892
+ 'components' ,
893
+ 'utilities' ,
894
+ ]
895
+ . map ( ( x ) => `/*__tw_intellisense_layer_${ x } __*/\n@tailwind ${ x } ;` )
896
+ . join ( '\n' ) ,
897
+ {
898
+ from : undefined ,
899
+ }
900
+ )
901
+ } catch ( error ) {
902
+ throw error
903
+ } finally {
904
+ hook . unhook ( )
905
+ }
887
906
}
888
907
889
908
if ( state . dependencies ) {
@@ -895,7 +914,9 @@ async function createProjectService(
895
914
state . configId = getConfigId ( state . configPath , state . dependencies )
896
915
897
916
state . plugins = await getPlugins ( originalConfig )
898
- state . classNames = ( await extractClassNames ( postcssResult . root ) ) as ClassNames
917
+ if ( postcssResult ) {
918
+ state . classNames = ( await extractClassNames ( postcssResult . root ) ) as ClassNames
919
+ }
899
920
state . variants = getVariants ( state )
900
921
901
922
let screens = dlv ( state . config , 'theme.screens' , dlv ( state . config , 'screens' , { } ) )
@@ -969,16 +990,25 @@ async function createProjectService(
969
990
let currentColor = match [ 1 ]
970
991
971
992
let isNamedColor = colorNames . includes ( currentColor )
972
- let color = fromRatio ( {
993
+
994
+ let color : culori . RgbColor = {
995
+ mode : 'rgb' ,
973
996
r : params . color . red ,
974
997
g : params . color . green ,
975
998
b : params . color . blue ,
976
- a : params . color . alpha ,
977
- } )
999
+ alpha : params . color . alpha ,
1000
+ }
1001
+
1002
+ let hexValue = culori . formatHex8 ( color )
1003
+
1004
+ if ( ! isNamedColor && ( currentColor . length === 4 || currentColor . length === 5 ) ) {
1005
+ let [ , ...chars ] =
1006
+ hexValue . match ( / ^ # ( [ a - f \d ] ) \1( [ a - f \d ] ) \2( [ a - f \d ] ) \3(?: ( [ a - f \d ] ) \4) ? $ / i) ?? [ ]
1007
+ if ( chars . length ) {
1008
+ hexValue = `#${ chars . filter ( Boolean ) . join ( '' ) } `
1009
+ }
1010
+ }
978
1011
979
- let hexValue = color . toHex8String (
980
- ! isNamedColor && ( currentColor . length === 4 || currentColor . length === 5 )
981
- )
982
1012
if ( hexValue . length === 5 ) {
983
1013
hexValue = hexValue . replace ( / f $ / , '' )
984
1014
} else if ( hexValue . length === 9 ) {
@@ -989,8 +1019,12 @@ async function createProjectService(
989
1019
990
1020
return [
991
1021
hexValue ,
992
- color . toRgbString ( ) . replace ( / / g, '' ) ,
993
- color . toHslString ( ) . replace ( / / g, '' ) ,
1022
+ culori . formatRgb ( color ) . replace ( / / g, '' ) ,
1023
+ culori
1024
+ . formatHsl ( color )
1025
+ . replace ( / / g, '' )
1026
+ // round numbers
1027
+ . replace ( / \d + \. \d + ( % ? ) / g, ( value , suffix ) => `${ Math . round ( parseFloat ( value ) ) } ${ suffix } ` ) ,
994
1028
] . map ( ( value ) => ( { label : `${ prefix } -[${ value } ]` } ) )
995
1029
} ,
996
1030
}
0 commit comments