js-conditional-compile-loader
A javascript conditional compiling loader for webpack.
Conditional compiling means that we can compile or not compile some js code according to some environment variables.
For example: we can output two different program for debug or release environment with a same source code project.
Usage
Just use it anywhere in js code like this:
or
Start with "/*IFDEBUG", end with"FIDEBUG*/", and js code in the center. you can use it any where in js files.
$state.go('win', {dir: menu.winId })
Output for debug:
$state.go('win', {dir: menu.winId, reload: true })
Output of production:
$state.go('win', {dir: menu.winId})
var tx = "This is app /* IFTRUE_Ali of debug FITRUE_Ali */ here";
Since it is designed by a js comment style, the code can run normaly even though the js-conditional-compile-loader is not used.
Setup
npm i -D js-conditional-compile-loader
Config in webpack
You should change webpack config like this:
See this sample: vue-element-ui-scaffold-webpack4(https://github.com/hzsrc/vue-element-ui-scaffold-webpack4)
module: {
rules: [
{
test: /\.js$/,
include: [resolve('src'), resolve('test')],
exclude: file => (
/node_modules/.test(file) && !/\.vue\.js/.test(file)
),
use: [
'babel-loader?cacheDirectory',
{
loader: 'js-conditional-compile-loader',
options: {
isDebug: process.env.NODE_ENV === 'development',
myFlag: process.env.ENV_COMPANY === 'ALI',
}
},
]
},
]
}
options
- isDebug: {bool = [process.env.NODE_ENV === 'development']}
If isDebug === false, all the codes between /\*IFDEBUG and FIDEBUG\*/ will be removed, otherwise the codes will be remained.
Samples
Vue.component('debugInfo', {
template: ''
,
watch: {
},
});
import { Layout } from 'my-layout-component'
var LayoutRun = Layout
export default {
components: {
LayoutRun
},
}