Skip to content

Commit

Permalink
CDM-RS: CRA custom output name based on package name and version when…
Browse files Browse the repository at this point in the history
  • Loading branch information
phorvicheka committed Oct 14, 2020
1 parent de0046b commit 9e525a6
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
98 changes: 98 additions & 0 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const ManifestPlugin = require('webpack-manifest-plugin');
const paths = require('react-scripts/config/paths');
const version = require('./package.json').version;
const moduleName = 'cdm-rcs';
const buildFileName = `${moduleName}-${version}`;

/**
* Utility function to replace plugins in the webpack config files used by react-scripts
*/
const replacePlugin = (plugins, nameMatcher, newPlugin) => {
const pluginIndex = plugins.findIndex((plugin) => {
return (
plugin.constructor &&
plugin.constructor.name &&
nameMatcher(plugin.constructor.name)
);
});

if (pluginIndex === -1) return plugins;

return plugins
.slice(0, pluginIndex)
.concat(newPlugin)
.concat(plugins.slice(pluginIndex + 1));
};

module.exports = {
webpack: function (config, env) {
const isEnvProduction = env === 'production';
if (isEnvProduction) {
// Entry Naming: buildFileName instead of main by CRA default build file name
// https://webpack.js.org/configuration/entry-context/#naming
config.entry = { [buildFileName]: './src/index.js' };

// Disable optimization runtimeChunk
config.optimization.runtimeChunk = false;
// Disable optimization splitChunks
config.optimization.splitChunks = {
cacheGroups: {
default: false,
},
};

// Multiple entries rewire error: Cannot read property 'filter' of undefined
// https://github.com/timarney/react-app-rewired/issues/421
const multiEntryManfiestPlugin = new ManifestPlugin({
fileName: 'asset-manifest.json',
publicPath: paths.publicUrlOrPath,
generate: (seed, files, entrypoints) => {
const manifestFiles = files.reduce((manifest, file) => {
manifest[file.name] = file.path;
return manifest;
}, seed);
const entrypointFiles = {};
Object.keys(entrypoints).forEach((entrypoint) => {
entrypointFiles[entrypoint] = entrypoints[
entrypoint
].filter((fileName) => !fileName.endsWith('.map'));
});

return {
files: manifestFiles,
entrypoints: entrypointFiles,
};
},
});

// replace CRA ManifestPlugin with multiEntryManfiestPlugin
config.plugins = replacePlugin(
config.plugins,
(name) => /ManifestPlugin/i.test(name),
multiEntryManfiestPlugin
);

// Override JS output filename and chunkFilename
config.output.filename = `static/js/[name].js`;
config.output.chunkFilename = `static/js/[name].chunk.js`;

// Override CSS output filename and chunkFilename
// eslint-disable-next-line array-callback-return
config.plugins.map((plugin, i) => {
if (
plugin.options &&
plugin.options.filename &&
plugin.options.filename.includes('static/css')
) {
config.plugins[i].options = {
...config.plugins[i].options,
filename: `static/css/[name].css`,
chunkFilename: `static/css/[name].chunk.css`,
};
}
});
}

return config;
},
};
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/runtime": "^7.11.2",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
Expand All @@ -18,9 +19,9 @@
"sagas": "^0.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject",
"docs": "jsdoc -c ./jsdoc.json"
},
Expand Down Expand Up @@ -52,6 +53,7 @@
},
"devDependencies": {
"better-docs": "^2.3.2",
"jsdoc": "^3.6.6"
"jsdoc": "^3.6.6",
"react-app-rewired": "^2.1.6"
}
}
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.4.2", "@babel/runtime@^7.5.1", "@babel/runtime@^7.5.5", "@babel/runtime@^7.6.3", "@babel/runtime@^7.7.6":
"@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.11.2", "@babel/runtime@^7.4.2", "@babel/runtime@^7.5.1", "@babel/runtime@^7.5.5", "@babel/runtime@^7.6.3", "@babel/runtime@^7.7.6":
version "7.11.2"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
integrity sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw==
Expand Down Expand Up @@ -9171,6 +9171,13 @@ react-app-polyfill@^1.0.6:
regenerator-runtime "^0.13.3"
whatwg-fetch "^3.0.0"

react-app-rewired@^2.1.6:
version "2.1.6"
resolved "https://registry.yarnpkg.com/react-app-rewired/-/react-app-rewired-2.1.6.tgz#33ee3076a7f34d6a7c94e649cac67e7c8c580de8"
integrity sha512-06flj0kK5tf/RN4naRv/sn6j3sQd7rsURoRLKLpffXDzJeNiAaTNic+0I8Basojy5WDwREkTqrMLewSAjcb13w==
dependencies:
semver "^5.6.0"

react-bootstrap@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/react-bootstrap/-/react-bootstrap-1.3.0.tgz#d9dde4ad554e9cd21d1465e8b5e5ef6679cae6a1"
Expand Down

0 comments on commit 9e525a6

Please sign in to comment.