Skip to content

Commit

Permalink
feat: Add ability to set production publicPath through ASSET_PATH env…
Browse files Browse the repository at this point in the history
… var
  • Loading branch information
reintroducing committed Sep 13, 2019
1 parent c46ff1d commit e3829ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ An array of additional [rules](https://webpack.js.org/configuration/module/#modu
The style of [source map](https://webpack.js.org/configuration/devtool/#devtool) to use. Set to false for any mode to disable.

**default:** `isDev ? 'cheap-module-source-map' : 'source-map'`

## Custom Production Asset Path
In some cases () you may want to pass a specific path for your static assets to replace the pre-configured `publicPath`. You can do so by setting a special `ASSET_PATH` environment variable before running the build script in your build configuration.

```bash
ASSET_PATH=/ npm run build
```
16 changes: 9 additions & 7 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ const flexbugsFixes = require('postcss-flexbugs-fixes');

module.exports = (mode = 'development') => {
const isDev = mode === 'development' || mode === 'test';
const assetPath = process.env.ASSET_PATH;
const dist = path.resolve(process.cwd(), 'dist');
const src = path.resolve(process.cwd(), 'src');
const entry = [`${src}/index.js`];
const output = {
filename: `js/[name]${isDev ? '' : '-[hash]'}.js`,
path: `${dist}`,
};
const stats = {
children: false,
chunkModules: false,
Expand All @@ -30,20 +35,17 @@ module.exports = (mode = 'development') => {

if (isDev) {
entry.unshift('react-hot-loader/patch');
output.publicPath = '/';
} else if (assetPath) {
output.publicPath = assetPath;
}

return {
mode: isDev ? 'development' : 'production',
target: 'web',
devtool: isDev ? 'cheap-module-source-map' : 'source-map',
entry,
output: {
filename: `js/[name]${isDev ? '' : '-[hash]'}.js`,
path: `${dist}`,
...(isDev && {
publicPath: '/',
}),
},
output,
plugins: [
new StyleLintPlugin(),
new HtmlWebpackPlugin({
Expand Down

0 comments on commit e3829ce

Please sign in to comment.