Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import part of the library in Typescript #247

Open
jooleeanh opened this issue Jun 1, 2018 · 4 comments
Open

Import part of the library in Typescript #247

jooleeanh opened this issue Jun 1, 2018 · 4 comments

Comments

@jooleeanh
Copy link

First of all, thanks for the awesome library !

Is there a way to import a section of the library ? I'm integrating JsBarcode in a widget, and JsBarcode ends up taking more space than my source code.

I'd like to either be able to import like such:
import { Code128 } from 'jsbarcode';
Or be able to use the minified versions for the web:
JsBarcode.code128.min.js

I tried importing JsBarcode.code128.min.js with the following statements:
import * as JsBarcode from './JsBarcode.code128.min.js'
import JsBarcode from './JsBarcode.code128.min.js'
But the first gives a {} object, and the second makes the compiler fail with a Error: 'default' is not exported by ./JsBarcode.code128.min.js.

What's the best way to go about that ?

I'm using Rollup.js to bundle everything into a single file.

@Freytag
Copy link

Freytag commented Mar 5, 2019

So this information some way too late, but this is what I did in case someone else is googling on how to import a specific piece into their project.
import '../../node_modules/jsbarcode/dist/barcodes/JsBarcode.ean-upc.min.js';
Now I can just call in my app.js
JsBarcode('#barcode', '123456789012', { format: 'upc' });

@jooleeanh
Copy link
Author

Hi @lindell, I'm trying out the v4 feature you mention in #263 to import only the parts we want:

import jsbarcode, { code128, canvasRenderer } from 'jsbarcode';

jsbarcode(value, element, {
  format: code128,
  render: canvasRenderer,
});

I've tried multiple import statements:

import jsbarcode from "jsbarcode";
import JsBarcode from 'jsbarcode';
import * as jsBarcode from 'jsbarcode';
import "jsbarcode";

Which yielded warnings during the rollup process:

'jsbarcode' is imported by app/build/ViewController.js, but could not be resolved – treating it as an external dependency
No name was provided for external module '[...]/node_modules/rollup/dist/rollup.js' in options.globals – guessing 'jsBarcode'

I started looking in your package.json to see which file was the main one, and that's when I saw it was "./lib".
However, all of the alpha versions (v4.0.0-alpha.1 through .5) as well as pointing directly towards a commit (I tried #839686084e31ff3d376c4feb0a1952653130f217 from 28 Feb 2019, branch jsbarcode-v4) are missing the lib/ folder.

Am I missing something ?

@vishwas097
Copy link

@jooleeanh
You need to follow these steps to import successfully.
1- npm install jsbarcode
2- import * as JsBarcode from "JsBarcode";
3- You can use it now Jsbarcode("#your_id","barcode_content");
barcode_content - Ex: 1234

@nimesh5692
Copy link

import * as JsBarcode from 'jsbarcode'; works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants