1

I am trying to use a legacy JavaScript in a npm package to distribute to other projects.

If I import the Javascript file directly into one of the projects with help of webpack's exports-loader I am able to use all of the functions without any issues.

However if I import the npm package into one of the projects I run into issues. I think my webpack setup in the npm package is incorrectly setup.

npm package:

import { BrowserPrint } from "./BrowserPrint.fat";

exports.printEan = function(ean) {
  BrowserPrint.getLocalDevices(
    function(printers) {
      console.log("something happened");
    },
    undefined,
    "printer"
  );
};

My webpack config from the 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"
      }
    ]
  }
};

When I import this module into my project and call printEan I get "Uncaught ReferenceError: finishedFunction is not defined". Which suggests that it finds the printEan function, and BrowserPrint, but somehow the code in the legacy script isn't being handled properly by webpack.

Looking at the code of the legacy script finishedFunction is never defined but then I have no idea why it works when I directly import the script in my other projects.

snippet from the legacy code:

  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),
LocalMagic
  • 107
  • 1
  • 7
  • Did you solve the problem? I'm also using BrowserPrint in Angular project and I have no idea why there is "Uncaught ReferenceError: finishedFunction is not defined" :/ – corry Nov 23 '18 at 13:05
  • I'm trying to simulate on [stackblitz](https://stackblitz.com/edit/angular-customjs), but the same error appears – corry Nov 23 '18 at 13:12
  • take a look at the https://stackoverflow.com/questions/53482399/cannot-access-anonymous-function/53482697#53482697 – corry Nov 27 '18 at 07:40

0 Answers0