Gathering detailed insights and metrics for yn-js-conditional-compile-loader
Gathering detailed insights and metrics for yn-js-conditional-compile-loader
Gathering detailed insights and metrics for yn-js-conditional-compile-loader
Gathering detailed insights and metrics for yn-js-conditional-compile-loader
npm install yn-js-conditional-compile-loader
Typescript
Module System
Node Version
NPM Version
64.3
Supply Chain
96.7
Quality
74.8
Maintenance
100
Vulnerability
100
License
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
158
Last Day
1
Last Week
1
Last Month
13
Last Year
57
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
yn-js-conditional-compile-loader@1.1.0
Unpacked Size
125.39 kB
Size
101.60 kB
File Count
6
NPM Version
8.19.4
Node Version
16.20.0
Published on
Jun 29, 2023
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-85.7%
1
Compared to previous week
Last Month
225%
13
Compared to previous month
Last Year
-43.6%
57
Compared to previous year
1
2
一个条件编译的webpack loader, 支持按条件构建各种代码文件,如js、ts、vue、css、scss、html等。
条件编译,是指 用同一套代码和同样的编译构建过程,根据设置的条件,选择性地编译指定的代码,从而输出不同程序的过程。
npm run build --ali
发布针对ali的程序,npm run build --tencent
发布针对tencent的程序。插件提供了IFDEBUG
和IFTRUE
两个条件编译指令。用法是:在js代码的任意地方以/*IFDEBUG
或/*IFTRUE_xxx
开头,以FIDEBUG*/
或FITRUE_xxx*/
结尾,中间是被包裹的js代码。xxx
是在webpack中指定的options条件属性名,比如myFlag
。
html和 vue template 捏需要使用 html的注释方式:<!-- IFDEBUG
或<!-- IFTRUE_xxx
开头,以FIDEBUG -->
或FITRUE_xxx -->
结尾
1 /*IFDEBUG Any js here FIDEBUG*/
1/* IFTRUE_yourFlagName ...js code... FITRUE_yourFlagName */
1<!-- IFDEBUG --> 2var anyJsHere = 'Any js here' 3<!-- FIDEBUG -->
1/* IFDEBUG */ 2var anyJsHere = 'Any js here' 3/*FIDEBUG */
1/* IFTRUE_yourFlagName*/ 2function anyJsHere(){ 3} 4/*FITRUE_yourFlagName */
源码:
1/* IFTRUE_forAlibaba */ 2var aliCode = require('./ali/alibaba-business.js') 3aliCode.doSomething() 4/* FITRUE_forAlibaba */ 5 6$state.go('win', {dir: menu.winId /*IFDEBUG , reload: true FIDEBUG*/})
当options为{isDebug: true, forAlibaba: true}
时,构建后输出的内容:
1var aliCode = require('./ali/alibaba-business.js') 2aliCode.doSomething() 3 4$state.go('win', {dir: menu.winId, reload: true })
当options为{isDebug: false, forAlibaba: false}
时,构建后输出的内容:
1$state.go('win', {dir: menu.winId})
1 npm i -D js-conditional-compile-loader
这样修改webpack配置:
查看样例: vue-element-ui-scaffold-webpack4
js-conditional-compile-loader
需要作为处理源文件的第一步,即放在use数组的末尾。
如下例子为vue、js文件的配置,ts文件的配置类似。css、scss等样式的配置略复杂,可参考这个样例
1const conditionalCompiler = { 2 loader: 'js-conditional-compile-loader', 3 options: { 4 isDebug: process.env.NODE_ENV === 'development', // optional, this expression is default 5 envTest: process.env.ENV_CONFIG === 'test', // any prop name you want, used for /* IFTRUE_evnTest ...js code... FITRUE_evnTest */ 6 myFlag: process.env.npm_config_myflag, // enabled when running command: `npm run build --myflag` 7 } 8} 9 10module.exports = { 11 // others... 12 module: { 13 rules: [ 14 { 15 test: /\.vue$/, 16 use: ['vue-loader', conditionalCompiler], 17 }, 18 { 19 test: /\.js$/, 20 include: [resolve('src'), resolve('test')], 21 use: [ 22 //step-2 23 'babel-loader?cacheDirectory', 24 //step-1 25 conditionalCompiler, 26 ], 27 }, 28 // others... 29 ] 30 } 31}
如果isDebug
=== false,则所有/\*IFDEBUG
和 FIDEBUG\*/
之间的代码都会被移除。 其他情况,这些代码则会被保留。
isDebug
默认取值为:process.env.NODE_ENV === 'development'
如果 [属性值] === false,则所有/\* IFTRUE_属性名
和 FITRUE_属性名 \*/
之间的代码都会被移除。 其他情况,这些代码则会被保留。
条件编译指令可以用在任意源代码文件的任意位置,不受代码语法限制。
例如:
1const tx = "This is app /* IFTRUE_Ali of debug FITRUE_Ali */ here"; 2 3 4/*IFDEBUG 5let tsFunc = function(arr: number[]) : string { 6 alert('Hi~'); 7 return arr.length.toString() 8} 9FIDEBUG*/
1/* IFTRUE_myFlag */ 2div > ul > li { 3 a { 4 color: red; 5 } 6} 7/*FITRUE_myFlag */ 8 9 10h2{ 11 background: red; 12 /* IFTRUE_myFlag 13 color: blue; 14 FITRUE_myFlag */ 15}
1Vue.component('debugInfo', { 2 template: '' 3 /* IFDEBUG 4 + '<pre style="font-size:13px;font-family:\'Courier\',\'Courier New\';z-index:9999;line-height: 1.1;position: fixed;top:0;right:0; pointer-events: none">{{JSON.stringify($attrs.info || "", null, 4).replace(/"(\\w+)":/g, "$1:")}}</pre>' 5 FIDEBUG */ 6 , 7 watch: { 8 /* IFTRUE_myFlag */ 9 curRule (v){ 10 this.ruleData = v 11 }, 12 /*FITRUE_myFlag */ 13 }, 14});
1<temeplate> 2 <div> 3 <!-- IFTRUE_myFlag --> 4 <h2>This is a test! For HTML. vue模板内也可以使用!</h2> 5 <pre> 6 {{$attrs.info || ''}} 7 </pre> 8 <!-- FITRUE_myFlag --> 9 </div> 10</temeplate> 11 12<script> 13 var vueComponent = { 14 data: { 15 /* IFTRUE_myFlag 16 falgData: 'Flag Data', 17 FITRUE_myFlag */ 18 }, 19 }; 20</script> 21 22/* IFTRUE_myFlag*/ 23<style scoped> 24 .any-where-test { 25 color: red; 26 } 27</style> 28/* FITRUE_myFlag*/ 29 30 31<style id="a" scoped> 32 /* IFTRUE_myFlag*/ 33 .test-for-css { 34 color: red; 35 } 36 /*FITRUE_myFlag */ 37</style>
No vulnerabilities found.
No security vulnerabilities found.