Skip to content

Commit

Permalink
Merge pull request #63 from kucrut/improve-dev-server-manifest-generator
Browse files Browse the repository at this point in the history
Improve dev server manifest generator
  • Loading branch information
kucrut authored Oct 16, 2023
2 parents d83de37 + c755432 commit 2d49790
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-baboons-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kucrut/vite-for-wp': patch
---

Ensure dev server manifest is created when the server (re)starts and deleted when it's stopped.
18 changes: 14 additions & 4 deletions src/exports/plugins/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { choose_port } from '../utils/choose-port.js';
export function dev_server() {
const plugins_to_check = [ 'vite:react-refresh' ];
/** @type {string} */
let dev_manifest_data;
/** @type {string} */
let dev_manifest_file;

return {
Expand Down Expand Up @@ -66,15 +68,23 @@ export function dev_server() {
plugins: plugins_to_check.filter( i => plugins.some( ( { name } ) => name === i ) ),
};

dev_manifest_data = JSON.stringify( data );
dev_manifest_file = build.outDir + '/vite-dev-server.json';

fs.mkdirSync( build.outDir, { recursive: true } );
fs.writeFileSync( dev_manifest_file, JSON.stringify( data ), 'utf8' );
},

configureServer( server ) {
server.httpServer?.once( 'close', () => {
fs.rmSync( dev_manifest_file );
configureServer( { httpServer } ) {
if ( ! httpServer ) {
return;
}

httpServer.on( 'listening', () => {
fs.writeFileSync( dev_manifest_file, dev_manifest_data, 'utf8' );
} );

httpServer.on( 'close', () => {
fs.rmSync( dev_manifest_file, { force: true } );
} );
},
};
Expand Down

0 comments on commit 2d49790

Please sign in to comment.