使用webpack,exports-loader和遗留JavaScript

时间:2018-04-19 19:05:07

标签: javascript node.js npm webpack exports-loader

我正在尝试在npm包中使用旧版JavaScript来分发给其他项目。

如果我在webpack的exports-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"
  );
};

我的webpack配置来自npm-package:

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:未定义finishFunction"。这表明它找到了printEan函数和BrowserPrint,但不知何故,遗留脚本中的代码没有被webpack正确处理。

查看遗留脚本的代码finishFunction从未定义,但是当我直接在其他项目中导入脚本时,我不知道它为什么会起作用。

来自遗留代码的

摘录:

  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),

0 个答案:

没有答案