Skip to content

Commit

Permalink
fix: serve static files added to watched paths after server was started
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Jul 31, 2024
1 parent 8c9324c commit ab7eb6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,33 @@ export default async (config = {}) => {
/**
* Global watcher
*
* Watch for changes in the config file, Tailwind CSS config, and CSS files
* Watch for changes in the config files, Tailwind CSS config, CSS files,
* configured static assets, and user-defined watch paths.
*/
const globalWatchedPaths = new Set([
'config*.{js,cjs,ts}',
'maizzle.config*.{js,cjs,ts}',
'tailwind*.config.{js,ts}',
'**/*.css',
...get(config, 'server.watch', [])
...get(config, 'build.static.source', []),
...get(config, 'server.watch', []),
])

async function globalPathsHandler(file, eventType) {
// Update express.static to serve new files
if (eventType === 'add') {
app.use(express.static(path.dirname(file)))
}

// Stop serving deleted files
if (eventType === 'unlink') {
app._router.stack = app._router.stack.filter(
layer => layer.regexp.source !== path.dirname(file).replace(/\\/g, '/')
)
}

// Not viewing a component in the browser, no need to rebuild
if (!viewing) {
spinner.info(`file ${eventType}: ${file}`)
return
}

Expand Down Expand Up @@ -326,7 +339,7 @@ export default async (config = {}) => {
}
})
} catch (error) {
spinner.fail('Failed to render template.')
spinner.fail(`Failed to render template: ${file}`)
throw error
}
}
Expand All @@ -349,8 +362,6 @@ export default async (config = {}) => {

/**
* Serve all folders in the cwd as static files
*
* TODO: change to include build.assets or build.static, which may be outside cwd
*/
const srcFoldersList = await fg.glob(
[
Expand Down
4 changes: 2 additions & 2 deletions test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { describe, expect, test, beforeAll } from 'vitest'
const init = async () => {
await serve({
build: {
content: 'test/fixtures/build/**/*.html',
content: ['test/fixtures/build/**/*.html'],
static: {
source: 'test/fixtures/build/**/*.png',
source: ['test/fixtures/build/**/*.png'],
}
},
components: {
Expand Down

0 comments on commit ab7eb6d

Please sign in to comment.