Skip to content

Commit

Permalink
Merge pull request #62 from kucrut/merge-dev-server-plugins
Browse files Browse the repository at this point in the history
Merge dev server plugins
  • Loading branch information
kucrut authored Oct 14, 2023
2 parents 3f64792 + 461687b commit d83de37
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 104 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-gifts-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kucrut/vite-for-wp': patch
---

Merge dev server plugins
7 changes: 3 additions & 4 deletions src/exports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/** @typedef {{input?: InputOption, outDir?: ResolvedConfig['build']['outDir']}} Options */

import { createLogger, mergeConfig } from 'vite';
import { dev_server_config } from './plugins/dev-server-config.js';
import { dev_server_manifest } from './plugins/dev-server-manifest.js';
import { dev_server } from './plugins/dev-server.js';

/**
* Vite for WP
Expand Down Expand Up @@ -39,7 +38,7 @@ export function v4wp( options = {} ) {
},
};

return [ plugin, dev_server_config(), dev_server_manifest() ];
return [ plugin, dev_server() ];
}

/**
Expand All @@ -63,7 +62,7 @@ export default function create_config( input, out_dir, extra_config ) {
/** @type {UserConfig} */
let config = {
clearScreen: false,
plugins: [ v4wp( { input, outDir: out_dir } ), dev_server_config(), dev_server_manifest() ],
plugins: [ v4wp( { input, outDir: out_dir } ), dev_server() ],
};

if ( extra_config ) {
Expand Down
50 changes: 0 additions & 50 deletions src/exports/plugins/dev-server-config.js

This file was deleted.

45 changes: 0 additions & 45 deletions src/exports/plugins/dev-server-manifest.js

This file was deleted.

81 changes: 81 additions & 0 deletions src/exports/plugins/dev-server.js
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 );
} );
},
};
}
3 changes: 1 addition & 2 deletions src/exports/plugins/index.js
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';
3 changes: 1 addition & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ declare module '@kucrut/vite-for-wp' {
}

declare module '@kucrut/vite-for-wp/plugins' {
export function dev_server_config(): import('vite').Plugin;
export function dev_server_manifest(): import('vite').Plugin;
export function dev_server(): import('vite').Plugin;
export function wp_scripts(options?: Options): import('vite').PluginOption[];
type Options = {
extraScripts?: Record<string, string>;
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
null,
null
],
"mappings": ";;;aAA6BA,cAAcA;aACdC,UAAUA;aACFC,WAAWA;aACuBC,WAAWA;aACDC,OAAOA;;;;;;;;;;MCJjCA,OAAOA;;;;;;;;MCAdC,iBAAiBA"
"mappings": ";;;aAA6BA,cAAcA;aACdC,UAAUA;aACFC,WAAWA;aACuBC,WAAWA;aACDC,OAAOA;;;;;;;;;MCJjCA,OAAOA;;;;;;;;MCAdC,iBAAiBA"
}

0 comments on commit d83de37

Please sign in to comment.