vite-plugin-ifdef
Version:
a plugin for condition complie in vite ,transplant from webpack plugin ifdef-loader(https://github.com/nippur72/ifdef-loader#readme)
39 lines (32 loc) • 1.11 kB
JavaScript
;
import { Plugin, ResolvedConfig } from 'vite';
import parse from './preprocessor.js';
const vitePluginIfDef = (userOptions = {}) => {
let data;
let filePath;
let option;
return {
name: 'vitePluginIfdefCompile',
enforce: 'pre',
configResolved(resolvedConfig) {
data = resolvedConfig['ifdef-define'];
option = resolvedConfig['ifdef-option']
},
resolveId(source){
filePath=source
},
transform(code, id) {
let verboseFlag = "verbose";
let verbose = option[verboseFlag]
let tripleSlashFlag = "ifdef-triple-slash";
let tripleSlash = option[tripleSlashFlag];
let fillWithBlanksFlag = "ifdef-fill-with-blanks";
let fillWithBlanks = option[fillWithBlanksFlag];
let uncommentPrefixFlag = "ifdef-uncomment-prefix";
let uncommentPrefix = option[uncommentPrefixFlag];
userOptions=data
return parse(code, userOptions,verbose, tripleSlash, filePath, fillWithBlanks, uncommentPrefix);
},
};
};
export default vitePluginIfDef;