Skip to content

feat(@angular-devkit/build-angular): add ignore option to assets #11868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions docs/documentation/stories/asset-configuration.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Project assets

You use the `assets` array inside the build target in `angular.json` to list files or folders
you want to copy as-is when building your project.
you want to copy as-is when building your project. if you think you need to exclude files,
consider not putting that thing in the assets

By default, the `src/assets/` folder and `src/favicon.ico` are copied over.

Expand All @@ -23,9 +24,10 @@ The array below does the same as the default one:
]
```

`glob` is the a [node-glob](https://github.com/isaacs/node-glob) using `input` as base directory.
`input` is relative to the workspace root, while `output` is relative to `outDir`
(`dist/project-name` default).
- `glob` is a [node-glob](https://github.com/isaacs/node-glob) using `input` as base directory.
- `input` is relative to the workspace root.
- `ignore` is a list of globs to ignore from copying.
- `output` is relative to `outDir` (`dist/project-name` default).

You can use this extended configuration to copy assets from outside your project.
For instance, you can copy assets from a node package:
Expand All @@ -36,7 +38,14 @@ The array below does the same as the default one:
]
```

The contents of `node_modules/some-package/images/` will be available in `dist/some-package/`.
You can ignore certain files from copying by using the `ignore` option:
```json
"assets": [
{ "glob": "**/*", "input": "src/assets/", "ignore": ["**/*.svg"], "output": "/assets/" },
]
```

The contents of `node_modules/some-package/images/` will be available in `dist/some-package/`.

## Writing assets outside of `dist/`

Expand Down
21 changes: 17 additions & 4 deletions packages/angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"default": false,
"alias": "t"
},
"module": {
"module": {
"type": "string",
"description": "Allows specification of the declaring module.",
"alias": "m"
Expand Down Expand Up @@ -160,7 +160,7 @@
"description": "Flag to indicate if a dir is created.",
"default": true
},
"module": {
"module": {
"type": "string",
"description": "Allows specification of the declaring module.",
"alias": "m"
Expand Down Expand Up @@ -219,7 +219,7 @@
"default": true,
"visible": false
},
"module": {
"module": {
"type": "string",
"description": "Allows specification of the declaring module.",
"alias": "m"
Expand Down Expand Up @@ -778,6 +778,13 @@
"output": {
"type": "string",
"description": "Absolute path within the output."
},
"ignore": {
"description": "An array of globs to ignore.",
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -1241,6 +1248,13 @@
"output": {
"type": "string",
"description": "Absolute path within the output."
},
"ignore": {
"description": "An array of globs to ignore.",
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -1541,7 +1555,6 @@
}
]
}

}
},
"tslint": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
context: asset.input,
// Now we remove starting slash to make Webpack place it from the output root.
to: asset.output.replace(/^\//, ''),
ignore: asset.ignore,
from: {
glob: asset.glob,
dot: true
Expand Down
5 changes: 5 additions & 0 deletions packages/angular_devkit/build_angular/src/browser/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ export interface AssetPatternObject {
*/
input: string;

/**
* An array of globs to ignore.
*/
ignore?: string[];

/**
* Absolute path within the output.
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/angular_devkit/build_angular/src/browser/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@
"type": "string",
"description": "The input path dir in which to apply 'glob'. Defaults to the project root."
},
"ignore": {
"description": "An array of globs to ignore.",
"type": "array",
"items": {
"type": "string"
}
},
"output": {
"type": "string",
"description": "Absolute path within the output."
Expand Down
7 changes: 7 additions & 0 deletions packages/angular_devkit/build_angular/src/karma/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@
"output": {
"type": "string",
"description": "Absolute path within the output."
},
"ignore": {
"description": "An array of globs to ignore.",
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ describe('Browser Builder assets', () => {
).toPromise().then(done, done.fail);
});

it('works with ignored patterns', (done) => {
const assets: { [path: string]: string } = {
'./src/folder/.gitkeep': '',
'./src/folder/asset-ignored.txt': '',
'./src/folder/asset.txt': '',
};

host.writeMultipleFiles(assets);

const overrides = {
assets: [
{
glob: '**/*',
ignore: ['asset-ignored.txt'],
input: 'src/folder',
output: '/folder',
},
],
};

runTargetSpec(host, browserTargetSpec, overrides).pipe(
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
tap(() => {
expect(host.scopedSync().exists(normalize('./dist/folder/asset.txt'))).toBe(true);
expect(host.scopedSync().exists(normalize('./dist/folder/asset-ignored.txt'))).toBe(false);
expect(host.scopedSync().exists(normalize('./dist/folder/.gitkeep'))).toBe(false);
}),
).toPromise().then(done, done.fail);
});

it('fails with non-absolute output path', (done) => {
const assets: { [path: string]: string } = {
'./node_modules/some-package/node_modules-asset.txt': 'node_modules-asset.txt',
Expand Down