Skip to content

Commit

Permalink
Merge pull request #57 from kucrut/wp-scripts
Browse files Browse the repository at this point in the history
WP scripts
  • Loading branch information
kucrut authored Oct 10, 2023
2 parents 7f20dd0 + 01efcf5 commit 4f8e496
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 35 deletions.
52 changes: 20 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ Feel free to [customise the configuration](https://vitejs.dev/config/) to add pl
```js
// vite.config.js
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { v4wp } from '@kucrut/vite-for-wp';
import react from '@vitejs/plugin-react';

Expand Down Expand Up @@ -128,49 +127,38 @@ You can now run `npm run dev` when developing your plugin/theme and run `npm run

### External Dependencies

If your JS package depends on one or more WordPress modules (eg. `@wordpress/i18n`), you can define them as externals with the help of `rollup-plugin-external-globals`.
If your package depends on one or more scripts registered by WordPress (eg. `jquery`, `react`, `@wordpress/i18n`, etc.) and you want to exclude them from the final build, add `wp_scripts()` to the list of Vite's plugins. But first, install the required dependencies:

```sh
npm add -D rollup-plugin-external-globals
npm add -D rollup-plugin-external-globals vite-plugin-external
```

For example, to externalise `react` and `react-dom` packages:

```js
// vite.config.js
import { defineConfig } from 'vite';
import { wp_globals } from '@kucrut/vite-for-wp/utils';
import create_config from '@kucrut/vite-for-wp';
import external_globals from 'rollup-plugin-external-globals';
import { v4wp } from '@kucrut/vite-for-wp';
import { wp_scripts } from '@kucrut/vite-for-wp/plugins';
import react from '@vitejs/plugin-react';

export default defineConfig( ( { command } ) => {
if ( command === 'serve' ) {
return create_config( 'js/src/main.jsx', 'js/dist', {
plugins: [ react() ],
} );
}

return create_config( 'js/src/main.jsx', 'js/dist', {
plugins: [
react(),
external_globals( {
...wp_globals(),
} ),
],
build: {
rollupOptions: {
external: [ 'react', 'react-dom' ],
output: {
globals: wp_globals(),
},
},
},
} );
} );
export default {
plugins: [
v4wp( {
input: 'js/src/main.jsx',
outDir: 'js/dist',
} ),
wp_scripts(),
react( {
jsxRuntime: 'classic',
} ),
],
};
```

Note that `react` and `react-dom` packages still need to be installed as dev dependencies and added to the `dependencies` array when enqueueing the script (see example above).
**Special Notes for React**

- `react` and `react-dom` packages still need to be installed as your package's dev dependencies as they're used by `@vitejs/plugin-react`.
- `react` and `react-dom` should be added to the `dependencies` array when enqueueing the script (see example above).

## Example plugins

Expand Down
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,23 @@
"eslint": "^8.51.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "npm:wp-prettier@^3.0.3",
"rollup-plugin-external-globals": "^0.8.0",
"typescript": "^5.2.2",
"vite": "^4.4.11"
"vite": "^4.4.11",
"vite-plugin-external": "^4.0.1"
},
"peerDependencies": {
"vite": "^4.4.11"
"rollup-plugin-external-globals": "^0.8.0",
"vite": "^4.4.11",
"vite-plugin-external": "^4.0.1"
},
"peerDependenciesMeta": {
"rollup-plugin-external-globals": {
"optional": true
},
"vite-plugin-external": {
"optional": true
}
},
"exports": {
"./package.json": "./package.json",
Expand Down
81 changes: 81 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/exports/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { dev_server_config } from './dev-server-config.js';
export { dev_server_manifest } from './dev-server-manifest.js';
export { wp_scripts } from './wp-scripts.js';
51 changes: 51 additions & 0 deletions src/exports/plugins/wp-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/** @typedef {{extraScripts?: Record<string, string>}} Options */

import externals from 'vite-plugin-external';
import globals from 'rollup-plugin-external-globals';
import { wp_globals } from '../utils/wp-globals.js';

/**
* WordPress scripts plugin
*
* This plugin provides easy access to built-in WordPress scripts and exclude them from the build.
*
* @type {(options?: Options) => import('vite').PluginOption[]}
* @return {import('vite').PluginOption[]} Vite plugins instances.
*/
export function wp_scripts( options = {} ) {
const { extraScripts = {} } = options;

const scripts = {
...wp_globals(),
...extraScripts,
};

/** @type { import('vite').Plugin } */
const plugin = {
name: 'v4wp:wp-scripts',
apply: 'build',

config() {
return {
build: {
rollupOptions: {
external: Object.keys( scripts ),
output: {
globals: scripts,
},
},
},
};
},
};

return [
plugin,
externals( {
externals: scripts,
} ),
globals( scripts, {
include: [ '**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx' ],
} ),
];
}
7 changes: 7 additions & 0 deletions src/exports/utils/wp-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ export function wp_globals() {
'components',
'compose',
'core-data',
'customize-widgets',
'data',
'data-controls',
'date',
'deprecated',
'dom',
'dom-ready',
'edit-post',
'edit-site',
'edit-widgets',
'editor',
'element',
'escape-html',
Expand All @@ -43,17 +46,21 @@ export function wp_globals() {
'notices',
'nux',
'plugins',
'preferences',
'preferences-persistence',
'primitives',
'priority-queue',
'redux-routine',
'reusable-blocks',
'rich-text',
'server-side-render',
'shortcode',
'style-engine',
'token-list',
'url',
'viewport',
'warning',
'widgets',
'wordcount',
];

Expand Down
4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ 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 wp_scripts(options?: Options): import('vite').PluginOption[];
type Options = {
extraScripts?: Record<string, string>;
};
}

declare module '@kucrut/vite-for-wp/utils' {
Expand Down
4 changes: 3 additions & 1 deletion types/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
],
"sources": [
"../src/exports/index.js",
"../src/exports/plugins/wp-scripts.js",
"../src/exports/utils/choose-port.js"
],
"sourcesContent": [
null,
null,
null
],
"mappings": ";;;aAA6BA,cAAcA;aACdC,UAAUA;aACFC,WAAWA;aACuBC,WAAWA;aACDC,OAAOA;;;;;;;;;;;;;;MCJxCC,iBAAiBA"
"mappings": ";;;aAA6BA,cAAcA;aACdC,UAAUA;aACFC,WAAWA;aACuBC,WAAWA;aACDC,OAAOA;;;;;;;;;;MCJjCA,OAAOA;;;;;;;;MCAdC,iBAAiBA"
}

0 comments on commit 4f8e496

Please sign in to comment.