@angular-builders/custom-esbuild 17.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Evgeny Barabanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,216 @@
1
+ # Custom ESBuild [builders](#builders) for Angular build facade
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@angular-builders/custom-esbuild.svg) ![npm (tag)](https://img.shields.io/npm/v/@angular-builders/custom-esbuild/next.svg) ![npm](https://img.shields.io/npm/dm/@angular-builders/custom-esbuild.svg)](https://www.npmjs.com/package/@angular-builders/custom-esbuild)
4
+
5
+ Allow customizing ESBuild configuration
6
+
7
+ # Table of Contents
8
+
9
+ - [Usage](#usage)
10
+ - [For Example](#for-example)
11
+ - [Builders](#builders)
12
+ - [Custom ESBuild `application`](#custom-esbuild-application)
13
+ - [Example](#example)
14
+ - [Custom ESBuild `dev-server`](#custom-esbuild-dev-server)
15
+ - [Example](#example)
16
+ - [Index Transform](#index-transform)
17
+ - [Example](#example-2)
18
+ - [ES Modules (ESM) Support](#es-modules-esm-support)
19
+
20
+ # This documentation is for the latest major version only
21
+
22
+ ## Previous versions
23
+
24
+ <details>
25
+ <summary>Click to expand</summary>
26
+
27
+ - [Version 17](https://github.com/just-jeb/angular-builders/blob/17.x.x/packages/custom-esbuild/README.md)
28
+
29
+ </details>
30
+
31
+ ## Prerequisites:
32
+
33
+ - [Angular CLI 17.1](https://www.npmjs.com/package/@angular/cli)
34
+
35
+ # Usage
36
+
37
+ 1. `npm i -D @angular-builders/custom-esbuild`
38
+ 2. In your `angular.json`:
39
+ ```js
40
+ "projects": {
41
+ ...
42
+ "[project]": {
43
+ ...
44
+ "architect": {
45
+ ...
46
+ "[architect-target]": {
47
+ "builder": "@angular-builders/custom-esbuild:[application|dev-server]",
48
+ "options": {
49
+ ...
50
+ }
51
+ ```
52
+ Where:
53
+ - [project] is the name of the project to which you want to add the builder
54
+ - [architect-target] is the name of build target you want to run (build, serve, test etc. or any custom target)
55
+ - [application|dev-server] one of the supported builders - [application](#Custom-webpack-browser) or [dev-server](#Custom-webpack-extract-i18n)
56
+ 3. If `[architect-target]` is not one of the predefined targets (like build, serve, test etc.) then run it like this:
57
+ `ng run [project]:[architect-target]`
58
+ If it is one of the predefined targets, you can run it with `ng [architect-target]`
59
+
60
+ ## For Example
61
+
62
+ - angular.json:
63
+ ```js
64
+ "projects": {
65
+ ...
66
+ "example-app": {
67
+ ...
68
+ "architect": {
69
+ ...
70
+ "build": {
71
+ "builder": "@angular-builders/custom-esbuild:browser",
72
+ "options": {
73
+ ...
74
+ }
75
+ ```
76
+ - Run the build: `ng build`
77
+
78
+ # Builders
79
+
80
+ - [@angular-builders/custom-esbuild:application](#Custom-esbuild-application)
81
+ - [@angular-builders/custom-esbuild:dev-server](#Custom-esbuild-dev-server)
82
+
83
+ ## Custom ESBuild `application`
84
+
85
+ The `@angular-builders/custom-esbuild:application` builder is an extension of the `@angular-devkit/build-angular:application` builder, allowing the specification of additional properties on top of the existing ones. The custom builder runs the original builder at the end, incorporating extra parameters specified in the extended configuration. It will also perform `index.html` transformations if specified.
86
+
87
+ Builder options:
88
+
89
+ - All the `@angular-devkit/build-angular:application` options
90
+ - `plugins`
91
+ - `indexHtmlTransformer`: [see below](#index-transform)
92
+
93
+ ### Example
94
+
95
+ `angular.json`:
96
+
97
+ ```js
98
+ "architect": {
99
+ ...
100
+ "build": {
101
+ "builder": "@angular-builders/custom-esbuild:application",
102
+ "options": {
103
+ "plugins": ["./esbuild/plugin-1.js", "./esbuild/plugin-2.js"],
104
+ "indexHtmlTransformer": "./esbuild/index-html-transformer.js",
105
+ "outputPath": "dist/my-cool-client",
106
+ "index": "src/index.html",
107
+ "browser": "src/main.ts",
108
+ "polyfills": ["zone.js"],
109
+ "tsConfig": "src/tsconfig.app.json"
110
+ }
111
+ ```
112
+
113
+ In the above example, we specify the list of `plugins` that should implement the ESBuild plugin schema. These plugins are custom user plugins and are added to the original ESBuild Angular configuration. Additionally, the `indexHtmlTransformer` property is used to specify the path to the file that exports the function used to modify the `index.html`.
114
+
115
+ ## Custom ESBuild `dev-server`
116
+
117
+ The `@angular-builders/custom-esbuild:dev-server` is an enhanced version of the `@angular-devkit/build-angular:dev-server` builder that allows the specification of `middlewares` (Vite's `Connect` functions). It also obtains `plugins` and `indexHtmlTransformer` from the `:application` configuration to run the Vite server with all the necessary configuration applied.
118
+
119
+ ### Example
120
+
121
+ `angular.json`:
122
+
123
+ ```js
124
+ "architect": {
125
+ ...
126
+ "build": {
127
+ "builder": "@angular-builders/custom-esbuild:application",
128
+ "options": {
129
+ "plugins": ["./esbuild/plugin-1.js"]
130
+ ...
131
+ }
132
+ },
133
+ "serve": {
134
+ "builder": "@angular-builders/custom-esbuild:dev-server",
135
+ "options": {
136
+ "middlewares": ["./esbuild/my-middleware.js"],
137
+ "buildTarget": "my-project:build"
138
+ }
139
+ }
140
+ ```
141
+
142
+ # Index Transform
143
+
144
+ Since Angular 8, `index.html` is not generated as part of the build. If you want to modify your `index.html`, you should use the `indexHtmlTransformer` option. `indexHtmlTransformer` is a path (relative to the workspace root) to a `.js` or `.ts` file that exports a transformation function for `index.html`. If `indexHtmlTransformer` is written in TypeScript, the application's `tsConfig` file will be used by `tsnode` for its execution:
145
+
146
+ ```typescript
147
+ (options: TargetOptions, indexHtmlContent: string) => string | Promise<string>;
148
+ ```
149
+
150
+ or, in other words, the function receives target options and original `index.html` content (generated by Angular CLI) and returns a new content as `string` or `Promise<string>`.
151
+ `TargetOptions` follows `target` definition from [this](https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/architect/src/input-schema.json) schema and looks like this:
152
+
153
+ ```typescript
154
+ export interface Target {
155
+ configuration?: string;
156
+ project: string;
157
+ target: string;
158
+ }
159
+ ```
160
+
161
+ It is useful when you want to transform your `index.html` according to the build options.
162
+
163
+ ## Example
164
+
165
+ `angular.json`:
166
+
167
+ ```js
168
+ "architect": {
169
+ ...
170
+ "build": {
171
+ "builder": "@angular-builders/custom-esbuild:application",
172
+ "options": {
173
+ "indexHtmlTransformer": "./esbuild/index-html-transformer.js"
174
+ ...
175
+ }
176
+ ```
177
+
178
+ `index-html-transformer.js`:
179
+
180
+ ```js
181
+ module.exports = (targetOptions, indexHtml) => {
182
+ const i = indexHtml.indexOf('</body>');
183
+ const config = `<p>Configuration: ${targetOptions.configuration}</p>`;
184
+ return `${indexHtml.slice(0, i)}
185
+ ${config}
186
+ ${indexHtml.slice(i)}`;
187
+ };
188
+ ```
189
+
190
+ Alternatively, using TypeScript:
191
+
192
+ ```ts
193
+ import { Target } from '@angular-devkit/architect/src/input-schema';
194
+ import { json } from '@angular-devkit/core';
195
+
196
+ type TargetOptions = json.JsonObject & Target;
197
+
198
+ export default (targetOptions: TargetOptions, indexHtml: string) => {
199
+ const i = indexHtml.indexOf('</body>');
200
+ const config = `<p>Configuration: ${targetOptions.configuration}</p>`;
201
+ return `${indexHtml.slice(0, i)}
202
+ ${config}
203
+ ${indexHtml.slice(i)}`;
204
+ };
205
+ ```
206
+
207
+ In the example we add a paragraph with build configuration to your `index.html`. It is a very simple example without any asynchronous code but you can also return a `Promise` from this function.
208
+
209
+ # ES Modules (ESM) Support
210
+
211
+ Custom ESBuild builder fully supports ESM.
212
+
213
+ - If your app has `"type": "module"` both `plugin.js` and `index-html-transformer.js` will be treated as ES modules, unless you change their file extension to `.cjs`. In that case they'll be treated as CommonJS Modules. [Example](../../examples/custom-esbuild/sanity-esbuild-app-esm).
214
+ - For `"type": "commonjs"` (or unspecified type) both `plugin.js` and `index-html-transformer.js` will be treated as CommonJS modules unless you change their file extension to `.mjs`. In that case they'll be treated as ES Modules. [Example](../../examples/custom-esbuild/sanity-esbuild-app).
215
+ - If you want to use TS config in ESM app, you must set the loader to `ts-node/esm` when running `ng build`. Also, in that case `tsconfig.json` for `ts-node` no longer defaults to `tsConfig` from the `application` target - you have to specify it manually via environment variable. [Example](../../examples/custom-esbuild/sanity-esbuild-app-esm/package.json#L9).
216
+ _Note that tsconfig paths are not supported in TS configs within ESM apps. That is because [tsconfig-paths](https://github.com/dividab/tsconfig-paths) do not support ESM._
package/builders.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "$schema": "./node_modules/@angular-devkit/architect/src/builders-schema.json",
3
+ "builders": {
4
+ "application": {
5
+ "implementation": "./dist/application",
6
+ "schema": "./dist/application/schema.json",
7
+ "description": "Build browser app extended with custom esbuild config"
8
+ },
9
+ "dev-server": {
10
+ "implementation": "./dist/dev-server",
11
+ "schema": "./dist/dev-server/schema.json",
12
+ "description": "Build browser app extended with custom esbuild config"
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,9 @@
1
+ import { BuilderContext } from '@angular-devkit/architect';
2
+ import { json } from '@angular-devkit/core';
3
+ import { CustomEsbuildApplicationSchema } from '../custom-esbuild-schema';
4
+ export declare function buildCustomEsbuildApplication(options: CustomEsbuildApplicationSchema, context: BuilderContext): import("rxjs").Observable<import("@angular-devkit/build-angular/src/builders/application").ApplicationBuilderOutput>;
5
+ declare const _default: import("@angular-devkit/architect/src/internal").Builder<json.JsonObject & import("@angular-devkit/build-angular").ApplicationBuilderOptions & {
6
+ plugins?: string[];
7
+ indexHtmlTransformer?: string;
8
+ }>;
9
+ export default _default;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.buildCustomEsbuildApplication = void 0;
13
+ const architect_1 = require("@angular-devkit/architect");
14
+ const build_angular_1 = require("@angular-devkit/build-angular");
15
+ const core_1 = require("@angular-devkit/core");
16
+ const rxjs_1 = require("rxjs");
17
+ const utils_1 = require("../utils");
18
+ function buildCustomEsbuildApplication(options, context) {
19
+ const workspaceRoot = (0, core_1.normalize)(context.workspaceRoot);
20
+ const tsConfig = `${(0, core_1.getSystemPath)(workspaceRoot)}/${options.tsConfig}`;
21
+ return (0, rxjs_1.defer)(() => __awaiter(this, void 0, void 0, function* () {
22
+ const paths = options.plugins || [];
23
+ const codePlugins = yield Promise.all(paths.map(path => (0, utils_1.loadModule)(workspaceRoot, path, tsConfig, context.logger)));
24
+ const indexHtmlTransformer = options.indexHtmlTransformer
25
+ ? yield (0, utils_1.loadModule)(workspaceRoot, options.indexHtmlTransformer, tsConfig, context.logger)
26
+ : undefined;
27
+ return { codePlugins, indexHtmlTransformer };
28
+ })).pipe((0, rxjs_1.switchMap)(extensions => (0, build_angular_1.buildApplication)(options, context, extensions)));
29
+ }
30
+ exports.buildCustomEsbuildApplication = buildCustomEsbuildApplication;
31
+ exports.default = (0, architect_1.createBuilder)(buildCustomEsbuildApplication);
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/application/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yDAA0E;AAC1E,iEAAiE;AACjE,+CAAsE;AAEtE,+BAAwC;AAGxC,oCAAsC;AAGtC,SAAgB,6BAA6B,CAC3C,OAAuC,EACvC,OAAuB;IAEvB,MAAM,aAAa,GAAG,IAAA,gBAAS,EAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IACvD,MAAM,QAAQ,GAAG,GAAG,IAAA,oBAAa,EAAC,aAAa,CAAC,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;IAEvE,OAAO,IAAA,YAAK,EAAC,GAAS,EAAE;QACtB,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;QACpC,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC,GAAG,CACnC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAA,kBAAU,EAAS,aAAa,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CACrF,CAAC;QAEF,MAAM,oBAAoB,GAAG,OAAO,CAAC,oBAAoB;YACvD,CAAC,CAAC,MAAM,IAAA,kBAAU,EAAC,aAAa,EAAE,OAAO,CAAC,oBAAoB,EAAE,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC;YACzF,CAAC,CAAC,SAAS,CAAC;QAEd,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAkC,CAAC;IAC/E,CAAC,CAAA,CAAC,CAAC,IAAI,CAAC,IAAA,gBAAS,EAAC,UAAU,CAAC,EAAE,CAAC,IAAA,gCAAgB,EAAC,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;AACnF,CAAC;AAnBD,sEAmBC;AAED,kBAAe,IAAA,yBAAa,EAC1B,6BAA6B,CAC9B,CAAC"}