@dream-encode/wp-js-plugin-utils 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Generates `.asset.php` files for CSS-only webpack entry points.
5
+ *
6
+ * `@wordpress/dependency-extraction-webpack-plugin` only emits asset
7
+ * manifests for JS entry points. Plugins that ship style-only entries
8
+ * (e.g. an admin stylesheet not paired with a script) need a sibling
9
+ * `.asset.php` file so PHP `wp_enqueue_style()` calls can pull the
10
+ * version hash and cache-bust correctly.
11
+ *
12
+ * For each entry point that does not produce a JS chunk, this plugin
13
+ * emits `js/<entry>.min.asset.php` with an empty dependencies array
14
+ * and the current compilation hash as the version.
15
+ */
16
+ class CSSAssetPlugin {
17
+ apply(compiler) {
18
+ compiler.hooks.compilation.tap('CSSAssetPlugin', compilation => {
19
+ compilation.hooks.processAssets.tap({
20
+ name: 'CSSAssetPlugin',
21
+ stage: compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
22
+ }, () => {
23
+ compilation.entrypoints.forEach((entrypoint, entryName) => {
24
+ const hasJS = entrypoint.chunks.some(chunk => chunk.files.has(`js/${entryName}.min.js`));
25
+ if (hasJS) {
26
+ return;
27
+ }
28
+ const version = compilation.hash.substring(0, 16);
29
+ const assetContent = `<?php return array('dependencies' => array(), 'version' => '${version}');`;
30
+ const assetFilename = `js/${entryName}.min.asset.php`;
31
+ compilation.emitAsset(assetFilename, {
32
+ source: () => assetContent,
33
+ size: () => assetContent.length
34
+ });
35
+ });
36
+ });
37
+ });
38
+ }
39
+ }
40
+ module.exports = CSSAssetPlugin;
@@ -10,6 +10,7 @@ const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
10
10
  const TerserPlugin = require('terser-webpack-plugin');
11
11
  const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
12
12
  const postcssPlugins = require('@wordpress/postcss-plugins-preset');
13
+ const CSSAssetPlugin = require('./CSSAssetPlugin');
13
14
  const tryRequire = id => {
14
15
  try {
15
16
  return require(id);
@@ -71,7 +72,7 @@ const createWebpackConfig = (options = {}) => {
71
72
  ignoreOrder: false
72
73
  }), new RtlCssPlugin({
73
74
  filename: 'css/[name]-rtl.css'
74
- }), new RemoveEmptyScriptsPlugin(), new DependencyExtractionWebpackPlugin()];
75
+ }), new RemoveEmptyScriptsPlugin(), new DependencyExtractionWebpackPlugin(), new CSSAssetPlugin()];
75
76
  if (sentry) {
76
77
  const sentryWebpackPlugin = tryRequire('@sentry/webpack-plugin');
77
78
  if (sentryWebpackPlugin && sentryWebpackPlugin.sentryWebpackPlugin) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dream-encode/wp-js-plugin-utils",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Common JS functionality used by custom WP plugins.",
5
5
  "keywords": [
6
6
  "wordpress",
@@ -21,6 +21,7 @@
21
21
  "exports": {
22
22
  ".": "./dist/index.js",
23
23
  "./webpack": "./dist/webpack/createWebpackConfig.js",
24
+ "./webpack/css-asset-plugin": "./dist/webpack/CSSAssetPlugin.js",
24
25
  "./postcss": "./dist/webpack/postcss.config.js",
25
26
  "./settings": "./dist/settings/index.js",
26
27
  "./settings/styles": "./dist/settings/styles/_settings-page.scss",