11import { Features } from '..'
22import { styleRule , toCss , walk , WalkAction , type AstNode } from '../ast'
33import type { DesignSystem } from '../design-system'
4+ import type { SourceLocation } from '../source-maps/source'
45import { segment } from '../utils/segment'
56import { applyConfigToTheme } from './apply-config-to-theme'
67import { applyKeyframesToTheme } from './apply-keyframes-to-theme'
@@ -38,9 +39,16 @@ export async function applyCompatibilityHooks({
3839 sources : { base : string ; pattern : string ; negated : boolean } [ ]
3940} ) {
4041 let features = Features . None
41- let pluginPaths : [ { id : string ; base : string ; reference : boolean } , CssPluginOptions | null ] [ ] =
42- [ ]
43- let configPaths : { id : string ; base : string ; reference : boolean } [ ] = [ ]
42+ let pluginPaths : [
43+ { id : string ; base : string ; reference : boolean ; src : SourceLocation | undefined } ,
44+ CssPluginOptions | null ,
45+ ] [ ] = [ ]
46+ let configPaths : {
47+ id : string
48+ base : string
49+ reference : boolean
50+ src : SourceLocation | undefined
51+ } [ ] = [ ]
4452
4553 walk ( ast , ( node , { parent, replaceWith, context } ) => {
4654 if ( node . kind !== 'at-rule' ) return
@@ -100,7 +108,12 @@ export async function applyCompatibilityHooks({
100108 }
101109
102110 pluginPaths . push ( [
103- { id : pluginPath , base : context . base as string , reference : ! ! context . reference } ,
111+ {
112+ id : pluginPath ,
113+ base : context . base as string ,
114+ reference : ! ! context . reference ,
115+ src : node . src ,
116+ } ,
104117 Object . keys ( options ) . length > 0 ? options : null ,
105118 ] )
106119
@@ -123,6 +136,7 @@ export async function applyCompatibilityHooks({
123136 id : node . params . slice ( 1 , - 1 ) ,
124137 base : context . base as string ,
125138 reference : ! ! context . reference ,
139+ src : node . src ,
126140 } )
127141 replaceWith ( [ ] )
128142 features |= Features . JsPluginCompat
@@ -162,25 +176,27 @@ export async function applyCompatibilityHooks({
162176
163177 let [ configs , pluginDetails ] = await Promise . all ( [
164178 Promise . all (
165- configPaths . map ( async ( { id, base, reference } ) => {
179+ configPaths . map ( async ( { id, base, reference, src } ) => {
166180 let loaded = await loadModule ( id , base , 'config' )
167181 return {
168182 path : id ,
169183 base : loaded . base ,
170184 config : loaded . module as UserConfig ,
171185 reference,
186+ src,
172187 }
173188 } ) ,
174189 ) ,
175190 Promise . all (
176- pluginPaths . map ( async ( [ { id, base, reference } , pluginOptions ] ) => {
191+ pluginPaths . map ( async ( [ { id, base, reference, src } , pluginOptions ] ) => {
177192 let loaded = await loadModule ( id , base , 'plugin' )
178193 return {
179194 path : id ,
180195 base : loaded . base ,
181196 plugin : loaded . module as Plugin ,
182197 options : pluginOptions ,
183198 reference,
199+ src,
184200 }
185201 } ) ,
186202 ) ,
@@ -215,13 +231,15 @@ function upgradeToFullPluginSupport({
215231 base : string
216232 config : UserConfig
217233 reference : boolean
234+ src : SourceLocation | undefined
218235 } [ ]
219236 pluginDetails : {
220237 path : string
221238 base : string
222239 plugin : Plugin
223240 options : CssPluginOptions | null
224241 reference : boolean
242+ src : SourceLocation | undefined
225243 } [ ]
226244} ) {
227245 let features = Features . None
@@ -231,6 +249,7 @@ function upgradeToFullPluginSupport({
231249 config : { plugins : [ detail . plugin ] } ,
232250 base : detail . base ,
233251 reference : detail . reference ,
252+ src : detail . src ,
234253 }
235254 }
236255
@@ -239,6 +258,7 @@ function upgradeToFullPluginSupport({
239258 config : { plugins : [ detail . plugin ( detail . options ) ] } ,
240259 base : detail . base ,
241260 reference : detail . reference ,
261+ src : detail . src ,
242262 }
243263 }
244264
@@ -248,15 +268,32 @@ function upgradeToFullPluginSupport({
248268 let userConfig = [ ...pluginConfigs , ...configs ]
249269
250270 let { resolvedConfig } = resolveConfig ( designSystem , [
251- { config : createCompatConfig ( designSystem . theme ) , base, reference : true } ,
271+ { config : createCompatConfig ( designSystem . theme ) , base, reference : true , src : undefined } ,
252272 ...userConfig ,
253- { config : { plugins : [ darkModePlugin ] } , base, reference : true } ,
273+ { config : { plugins : [ darkModePlugin ] } , base, reference : true , src : undefined } ,
254274 ] )
255275 let { resolvedConfig : resolvedUserConfig , replacedThemeKeys } = resolveConfig (
256276 designSystem ,
257277 userConfig ,
258278 )
259279
280+ let pluginApiConfig = {
281+ designSystem,
282+ ast,
283+ resolvedConfig,
284+ featuresRef : {
285+ set current ( value : number ) {
286+ features |= value
287+ } ,
288+ } ,
289+ }
290+
291+ let sharedPluginApi = buildPluginApi ( {
292+ ...pluginApiConfig ,
293+ referenceMode : false ,
294+ src : undefined ,
295+ } )
296+
260297 // Replace `resolveThemeValue` with a version that is backwards compatible
261298 // with dot-notation but also aware of any JS theme configurations registered
262299 // by plugins or JS config files. This is significantly slower than just
@@ -270,7 +307,7 @@ function upgradeToFullPluginSupport({
270307 return defaultResolveThemeValue ( path , forceInline )
271308 }
272309
273- let resolvedValue = pluginApi . theme ( path , undefined )
310+ let resolvedValue = sharedPluginApi . theme ( path , undefined )
274311
275312 if ( Array . isArray ( resolvedValue ) && resolvedValue . length === 2 ) {
276313 // When a tuple is returned, return the first element
@@ -285,27 +322,17 @@ function upgradeToFullPluginSupport({
285322 }
286323 }
287324
288- let pluginApiConfig = {
289- designSystem,
290- ast,
291- resolvedConfig,
292- featuresRef : {
293- set current ( value : number ) {
294- features |= value
295- } ,
296- } ,
297- }
298-
299- let pluginApi = buildPluginApi ( { ...pluginApiConfig , referenceMode : false } )
300- let referenceModePluginApi = undefined
325+ for ( let { handler, reference, src } of resolvedConfig . plugins ) {
326+ // Each plugin gets its own instance of the plugin API because nodes added
327+ // to the AST may need to point to the `@config` or `@plugin` that they
328+ // originated from
329+ let api = buildPluginApi ( {
330+ ...pluginApiConfig ,
331+ referenceMode : reference ?? false ,
332+ src,
333+ } )
301334
302- for ( let { handler, reference } of resolvedConfig . plugins ) {
303- if ( reference ) {
304- referenceModePluginApi ||= buildPluginApi ( { ...pluginApiConfig , referenceMode : true } )
305- handler ( referenceModePluginApi )
306- } else {
307- handler ( pluginApi )
308- }
335+ handler ( api )
309336 }
310337
311338 // Merge the user-configured theme keys into the design system. The compat
0 commit comments