Skip to content

Commit a087ce6

Browse files
committed
Add tools for integrating 3rd party libraries
Make two new commands for adding and deleting 3rd party libraries. - npm run ng-add-3rd-party - npm run ng-delete-3rd-party Update README.md to explain the steps needed.
1 parent 113296d commit a087ce6

File tree

5 files changed

+128
-6
lines changed

5 files changed

+128
-6
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Than install JavaScript dependencies with **npm**.
2424
npm install
2525
```
2626

27-
Initialize Agular and compile and publish code with these two commands.
27+
Initialize Angular and compile and publish code with these two commands.
2828

2929
```
3030
npm run ng-init
@@ -121,7 +121,8 @@ In that case you don't have to delete `public/Ng` directory, running this comman
121121
* **`npm run ng-compile-prod`** - *same as the above but produces minified HTML and CSS files, also compiled JS files from the TS, are minified.
122122
Note that original JS files in your `angular_apps` directory (the ones that were not TS) will not be minified by this command. That is because I can't find decent,stable and reliable Gulp plugin that supports minifing ES6, if you know of one let me know.*
123123

124-
* **`npm run ng-bootstrap`** - *this command is combination of `npm run ng-init` and `npm run ng-compile`, that is it installs Angular and compiles your apps in one go.*
124+
* **`npm run ng-bootstrap`** - *this command is combination of `npm run ng-init`, `npm run ng-add-3rd-party` and `npm run ng-compile`, it installs Angular, installs 3rd party libraries and compiles your apps in one go.
125+
It is useful when you join existing project. Whith just this command you can build working application on your machine and start working on it.*
125126

126127
* **`npm run ng-watch`** - *starts watch mode, starts browser and puts your browser in sync, actually all the browsers tabs that have wached URL open.
127128
When you change, add, move, delete or rename HTML, JS, CSS and TS files in `angular_apps` it will distribute them in real time to `public`. TS files will be compiled to JS first than distributed.
@@ -151,6 +152,37 @@ So if you have, for example, two files in `public/somedir`:*
151152
*First file `public/somedir/apples.js` would be deleted, second one `public/somedir/oranges.js` would be left.
152153
Empty directories in distribution (`public`) directory and all nested empty directories will also be deleted if there is any.*
153154

155+
* **`npm run ng-add-3rd-party`** - *sometimes you have to use third-party libraries in your project. `npm run ng-init` initializes Angular and other standard libraries like `rxjs` but what if you need other libraries as well in your project?
156+
Best way to explain is by example, so let's say you want to use a third-party library in your project e.g. **angular2-multiselect-dropdown**.
157+
First install it of course using `npm install angular2-multiselect-dropdown --save`.
158+
Than open `resources/assets/js/ng-vendor.js` and add library name to the return array of the `vendor_libraries` callback*
159+
160+
```JavaScript
161+
exports.vendor_libraries = function() {
162+
return [
163+
'angular2-multiselect-dropdown',
164+
];
165+
}
166+
```
167+
168+
*After that run this command `npm run ng-add-3rd-party`.
169+
Next step is to tell SystemJS where from to load new library. Open `angular_apps/config/<app_name>.config.js` and as a last entry in `map` object add this mapping:*
170+
171+
```JavaScript
172+
map: {
173+
...
174+
// other libraries
175+
...
176+
'angular2-multiselect-dropdown/angular2-multiselect-dropdown': 'npm:angular2-multiselect-dropdown/angular2-multiselect-dropdown.umd.js'
177+
},
178+
```
179+
180+
*Last step is to run `npm run ng-compile`.
181+
Than you can use external library in your application.
182+
File `ng-vendor.js` is located outside of the Angular build directory (`angular_apps`), because it does not need to be published to distribution directory (`public`).*
183+
184+
* **`npm run ng-delete-3rd-party`** - *opposite of `npm run ng-add-3rd-party`. It will only remove 3rd party libraries from `public/Ng`, it does not remove them from `node_modules`.*
185+
154186
* **`npm run ng-remove-empty-dirs`** - *removes empty directories and all empty sub-directories from both buld (`angular_apps`) directory and distribution (`public`) directory.
155187
Unlike `npm run ng-clean`, it does touch `angular_apps` directory and only removes empty directories, thats all it does.
156188
You can pass `--build` switch to only remove empty directories and sub-directories from `angular_apps` directory or `--dist` switch to remove them only from `public` directory.*
@@ -200,6 +232,8 @@ So for example, **not** ~~`php -S localhost:8080`~~, but `php -S 127.0.0.1:8080`
200232

201233
Details on how you integrate your own Angular app from start to finish:
202234

235+
> Directory in which this new app will be is gonna be called `NewApp`, in production you should use more descriptive name of what your app does e.g. `Store` or `StockManagement`. That way if you have more Angular applications you can see right away what each one is designated to do soon as you look into `angular-apps` directory.
236+
203237
If you already haven't done so trough composer or manually, run these commands.
204238

