@@ -146,7 +146,98 @@ module.exports = function (
146146 // the browserslist targets.
147147 ! useTailwind && require ( 'postcss-normalize' ) ,
148148 // Resolution indepedence support
149- app . ri !== false && require ( 'postcss-resolution-independence' ) ( app . ri )
149+ app . ri !== false && require ( 'postcss-resolution-independence' ) ( app . ri ) ,
150+ // Support importing JSON files with ~ alias - custom plugin (must run first)
151+ {
152+ postcssPlugin : 'postcss-import-json-tilde' ,
153+ Once ( root ) {
154+ // Process all @import -json rules with ~ prefix first, before other plugins
155+ root . walkAtRules ( 'import-json' , atRule => {
156+ let src = atRule . params . slice ( 1 , - 1 ) ; // Remove quotes
157+
158+ // Only handle ~ alias paths
159+ if ( src . startsWith ( '~' ) ) {
160+ const packagePath = src . substring ( 1 ) ; // Remove ~
161+
162+ try {
163+ // Use Node.js standard module resolution
164+ // This mimics webpack's ~ alias behavior
165+ const currentFileDir = path . dirname ( atRule . source . input . file || '' ) ;
166+
167+ // Try to resolve the module using require.resolve
168+ // This follows standard Node.js module resolution algorithm
169+ let resolvedPath ;
170+ try {
171+ // First try from current file's directory
172+ resolvedPath = require . resolve ( packagePath , {
173+ paths : [ currentFileDir ]
174+ } ) ;
175+ } catch ( e ) {
176+ // Fallback to current working directory
177+ resolvedPath = require . resolve ( packagePath , {
178+ paths : [ process . cwd ( ) ]
179+ } ) ;
180+ }
181+
182+ // Convert to relative path for the original plugin
183+ const relativePath = path . relative ( currentFileDir , resolvedPath ) ;
184+ atRule . params = `"${ relativePath } "` ;
185+ } catch ( error ) {
186+ // If resolution fails, try manual node_modules lookup
187+ try {
188+ let currentDir = path . dirname (
189+ atRule . source . input . file || process . cwd ( )
190+ ) ;
191+ let found = false ;
192+
193+ // Walk up directories to find node_modules
194+ while ( currentDir !== path . parse ( currentDir ) . root && ! found ) {
195+ const moduleDir = path . join (
196+ currentDir ,
197+ 'node_modules' ,
198+ packagePath
199+ ) ;
200+ if ( fs . existsSync ( moduleDir ) ) {
201+ const relativePath = path . relative (
202+ path . dirname ( atRule . source . input . file || '' ) ,
203+ moduleDir
204+ ) ;
205+ atRule . params = `"${ relativePath } "` ;
206+ found = true ;
207+ break ;
208+ }
209+ currentDir = path . dirname ( currentDir ) ;
210+ }
211+
212+ if ( ! found ) {
213+ console . warn ( `Could not resolve module path: ${ packagePath } ` ) ;
214+ }
215+ } catch ( fallbackError ) {
216+ console . warn (
217+ `Failed to resolve ${ packagePath } :` ,
218+ fallbackError . message
219+ ) ;
220+ }
221+ }
222+ }
223+ } ) ;
224+ }
225+ } ,
226+ // Support importing JSON files in CSS - original plugin (for non-~ paths)
227+ [
228+ '@daltontan/postcss-import-json' ,
229+ {
230+ map : ( selector , value ) => {
231+ if ( typeof value === 'object' && value !== null && value . $ref ) {
232+ const tokenPath = value . $ref . split ( '#/' ) [ 1 ] ;
233+ const cssVariableName = '--' + tokenPath . replace ( / \/ / g, '-' ) ;
234+
235+ return `var(${ cssVariableName } )` ;
236+ }
237+ return value ;
238+ }
239+ }
240+ ]
150241 ] . filter ( Boolean )
151242 } ,
152243 sourceMap : shouldUseSourceMap
0 commit comments