UNPKG

16.5 kBMarkdownView Raw
1# ez-build
2
3A build tool for the Zambezi web platform, enabling you to easily implement a workflow utilizing modern web technologies such as ES2015.
4
5For more information on why ez-build exists, [please read the rationale](RATIONALE.md).
6
7# Installation
8
9The recommended approach to using ez-build is to install it as a local development dependency of your project:
10
11```bash
12$ npm install --save-dev @zambezi/ez-build
13```
14
15This will make the binary `ez-build` available to npm scripts. This tool is self contained, and not dependent on additional peer dependencies. However, if you wish to use additional plugins or presets, you must install those as well. (See the section on using additional plugins below.)
16
17# Usage
18
19Quick help can be retrieved with the `-h` or `--help` flags. Further explanation of the options can be found in a section below.
20
21```bash
22$ ez-build --help
23
24 Usage: ez-build [options]
25
26 Options:
27
28 -h, --help output usage information
29 -V, --version output the version number
30 -i, --src <dir> the root directory from which all sources are relative [src]
31 -o, --out <prefix> write optimized output to files with the specified prefix [project-min]
32 -L, --lib <dir> write unoptimized files to the specified directory [lib]
33 -I, --include [js|css:]<path> include a path or glob (relative to source root) [js:**/*.js,css:**/*.css]
34 -X, --exclude [js|css:]<path> exclude a path or glob (relative to source root) [../node_modules/**/*]
35 -O, --optimize <level> optimization level (0 = none) [0]
36 --no-copy disable copying of non-code files to lib
37 --no-debug disable source map generation
38 --log <normal|json> log output format [normal]
39 --interactive watch for and recompile on changes (implies -O 0)
40 --production enable production options (implies -O 1)
41 --flags <flags> toggle build flags
42 @<path> read options from the file at <path> (relative to cwd)
43```
44
45## Using ez-build in npm scripts
46
47A typical scripts section of a package.json when using ez-build looks something like this:
48
49```json
50{ "build": "ez-build --production"
51, "build:dev": "ez-build --interactive"
52}
53```
54
55The first script – `npm run dev` – will run ez-build in interactive mode, which continously watches for changes to input files and directories, and rebuilds the project as necessary.
56
57The second script – `npm run build` – is the recommended approach to building production artefacts for the Zambezi platform.
58
59## CLI Options
60
61### `-h, --help`
62
63Outputs information on how to use ez-build.
64
65### `-V, --version`
66
67Outputs the version number of ez-build.
68
69### `-i, --src <dir>`
70
71The root directory from which the build tool will reference source files. This affects the matching of the `--include` and `--exclude` flags, and potentially other – e.g. `--include **/*.js` will really resolve to `--include <src dir>/**/*.js`. If left unspecified, this flag will default to `directories.src` from `package.json`, or `<project root>/src` if no such field is specified.
72
73### `-o, --out <prefix>`
74
75The prefix used for linked and – depending on optimization level – optimized output. This option is for advanced usage and its usage is not generally recommended.
76
77### `-L, --lib <dir>`
78
79Unoptimized files, source maps, and other generated content will be written to the directory specified by this flag. If left unspecified, this flag will default to `directories.lib` from `package.json`, or `./lib` if no such field is specified.
80
81### `-I, --include [js|css:]<path>`
82
83*Note that using this flag will overwrite the default values, so if you wish to include those you will have to repeat them.*
84
85Sets a pattern to describe which files to include in the build process. By default, the patterns `js:**/*.js,css:**/*.css` are specified. The patterns can be defined for different pipelines (`js:` or `css:`.) If no namespace is specified – e.g. `*.txt` – it will be added to all pipelines. Generally, you would always specify a namespace for `--include` to avoid pipelines working on incompatible files. (E.g. if you try `--include "**/*.js"` without namespace, the css compiler will also try to compile js files, which is probably not what you want.)
86
87**Note:** It's important to properly quote the pattern, to avoid the shell expanding the pattern before executing ez-build.
88
89The namespace must be added to all patterns, so to define multiple patterns either repeat the `--include` option or use comma to separate the patterns. The following are equivalent:
90
91```bash
92$ ez-build --include "js:**/*.js" --include "js:**/*.jsx"
93```
94
95```bash
96$ ez-build --include "js:**/*.js,js:**/*.jsx"
97```
98
99It's also possible to use braced patterns, instead of multiple comma separated patterns, which can make some configurations neater. For instance, the above pattern could be rewritten as such:
100
101```bash
102$ ez-build --include "js:**/*.{js,jsx}"
103```
104
105Typically, this flag is only used if you have also configured additional presets or plugins.
106
107### `-X, --exclude [js|css:]<path>`
108
109*Note that using this flag will overwrite the default values, so if you wish to include those you will have to repeat them.*
110
111Works just like the `--include` flag, but defines which files to *exclude* from the build process entirely (including file copying.) By default, the pattern `../node_modules/**/*` is specified. (Remember, both `--include` and `--exclude` are relative to `src`.)
112
113### `-O, --optimize <level>`
114
115Sets the level of optimization. By default, no optimization occurs, which is a useful default for development purposes but not recommended for production environments. The `level` should be a positive integer, with `0` meaning no optimization should occur. Disabling optimizations altogether usually produces the most accurate debug artefacts such as source maps. Currently, the only optimization that is done for levels over 0 is to bundle all modules together. This will likely change over time.
116
117### `--no-copy`
118
119By default ez-build will copy any non-code files verbatim to the output directory (as specified by `--lib`.) Use this flag to disable this behavior. Non-code files are any files that are not covered by any `--include` patterns, and likewise not covered by any `--exclude` patterns.
120
121### `--no-debug`
122
123By default ez-build will generate source maps and other debugging information for all built artefacts. Use this flag to disable this behavior. Generally, it is not recommended that this flag be used, since it makes debugging a lot more difficult. However, it may have a very small but positive performance implications on builds.
124
125### `--log <normal|json>`
126
127Determines the output format of ez-build's log. This is generally not used, but setting it to `json` provides additional detail and can sometimes help in debugging issues.
128
129### `--interactive`
130
131Runs ez-build in interactive mode, meaning it will run continuously and watch for changes to input files and directories. This is very useful for rapid development, since it's much faster to rebuild only what changed than the entire project. Setting this flag implies `-O 0` which disables all optimizations.
132
133This flag is ignored entirely if combined with `--production`.
134
135If `NODE_ENV=production` is set when invoking ez-build with `--interactive`, it will still enter interactive mode, but will *not* change the value of `NODE_ENV`.
136
137### `--production`
138
139Runs ez-build in production mode, which implies a higher optimization level (currently `-O 1`,) as well as the generation of additional artefacts, such as a module manifest. For builds destined for deployment into Zambezi environments, this flag must be used or the builds will not work outside of debug mode.
140
141If `NODE_ENV` is not set when ez-build is invoked in production mode, it will be set to `production`. Conversely if `NODE_ENV=production` when ez-build is invoked and no other mode is specified, e.g. `--interactive`, it will also enable production mode. This means there are three different ways one can enable ez-build production mode:
142
143- `ez-build --production`
144- `NODE_ENV=production ez-build`
145- `npm run --production build` (assuming `build` is a script which invokes ez-build; substitute for whatever script name you may be using)
146
147*Note: the `NODE_ENV=production ez-build` syntax may not work on all systems. For compatibility across operating systems, you may want to consider using something like [cross-env](https://www.npmjs.com/package/cross-env).*
148
149### `--target-browsers <spec|false>`
150
151Define which browsers that ez-build should do its best to target when producing its output. What this means is that only those features that actually need compiling will be compiled, and the rest will be left alone. The `spec` string can be any query supported by [browserslist](https://github.com/ai/browserslist). This option defaults to `"last 3 versions"` which is a rather broad range, intended to capture most reasonably modern browsers.
152
153**Please note:** because target queries will likely include spaces, make sure to quote your queries to avoid any confusion, like so:
154
155```bash
156$ ez-build --target-browsers "last 3 versions"
157```
158To configure multiple browsers:
159
160```sh
161$ ez-build --target-browsers "Firefox >= 44,Chrome >= 48,IE >= 9,last 2 versions"
162```
163
164This feature will *not* enable experimental features, even though target browsers may support them.
165
166The same query is used to determine the feature set in the JavaScript and CSS auto-prefixer output. It is currently not possible to configure these independently.
167
168### `--target-node [<current|number|false>]`
169
170Define which version of node ez-build should target in its output. This flag is disabled by default, since browsers are the typical targets for ez-build. If no value is specified when this option is enabled however, the default is `current`, which means the currently installed version of node.
171
172Combining this option with `--target-browsers` should be safe, however if you want to only target node it may be useful to disable browser targets since it can produce more optimal code. To do this, simply do the following:
173
174```bash
175$ ez-build --target-node --target-browsers false
176```
177
178This would enable the currently installed node version as the target. To use a different version, simply specify a version:
179
180```bash
181$ ez-build --target-node 4.1.2 --target-browsers false
182```
183
184It's not necessary to specify a full version, simply `4` or `4.1` would also do.
185
186### `--flags <flags>`
187
188Toggles flags that may affect the output or behavior of ez-build. Multiple flags can be toggled at once, just separate them with a comma. For example, `--flags modules:commonjs,add-module-exports` would set the `modules` flag value to `commonjs`, and enable the `add-module-exports` flag.
189
190The available flags are:
191
192 - `modules:<umd|amd|commonjs|systemjs|ecmascript>` allows you to control the output module format. Setting this value to `ecmascript` will disable the transformation of output module format altogether, keeping `import` and `export` statements largely intact. This flag defaults to `umd`.
193 - `add-module-exports` toggles whether the UMD output of ez-build should be backwards compatible with AMD and CJS module formats. If this flag is specified, ez-build will ensure any module with a single `export default` will not export an object with a `default` key. This flag is disabled by default. It is only recommended you use this flag if you *must* keep backwards compatibility with legacy code.
194 - `es-stage:<0|1|2|3>` enables experimental EcmaScript features as determined by the [TC39 proposals documentation](https://github.com/tc39/proposals). Note that [finished proposals](https://github.com/tc39/proposals/blob/master/finished-proposals.md), i.e. stage 4, are enabled by default. This flag is disabled by default.
195 - `react` enables support for React specific features, such as JSX. See section on React & JSX below for more information. This flag is disabled by default.
196
197### `@<path>`
198
199Reads ez-build options from the file at `<path>`, resolved from the current working directory. This can be used to share configuration among different builds. For instance, a common scenario is to share the same build configuration between `--production` and `--interactive` builds in npm scripts, like so:
200
201```json
202{
203 "build": "ez-build --production --include \"js:**/*.{js,jsx}\" --flags add-module-exports",
204 "build:dev": "ez-build --interactive --include \"js:**/*.{js,jsx}\" --flags add-module-exports"
205}
206```
207
208To avoid this repetition, and also make things a bit neater, we can place this configuration in a file. The name and extension of the file doesn't matter, just pick something useful to your project. In this example, we'll use the name `build.opts`, and the contents look like this:
209
210```
211--include js:**/*.js
212--include js:**/*.jsx
213--flags add-module-exports"
214```
215
216*(Note how we repeat the `--include` option – any option that allows a list of values can be specified multiple times like this. It can make options much more readable, and also make it easy to maintain. No longer want to include jsx files? Just remove the line.)*
217
218Now we can change our scripts to include the options from the file, making them less repetitive, and making it easier to maintain our configuration:
219
220```json
221{
222 "build": "ez-build --production @build.opts",
223 "build:dev": "ez-build --interactive @build.opts"
224}
225```
226## Enabling experimental JavaScript features
227
228Some features are not yet in the standard set, but may well be on their way. The features are categorized by their maturity, and there are four different such maturity categories that features must progress through before being considered *finished*. These are explained in depth in the [TC39 process document](https://tc39.github.io/process-document/) but can be summarized as such:
229
230- stage-0 - Strawman: just an idea.
231- stage-1 - Proposal: an idea worth exploring further.
232- stage-2 - Draft: initial spec.
233- stage-3 - Candidate: complete spec and initial browser implementations.
234- stage-4 - Finished: will be added to the next yearly release.
235
236Because of their experimental nature and often fast paced development and – particularly for lower stage features – volatility, staged features are disabled by default. It's very simply to toggle them on however, by using the `es-stage` flag. For instance, to enable the current stage 3 features, use the following option:
237
238```
239$ ez-build --flags es-stage:3
240```
241
242When enabling a more experimental stage, it will also enable all stages "above it". The following example will enable stages 1, 2, and 3:
243
244```bash
245$ ez-build --flags es-stage:1
246```
247
248Stages are usually updated shortly after TC39 holds their meetings, in case there are any relevant changes. You may need to reinstall ez-build in order to retreive the latest updates.
249
250## React, JSX, and other non-standard features
251
252React is a popular library for front-end web development, and with that comes the non-standard language extension JSX. While [ez-build takes a conservative stance with regards to the languages and features it supports](RATIONALE.md), it also recognizes that some non-standard extensions and features are so popular that it wouldn't be very practical to exclude them. React is such an example.
253
254To enable support for React features, simply toggle the `react` flag:
255
256```
257--flags react
258```
259
260It's very common with the use of JSX to use a different file extension, and in order for ez-build to pick those files up, they must be included with the build:
261
262```bash
263$ ez-build --include "js:**/*.{js,jsx}"
264```
265
266Note that we add the `js:**/*.js` pattern in addition to `js:**/*.jsx`, since using this option overwrites the defaults. Also note the use of namespaces in the patterns, to determine which pipeline the pattern should affect.
267
268Found an issue, or want to contribute?
269--------------------------------------
270
271If you find an issue, want to start a discussion on something related to this project, or have suggestions on how to improve it? Please [create an issue](../../issues/new)!
272
273See an error and want to fix it? Want to add a file or otherwise make some changes? All contributions are welcome! Please refer to the [contribution guidelines](CONTRIBUTING.md) for more information.
274
275License
276-------
277
278Please refer to the [license](LICENSE.md) for more information on licensing and copyright information.