angular-builder https://www.e-learn.cn/tag/angular-builder zh-hans angular 7 build app - replace assets url with real url https://www.e-learn.cn/topic/4033814 <span>angular 7 build app - replace assets url with real url</span> <span><span lang="" about="/user/109" typeof="schema:Person" property="schema:name" datatype="">試著忘記壹切</span></span> <span>2021-01-29 05:21:12</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>In an Angular 7 app i have different assets, images, fonts etc. All are located in src/assets/images or src/assets/fonts, etc</p> <p>I my files i use them like this: <code>&lt;img src="/assets/images/img.png"&gt;</code></p> <p>angular.json look like this: </p> <pre><code> ..... "assets": [ "src/favicon.ico", "src/assets" ] ..... </code></pre> <p>When building I use:</p> <p><code>ng build --prod --build-optimizer --deploy-url=https://cdn.domain.com</code></p> <p>This will replace all the reference to assets resource from this:</p> <p><code>&lt;img src="/assets/images/img.png"&gt;</code></p> <p>to this:</p> <p><code>&lt;img src="https://cdn.domain.com/assets/images/img.png"&gt;</code></p> <p>My question how I can remove the <code>assets/images</code> path when building so that after building the output will be:</p> <p><code>&lt;img src="https://cdn.domain.com/img.png"&gt;</code></p> <br /><h3>回答1:</h3><br /><p>You could replace </p> <pre><code> "assets": [ ... "src/assets", ... ] </code></pre> <p>by </p> <pre><code>"assets": [ ... { "glob": "**/*", "input": "src/assets", "output": "/" }, ... ] </code></pre> <p>All the files will be in "/dist/environment/" and not in "/dist/environment/..."</p> <p>Then in your html files, use directly : </p> <pre><code>&lt;img src="/img.png"&gt; </code></pre> <p>Have a look here : https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/asset-configuration.md</p> <p>:-)</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/53962936/angular-7-build-app-replace-assets-url-with-real-url</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/angular" hreflang="zh-hans">angular</a></div> <div class="field--item"><a href="/tag/angular6" hreflang="zh-hans">angular6</a></div> <div class="field--item"><a href="/tag/angular-cli" hreflang="zh-hans">angular-cli</a></div> <div class="field--item"><a href="/tag/angular7" hreflang="zh-hans">angular7</a></div> <div class="field--item"><a href="/tag/angular-builder" hreflang="zh-hans">angular-builder</a></div> </div> </div> Thu, 28 Jan 2021 21:21:12 +0000 試著忘記壹切 4033814 at https://www.e-learn.cn Angular CLI custom builder https://www.e-learn.cn/topic/3402792 <span>Angular CLI custom builder</span> <span><span lang="" about="/user/216" typeof="schema:Person" property="schema:name" datatype="">假如想象</span></span> <span>2020-02-18 12:42:35</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Angular CLI 6 introduced a new concept of builders (aka Architect Targets).<br /> There are a couple of prebuilt builders that come with @angular-devkit/build_angular, but unfortunately there is no documentation that explains how to create your own builder. </p> <p>How do I create my own builder (for example to modify the underlying webpack configuration)?</p> <br /><h3>回答1:</h3><br /><h2>Update:</h2> <p>For Angular 8+ read this AngularInDepth article.</p> <h2>For Angular 6 and 7:</h2> <p>The full article can be found here.</p> <p>For the sake of simplicity I assume that the new builder is implemented in Typescript, but it can be implemented in pure JavaScript as well.</p> <h2>Implementation:</h2> <ol><li>Create a directory for your custom builders (for example <em>custom-builders</em>) in the root of your project (or alternatively install it as a local npm module)</li> <li>Inside the directory create a file called <em>builders.json</em>, <em>index.ts</em> and <em>package.json</em> that contains <code>builders</code> entry pointing to <em>builders.json</em></li> <li>Inside <em>custom-builders</em> create a directory for the builder you want to implement (say, <em>custom-builders/my-cool-builder</em></li> <li>Add <em>index.ts</em>, <em>schema.json</em> and <em>schema.d.ts</em> to <em>my-cool-builder</em> directory</li> <li>Populate <em>schema.json</em> with the schema of your builder options. See an example here.</li> <li>Define your <em>schema.d.ts</em> according to the <em>schema.json</em> you defined. See an example here.</li> <li><p>Implement your builder in <em>my-cool-builder/index.ts</em>. The builder has to implement the following interface: </p> <pre><code>export interface Builder&lt;OptionsT&gt; { run(builderConfig: BuilderConfiguration&lt;Partial&lt;OptionsT&gt;&gt;): Observable&lt;BuildEvent&gt;; } </code></pre> <p>While <code>BuildEvent</code> is this: </p> <pre><code>export interface BuildEvent { success: boolean; } </code></pre> <p><code>BuilderConfiguration</code> is this:</p> <pre><code>export interface BuilderConfiguration&lt;OptionsT = {}&gt; { root: Path; sourceRoot?: Path; projectType: string; builder: string; options: OptionsT; } </code></pre> <p>And <code>OptionsT</code> is the interface you defined for your builder options in <em>schema.d.ts</em> </p> <p>You can use browser architect target as a reference.</p></li> <li><p>Once implemented, add your builder to <em>builders.json</em>: </p> <pre><code>{ "$schema": "@angular-devkit/architect/src/builders-schema.json", "builders": { "cool-builder": { "class": "./my-cool-builder", "schema": "./my-cool-builder/schema.json", "description": "My cool builder that builds things up" } } } </code></pre></li> </ol><h2>Usage:</h2> <p>In your <em>angular.json</em>:</p> <pre><code> "architect": { ... "build": { "builder": "./custom-builders:cool-builder" "options": { your options here } </code></pre> <h2>Example</h2> <p>For the full example check out this library: https://github.com/meltedspark/angular-builders</p> <br /><br /><br /><h3>回答2:</h3><br /><p>I didn't test it and I'm not quite sure, but this concept might be solution.</p> <p><code>architect.build.builder</code> uses some Angular schematics in order to perform build process. You can create your own schematics which uses/inherits regular build schematics with additional logic that you want to implement.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>For those who use Angular 8 and higher, builders API is now officially supported and documented: https://angular.io/guide/cli-builder</p> <p>It has a lot of changes compared to the previous version, so migration from Angular 7 to 8 might become complicated if you are using undocumented Architect API.</p> <p>Here's a nice article to get started: https://blog.angular.io/introducing-cli-builders-d012d4489f1b</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/51069290/angular-cli-custom-builder</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/angular" hreflang="zh-hans">angular</a></div> <div class="field--item"><a href="/tag/angular-cli" hreflang="zh-hans">angular-cli</a></div> <div class="field--item"><a href="/tag/angular-builder" hreflang="zh-hans">angular-builder</a></div> <div class="field--item"><a href="/tag/angular-devkit" hreflang="zh-hans">angular-devkit</a></div> </div> </div> Tue, 18 Feb 2020 04:42:35 +0000 假如想象 3402792 at https://www.e-learn.cn Angular CLI custom builder https://www.e-learn.cn/topic/3402790 <span>Angular CLI custom builder</span> <span><span lang="" about="/user/119" typeof="schema:Person" property="schema:name" datatype="">一个人想着一个人</span></span> <span>2020-02-18 12:38:51</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Angular CLI 6 introduced a new concept of builders (aka Architect Targets).<br /> There are a couple of prebuilt builders that come with @angular-devkit/build_angular, but unfortunately there is no documentation that explains how to create your own builder. </p> <p>How do I create my own builder (for example to modify the underlying webpack configuration)?</p> <br /><h3>回答1:</h3><br /><h2>Update:</h2> <p>For Angular 8+ read this AngularInDepth article.</p> <h2>For Angular 6 and 7:</h2> <p>The full article can be found here.</p> <p>For the sake of simplicity I assume that the new builder is implemented in Typescript, but it can be implemented in pure JavaScript as well.</p> <h2>Implementation:</h2> <ol><li>Create a directory for your custom builders (for example <em>custom-builders</em>) in the root of your project (or alternatively install it as a local npm module)</li> <li>Inside the directory create a file called <em>builders.json</em>, <em>index.ts</em> and <em>package.json</em> that contains <code>builders</code> entry pointing to <em>builders.json</em></li> <li>Inside <em>custom-builders</em> create a directory for the builder you want to implement (say, <em>custom-builders/my-cool-builder</em></li> <li>Add <em>index.ts</em>, <em>schema.json</em> and <em>schema.d.ts</em> to <em>my-cool-builder</em> directory</li> <li>Populate <em>schema.json</em> with the schema of your builder options. See an example here.</li> <li>Define your <em>schema.d.ts</em> according to the <em>schema.json</em> you defined. See an example here.</li> <li><p>Implement your builder in <em>my-cool-builder/index.ts</em>. The builder has to implement the following interface: </p> <pre><code>export interface Builder&lt;OptionsT&gt; { run(builderConfig: BuilderConfiguration&lt;Partial&lt;OptionsT&gt;&gt;): Observable&lt;BuildEvent&gt;; } </code></pre> <p>While <code>BuildEvent</code> is this: </p> <pre><code>export interface BuildEvent { success: boolean; } </code></pre> <p><code>BuilderConfiguration</code> is this:</p> <pre><code>export interface BuilderConfiguration&lt;OptionsT = {}&gt; { root: Path; sourceRoot?: Path; projectType: string; builder: string; options: OptionsT; } </code></pre> <p>And <code>OptionsT</code> is the interface you defined for your builder options in <em>schema.d.ts</em> </p> <p>You can use browser architect target as a reference.</p></li> <li><p>Once implemented, add your builder to <em>builders.json</em>: </p> <pre><code>{ "$schema": "@angular-devkit/architect/src/builders-schema.json", "builders": { "cool-builder": { "class": "./my-cool-builder", "schema": "./my-cool-builder/schema.json", "description": "My cool builder that builds things up" } } } </code></pre></li> </ol><h2>Usage:</h2> <p>In your <em>angular.json</em>:</p> <pre><code> "architect": { ... "build": { "builder": "./custom-builders:cool-builder" "options": { your options here } </code></pre> <h2>Example</h2> <p>For the full example check out this library: https://github.com/meltedspark/angular-builders</p> <br /><br /><br /><h3>回答2:</h3><br /><p>I didn't test it and I'm not quite sure, but this concept might be solution.</p> <p><code>architect.build.builder</code> uses some Angular schematics in order to perform build process. You can create your own schematics which uses/inherits regular build schematics with additional logic that you want to implement.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>For those who use Angular 8 and higher, builders API is now officially supported and documented: https://angular.io/guide/cli-builder</p> <p>It has a lot of changes compared to the previous version, so migration from Angular 7 to 8 might become complicated if you are using undocumented Architect API.</p> <p>Here's a nice article to get started: https://blog.angular.io/introducing-cli-builders-d012d4489f1b</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/51069290/angular-cli-custom-builder</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/angular" hreflang="zh-hans">angular</a></div> <div class="field--item"><a href="/tag/angular-cli" hreflang="zh-hans">angular-cli</a></div> <div class="field--item"><a href="/tag/angular-builder" hreflang="zh-hans">angular-builder</a></div> <div class="field--item"><a href="/tag/angular-devkit" hreflang="zh-hans">angular-devkit</a></div> </div> </div> Tue, 18 Feb 2020 04:38:51 +0000 一个人想着一个人 3402790 at https://www.e-learn.cn