[英]How to export a webpack-bundled function to global
我有一个 webpack 捆绑的 TypeScript 文件,它导出一个我需要从全局使用的函数,例如:
// bundled.ts
import * as Excel from 'exceljs';
import { saveAs } from 'file-saver';
declare const $find: any;
export function configExport() {
$('#ExportToExcelBtn').click( async () => {
...
let dataItems = $find('ViewGrid').get_masterTableView().get_dataItems();
...
});
}
// notBundled.js
configExport(); // does not exist in global window object
我一定没有正确阅读文档或其他内容,但是我无法将configExport
函数暴露/导出/提供/无论什么都显示到window
。 我已经研究了export-loader
、 expose-loader
和ProvidePlugin
,但我并不清楚我应该在这里做什么。
到目前为止,我已经在我的 webpack.config.js 中尝试过这样的事情:
module: {
rules: [
{
test: require.resolve("./Module/js/dist/bundled.js"),
use: [{
loader: "expose-loader",
options: "bundledModuleCode",
}]
},
但是configExport
和bundledModuleCode
都没有像我想要的bundledModuleCode
出现在window
。
我最终采用了一种类似于这里的方法: 如何在 TypeScript 中在 `window` 上显式设置新属性?
// bundled.ts
import * as Excel from 'exceljs';
import { saveAs } from 'file-saver';
declare const $find: any;
// type configExport as window property
declare global {
interface Window {
configExport: any;
}
}
// add configExport to window global
window.configExport = function() {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.