Net*_*eta 12 javascript reactjs webpack webpack-plugin
我正在尝试创建一个webpack插件,它将解析某个函数的代码并将其替换为另一个函数,该插件还将新函数公开为全局函数.
class someName {
constructor(local, domain, translationFile, options) {
}
apply(compiler) {
// exposing ngt function as a global
compiler.plugin('make', function(compilation, callback) {
var childCompiler = compilation.createChildCompiler('someNameExpose');
childCompiler.apply(new webpack.DefinePlugin({
ngt: function(singular , plural, quantity) {
return quantity == 1 ? singular : plural;
}
}));
childCompiler.runAsChild(callback);
});
// searching for the getValue function
compiler.parser.plugin(`call getValue`, function someNameHandler(expr) {
// create a function to replace getValue with
let results = 'ngt('+ expr.arguments +')';
const dep = new ConstDependency(results, expr.range);
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
});
}
}
module.exports = someName;
Run Code Online (Sandbox Code Playgroud)
更新/改写
我在这里有一个问题,当compiler.parser.plugin('call getValue', function someNameHandler(expr) {...}块被注释时,该ngt函数作为全局存在.
当它没有评论时,我得到一个错误,ngt是未定义的.
评论我的意思是 /**/
我找到了一个解决方法,但它远远的想法.现在我做的是导出一个匿名函数,它做我想要的.
你可以在这里看到插件: Github
您可以根据环境覆盖该方法。假设你有一个方法
function a(){
//original defination
}
Run Code Online (Sandbox Code Playgroud)
现在根据环境,如果是制作,你可以这样做
if (environment.production) {
function a(){
//overridden defination
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
777 次 |
| 最近记录: |