Skip to content

Commit 571c383

Browse files
committed
docs: update global styles/scripts/lib wiki
1 parent 518ff63 commit 571c383

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
# Global Library Installation
22

3-
Some javascript libraries need to be added to the global scope, and loaded as if
4-
they were in a script tag. We can do this using the `apps[0].scripts` and
5-
`apps[0].styles` properties of `.angular-cli.json`.
3+
Some javascript libraries need to be added to the global scope and loaded as if
4+
they were in a script tag.
5+
We can do this using the `scripts` and `styles` options of the build target in `angular.json`.
66

7-
As an example, to use [Bootstrap 4](https://getbootstrap.com/docs/4.0/getting-started/introduction/) this is
8-
what you need to do:
7+
As an example, to use [Bootstrap 4](https://getbootstrap.com/docs/4.0/getting-started/introduction/)
8+
this is what you need to do:
99

1010
First install Bootstrap from `npm`:
1111

1212
```bash
1313
npm install jquery --save
1414
npm install popper.js --save
15-
npm install bootstrap@next --save
15+
npm install bootstrap --save
1616
```
1717

18-
Then add the needed script files to `apps[0].scripts`:
18+
Then add the needed script files to `scripts`:
1919

2020
```json
2121
"scripts": [
22-
"../node_modules/jquery/dist/jquery.slim.js",
23-
"../node_modules/popper.js/dist/umd/popper.js",
24-
"../node_modules/bootstrap/dist/js/bootstrap.js"
22+
"node_modules/jquery/dist/jquery.slim.js",
23+
"node_modules/popper.js/dist/umd/popper.js",
24+
"node_modules/bootstrap/dist/js/bootstrap.js"
2525
],
2626
```
2727

28-
Finally add the Bootstrap CSS to the `apps[0].styles` array:
28+
Finally add the Bootstrap CSS to the `styles` array:
2929
```json
3030
"styles": [
31-
"../node_modules/bootstrap/dist/css/bootstrap.css",
32-
"styles.css"
31+
"node_modules/bootstrap/dist/css/bootstrap.css",
32+
"src/styles.css"
3333
],
3434
```
3535

36-
Restart `ng serve` if you're running it, and Bootstrap 4 should be working on
37-
your app.
36+
Restart `ng serve` if you're running it, and Bootstrap 4 should be working on your app.

docs/documentation/stories/global-scripts.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
# Global scripts
22

3-
You can add Javascript files to the global scope via the `apps[0].scripts`
4-
property in `.angular-cli.json`.
3+
You can add Javascript files to the global scope via the `scripts` option inside your
4+
project's build target options in `angular.json`.
55
These will be loaded exactly as if you had added them in a `<script>` tag inside `index.html`.
66

77
This is especially useful for legacy libraries or analytic snippets.
88

99
```json
10-
"scripts": [
11-
"global-script.js",
12-
],
10+
"architect": {
11+
"build": {
12+
"builder": "@angular-devkit/build-angular:browser",
13+
"options": {
14+
"scripts": [
15+
"src/global-script.js",
16+
],
1317
```
1418

1519
You can also rename the output and lazy load it by using the object format:
1620

1721
```json
1822
"scripts": [
19-
"global-script.js",
20-
{ "input": "lazy-script.js", "lazy": true },
21-
{ "input": "pre-rename-script.js", "output": "renamed-script" },
23+
"src/global-script.js",
24+
{ "input": "src/lazy-script.js", "lazy": true },
25+
{ "input": "src/pre-rename-script.js", "bundleName": "renamed-script" },
2226
],
2327
```
2428

29+
Note: you will also need to add any scripts to the `test` builder if you need them for unit tests.
30+
2531
## Using global libraries inside your app
2632

2733
Once you import a library via the scripts array, you should **not** import it via a import statement
2834
in your TypeScript code (e.g. `import * as $ from 'jquery';`).
29-
If you do that, you'll end up with two different copies of the library: one imported as a
35+
If you do that you'll end up with two different copies of the library: one imported as a
3036
global library, and one imported as a module.
3137

3238
This is especially bad for libraries with plugins, like JQuery, because each copy will have

docs/documentation/stories/global-styles.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,42 @@ The `styles.css` file allows users to add global styles and supports
66
If the project is created with the `--style=sass` option, this will be a `.sass`
77
file instead, and the same applies to `scss/less/styl`.
88

9-
You can add more global styles via the `apps[0].styles` property in `.angular-cli.json`.
9+
You can add more global styles via the `styles` option inside your project's build target options
10+
in `angular.json`.
1011
These will be loaded exactly as if you had added them in a `<link>` tag inside `index.html`.
1112

1213
```json
13-
"styles": [
14-
"styles.css",
15-
"more-styles.css",
16-
],
14+
"architect": {
15+
"build": {
16+
"builder": "@angular-devkit/build-angular:browser",
17+
"options": {
18+
"styles": [
19+
"src/styles.css",
20+
"src/more-styles.css",
21+
],
22+
...
1723
```
1824

1925
You can also rename the output and lazy load it by using the object format:
2026

2127
```json
2228
"styles": [
23-
"styles.css",
24-
"more-styles.css",
25-
{ "input": "lazy-style.scss", "lazy": true },
26-
{ "input": "pre-rename-style.scss", "output": "renamed-style" },
29+
"src/styles.css",
30+
"src/more-styles.css",
31+
{ "input": "src/lazy-style.scss", "lazy": true },
32+
{ "input": "src/pre-rename-style.scss", "bundleName": "renamed-style" },
2733
],
2834
```
2935

3036
In Sass and Stylus you can also make use of the `includePaths` functionality for both component and
3137
global styles, which allows you to add extra base paths that will be checked for imports.
3238

33-
To add paths, use the `stylePreprocessorOptions` entry in angular-cli.json `app` object:
39+
To add paths, use the `stylePreprocessorOptions` option:
3440

3541
```
3642
"stylePreprocessorOptions": {
3743
"includePaths": [
38-
"style-paths"
44+
"src/style-paths"
3945
]
4046
},
4147
```
@@ -49,4 +55,6 @@ project without the need for a relative path:
4955
@import '../style-paths/variables';
5056
// But now this works as well
5157
@import 'variables';
52-
```
58+
```
59+
60+
Note: you will also need to add any styles to the `test` builder if you need them for unit tests.

0 commit comments

Comments
 (0)