Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-FRONTIER.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.8.5

- Add options for injecting scripts and styles (for hf-inj-react)

## 8.8.3

- Add photos/apiHelper endpoint to proxy list
Expand Down
28 changes: 28 additions & 0 deletions packages/react-scripts/config/InjectEntrypointsPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
const { getCompilerHooks } = require('webpack-manifest-plugin');
const fs = require('fs');
const path = require('path');

class InjectEntrypointsPlugin {
constructor(options) {
this.options = options;
}

apply(compiler) {
const {afterEmit} = getCompilerHooks(compiler)

afterEmit.tap('InjectEntrypointsPlugin', (manifest) => {
const outputPath = path.join(
compiler.options.context,
this.options.outputFile
);

fs.writeFileSync(outputPath,
JSON.stringify(
manifest.entrypoints.map((entry) => this.options.outputPath + entry)),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entrypoints didn't have a full path (prepended w/the CDN when doing a prod build) So I needed to do this to know where to look for them. However, I am going to change this variable name because it's confusing with line 15. And it's just paths.publicUrlOrPathso it doesn't even need passed in

'utf8');
})
}
}

module.exports = InjectEntrypointsPlugin;
22 changes: 19 additions & 3 deletions packages/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ console.log(`In webpack.config from @fs/react-scripts version ${reactScriptsVers
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier')
// @remove-on-eject-end
const createEnvironmentHash = require('./webpack/persistentCache/createEnvironmentHash')
const InjectEntrypointsPlugin = require('./InjectEntrypointsPlugin.js')

// Source maps are resource heavy and can cause out of memory issue for large source files.
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false'
Expand Down Expand Up @@ -140,8 +141,17 @@ module.exports = function (webpackEnv) {
// common function to get style loaders
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
isEnvDevelopment && require.resolve('style-loader'),
isEnvProduction && {
(isEnvDevelopment || process.env.REACT_APP_INJECT_STYLES) && {
loader: require.resolve('style-loader'),
options: process.env.REACT_APP_INJECT_STYLES ? {
injectType: 'singletonStyleTag',
insert: function addToWindowObject(element) {
const _window = typeof window !== 'undefined' ? window : {}
_window[process.env.REACT_APP_INJECT_STYLES] = element
}
} : {},
},
(isEnvProduction && !process.env.REACT_APP_INJECT_STYLES) && {
loader: MiniCssExtractPlugin.loader,
// css is located in `static/css`, use '../../' to locate index.html folder
// in production `paths.publicUrlOrPath` can be a relative path
Expand Down Expand Up @@ -731,7 +741,7 @@ module.exports = function (webpackEnv) {
// a plugin that prints an error when you attempt to do this.
// See https://github.com/facebook/create-react-app/issues/240
isEnvDevelopment && new CaseSensitivePathsPlugin(),
isEnvProduction &&
(isEnvProduction && !process.env.REACT_APP_INJECT_STYLES) &&
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
Expand Down Expand Up @@ -762,6 +772,12 @@ module.exports = function (webpackEnv) {
}
},
}),
// Inject the manifest entrypoint file names into the outputFile
process.env.REACT_APP_INJECT_SCRIPTS == 'true' &&
new InjectEntrypointsPlugin({
outputPath: paths.publicUrlOrPath,
outputFile: 'hf-inj-react-scripts.js',
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fs/react-scripts",
"version": "8.8.4",
"version": "8.8.5",
"upstreamVersion": "5.0.1",
"description": "Configuration and scripts for Create React App.",
"repository": {
Expand Down