Skip to content

Commit f296313

Browse files
committed
created watcher plugin to watch and update public dir
1 parent a0401ff commit f296313

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
"@vitejs/plugin-basic-ssl": "^1.0.1",
2929
"alpinejs": "^3.13.2",
3030
"autoprefixer": "^10.4.16",
31+
"chokidar": "^3.5.3",
3132
"concurrently": "^8.2.2",
3233
"cross-env": "^7.0.3",
34+
"fs-extra": "^11.1.1",
3335
"liquid-ajax-cart": "^2.0.2",
3436
"npm-run-all": "^4.1.5",
3537
"postcss": "^8.4.27",

vite.config.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1-
import { resolve } from 'node:path'
21
import shopify from 'vite-plugin-shopify'
32
import pageReload from 'vite-plugin-page-reload'
43
import basicSsl from '@vitejs/plugin-basic-ssl'
5-
import fs from 'fs'
4+
import { watch } from 'chokidar';
5+
import fs from 'fs-extra'
66

7-
export default {
7+
const watchStaticAssets = () => ({
8+
name: 'watch-static-assets',
9+
configureServer(server) {
10+
const watcher = watch('./public/*', {
11+
persistent: true
12+
});
13+
14+
const copyAsset = async path => {
15+
await fs.copy(path, `assets/${path.replace('public/', '')}`);
16+
}
17+
18+
const removeAsset = async path => {
19+
await fs.remove(`assets/${path.replace('public/', '')}`);
20+
}
21+
22+
watcher.on('add', copyAsset);
23+
watcher.on('change', copyAsset);
24+
watcher.on('unlink', removeAsset);
25+
}
26+
})
827

28+
export default {
929
clearScreen: false,
1030
server: {
1131
host: '127.0.0.1',
@@ -15,6 +35,7 @@ export default {
1535
publicDir: true,
1636
plugins: [
1737
basicSsl(),
38+
watchStaticAssets(),
1839
shopify({
1940
snippetFile: 'vite.liquid'
2041
}),

0 commit comments

Comments
 (0)