Skip to content

v2.12.0

Choose a tag to compare

@widoz widoz released this 21 Nov 10:51
· 5 commits to master since this release
9bf2a42

What's Changed

  • Add support for manifest configuration with advanced asset options by @widoz in #66
  • Remove unnecessary internal docblock from the Asset interface by @widoz in #67
  • Add support for sharing data in Script Modules by @widoz in #65

Add support for manifest configuration with advanced asset options

An alternative output could be where the value associated with the handle is an object.
In such a case, the configuration given must be compliant to the AssetFactory configuration.

You can know which keys and values are supported by checking the AssetFactory types, respectively AssetConfig and AssetExtensionConfig.

Here is an example of such a manifest:

{
  "my-handle": {
    "filePath": "script.js",
    "location": [
      "frontend",
      "backend"
    ],
    "enqueue": false,
    "inFooter": true,
    "version": "1.0.0",
    "condition": "",
    "attributes": {
      "async": false,
      "defer": false
    }
  }
}

You can build the aforementioned configuration by implementing a custom generator for the webpack-manifest-plugin.

For instance, you can have the following generate callback passed to the ManifestPlugin:

new WebpackManifestPlugin({
    // ... other options
    
    generate: (seed, files) => {
       return files.reduce((manifest, { name, path }) => {
          const cleanName = name.replace(/\.js$/, '');
          switch(cleanName) {
                case 'handle-name':
                     manifest[cleanName] = {
                        filePath: path,
                        location: ['frontend'],
                        enqueue: true,
                        inFooter: true,
                        version: '1.0.0',
                        condition: '',
                        attributes: {
                            async: false,
                            defer: false
                        }
                    };
                    return manifest;
                default:
                    manifest[cleanName] = path;
                    return manifest;
            }
       }, seed);
    },
    
    // ... other options
})

Note: An alternative could be to have a json file from which retrieve the configuration for each handle.

Remove unnecessary internal docblock from the Asset interface

Keep the main Asset inteface open to third parties, this solves a bc issue in 2.11.0

Add support for sharing data in Script Modules

Scripts Modules allow sharing data from the server to the client via a filter "script_module_data_{$handle}".

Assets registered as ScriptModule can have shared data added like following:

$module = new ScriptModule('@my-plugin/main', 'assets/main.js');
$module->withData([
    'apiUrl' => get_rest_url(),
    'nonce'  => wp_create_nonce('wp_rest'),
]);

Full Changelog: 2.11.0...2.12.0