Skip to content

Instantly share code, notes, and snippets.

@mgussekloo
Last active August 12, 2021 11:56
Show Gist options
  • Save mgussekloo/c0e508b93e32d0dbf8ff2966050766a0 to your computer and use it in GitHub Desktop.
Save mgussekloo/c0e508b93e32d0dbf8ff2966050766a0 to your computer and use it in GitHub Desktop.
Using Webpack ifdef-loader with Laravel Mix
1. Install ifdef-loader
```bash
yarn add ifdef-loader
```
2. Add the loader to Webpack through Laravel Mix 'webpackConfig' method. Apparently this doesn't overwrite the defaults but merges it nicely.
```js
mix.webpackConfig({
module: {
rules: [
{
test: /\.vue$/,
use: { loader: 'ifdef-loader', options: { "ifdef-fill-with-blanks": true } }
},
{
test: /\.js$/,
use: { loader: 'ifdef-loader', options: { "ifdef-fill-with-blanks": true } }
}
]
}
});
```
3. Profit! Only load modules if your env requires it.
/// #if process.env.MIX_THEME == 'ajax'
import axios from "axios";
axios.defaults.headers.common['X-WP-Nonce'] = game_api.nonce;
Vue.prototype.$http = axios;
/// #endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment