@automattic/wordpress-external-dependencies-plugin

1.0.0 • Public • Published

@automattic/wordpress-external-dependencies-plugin

This webpack plugin serves two purposes:

  • Externalize dependencies that are available as script dependencies on modern WordPress sites.
  • Add a JSON file for each entrypoint that declares the WordPress script dependencies for the entrypoint.

This allows JavaScript bundles produced by webpack to leverage WordPress style dependency sharing without an error-prone process of manually maintaining a dependency list.

Consult the webpack website for additional information on webpack concepts.

Usage

Webpack

Use this plugin as you would other webpack plugins:

// webpack.config.js
const WordPressExternalDependenciesPlugin = require( '@automattic/wordpress-external-dependencies-plugin' );

module.exports = {
  // …snip
  plugins: [
    new WordPressExternalDependenciesPlugin(),
  ]
}

Each entrypoint in the webpack bundle will include JSON file that declares the WordPress script dependencies that should be enqueued.

For example:

// Source file entrypoint.js
import { Component } from '@wordpress/element';

// Webpack will produce the output output/entrypoint.js
/* bundle JS output */

// Webpack will also produce output/entrypoint.deps.json declaring script dependencies
['wp-element']

WordPress

Enqueue your script as usual and read the script dependencies dynamically:

$script_path      = 'path/to/script.js';
$script_deps_path = 'path/to/script.deps.json';
$script_dependencies = file_exists( $script_deps_path )
	? json_decode( file_get_contents( $script_deps_path ) )
	: array();
$script_url = plugins_url( $script_path, __FILE__ );
wp_enqueue_script( 'script', $script_url, $script_dependencies );

Readme

Keywords

Package Sidebar

Install

npm i @automattic/wordpress-external-dependencies-plugin

Weekly Downloads

1

Version

1.0.0

License

GPL-2.0-or-later

Unpacked Size

21.7 kB

Total Files

4

Last publish

Collaborators

  • porada
  • imranh920
  • gmjuhasz
  • rcarvalho
  • briowill
  • bgrgicak
  • dhenridev
  • daledupreez-a8c
  • jeherve
  • yuliyan
  • micbosia8c
  • jeremy.yip
  • wpvip-bot
  • etobiesen
  • kzoschke
  • brunobasto
  • kat3samsin
  • fmfernandes
  • newspack
  • robertsreberski_a8c
  • msurdi-a8c
  • chihsuan
  • manzoorwanijk
  • rjchow
  • andrea-sdl
  • scjr
  • spsiddarthan
  • t2dw4t
  • ehg_
  • wwa
  • sirreal
  • elazzabi
  • royho
  • luismulinari
  • macbre
  • mjangda
  • matticbot
  • a8c
  • blowery
  • noahtallen
  • hanifn
  • sgomes
  • tyxla
  • saroshaga
  • parkcityj
  • nejclovrencic
  • sirbrillig
  • chriszarate
  • robersongomes
  • johngodley