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

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,210 @@
1
+ "use strict";
2
+
3
+ const webpack = require('webpack');
4
+ const {
5
+ resolve
6
+ } = require('path');
7
+ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
8
+ const RtlCssPlugin = require('rtlcss-webpack-plugin');
9
+ const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
10
+ const TerserPlugin = require('terser-webpack-plugin');
11
+ const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');
12
+ const postcssPlugins = require('@wordpress/postcss-plugins-preset');
13
+ const tryRequire = id => {
14
+ try {
15
+ return require(id);
16
+ } catch (e) {
17
+ return null;
18
+ }
19
+ };
20
+
21
+ /**
22
+ * Create a Webpack configuration for a WordPress plugin.
23
+ *
24
+ * @param {Object} options Configuration options.
25
+ * @param {string} options.context Absolute path to the plugin root.
26
+ * @param {Object} options.entry Webpack entry map.
27
+ * @param {string} [options.outputPath] Absolute path to the output directory.
28
+ * @param {Object} [options.alias] Additional resolve aliases.
29
+ * @param {Object} [options.appVersion] Plugin package.json (for APP_VERSION define).
30
+ * @param {Object} [options.define] Additional DefinePlugin values.
31
+ * @param {boolean} [options.sentry] Enable Sentry webpack plugin.
32
+ * @param {Object} [options.sentryOptions] Overrides for Sentry plugin options.
33
+ * @param {Array} [options.extraPlugins] Additional webpack plugins.
34
+ * @param {Array} [options.extraRules] Additional module rules.
35
+ * @param {string} [options.devtool] Webpack devtool.
36
+ * @param {Function}[options.modify] Final mutation hook receiving the config.
37
+ * @return {Object} Webpack configuration array.
38
+ */
39
+ const createWebpackConfig = (options = {}) => {
40
+ const {
41
+ context,
42
+ entry,
43
+ outputPath,
44
+ alias = {},
45
+ appVersion,
46
+ define = {},
47
+ sentry = false,
48
+ sentryOptions = {},
49
+ extraPlugins = [],
50
+ extraRules = [],
51
+ devtool = 'source-map',
52
+ modify
53
+ } = options;
54
+ if (!context) {
55
+ throw new Error('[wp-plugin-utils] createWebpackConfig: "context" (plugin root path) is required.');
56
+ }
57
+ if (!entry) {
58
+ throw new Error('[wp-plugin-utils] createWebpackConfig: "entry" is required.');
59
+ }
60
+ const isProduction = process.argv[process.argv.indexOf('--mode') + 1] === 'production';
61
+ const resolvedOutputPath = outputPath || resolve(context, 'admin/assets/dist/');
62
+ const definePluginValues = {
63
+ ...define
64
+ };
65
+ if (appVersion && appVersion.version) {
66
+ definePluginValues.APP_VERSION = JSON.stringify(appVersion.version);
67
+ }
68
+ const plugins = [new webpack.DefinePlugin(definePluginValues), new MiniCssExtractPlugin({
69
+ filename: 'css/[name].min.css',
70
+ chunkFilename: '[id].css',
71
+ ignoreOrder: false
72
+ }), new RtlCssPlugin({
73
+ filename: 'css/[name]-rtl.css'
74
+ }), new RemoveEmptyScriptsPlugin(), new DependencyExtractionWebpackPlugin()];
75
+ if (sentry) {
76
+ const sentryWebpackPlugin = tryRequire('@sentry/webpack-plugin');
77
+ if (sentryWebpackPlugin && sentryWebpackPlugin.sentryWebpackPlugin) {
78
+ plugins.push(sentryWebpackPlugin.sentryWebpackPlugin({
79
+ org: process.env.SENTRY_ORG,
80
+ project: process.env.SENTRY_PROJECT,
81
+ authToken: process.env.SENTRY_AUTH_TOKEN,
82
+ ...sentryOptions
83
+ }));
84
+ }
85
+ }
86
+ plugins.push(...extraPlugins);
87
+ const svgrAvailable = !!tryRequire('@svgr/webpack');
88
+ const urlLoaderAvailable = !!tryRequire('url-loader');
89
+ const rules = [{
90
+ test: /\.jsx?$/,
91
+ exclude: /node_modules/,
92
+ use: {
93
+ loader: 'babel-loader'
94
+ }
95
+ }, {
96
+ test: /\.(j|t)sx?$/,
97
+ exclude: [/node_modules/],
98
+ use: require.resolve('source-map-loader'),
99
+ enforce: 'pre'
100
+ }, {
101
+ test: /\.(css|scss)$/,
102
+ use: [{
103
+ loader: MiniCssExtractPlugin.loader
104
+ }, {
105
+ loader: require.resolve('css-loader'),
106
+ options: {
107
+ importLoaders: 1,
108
+ sourceMap: true,
109
+ modules: {
110
+ auto: true
111
+ }
112
+ }
113
+ }, {
114
+ loader: 'sass-loader',
115
+ options: {
116
+ implementation: require('sass'),
117
+ sourceMap: true
118
+ }
119
+ }, {
120
+ loader: require.resolve('postcss-loader'),
121
+ options: {
122
+ postcssOptions: {
123
+ ident: 'postcss',
124
+ sourceMap: !isProduction,
125
+ plugins: isProduction ? [...postcssPlugins, require('cssnano')({
126
+ preset: ['default', {
127
+ discardComments: {
128
+ removeAll: true
129
+ }
130
+ }]
131
+ })] : postcssPlugins
132
+ }
133
+ }
134
+ }]
135
+ }];
136
+ if (svgrAvailable) {
137
+ rules.push({
138
+ test: /\.svg$/,
139
+ issuer: /\.(j|t)sx?$/,
140
+ use: urlLoaderAvailable ? ['@svgr/webpack', 'url-loader'] : ['@svgr/webpack'],
141
+ type: 'javascript/auto'
142
+ });
143
+ }
144
+ rules.push({
145
+ test: /\.svg$/,
146
+ issuer: /\.(pc|sc|sa|c)ss$/,
147
+ type: 'asset/inline'
148
+ }, {
149
+ test: /\.(bmp|png|jpe?g|gif|webp)$/i,
150
+ type: 'asset/resource',
151
+ generator: {
152
+ filename: 'images/[name].[hash:8][ext]'
153
+ }
154
+ }, {
155
+ test: /\.(woff|woff2|eot|ttf|otf)$/i,
156
+ type: 'asset/resource',
157
+ generator: {
158
+ filename: 'fonts/[name].[hash:8][ext]'
159
+ }
160
+ });
161
+ rules.push(...extraRules);
162
+ const config = {
163
+ context,
164
+ entry,
165
+ output: {
166
+ filename: 'js/[name].min.js',
167
+ path: resolvedOutputPath,
168
+ clean: true
169
+ },
170
+ devtool,
171
+ resolve: {
172
+ extensions: ['.jsx', '.ts', '.tsx', '.js'],
173
+ alias: {
174
+ '@': resolve(context, 'admin/assets/src/js'),
175
+ ...alias
176
+ }
177
+ },
178
+ optimization: {
179
+ concatenateModules: isProduction,
180
+ minimizer: [new TerserPlugin({
181
+ parallel: true,
182
+ terserOptions: {
183
+ output: {
184
+ comments: /translators:/i
185
+ },
186
+ compress: {
187
+ passes: 2
188
+ },
189
+ mangle: {
190
+ reserved: ['__', '_n', '_nx', '_x']
191
+ },
192
+ sourceMap: true
193
+ },
194
+ extractComments: false
195
+ })]
196
+ },
197
+ module: {
198
+ rules
199
+ },
200
+ plugins
201
+ };
202
+ if (typeof modify === 'function') {
203
+ const modified = modify(config);
204
+ return Array.isArray(modified) ? modified : [modified || config];
205
+ }
206
+ return [config];
207
+ };
208
+ module.exports = createWebpackConfig;
209
+ module.exports.createWebpackConfig = createWebpackConfig;
210
+ module.exports.default = createWebpackConfig;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+
3
+ const postcssPlugins = require('@wordpress/postcss-plugins-preset');
4
+ module.exports = {
5
+ plugins: postcssPlugins
6
+ };
package/package.json ADDED
@@ -0,0 +1,165 @@
1
+ {
2
+ "name": "@dream-encode/wp-js-plugin-utils",
3
+ "version": "0.1.0",
4
+ "description": "Common JS functionality used by custom WP plugins.",
5
+ "keywords": [
6
+ "wordpress",
7
+ "utilities",
8
+ "settings"
9
+ ],
10
+ "homepage": "https://github.com/dream-encode/wp-js-plugin-utils#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/dream-encode/wp-js-plugin-utils/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/dream-encode/wp-js-plugin-utils.git"
17
+ },
18
+ "license": "GPL-2.0-or-later",
19
+ "author": "David B",
20
+ "type": "commonjs",
21
+ "exports": {
22
+ ".": "./dist/index.js",
23
+ "./webpack": "./dist/webpack/createWebpackConfig.js",
24
+ "./postcss": "./dist/webpack/postcss.config.js",
25
+ "./settings": "./dist/settings/index.js",
26
+ "./settings/styles": "./dist/settings/styles/_settings-page.scss",
27
+ "./data-migrations": "./dist/data-migrations/index.js",
28
+ "./components": "./dist/components/index.js",
29
+ "./hooks": "./dist/hooks/index.js",
30
+ "./api": "./dist/api/index.js",
31
+ "./utils": "./dist/utils/index.js",
32
+ "./constants": "./dist/utils/constants.js"
33
+ },
34
+ "main": "./dist/index.js",
35
+ "files": [
36
+ "dist/"
37
+ ],
38
+ "scripts": {
39
+ "build": "babel src --out-dir dist --extensions \".js,.jsx\" --copy-files --no-copy-ignored",
40
+ "clean": "rimraf dist",
41
+ "prepublishOnly": "yarn clean && yarn build"
42
+ },
43
+ "dependencies": {
44
+ "anymatch": "^3.1.3",
45
+ "babel-plugin-polyfill-corejs2": "^0.4.17",
46
+ "babel-plugin-polyfill-corejs3": "^0.14.2",
47
+ "babel-plugin-polyfill-regenerator": "^0.6.8",
48
+ "balanced-match": "^1.0.2",
49
+ "baseline-browser-mapping": "^2.10.23",
50
+ "binary-extensions": "^2.3.0",
51
+ "brace-expansion": "^1.1.14",
52
+ "braces": "^3.0.3",
53
+ "browserslist": "^4.28.2",
54
+ "caniuse-lite": "^1.0.30001791",
55
+ "chokidar": "^3.6.0",
56
+ "commander": "^6.2.1",
57
+ "concat-map": "^0.0.1",
58
+ "convert-source-map": "^2.0.0",
59
+ "core-js-compat": "^3.49.0",
60
+ "debug": "^4.4.3",
61
+ "electron-to-chromium": "^1.5.344",
62
+ "es-errors": "^1.3.0",
63
+ "escalade": "^3.2.0",
64
+ "esutils": "^2.0.3",
65
+ "fill-range": "^7.1.1",
66
+ "fs-readdir-recursive": "^1.1.0",
67
+ "fs.realpath": "^1.0.0",
68
+ "function-bind": "^1.1.2",
69
+ "gensync": "^1.0.0-beta.2",
70
+ "glob": "^13.0.6",
71
+ "glob-parent": "^5.1.2",
72
+ "hasown": "^2.0.3",
73
+ "inflight": "^1.0.6",
74
+ "inherits": "^2.0.4",
75
+ "is-binary-path": "^2.1.0",
76
+ "is-core-module": "^2.16.1",
77
+ "is-extglob": "^2.1.1",
78
+ "is-glob": "^4.0.3",
79
+ "is-number": "^7.0.0",
80
+ "js-tokens": "^4.0.0",
81
+ "jsesc": "^3.1.0",
82
+ "json5": "^2.2.3",
83
+ "lodash.debounce": "^4.0.8",
84
+ "lru-cache": "^5.1.1",
85
+ "make-dir": "^2.1.0",
86
+ "minimatch": "^10.2.5",
87
+ "minipass": "^7.1.3",
88
+ "ms": "^2.1.3",
89
+ "node-releases": "^2.0.38",
90
+ "normalize-path": "^3.0.0",
91
+ "once": "^1.4.0",
92
+ "package-json-from-dist": "^1.0.1",
93
+ "path-is-absolute": "^1.0.1",
94
+ "path-parse": "^1.0.7",
95
+ "path-scurry": "^2.0.2",
96
+ "picocolors": "^1.1.1",
97
+ "picomatch": "^2.3.2",
98
+ "pify": "^4.0.1",
99
+ "readdirp": "^3.6.0",
100
+ "regenerate": "^1.4.2",
101
+ "regenerate-unicode-properties": "^10.2.2",
102
+ "regexpu-core": "^6.4.0",
103
+ "regjsgen": "^0.8.0",
104
+ "regjsparser": "^0.13.1",
105
+ "resolve": "^1.22.12",
106
+ "semver": "^6.3.1",
107
+ "slash": "^2.0.0",
108
+ "supports-preserve-symlinks-flag": "^1.0.0",
109
+ "to-regex-range": "^5.0.1",
110
+ "unicode-canonical-property-names-ecmascript": "^2.0.1",
111
+ "unicode-match-property-ecmascript": "^2.0.0",
112
+ "unicode-match-property-value-ecmascript": "^2.2.1",
113
+ "unicode-property-aliases-ecmascript": "^2.2.0",
114
+ "update-browserslist-db": "^1.2.3",
115
+ "wrappy": "^1.0.2",
116
+ "yallist": "^3.1.1"
117
+ },
118
+ "devDependencies": {
119
+ "@babel/cli": "^7.28.6",
120
+ "@babel/core": "^7.29.0",
121
+ "@babel/preset-env": "^7.29.2",
122
+ "@babel/preset-react": "^7.28.5",
123
+ "rimraf": "^6.1.3"
124
+ },
125
+ "peerDependencies": {
126
+ "@sentry/webpack-plugin": "^3.0.0",
127
+ "@svgr/webpack": "^8.0.0",
128
+ "@wordpress/api-fetch": "^7.0.0",
129
+ "@wordpress/components": "^28.0.0",
130
+ "@wordpress/data": "^10.0.0",
131
+ "@wordpress/dependency-extraction-webpack-plugin": "^6.0.0",
132
+ "@wordpress/dom-ready": "^4.0.0",
133
+ "@wordpress/element": "^6.0.0",
134
+ "@wordpress/i18n": "^5.0.0",
135
+ "@wordpress/notices": "^5.0.0",
136
+ "@wordpress/postcss-plugins-preset": "^5.0.0",
137
+ "@wordpress/url": "^4.0.0",
138
+ "babel-loader": "^9.0.0",
139
+ "css-loader": "^7.0.0",
140
+ "cssnano": "^7.0.0",
141
+ "mini-css-extract-plugin": "^2.0.0",
142
+ "postcss": "^8.0.0",
143
+ "postcss-loader": "^8.0.0",
144
+ "react": "^18.0.0",
145
+ "rtlcss-webpack-plugin": "^4.0.0",
146
+ "sass": "^1.0.0",
147
+ "sass-loader": "^16.0.0",
148
+ "source-map-loader": "^5.0.0",
149
+ "terser-webpack-plugin": "^5.0.0",
150
+ "url-loader": "^4.0.0",
151
+ "webpack": "^5.0.0",
152
+ "webpack-remove-empty-scripts": "^1.0.0"
153
+ },
154
+ "peerDependenciesMeta": {
155
+ "@sentry/webpack-plugin": {
156
+ "optional": true
157
+ },
158
+ "@svgr/webpack": {
159
+ "optional": true
160
+ },
161
+ "url-loader": {
162
+ "optional": true
163
+ }
164
+ }
165
+ }