lindell / JsBarcode

Barcode generation library written in JavaScript that works in both the browser and on Node.js
http://lindell.me/JsBarcode
MIT License
5.45k stars 1.1k forks source link

Import part of the library in Typescript #247

Open jooleeanh opened 6 years ago

jooleeanh commented 6 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

@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 commented 2 years ago

import * as JsBarcode from 'jsbarcode'; works