I am not sure how to include JS files (vendors) after switching Angular Cli from SystemJs to Webpack.
For example
Option A
I have some js files that were installed via npm. Adding script tags to the head tag like this does not work. Nor does it seem like the best way.
<head>
<script src="node_modules/some_package/somejs.js">
</head>
//With systemJs I could do this
<head>
<script src="vendor/some_package/somejs.js">
</head>
Option B
Include these js files as part of the webpack bundle. This seems like the way it probably should be done. However I am not sure how to do this as all of the webpack code seems to be hidden behind the angular-cli-webpack node package. I was thinking maybe there is another webpack config that we might have access to. But I am not sure as I didn't see one when creating a new angular-cli-webpack project.
More Info:
The js files I am trying to include need to be included before the Angular project. For example jQuery and a third party js lib that isn't really setup for module loading or typescript.
References https://github.com/angular/angular-cli/blob/master/WEBPACK_UPDATE.md https://github.com/angular/angular-cli/tree/webpack
You need to open file
.angular-cli.json
file and need to search for"scripts:"
or if you want to add external css you need to find the word"styles":
in the same file.as an example shown below you will see how the bootstrap Js(
bootstrap.min.js
) and bootstrap CSS(bootstrap.min.css
) includes in.angular-cli.json
:For sure if you have your own js file you can add your file path here in
.angular-cli.json
at the same place(in"scripts":[]
).Last tested using angular-cli 1.0.0 with Angular 4.0.0
This can be accomplished using
scripts:[]
in.angular-cli.json
.Note: As the documentation suggests in the global library installation: if you change the value of your
styles
(orscripts
!) property, then:..to see the scripts executed in a **globalcontext via the
scripts.bundle.js
file.Note: As discussed in the comments below. JS libs that support UMD modules via es6 imports such as jQuery can also be imported into your typescript files using the es6 import syntax. For example:
import $ from 'jquery';
.I havn't used angular-cli before but I'm currently working with an Angular/Webpack build. In order to provide my application with jQuery and other vendors I use webpack's plugin,
ProvidePlugin()
. This will typically sit in yourwebpack.config.js
: Here's an example for jquery, lodash and moment libraries. Here's a link to the documentation (which is vague at best)Incredibly, it actually allows you to use it right away, providing all other webpack setup has been done correctly and have been installed with
npm
.You might want to have a look at this page: https://github.com/angular/angular-cli#global-library-installation
It show the basics of how to include .js and .css files
There is a subtle difference to using
scripts:[]
then to adding something to the<head>
with<script>
. Scripts fromscripts:[]
get added to thescripts.bundle.js
that gets always loaded in the body tag and will thus be loaded AFTER scripts in<head>
. Thus if script loading order matters (i.e. you need to load a global polyfill), then your only option is to manually copy scripts to a folder (e.g. with a npm script) and add this folder as an asset to.angular-cli.json
.So if you really depend on something being loaded before angular itself (Option A), then you need to copy it manually to a folder that will be included in the angular build and then you can load it manually with a
<script>
in<head>
.Thus, for achieving option a you have to:
vendor
folder insrc/
add this folder as an asset to
.angular-cli.json
:"assets": [ "assets", "favicon.ico", "vendor" ]
copy your vendor script
node_modules/some_package/somejs.js
tovendor
load it manually in
index.html
:<head> <script src="vendor/some_package/somejs.js"> </head>
However most of the time you only need this approach for packages, that need to be available globally, before everything else (i.e. certain polyfills). Kris' answer holds true for Option B and you get the benefit of the webpack build (Minification, Hashes, ...).
However if your scripts need not be globally available and if they are module-ready you can import them in
src/polyfills.ts
or even better import them only when you need them in your specific components.Making scripts globally available via
scripts:[]
or via manually loading them brings it own set of problems and should really only be used, when it is absolutely necessary.