create-react-app: Change src of bundle.js in devel

2019-06-05 02:43发布

I'm trying to change the src of bundle.js in development with create-react-app.

By default the path is: /static/js/bundle.js

<body>
<div id="root"></div>
</script><script type="text/javascript" src="/static/js/bundle.js"></script></body>

In our production we use Apache as proxy to our API, to test SSO and other functionalities. So I have to add some string to the path, to be like this: myApp/static/js/bundle.js

<body>
<div id="root"></div>
</script><script type="text/javascript" src="myApp/static/js/bundle.js"></script></body>

I tried homepage in package.json, but it only works in npm run build. It also isn't proxy settings, not HOSt in .env

Is this even possible with create-react-app? I checked documentation but didn't find any solution.

2条回答
我想做一个坏孩纸
2楼-- · 2019-06-05 03:04

you will have to 'npm run eject' and modify the webpack files to change the output.

查看更多
不美不萌又怎样
3楼-- · 2019-06-05 03:04

Little late but if this helps others, this is how you achieve it. After eject, take a look in the config folder, the file: webpack.config.dev.js:

const publicPath = '/';
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
const publicUrl = '';

There you could change this values or, second option is to create a .env file and add:

PUBLIC_URL=/xxx
查看更多
登录 后发表回答