Skip to content

Commit 4d90dc9

Browse files
committed
feat: allowOutsideReadDir configuration
1 parent 00bc1f0 commit 4d90dc9

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

docs/documentation/stories/asset-configuration.md

+21
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,24 @@ to `true` on your asset definition, like so:
6060
```
6161

6262
This needs to be set for every assets you want to write outside of your build output directory.
63+
64+
## Read assets outside of `dist/`
65+
66+
This needs to be set for every assets you want to write outside of your build output directory.
67+
68+
Because reading files in your project isn't an expected effect of `ng build`, it is disabled by
69+
default on every assets. In order to allow this behaviour, you need to set `allowOutsideReadDir`
70+
to `true` on your asset definition, like so:
71+
72+
```json
73+
"assets": [
74+
{
75+
"glob": "**/*",
76+
"input": "./assets/",
77+
"output": "../not-dist/some/folder/",
78+
"allowOutsideReadDir": true
79+
},
80+
]
81+
```
82+
83+
This needs to be set for every assets you want to write outside of your build output directory.

packages/@angular/cli/lib/config/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
"type": "boolean",
9292
"description": "Allow assets to be copied outside the outDir.",
9393
"default": false
94+
},
95+
"allowOutsideReadDir": {
96+
"type": "boolean",
97+
"description": "Allow assets to be read outside the project dir.",
98+
"default": false
9499
}
95100
},
96101
"additionalProperties": false

packages/@angular/cli/models/webpack-configs/common.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
114114
}
115115

116116
// Prevent asset configurations from reading files outside of the project.
117-
if (!asset.input.startsWith(projectRoot)) {
118-
const message = 'An asset cannot be read from a location outside the project.';
119-
throw new SilentError(message);
117+
if (!asset.input.startsWith(projectRoot) && !asset.allowOutsideReadDir) {
118+
const message = `${asset.input} cannot be read from a location outside the project.`
119+
+ 'You can override this message by setting the `allowOutsideReadDir` '
120+
+ 'property on the asset to true in the CLI configuration.';
121+
throw new SilentError(message);
120122
}
121123

122124
// Ensure trailing slash.

packages/@angular/cli/models/webpack-configs/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,5 @@ export interface AssetPattern {
9393
input?: string;
9494
output?: string;
9595
allowOutsideOutDir?: boolean;
96+
allowOutsideReadDir?: boolean;
9697
}

tests/e2e/tests/basic/assets.ts

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ export default function () {
8383
];
8484
}))
8585
.then(() => expectToFail(() => ng('build')))
86+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
87+
const app = configJson['apps'][0];
88+
app['assets'] = [
89+
{ 'glob': '**/*', 'input': '/temp-folder/outside-allowed/of/project', 'output': 'temp',
90+
'allowOutsideReadDir': true }
91+
];
92+
}))
93+
.then(() => ng('build'))
8694

8795
// Add asset config in .angular-cli.json.
8896
.then(() => updateJsonFile('.angular-cli.json', configJson => {

0 commit comments

Comments
 (0)