From 1795f13234efc4625baba602b5208d96498b17bd Mon Sep 17 00:00:00 2001 From: Mike Crantea Date: Wed, 7 Apr 2021 20:59:39 +0100 Subject: [PATCH 01/27] Build: Add JS build process --- package.json | 51 ++++++++++++++++ webpack.config.js | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 package.json create mode 100644 webpack.config.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..2fb7937 --- /dev/null +++ b/package.json @@ -0,0 +1,51 @@ +{ + "name": "site-performance-tracker", + "version": "1.0.0", + "description": "Site Performance Tracker", + "author": "XWP", + "license": "GPL-2.0-or-later", + "keywords": [ + "WordPress", + "Plugin" + ], + "engines": { + "node": ">=14.0.0" + }, + "homepage": "https://github.com/xwp/site-performance-tracker#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/xwp/site-performance-tracker.git" + }, + "bugs": { + "url": "https://github.com/xwp/site-performance-tracker/issues" + }, + "browserslist": [ + "last 2 Chrome versions", + "last 2 Firefox versions", + "last 2 Safari versions", + "last 2 Edge versions", + "last 2 iOS versions", + "last 1 Android version", + "last 1 ChromeAndroid version", + "> 2%" + ], + "devDependencies": { + "@babel/plugin-syntax-dynamic-import": "^7.8.3", + "@wordpress/scripts": "^13.0.3", + "del": "^6.0.0", + "dir-archiver": "^1.1.1", + "gulp": "^4.0.2", + "gulp-rename": "^2.0.0", + "npm-run-all": "^4.1.5", + "webpackbar": "^5.0.0-3" + }, + "scripts": { + "dev:js": "wp-scripts start", + "build:js": "wp-scripts build", + "lint:js": "wp-scripts lint-js", + "format:js": "npm run lint:js -- --fix" + }, + "dependencies": { + "web-vitals": "^1.1.1" + } +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..3d93312 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,146 @@ +/** + * External dependencies + */ + +// TerserPlugin is bundled in Webpack 5. +// eslint-disable-next-line import/no-extraneous-dependencies +const TerserPlugin = require( 'terser-webpack-plugin' ); +// path is a native Node module +// eslint-disable-next-line import/no-extraneous-dependencies +const path = require( 'path' ); +const WebpackBar = require( 'webpackbar' ); + +const config = { + srcDir: '/js/src/', + distDirModern: 'js/dist/module/', + distDirLegacy: 'js/dist/nomodule/' +}; + +config.modernJsEntries = { + 'web-vitals-analytics': config.srcDir + 'web-vitals-analytics.js', +}; + +config.legacyJsEntries = {}; + +/** + * WordPress dependencies + */ +const defaultConfig = require( '@wordpress/scripts/config/webpack.config' ); + +const sharedConfig = { + optimization: { + minimizer: [ + new TerserPlugin( { + parallel: true, + sourceMap: false, + cache: true, + terserOptions: { + output: { + comments: /translators:/i, + }, + }, + extractComments: false, + } ), + ], + }, + plugins: [ ...defaultConfig.plugins ], +}; + +const configureBabelLoader = ( browserlist ) => { + return { + test: /\.js$/, + use: { + loader: 'babel-loader', + options: { + babelrc: false, + exclude: [ + /core-js/, + /regenerator-runtime/, + ], + presets: [ + [ '@babel/preset-env', { + loose: true, + modules: false, + // debug: true, + corejs: 3, + useBuiltIns: 'usage', + targets: { + browsers: browserlist, + }, + } ], + ], + plugins: [ '@babel/plugin-syntax-dynamic-import' ], + }, + }, + }; +}; + +const legacyConfig = { + output: { + path: path.join( __dirname, config.distDirLegacy ), + filename: `[name].js`, + chunkFilename: `[name].js`, + }, + module: { + rules: [ + configureBabelLoader( [ + '> 2%', + 'not IE 11', + 'last 2 versions', + ] ), + ], + }, +}; + +const modernConfig = { + output: { + path: path.join( __dirname, config.distDirModern ), + filename: `[name].js`, + chunkFilename: `[name].js`, + }, + module: { + rules: [ + configureBabelLoader( [ + // The last two versions of each browser, excluding versions + // that don't support