1 | # @loopback/build
|
2 |
|
3 | This module contains a set of common scripts and default configurations to build
|
4 | LoopBack 4 or other TypeScript modules, including:
|
5 |
|
6 | - lb-tsc: Use
|
7 | [`tsc`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) to
|
8 | compile typescript files
|
9 | - lb-eslint: Run [`eslint`](https://typescript-eslint.io/)
|
10 | - lb-prettier: Run [`prettier`](https://github.com/prettier/prettier)
|
11 | - lb-mocha: Run [`mocha`](https://mochajs.org/) to execute test cases
|
12 | - lb-nyc: Run [`nyc`](https://github.com/istanbuljs/nyc)
|
13 |
|
14 | These scripts first try to locate the CLI from target project dependencies and
|
15 | fall back to bundled ones in `@loopback/build`.
|
16 |
|
17 | ## Basic use
|
18 |
|
19 | To use `@loopback/build` for your package:
|
20 |
|
21 | 1. Run the following command to add `@loopback/build` as a dev dependency.
|
22 |
|
23 | `npm i @loopback/build --save-dev`
|
24 |
|
25 | 2. Configure your project package.json as follows:
|
26 |
|
27 | ```json
|
28 | "scripts": {
|
29 | "build": "lb-tsc",
|
30 | "build:watch": "lb-tsc --watch",
|
31 | "clean": "lb-clean",
|
32 | "lint": "npm run prettier:check && npm run eslint",
|
33 | "lint:fix": "npm run prettier:fix && npm run eslint:fix",
|
34 | "prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\"",
|
35 | "prettier:check": "npm run prettier:cli -- -l",
|
36 | "prettier:fix": "npm run prettier:cli -- --write",
|
37 | "eslint": "lb-eslint --report-unused-disable-directives .",
|
38 | "eslint:fix": "npm run eslint -- --fix",
|
39 | "pretest": "npm run clean && npm run build",
|
40 | "test": "lb-mocha \"dist/__tests__\"",
|
41 | "posttest": "npm run lint",
|
42 | "start": "npm run build && node .",
|
43 | "prepublishOnly": "npm run test"
|
44 | },
|
45 | ```
|
46 |
|
47 | Please remember to replace `your-module-name` with the name of your module.
|
48 |
|
49 | Now you run the scripts, such as:
|
50 |
|
51 | - `npm run build` - Compile TypeScript files and copy resources (non `.ts`
|
52 | files) to outDir
|
53 | - `npm test` - Run all mocha tests
|
54 | - `npm run lint` - Run `eslint` and `prettier` on source files
|
55 |
|
56 | 3. Override default configurations in your project
|
57 |
|
58 | - lb-tsc
|
59 |
|
60 | By default, `lb-tsc` searches your project's root directory for
|
61 | `tsconfig.build.json` then `tsconfig.json`. If neither of them exists, a
|
62 | `tsconfig.json` will be created to extend from
|
63 | `@loopback/build/config/tsconfig.common.json`.
|
64 |
|
65 | To customize the configuration:
|
66 |
|
67 | - Create `tsconfig.build.json` or `tsconfig.json` in your project's root
|
68 | directory
|
69 |
|
70 | ```json
|
71 | {
|
72 | "$schema": "http://json.schemastore.org/tsconfig",
|
73 | "extends": "@loopback/build/config/tsconfig.common.json",
|
74 | "compilerOptions": {
|
75 | "outDir": "dist",
|
76 | "rootDir": "src"
|
77 | },
|
78 | "include": ["src"]
|
79 | }
|
80 | ```
|
81 |
|
82 | - Set options explicitly for the script
|
83 |
|
84 | ```sh
|
85 | lb-tsc -p tsconfig.json --target es2017 --outDir dist
|
86 | ```
|
87 |
|
88 | For more information, see
|
89 | <https://www.typescriptlang.org/docs/handbook/compiler-options.html>.
|
90 |
|
91 | - The following un-official compiler options are available:
|
92 |
|
93 | | Option | Description |
|
94 | | ------------------ | ------------------------------------------------------------------------------------------------- |
|
95 | | `--copy-resources` | Copy all non-typescript files from `src` and `test` to `outDir`, preserving their relative paths. |
|
96 |
|
97 | - Using [`ttypescript`](https://github.com/cevek/ttypescript)
|
98 |
|
99 | ### Stability: ⚠️Experimental⚠️
|
100 |
|
101 | If you would like to use `ttypescript` and its available plugins, you can
|
102 | substitute `lb-tsc` with `lb-ttsc`, or pass the option
|
103 | `lb-tsc --use-ttypescript`. If `ttypescript` is not installed, the default
|
104 | TypeScript compiler `tsc` will be used instead.
|
105 |
|
106 | 4. Run builds
|
107 |
|
108 | ```sh
|
109 | npm run build
|
110 | ```
|
111 |
|
112 | 5. Run code coverage reports
|
113 |
|
114 | - `lb-nyc`
|
115 |
|
116 | `lb-nyc` is a simple wrapper for [`nyc`](https://github.com/istanbuljs/nyc).
|
117 |
|
118 | To customize the configuration:
|
119 |
|
120 | - Create `.nycrc` in your project's root directory
|
121 |
|
122 | ```json
|
123 | {
|
124 | "include": ["dist"],
|
125 | "exclude": ["dist/__tests__/"],
|
126 | "extension": [".js", ".ts"],
|
127 | "reporter": ["text", "html"],
|
128 | "exclude-after-remap": false
|
129 | }
|
130 | ```
|
131 |
|
132 | - Update your `package.json` scripts:
|
133 |
|
134 | ```json
|
135 | "precoverage": "npm test",
|
136 | "coverage": "open coverage/index.html",
|
137 | "coverage:ci": "lb-nyc report --reporter=text-lcov | coveralls",
|
138 | "test": "lb-nyc npm run mocha",
|
139 | "test:ci": "lb-nyc npm run mocha"
|
140 | ```
|
141 |
|
142 | `coverage:ci` sets up integration with [Coveralls](https://coveralls.io/).
|
143 |
|
144 | ## A note on console logs printed by tests
|
145 |
|
146 | We consider (console) logging from tests as a bad practice, because such logs
|
147 | usually clutter the test output and make it difficult to distinguish legitimate
|
148 | error messages from the noise.
|
149 |
|
150 | By default, `lb-mocha` detects when the tests and/or the application tested have
|
151 | printed console logs and fails the test run with the following message:
|
152 |
|
153 | ```
|
154 | === ATTENTION - INVALID USAGE OF CONSOLE LOGS DETECTED ===
|
155 | ```
|
156 |
|
157 | If you need more information about behavior in the test, then the first choice
|
158 | should be to use a better or more descriptive error assertion. If that's not
|
159 | possible, then use debug statements to print additional information when
|
160 | explicitly requested.
|
161 |
|
162 | A typical situation is that a test is sending an HTTP request and the server
|
163 | responds with an error code as expected. However, because the server is
|
164 | configured to log failed requests, it will print a log also for requests where
|
165 | the failure was expected and intentional. The solution is to configure your REST
|
166 | server to suppress error messages for that specific error code only. Our
|
167 | `@loopback/testlab` module is providing a helper
|
168 | [`createUnexpectedHttpErrorLogger`](https://github.com/loopbackio/loopback-next/tree/master/packages/testlab#createUnexpectedHttpErrorLogger)
|
169 | that makes this task super easy.
|
170 |
|
171 | Alternatively, it's also possible to disable detection of console logs by
|
172 | calling `lb-mocha` with `--allow-console-logs` argument.
|
173 |
|
174 | ## Contributions
|
175 |
|
176 | - [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
|
177 | - [Join the team](https://github.com/loopbackio/loopback-next/issues/110)
|
178 |
|
179 | ## Tests
|
180 |
|
181 | run `npm test` from the root folder.
|
182 |
|
183 | ## Contributors
|
184 |
|
185 | See
|
186 | [all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
|
187 |
|
188 | ## License
|
189 |
|
190 | MIT
|