问题
I have built a web app on top of Stephen Grider's ReduxSimpleStarter, a react-redux boilerplate. It uses Webpack for bundling. Now I want to host my app on firebase. When I do so, the bundling doesn't work, and I am left with a simple index.html.
Could someone please explain how I trigger Webpack bundling for an app that is not locally hosted?
These files might be relevant, though I am not sure.
//package.json
{
"name": "testapp",
"version": "0.0.0",
"description": "testapp",
"main": "index.js",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.9.1",
"babel-preset-stage-1": "^6.1.18",
"jquery": "^2.2.0",
"lodash": "^3.10.1",
"material-ui": "^0.14.4",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-redux": "^4.0.0",
"react-tap-event-plugin": "^0.2.2",
"redux": "^3.0.4",
"redux-promise": "^0.5.1"
}
}
.
//webpack.config
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel'
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
回答1:
I found the solution. Include "webpack": "^1.12.9"
in the dependencies
object. Then, change
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js"
},
to
"scripts": {
"start": "node ./node_modules/webpack/bin/webpack.js"
},
回答2:
I know it is answered but this is an additional note to the answer which will help a lot.
Tip 1: When you deploy the application on Firebase and try to access browser (especially React,Redux) application you may get an error saying :
can not find module "run" in bundle.js
So as mentioned in the answer it is must, and after this- you must execute the command:
npm startThis command will regenerate the bundle.js and will not include/require the "run" module which we were using while development. After this- you can deploy the latest bundle.js to the server.
Tip 2: In webpack.config inside output:{...}
section you should set path
and publicPath
to a new directory i.e. /public/. Otherwise on Firebase host when you mentioned 'public' directory as default directory to deploy - then it will create problem and application will not run on Firebase server.
Note: actually I am not sure and do not know how to tell firebase to use files on my root and not in my 'public' folder
But I think that outputting the Webpack generated files and keeping other public files (css, html
) inside public folder is good as otherwise firebase deploy may upload other files sitting in root directory as well. (correct me if I'm wrong).
Ok so finally when you are updated the webpack.config output values as:
output: {
path: __dirname+ '/public',
publicPath: '/public/',
filename: 'bundle.js'
}
Then (finally making sure you are done with Tip.1 as well) and run the command to get latest bundle.js
npm start. And finally you are good to go and deploy using:
firebase deployI believe you may have followed the initial steps of initiating
firebase login
and other init commands before running last deploy command.
Note: The "firebase serve"
command is also helpful to debug and test if application is running well on local machine then it will run well on live Firebase server as well.
来源:https://stackoverflow.com/questions/35583858/hosting-web-app-bundled-with-webpack