Skip to content

Commit c57c872

Browse files
authored
Merge pull request #6 from dev-five-git/watch-logic
Fix watch logic
2 parents b731a3d + 43c7c1b commit c57c872

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

.changeset/many-mirrors-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/webpack-plugin": patch
3+
---
4+
5+
Fix watch logic

.github/workflows/publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ jobs:
5353
env:
5454
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5555
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
56+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'

packages/webpack-plugin/src/__tests__/plugin.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ describe('devupUIPlugin', () => {
190190
rules: [],
191191
},
192192
},
193+
hooks: {
194+
afterCompile: {
195+
tap: vi.fn(),
196+
},
197+
watchRun: {
198+
tapAsync: vi.fn(),
199+
},
200+
},
193201
} as any)
194202

195203
expect(writeFileSync).toHaveBeenCalledWith('css', '', {

packages/webpack-plugin/src/plugin.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,39 +67,38 @@ export class DevupUIWebpackPlugin {
6767

6868
apply(compiler: Compiler) {
6969
// read devup.json
70-
if (existsSync(this.options.devupPath)) {
70+
const existsDevup = existsSync(this.options.devupPath)
71+
if (existsDevup) {
7172
try {
7273
this.writeDataFiles()
7374
} catch (error) {
7475
console.error(error)
7576
}
7677

77-
let lastModifiedTime: number | null = null
78-
7978
compiler.hooks.afterCompile.tap('DevupUIWebpackPlugin', (compilation) => {
8079
compilation.fileDependencies.add(this.options.devupPath)
81-
this.watch = true
8280
})
83-
compiler.hooks.watchRun.tapAsync(
84-
'DevupUIWebpackPlugin',
85-
(_, callback) => {
86-
stat(this.options.devupPath, (err, stats) => {
87-
if (err) {
88-
console.error(`Error checking ${this.options.devupPath}:`, err)
89-
return callback()
90-
}
81+
}
9182

92-
const modifiedTime = stats.mtimeMs
93-
if (lastModifiedTime && lastModifiedTime !== modifiedTime) {
94-
this.writeDataFiles()
95-
}
83+
let lastModifiedTime: number | null = null
84+
compiler.hooks.watchRun.tapAsync('DevupUIWebpackPlugin', (_, callback) => {
85+
this.watch = true
86+
if (existsDevup)
87+
stat(this.options.devupPath, (err, stats) => {
88+
if (err) {
89+
console.error(`Error checking ${this.options.devupPath}:`, err)
90+
return callback()
91+
}
9692

97-
lastModifiedTime = modifiedTime
98-
callback()
99-
})
100-
},
101-
)
102-
}
93+
const modifiedTime = stats.mtimeMs
94+
if (lastModifiedTime && lastModifiedTime !== modifiedTime) {
95+
this.writeDataFiles()
96+
}
97+
98+
lastModifiedTime = modifiedTime
99+
callback()
100+
})
101+
})
103102
// Create an empty CSS file
104103
if (!existsSync(this.options.cssFile)) {
105104
writeFileSync(this.options.cssFile, '', { encoding: 'utf-8' })

0 commit comments

Comments
 (0)