From e3829ce58e1f1e132651074bd7d15f52ea14e945 Mon Sep 17 00:00:00 2001 From: Matt Przybylski Date: Thu, 12 Sep 2019 19:25:54 -0700 Subject: [PATCH] feat: Add ability to set production publicPath through ASSET_PATH env var --- README.md | 7 +++++++ config/webpack.config.js | 16 +++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8936ce8..b987257 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/config/webpack.config.js b/config/webpack.config.js index 1b5cc92..e0b2233 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -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, @@ -30,6 +35,9 @@ module.exports = (mode = 'development') => { if (isDev) { entry.unshift('react-hot-loader/patch'); + output.publicPath = '/'; + } else if (assetPath) { + output.publicPath = assetPath; } return { @@ -37,13 +45,7 @@ module.exports = (mode = 'development') => { 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({