diff --git a/package-lock.json b/package-lock.json index 551120f2..a50ce23a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25691,6 +25691,69 @@ "node": ">=0.10.0" } }, + "node_modules/replace-asset-name-webpack-plugin": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/replace-asset-name-webpack-plugin/-/replace-asset-name-webpack-plugin-1.1.1.tgz", + "integrity": "sha512-97vq4TLxxIiVlP56ovpG4DIm/tCxdncWIcqrGOzlkBJVeJVkzKFAkAh9U1YUsfWJvGc/8BUUQQpudxKu+g5QVg==", + "dependencies": { + "schema-utils": "^4.0.0" + }, + "engines": { + "node": ">=12.20" + }, + "peerDependencies": { + "webpack": "^5.20.0" + } + }, + "node_modules/replace-asset-name-webpack-plugin/node_modules/ajv": { + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.12.0.tgz", + "integrity": "sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==", + "dependencies": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/replace-asset-name-webpack-plugin/node_modules/ajv-keywords": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-5.1.0.tgz", + "integrity": "sha512-YCS/JNFAUyr5vAuhk1DWm1CBxRHW9LbJ2ozWeemrIqpbsqKjHVxYPyi5GC0rjZIT5JxJ3virVTS8wk4i/Z+krw==", + "dependencies": { + "fast-deep-equal": "^3.1.3" + }, + "peerDependencies": { + "ajv": "^8.8.2" + } + }, + "node_modules/replace-asset-name-webpack-plugin/node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" + }, + "node_modules/replace-asset-name-webpack-plugin/node_modules/schema-utils": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-4.2.0.tgz", + "integrity": "sha512-L0jRsrPpjdckP3oPug3/VxNKt2trR8TcabrM6FOAAlvC/9Phcmm+cuAgTlxBqdBR1WJx7Naj9WHw+aOmheSVbw==", + "dependencies": { + "@types/json-schema": "^7.0.9", + "ajv": "^8.9.0", + "ajv-formats": "^2.1.1", + "ajv-keywords": "^5.1.0" + }, + "engines": { + "node": ">= 12.13.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/webpack" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -32372,6 +32435,7 @@ "react-app-polyfill": "^3.0.0", "react-dev-utils": "^12.0.1", "react-refresh": "^0.11.0", + "replace-asset-name-webpack-plugin": "^1.1.1", "resolve": "^1.20.0", "resolve-url-loader": "^4.0.0", "sass-loader": "^12.3.0", @@ -50749,6 +50813,7 @@ "react-dev-utils": "^12.0.1", "react-dom": "^18.0.0", "react-refresh": "^0.11.0", + "replace-asset-name-webpack-plugin": "^1.1.1", "resolve": "^1.20.0", "resolve-url-loader": "^4.0.0", "sass-loader": "^12.3.0", @@ -51500,6 +51565,51 @@ "is-finite": "^1.0.0" } }, + "replace-asset-name-webpack-plugin": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/replace-asset-name-webpack-plugin/-/replace-asset-name-webpack-plugin-1.1.1.tgz", + "integrity": "sha512-97vq4TLxxIiVlP56ovpG4DIm/tCxdncWIcqrGOzlkBJVeJVkzKFAkAh9U1YUsfWJvGc/8BUUQQpudxKu+g5QVg==", + "requires": { + "schema-utils": "^4.0.0" + }, + "dependencies": { + "ajv": { + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.12.0.tgz", + "integrity": "sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==", + "requires": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + } + }, + "ajv-keywords": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-5.1.0.tgz", + "integrity": "sha512-YCS/JNFAUyr5vAuhk1DWm1CBxRHW9LbJ2ozWeemrIqpbsqKjHVxYPyi5GC0rjZIT5JxJ3virVTS8wk4i/Z+krw==", + "requires": { + "fast-deep-equal": "^3.1.3" + } + }, + "json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" + }, + "schema-utils": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-4.2.0.tgz", + "integrity": "sha512-L0jRsrPpjdckP3oPug3/VxNKt2trR8TcabrM6FOAAlvC/9Phcmm+cuAgTlxBqdBR1WJx7Naj9WHw+aOmheSVbw==", + "requires": { + "@types/json-schema": "^7.0.9", + "ajv": "^8.9.0", + "ajv-formats": "^2.1.1", + "ajv-keywords": "^5.1.0" + } + } + } + }, "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", diff --git a/packages/react-scripts/config/webpack.config.js b/packages/react-scripts/config/webpack.config.js index 7a930cd2..2c299cdc 100644 --- a/packages/react-scripts/config/webpack.config.js +++ b/packages/react-scripts/config/webpack.config.js @@ -22,7 +22,7 @@ const { WebpackManifestPlugin } = require('webpack-manifest-plugin'); const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); const WorkboxWebpackPlugin = require('workbox-webpack-plugin'); const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin'); -const ReplaceAssetNamePlugin = require('./webpack/plugins/replace-asset-name-webpack-plugin'); +const ReplaceAssetNamePlugin = require('replace-asset-name-webpack-plugin'); const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent'); const ESLintPlugin = require('eslint-webpack-plugin'); const paths = require('./paths'); diff --git a/packages/react-scripts/config/webpack/plugins/replace-asset-name-webpack-plugin.js b/packages/react-scripts/config/webpack/plugins/replace-asset-name-webpack-plugin.js deleted file mode 100644 index 7acf194c..00000000 --- a/packages/react-scripts/config/webpack/plugins/replace-asset-name-webpack-plugin.js +++ /dev/null @@ -1,108 +0,0 @@ -'use strict'; - -/** - * Copy obtained from: https://github.com/sibiraj-s/replace-asset-name-webpack-plugin - * - * MIT License - * - * Copyright (c) 2021-present Sibiraj - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -const PLUGIN_NAME = 'ReplaceAssetNamePlugin'; - -class ReplaceAssetNameWebpackPlugin { - constructor(options) { - this.options = options; - } - - replace(compiler, compilation, assets) { - const { - ModuleFilenameHelpers, - sources: { RawSource }, - } = compiler.webpack; - - const logger = compilation.getLogger(PLUGIN_NAME); - const allAssets = Object.keys(assets); - - const assetsToSearch = allAssets.filter(assetName => { - return ModuleFilenameHelpers.matchObject.bind(undefined, { - test: this.options.asset, - })(assetName); - }); - - assetsToSearch.forEach(assetName => { - const { source, info } = compilation.getAsset(assetName); - const assetContent = source.source(); - - let assetString = assetContent.toString('utf8'); - - this.options.rules.forEach(rule => { - const searchRegex = - rule.search instanceof RegExp - ? rule.search - : new RegExp(rule.search, 'g'); - - const assetToReplace = allAssets.find(asset => { - if (rule.replace instanceof RegExp) { - return rule.replace.test(asset); - } - - return asset === rule.replace; - }); - - if (!assetToReplace) { - logger.warn(`no asset matches: ${rule.replace}`); - return; - } - - const transformedAssetToReplace = - typeof rule.transform === 'function' - ? rule.transform(assetToReplace) - : assetToReplace; - - assetString = assetString.replace( - searchRegex, - transformedAssetToReplace - ); - }); - - compilation.updateAsset(assetName, new RawSource(assetString), info); - }); - } - - apply(compiler) { - const { Compilation } = compiler.webpack; - - compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => { - compilation.hooks.processAssets.tap( - { - name: PLUGIN_NAME, - stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE, - }, - assets => { - this.replace(compiler, compilation, assets); - } - ); - }); - } -} - -module.exports = ReplaceAssetNameWebpackPlugin; diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index 0f177b01..d8fbfa1e 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -66,6 +66,7 @@ "react-app-polyfill": "^3.0.0", "react-dev-utils": "^12.0.1", "react-refresh": "^0.11.0", + "replace-asset-name-webpack-plugin": "^1.1.1", "resolve": "^1.20.0", "resolve-url-loader": "^4.0.0", "sass-loader": "^12.3.0",