Last active
August 12, 2021 11:56
-
-
Save mgussekloo/c0e508b93e32d0dbf8ff2966050766a0 to your computer and use it in GitHub Desktop.
Using Webpack ifdef-loader with Laravel Mix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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