First things first, add your scripts to the index.html page. Don’t worry about the location, we’ll fix it later. In my scenario, I needed to include these 3 libraries that rollup doesn’t play nice with, along with font-awesome.
<head>
...
<link href="css/font-awesome.min.css" rel="stylesheet">
<script src="libs/forge.bundle.js" type="text/javascript"></script>
<script src="libs/encoding.js" type="text/javascript"></script>
<script src="libs/encoding-indexes.js" type="text/javascript"></script>
</head>
Next thing we do is add these three files to your project. You have some choices here. Choose your preference.
- Add them via
npm install
- Download the raw JavaScript and save them to a folder. I named my
libs
, and put it in my src
folder
Now we tell ionic to copy these files when doing a build.
Create a folder named config
in your project root.
Copy the following file node_modules/@ionic/app-scripts/config/copy.config.js
to this folder.
Open it up in your favourite JavaScript ide and add an entry to it like so.
{
src: 'src/libs/', //copy all files from here
dest: 'www/libs/' //and put them here
},
{
src: 'node_modules/font-awesome/fonts/',
dest: 'www/fonts/'
},
{
src: 'node_modules/font-awesome/css/',
dest: 'www/css/'
}
Last step. Tell ionic to use your copy.config.js file rather than the default one
Crack open package.json
and add an config entry. Doesn’t matter where you add it.
"config": {
"ionic_copy": "./config/copy.config.js"
},
All done. Run npm run build
and it will copy these javascript files to the www folder.
BUT WAIT. THERE’S MORE!!
Need to tell Typescript to ignore things. In my case I would put this at the top of my .ts
file that I use these libraries in
declare var forge: any;
declare var TextEncoder: any;
You could get reeeal fancy and put all this into a single declarations.d.ts
file, as mentioned here
Once all this nonsense with rollingup, tree-shaking, angular compiling, typescript definition crap is sorted out, and the authors of these fine libraries migrate from CommonJs to ES5 modules, then I’ll remove my work-around.
Hope this helps.
Links that helped me.