Skip to content

Can't use the library as an AMD module #96

@nullquery

Description

@nullquery

Possibly related: #72

In order to use ZXing I need to import the library using the following statement:

import * as ZXing from "@zxing/library";

const codeReader = new ZXing.BrowserQRCodeReader();

The generated Javascript code (with AMD module generation) looks like this:

define("test", ["require", "exports", "@zxing/library"], function (require, exports, ZXing) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const codeReader = new ZXing.BrowserQRCodeReader();
});

However, the file "library/umd/index.min.js" specifies that the AMD module name is "ZXing" and not "@zxing/library". As a result, my code will compile but not run; the browser gives an error that the module "@zxing/library" cannot be found.

The fix seems easy. I added the following line in the "library/esm5/index.d.ts" at the top:

///<amd-module name='ZXing'/>
export * from './browser/BrowserQRCodeReader';
export * from './browser/BrowserDatamatrixCodeReader';
export * from './browser/BrowserQRCodeSvgWriter';
export * from './browser/BrowserBarcodeReader';
// etc...

The "amd-module" declaration changes the name in the generated Javascript code. It then looks like this:

define("test", ["require", "exports", "ZXing"], function (require, exports, ZXing) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const codeReader = new ZXing.BrowserQRCodeReader();
});
//# sourceMappingURL=compiled.js.map

The code then works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions