A project built with webpack can be deployed to a variety of environments. A public project that doesn't rely on a backend can be pushed to GitHub Pages using the gh-pages package. Also, there are a variety of webpack plugins that can target other environments, such as S3.
gh-pages allows you to host stand-alone applications on GitHub Pages easily. It has to be pointed to a build directory first. It picks up the contents and pushes them to the gh-pages
branch.
Despite its name, the package works with other services that support hosting from a Git repository as well. But given GitHub is so popular, it can be used to demonstrate the idea. In practice, you would likely have more complicated setup in place that would push the result to another service through a Continuous Integration system.
To get started, execute
npm add gh-pages --develop
You are also going to need a script in package.json
:
package.json
{
"scripts": {
"deploy": "gh-pages -d dist"
}
}
To make the asset paths work on GitHub Pages, output.publicPath
field has to be adjusted. Otherwise, the asset paths end up pointing at the root, and that doesn't work unless you are hosting behind a domain root (say survivejs.com
) directly.
publicPath
gives control over the resulting urls you see at index.html
for instance. If you are hosting your assets on a CDN, this would be the place to tweak.
In this case, it's enough to set it to point the GitHub project as below:
webpack.config.js
const productionConfig = merge([
{
output: {
publicPath: "/",
// Tweak this to match your GitHub project name
publicPath: "/webpack-demo/",
},
},
...
]);
After building (npm run build
) and deploying (npm run deploy
), you should have your application from the dist/
directory hosted on GitHub Pages. You should find it at https://<name>.github.io/<project>
assuming everything went fine.
If you need a more elaborate setup, use the Node API that **gh-pages** provides. The default command line tool it gives is enough for essential purposes, though.
GitHub Pages allows you to choose the branch where you deploy. It's possible to use the `master` branch even as it's enough for minimal sites that don't need bundling. You can also point below the `./docs` directory within your `master` branch and maintain your site.
gh-pages provides an add
option for archival purposes. The idea goes as follows:
dist/archive/<version>
ghpages.publish(path.join(__dirname, "dist"), { add: true }, cb);
Even though you can push the problem of deployment outside of webpack, there are a couple of webpack specific utilities that come in handy:
To get access to the generated files and their paths, consider using [assets-webpack-plugin](https://www.npmjs.com/package/assets-webpack-plugin). The path information allows you to integrate webpack with other environments while deploying.
To make sure clients relying on the older bundles still work after deploying a new version, do **not** remove the old files until they are old enough. You can perform a specific check on what to remove when deploying instead of removing every old asset.
output.publicPath
dynamically#If you don't know publicPath
beforehand, it's possible to resolve it based on the environment by following these steps:
__webpack_public_path__ = window.myDynamicPublicPath;
in the application entry point and resolve it as you see fit.output.publicPath
setting from your webpack configuration.globals.__webpack_public_path__: true
.When you compile, webpack picks up __webpack_public_path__
and rewrites it so that it points to webpack logic.
See [webpack documentation](https://webpack.js.org/api/module-variables/) for other webpack specific variables available at the module level.
Even though webpack isn't a deployment tool, you can find plugins for it.
To recap:
output.publicPath
dynamically. This technique is valuable if you don't know it compile-time and want to decide it later. This is possible through the __webpack_public_path__
global.This book is available through Leanpub (digital), Amazon (paperback), and Kindle (digital). By purchasing the book you support the development of further content. A part of profit (~30%) goes to Tobias Koppers, the author of webpack.