js-conditional-compile-loader
A javascript conditional compiling loader for webpack.
Conditional compiling means that we can use the same codes and compiling process, to build different applications with different environment conditions.
For example: we can output two different program for debug or release environment with a same source code project.
Usage
This loader provides two commands: IFDEBUG and IFTRUE. Just use them anywhere in js code like this: Start with /*IFDEBUG or /*IFTRUE_xxx, end with FIDEBUG*/ or FITRUE_xxx*/, place js code in the center. The xxx is any condition property of the options in webpack, such like myFlag.
- Mode 1 - comment all
Since it is designed by a js comment style, the code can run normaly even though the js-conditional-compile-loader is not used.
- Mode 2 -- head and foot
In this mode, you can use eslint to check your code.
var anyJsHere = 'Any js here'
function anyJsHere(){
}
Build result with source code
Source code:
var aliCode = require('./ali/alibaba-business.js')
aliCode.doSomething()
$state.go('win', {dir: menu.winId })
Compiled output by options: {isDebug: true, forAlibaba: true}:
var aliCode = require('./ali/alibaba-business.js')
aliCode.doSomething()
$state.go('win', {dir: menu.winId, reload: true })
Compiled output by options: {isDebug: false, forAlibaba: false}:
$state.go('win', {dir: menu.winId})
Setup
npm i -D js-conditional-compile-loader
Config in webpack
Change webpack config like this:
See this sample: vue-element-ui-scaffold-webpack4(https://github.com/hzsrc/vue-element-ui-scaffold-webpack4)
js-conditional-compile-loader needs to be add as step 1 for js files.
module: {
rules: [
{
test: /\.js$/,
include: [resolve('src'), resolve('test')],
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
var tx = "This is app /* IFTRUE_Ali of debug FITRUE_Ali */ here";
Vue.component('debugInfo', {
template: ''
,
watch: {
curRule (v){
this.ruleData = v
},
},
});
import { Layout } from 'my-layout-component'
var LayoutRun = Layout
export default {
components: {
LayoutRun
},
}