@@ -10,6 +10,7 @@ const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'
1010const gitBin = isWindows ? 'git.exe' : 'git' ;
1111const pnpmBin = isWindows ? 'pnpm.cmd' : 'pnpm' ;
1212const pnpmLockfilePath = path . join ( repoRoot , 'pnpm-lock.yaml' ) ;
13+ const rolldownPackageJsonRelativePath = 'packages/rolldown/package.json' ;
1314const upstreamVersions = JSON . parse (
1415 readFileSync ( path . join ( repoRoot , 'packages' , 'tools' , '.upstream-versions.json' ) , 'utf-8' ) ,
1516) ;
@@ -59,6 +60,14 @@ function capture(command, args, cwd) {
5960 } ) . stdout . trim ( ) ;
6061}
6162
63+ function stringifyJson ( value ) {
64+ return JSON . stringify ( value , null , 2 ) + '\n' ;
65+ }
66+
67+ function rolldownPackageJsonPath ( rootDir = repoRoot ) {
68+ return path . join ( rootDir , rolldownPackageJsonRelativePath ) ;
69+ }
70+
6271function isGitRepo ( dir ) {
6372 const result = spawnSync ( gitBin , [ 'rev-parse' , '--git-dir' ] , {
6473 cwd : dir ,
@@ -140,14 +149,12 @@ function rolldownBindingCandidates() {
140149 }
141150}
142151
143- function ensureRolldownHostBindings ( ) {
152+ function withRolldownHostBindings ( pkg ) {
144153 const candidates = rolldownBindingCandidates ( ) ;
145154 if ( candidates . length === 0 ) {
146- return ;
155+ return { changed : false , pkg } ;
147156 }
148157
149- const packageJsonPath = path . join ( repoRoot , 'rolldown' , 'packages' , 'rolldown' , 'package.json' ) ;
150- const pkg = JSON . parse ( readFileSync ( packageJsonPath , 'utf-8' ) ) ;
151158 const optionalDependencies = {
152159 ...( pkg . optionalDependencies ?? { } ) ,
153160 } ;
@@ -160,15 +167,50 @@ function ensureRolldownHostBindings() {
160167 }
161168 }
162169
170+ if ( ! changed ) {
171+ return { changed : false , pkg } ;
172+ }
173+
174+ return {
175+ changed : true ,
176+ pkg : {
177+ ...pkg ,
178+ optionalDependencies,
179+ } ,
180+ } ;
181+ }
182+
183+ function ensureRolldownHostBindings ( ) {
184+ const packageJsonPath = rolldownPackageJsonPath ( ) ;
185+ const { changed, pkg } = withRolldownHostBindings (
186+ JSON . parse ( readFileSync ( packageJsonPath , 'utf-8' ) ) ,
187+ ) ;
163188 if ( ! changed ) {
164189 return ;
165190 }
166191
167- pkg . optionalDependencies = optionalDependencies ;
168- writeFileSync ( packageJsonPath , JSON . stringify ( pkg , null , 2 ) + '\n' , 'utf-8' ) ;
192+ writeFileSync ( packageJsonPath , stringifyJson ( pkg ) , 'utf-8' ) ;
169193 log ( `Added host rolldown bindings to ${ packageJsonPath } ` ) ;
170194}
171195
196+ function hasOnlyManagedRolldownBindingsChange ( dir ) {
197+ const statusEntries = capture ( gitBin , [ 'status' , '--porcelain' ] , dir )
198+ . split ( '\n' )
199+ . filter ( Boolean ) ;
200+ if ( statusEntries . length !== 1 || statusEntries [ 0 ] . slice ( 3 ) !== rolldownPackageJsonRelativePath ) {
201+ return false ;
202+ }
203+
204+ const { changed, pkg } = withRolldownHostBindings (
205+ JSON . parse ( capture ( gitBin , [ 'show' , `HEAD:${ rolldownPackageJsonRelativePath } ` ] , dir ) ) ,
206+ ) ;
207+ if ( ! changed ) {
208+ return false ;
209+ }
210+
211+ return readFileSync ( rolldownPackageJsonPath ( dir ) , 'utf-8' ) === stringifyJson ( pkg ) ;
212+ }
213+
172214function syncCleanCheckout ( name , config ) {
173215 const dir = path . join ( repoRoot , name ) ;
174216
@@ -188,10 +230,15 @@ function syncCleanCheckout(name, config) {
188230
189231 ensureExpectedRemote ( name , dir , config . repo ) ;
190232
191- if ( isDirty ( dir ) ) {
233+ const hasManagedRolldownBindingsChange =
234+ name === 'rolldown' && isDirty ( dir ) && hasOnlyManagedRolldownBindingsChange ( dir ) ;
235+ if ( isDirty ( dir ) && ! hasManagedRolldownBindingsChange ) {
192236 log ( `Keeping existing dirty ${ name } checkout at ${ dir } ` ) ;
193237 return ;
194238 }
239+ if ( hasManagedRolldownBindingsChange ) {
240+ log ( `Ignoring managed rolldown host binding diff at ${ dir } ` ) ;
241+ }
195242
196243 log ( `Updating clean ${ name } checkout...` ) ;
197244 run ( gitBin , [ 'fetch' , 'origin' , '--tags' ] , { cwd : dir } ) ;
0 commit comments