我正在尝试在 npm 包中使用旧版 JavaScript 分发到其他项目。
如果我在 webpack 的 export-loader 的帮助下将 Javascript 文件直接导入其中一个项目,我就可以毫无问题地使用所有功能。
但是,如果我将 npm 包导入到我遇到问题的项目之一。我认为我在 npm 包中的 webpack 设置设置不正确。
npm 包:
import { BrowserPrint } from "./BrowserPrint.fat";
exports.printEan = function(ean) {
BrowserPrint.getLocalDevices(
function(printers) {
console.log("something happened");
},
undefined,
"printer"
);
};
我来自 npm-package 的 webpack 配置:
const webpack = require("webpack");
module.exports = {
entry: ["./src/index.js"],
output: {
path: __dirname + "/lib",
filename: "main.js",
library: "zpl-browser-print",
libraryTarget: "umd"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: require.resolve("./src/BrowserPrint.fat.js"),
use: "exports-loader?BrowserPrint=BrowserPrint"
}
]
}
};
当我将此模块导入我的项目并调用 printEan 时,我得到“未捕获的 ReferenceError:finishedFunction 未定义”。这表明它找到了 printEan 函数和 BrowserPrint,但不知何故,旧脚本中的代码没有被 webpack 正确处理。
查看遗留脚本的代码finishedFunction 从未定义,但是当我直接在其他项目中导入脚本时,我不知道为什么它会起作用。
旧代码的片段:
c &&
((finishedFunction = function(e) {
if (((response = e), "" == response)) return void s(null);
response = JSON.parse(response);
var n = new t.Device(response);
s(n);
}),
i(void 0, c, finishedFunction, o),