码迷,mamicode.com
首页 > Web开发 > 详细

[Tools] Using Webpack require.context to conditionally load file

时间:2020-12-31 12:11:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:cond   code   ken   condition   enc   get   lock   pre   exp   

require.context: https://webpack.js.org/guides/dependency-management/#requirecontext

require.context(directory, useSubdirectories = true, regExp = /^\.\/.*$/, mode = ‘sync‘);

 

For example, we have a file for local env called: ‘dev-tools.local.js‘, we want to load it if exists, but don‘t cause any error if file not exists. 

 

const requireDevToolsLocal = require.context(
    ‘./‘, // look for current dir
    false, // don‘t look into sub-dir
    /dev-tools\.local\.js/ // regex filename
)
const localFile + requireDevToolsLocal.keys()[0];
// if localfile not exist, don‘t load the file
if (local) {
    requireDevToolsLocal(local)
}

 

[Tools] Using Webpack require.context to conditionally load file

标签:cond   code   ken   condition   enc   get   lock   pre   exp   

原文地址:https://www.cnblogs.com/Answer1215/p/14193702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!