-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from kucrut/merge-dev-server-plugins
Merge dev server plugins
- Loading branch information
Showing
8 changed files
with
92 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@kucrut/vite-for-wp': patch | ||
--- | ||
|
||
Merge dev server plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import fs from 'node:fs'; | ||
import { choose_port } from '../utils/choose-port.js'; | ||
|
||
/** | ||
* Dev server plugin | ||
* | ||
* @type {() => import('vite').Plugin} | ||
* @return {import('vite').Plugin} Plugin object. | ||
*/ | ||
export function dev_server() { | ||
const plugins_to_check = [ 'vite:react-refresh' ]; | ||
/** @type {string} */ | ||
let dev_manifest_file; | ||
|
||
return { | ||
apply: 'serve', | ||
name: 'v4wp:dev-server', | ||
|
||
async config( config ) { | ||
const { server = {} } = config; | ||
let { host = 'localhost', port = 5173, ...server_config } = server; | ||
|
||
// We need actual host name or IP address for choose_port() to work. | ||
if ( typeof host === 'boolean' ) { | ||
host = '0.0.0.0'; | ||
} | ||
|
||
const hmr_protocol = server_config.https ? 'wss' : 'ws'; | ||
const server_protocol = server_config.https ? 'https' : 'http'; | ||
|
||
// Ensure chosen port is available because we need to enable strictPort below. | ||
// If the chosen port is already in use, a free one will be selected. | ||
port = await choose_port( { host, port } ); | ||
|
||
// This will be used by the PHP helper. | ||
const origin = `${ server_protocol }://${ host }:${ port }`; | ||
|
||
return { | ||
server: { | ||
...server_config, | ||
host, | ||
origin, | ||
port, | ||
strictPort: true, | ||
hmr: { | ||
port, | ||
host, | ||
protocol: hmr_protocol, | ||
}, | ||
}, | ||
}; | ||
}, | ||
|
||
configResolved( config ) { | ||
const { base, build, plugins, server } = config; | ||
const prod_manifest_file = build.outDir + '/manifest.json'; | ||
|
||
// Remove build manifest as the PHP helper uses it to determine | ||
// which manifest to load when enqueueing assets. | ||
fs.rmSync( prod_manifest_file, { force: true } ); | ||
|
||
const data = { | ||
base, | ||
origin: server.origin, | ||
port: server.port, | ||
plugins: plugins_to_check.filter( i => plugins.some( ( { name } ) => name === i ) ), | ||
}; | ||
|
||
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 ); | ||
} ); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { dev_server_config } from './dev-server-config.js'; | ||
export { dev_server_manifest } from './dev-server-manifest.js'; | ||
export { dev_server } from './dev-server.js'; | ||
export { wp_scripts } from './wp-scripts.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters