Now, we don't need to edit the Webpack.config.js anymore instead, we can configure our own using
customize-cra,
react-app-rewired,
copy-webpack-plugin,
react-app-rewire-multiple-entry
1 and 2 will overwrite the webpack default configuration, 3 to copy all of our assets files, and 4 to add multiple files
Just install it:
npm -i customize-cra react-app-rewired copy-webpack-plugin react-app-rewire-multiple-entry --save-dev
Yarn
yarn add customize-cra react-app-rewired copy-webpack-plugin react-app-rewire-multiple-entry --dev
Configuration
create a config-overrides.js file in your root directory / and put use this code
const {
override,
overrideDevServer,
addWebpackPlugin
} = require("customize-cra");
const CopyPlugin = require('copy-webpack-plugin');
const multipleEntry = require('react-app-rewire-multiple-entry')([
{
// points to the popup entry point
entry: 'src/popup/index.js',
template: 'public/popup.html',
outPath: '/popup.html'
},
{
// points to the options page entry point
entry: 'src/options/index.js',
template: 'public/index.html',
outPath: '/index.html'
}
]);
const devServerConfig = () => config => {
return {
...config,
// webpackDevService doesn't write the files to desk
// so we need to tell it to do so so we can load the
// extension with chrome
writeToDisk: true
}
}
const copyPlugin = new CopyPlugin({
patterns: [
// copy assets
{ from: 'public', to: '' },
{ from: 'src/background.js', to: '' }
]
})
module.exports = {
webpack: override(
addWebpackPlugin(
copyPlugin
),
multipleEntry.addMultiEntry,
),
devServer: overrideDevServer(
devServerConfig()
),
};
go ahead and create two folders in src/, options/, and popup containing the index.js and app.js files, mean copy all the files from src/ when you install React and past it to these both folders. for the background.js file you should put this on the src/:
/
src/
background.js
index.js // empty file only
options
index.js
app.js
app.css
popup
index.js
app.js
app.css
Also overide the script in the package.json file to:
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
}
now you don't need to reload the extension after changes, it will automatically trigger when you save your code.
another file is to create is .env in the root directory and past this:
INLINE_RUNTIME_CHUNK=false
Now run the command yarn start or npm start, load the extension you should be good to go!
Resource to learn
content.chunk.jsin an HTML page as a script tag, it works as expected. However, through content script I don't see anything happening.