3

My requirement is to create libraries which have their own view and according to a User's configuration, the library will be fetched from AWS S3 Bucket and included in the final app on runtime. See this example. I am able to add the dist/library/bundles/library.lib.umd.js in the main app and show it to user. But when I try to use third party libraries like uuid, moment or ng2-dragula, the ng build command does not include any third party library in the final bundle. I get warning like

'ng2-dragula' is imported by ..\..\dist\torab-custom-lib\esm5\lib\custom-lib.service.js, but could not be resolved – treating it as an external dependency

while building the library and my app in which the projects/library exists shows

ERROR in dist/torab-custom-lib/lib/custom-lib.component.d.ts(2,32): error TS2307: Cannot find module 'ng2-dragula

What I want is to include any third party library I use in the final bundle as I am not able to install/import them on runtime. I have tried

"bundledDependencies": [
    "ng2-dragula"
  ]

in package.json as suggested here and

"embedded": [
    "ng2-dragula"
      ],

in my ng-package.json suggested here. This is my environment info

Angular CLI: 7.1.4
Node: 11.10.0
OS: win32 x64
Angular: 7.1.4
... animations, cli, common, compiler, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.11.4
@angular-devkit/build-angular      0.13.5
@angular-devkit/build-ng-packagr   0.11.4
@angular-devkit/build-optimizer    0.13.5
@angular-devkit/build-webpack      0.13.5
@angular-devkit/core               7.1.4
@angular-devkit/schematics         7.1.4
@angular/compiler-cli              7.2.9
@ngtools/json-schema               1.1.0
@ngtools/webpack                   7.3.5
@schematics/angular                7.1.4
@schematics/update                 0.11.4
ng-packagr                         4.7.1
rxjs                               6.3.3
typescript                         3.1.6
webpack                            4.29.0

Please help. Also please suggested if there is a different way to load third party library on runtime in the app.

Torab Shaikh
  • 456
  • 1
  • 5
  • 17
  • I think you need to use the `npm pack` and `npm publish` from `bundledDependencies`. That's what the ng-packgr docs suggest – Drenai Oct 21 '19 at 06:39

1 Answers1

0

After building you library, use rollupjs to bundle third party libraries from fesm5 file.

Here is my rollup.config :

import resolve from 'rollup-plugin-node-resolve';
import sourcemaps from 'rollup-plugin-sourcemaps';
import commonjs from 'rollup-plugin-commonjs';

export default {
    plugins: [
       resolve(), 
       sourcemaps(),
       commonjs({
        namedExports: { 
            'node_modules/chart.js/dist/Chart.js': ['Chart' ]
        }
      })
   ],
   onwarn: () => { return },
   output: {
       format: 'umd',
       name: 'mylib',
       globals: globals,
       sourcemap: true,
       amd: { id: 'mylib' }
    }
}

Then I use this rollup command :

 rollup -c rollup.config.js -i .\dist\mylib\fesm5\mylib.js -o .\dist\mylibwiththirdparty.umd.js
Nicolas Law-Dune
  • 1,631
  • 2
  • 13
  • 30