From 19def1cd8ade6dc28401cfda1e671eba815e03e4 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Wed, 15 Jan 2025 14:18:57 +0900 Subject: [PATCH 1/2] Fix watch logic --- .changeset/many-mirrors-lie.md | 5 +++ .../src/__tests__/plugin.test.ts | 8 ++++ packages/webpack-plugin/src/plugin.ts | 43 +++++++++---------- 3 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 .changeset/many-mirrors-lie.md 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/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' }) From 43c7c1b3b8fc885df3d61d127aa4c161569911df Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Wed, 15 Jan 2025 14:28:36 +0900 Subject: [PATCH 2/2] Fix script --- .github/workflows/publish.yml | 1 + 1 file changed, 1 insertion(+) 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'