diff --git a/.changeset/many-mirrors-lie.md b/.changeset/many-mirrors-lie.md new file mode 100644 index 00000000..4dd2c462 --- /dev/null +++ b/.changeset/many-mirrors-lie.md @@ -0,0 +1,5 @@ +--- +"@devup-ui/webpack-plugin": patch +--- + +Fix watch logic diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 65a1e470..ea293d2a 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -53,3 +53,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + if: github.event_name == 'push' && github.ref == 'refs/heads/main' diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index c0454798..5e48f72e 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -190,6 +190,14 @@ describe('devupUIPlugin', () => { rules: [], }, }, + hooks: { + afterCompile: { + tap: vi.fn(), + }, + watchRun: { + tapAsync: vi.fn(), + }, + }, } as any) expect(writeFileSync).toHaveBeenCalledWith('css', '', { diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index 9db9adf3..1017dd5b 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -67,39 +67,38 @@ export class DevupUIWebpackPlugin { apply(compiler: Compiler) { // read devup.json - if (existsSync(this.options.devupPath)) { + const existsDevup = existsSync(this.options.devupPath) + if (existsDevup) { try { this.writeDataFiles() } catch (error) { console.error(error) } - let lastModifiedTime: number | null = null - compiler.hooks.afterCompile.tap('DevupUIWebpackPlugin', (compilation) => { compilation.fileDependencies.add(this.options.devupPath) - this.watch = true }) - compiler.hooks.watchRun.tapAsync( - 'DevupUIWebpackPlugin', - (_, callback) => { - stat(this.options.devupPath, (err, stats) => { - if (err) { - console.error(`Error checking ${this.options.devupPath}:`, err) - return callback() - } + } - const modifiedTime = stats.mtimeMs - if (lastModifiedTime && lastModifiedTime !== modifiedTime) { - this.writeDataFiles() - } + let lastModifiedTime: number | null = null + compiler.hooks.watchRun.tapAsync('DevupUIWebpackPlugin', (_, callback) => { + this.watch = true + if (existsDevup) + stat(this.options.devupPath, (err, stats) => { + if (err) { + console.error(`Error checking ${this.options.devupPath}:`, err) + return callback() + } - lastModifiedTime = modifiedTime - callback() - }) - }, - ) - } + const modifiedTime = stats.mtimeMs + if (lastModifiedTime && lastModifiedTime !== modifiedTime) { + this.writeDataFiles() + } + + lastModifiedTime = modifiedTime + callback() + }) + }) // Create an empty CSS file if (!existsSync(this.options.cssFile)) { writeFileSync(this.options.cssFile, '', { encoding: 'utf-8' })