205239
```

angular_apps/config/app2.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
// other libraries
3333
'rxjs': 'npm:rxjs',
34-
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
34+
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
35+
// 'angular2-multiselect-dropdown/angular2-multiselect-dropdown': 'npm:angular2-multiselect-dropdown/angular2-multiselect-dropdown.umd.js'
3536
},
3637
// packages tells the System loader how to load when no filename and/or no extension
3738
packages: {

gulpfile.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ gulp.task('compile', ['publish'], actions.compileTS());
4141
// Initialize entire Angular environment.
4242
gulp.task('initNg', actions.initializeAngular());
4343

44-
// Initialize Angular environment and compile and distribute Angular apps.
45-
gulp.task('bootstrapNg', ['initNg', 'compile']);
44+
// Publish 3rd party packages Angular applications will use.
45+
gulp.task('publish-packages', actions.publishPackages());
46+
47+
// Delete published 3rd party packages from Angular directory.
48+
gulp.task('delete-packages', actions.deletePackages());
49+
50+
// Initialize Angular environment, publish packages and compile and distribute Angular apps.
51+
gulp.task('bootstrapNg', ['initNg', 'publish-packages', 'compile']);
4652

4753
// Assembles files and directories to be cleaned from distribution directory.
4854
gulp.task('assemble-for-clean', actions.assembleForCleaning());
@@ -472,6 +478,49 @@ function Actions()
472478
}
473479
};
474480

481+
/**
482+
* Create function that publishes 3rd party libraries Angular apps may need.
483+
* Read libraries from array and copy them from "node_modules" to the Angular directory.
484+
*
485+
* @access public
486+
* @return function
487+
*/
488+
this.publishPackages = function ()
489+
{
490+
var angular_directory = angular_dir;
491+
492+
const external = require('./resources/assets/js/ng-vendor');
493+
let e = external.vendor_libraries();
494+
495+
let _3rd_party_libs = e.map(function (el) {return 'node_modules/' + el + '/**/*'; });
496+
497+
return function ()
498+
{
499+
return gulp.src(
500+
_3rd_party_libs, { "base" : "node_modules" }).pipe(gulp.dest(angular_directory));
501+
}
502+
};
503+
504+
/**
505+
* Create function that deletes 3rd party libraries.
506+
* Read libraries from array and delete them from the Angular directory.
507+
*
508+
* @access public
509+
* @return function
510+
*/
511+
this.deletePackages = function ()
512+
{
513+
const external = require('./resources/assets/js/ng-vendor');
514+
let e = external.vendor_libraries();
515+
516+
let _3rd_party_libs = e.map(function (el) {return angular_dir + '/' + el + '/**'; });
517+
518+
return function ()
519+
{
520+
return del.sync(_3rd_party_libs);
521+
}
522+
};
523+
475524
/**
476525
* Create function that removes empty directories from
477526
* build and distribution directories.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"ng-watch": "gulp watch --sync",
1717
"ng-watch-nosync": "gulp watch",
1818
"ng-clean": "gulp clean",
19-
"ng-remove-empty-dirs": "gulp remove-empty-dirs"
19+
"ng-remove-empty-dirs": "gulp remove-empty-dirs",
20+
"ng-add-3rd-party": "gulp publish-packages",
21+
"ng-delete-3rd-party": "gulp delete-packages"
2022
},
2123
"dependencies": {
2224
"@angular/animations": "~4.3.1",

resources/assets/js/ng-vendor.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
|--------------------------------------------------------------------------
3+
| External Angular Libraries
4+
|--------------------------------------------------------------------------
5+
|
6+
| This file is where you may define all of your external Angular libraries.
7+
|
8+
| Follow these steps to get your external library working:
9+
|
10+
| - install library with "npm install <package_name> --save"
11+
| - put library name in the array bellow
12+
| - run "npm run ng-add-3rd-party" command
13+
|
14+
| After steps above tell SystemJS where from to load new library.
15+
| Open "angular_apps/config/<app_name>.config.js" and add new mapping at the end of the "map" property object.
16+
| For example entry for 'angular2-multiselect-dropdown' library would be:
17+
|
18+
| map :{
19+
| ...
20+
| 'angular2-multiselect-dropdown/angular2-multiselect-dropdown': 'npm:angular2-multiselect-dropdown/angular2-multiselect-dropdown.umd.js'
21+
| },
22+
|
23+
| than run "npm run ng-compile" command.
24+
| Now you can use external library in your Angular app.
25+
*/
26+
27+
exports.vendor_libraries = function() {
28+
return [
29+
// Add names of the 3rd party packages here that you want your Angular app to have access to.
30+
// If for example you have used "npm install angular2-multiselect-dropdown --save" command, add the
31+
// package name in this array like in the example bellow.
32+
33+
// 'angular2-multiselect-dropdown',
34+
35+
];
36+
}

0 commit comments

Comments
 (0)