简体   繁体   English

导出webpack.config.js中的全局函数以在打字稿中使用

[英]Export a global function in webpack.config.js to be used in a typescript

New to webpack. Webpack的新功能。 I want to export a global function (similar to exporting variables through webpack.EnvironmentPlugin) to be used in a typescript 我想导出一个全局函数(类似于通过webpack.EnvironmentPlugin导出变量)以在打字稿中使用

I tried something like below just to see the the behavior. 我尝试了如下所示的操作,只是为了查看其行为。

module.exports = [{
  ...
  ...
  plugins: [
    new webpack.EnvironmentPlugin({
     env_func: function bindObj() {
        console.log("In plugin");
       }
    })
  ]
}]

It gives me an error (as below) when accessed as envFunc = process.env.env_func; envFunc.bindObj() 当以envFunc = process.env.env_func; envFunc.bindObj()访问时,它给我一个错误(如下) envFunc = process.env.env_func; envFunc.bindObj() envFunc = process.env.env_func; envFunc.bindObj()

TypeError: Cannot read property 'bindObj' of undefined TypeError:无法读取未定义的属性“ bindObj”

Is there a way or any plugin (similar to EnvironmentPlugin) I can export a function in webpack config that can be used from the typescript? 有没有办法或任何插件(类似于EnvironmentPlugin)我可以在webpack配置中导出可以从打字稿中使用的功能?

webpack.EnvironmentPlugin is a shorthand notation for DefinePlugin which you can use to define a function. webpack.EnvironmentPluginwebpack.EnvironmentPlugin的简写DefinePlugin ,可用于定义函数。 To make Typescript compiler accept it you need to declare the function eg include some .d.ts file into the project: 为了使Typescript编译器接受它,您需要声明函数,例如在项目中包含一些.d.ts文件:

xxx.d.ts
--------
declare function bindObj(): void;
declare function fun1(abc: string): number;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM