|
1 | 1 | # @hypothesis/frontend-build |
2 | 2 |
|
3 | | -Utilities for building assets and running tests in frontend projects. |
| 3 | +This package contains functions for building assets and running tests in |
| 4 | +Hypothesis frontend projects, typically as part of [Gulp](https://gulpjs.com) tasks. |
| 5 | + |
| 6 | +It assumes that the project is using our standard frontend tech stack and |
| 7 | +follows certain conventions about the location and naming of files. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +This project assumes our standard tech stack for frontend projects: |
| 12 | + |
| 13 | +- [Gulp](https://gulpjs.com) for running tasks [1] |
| 14 | +- [Rollup](https://rollupjs.org/guide/en/) for building JavaScript bundles |
| 15 | +- [Sass](https://sass-lang.com) for authoring styles |
| 16 | +- [Karma](https://karma-runner.github.io/latest/index.html) for running tests |
| 17 | + |
| 18 | +[1] Gulp is not required as the task runner, but is how most of our projects |
| 19 | +are currently set up. |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +The typical structure of a Hypothesis frontend project looks like: |
| 24 | + |
| 25 | +```sh |
| 26 | +gulpfile.mjs # Gulp tasks |
| 27 | + |
| 28 | +src/ # Source files ("$PROJECT_NAME/static/scripts" in Python projects) |
| 29 | + karma.config.js # Karma config file |
| 30 | + tests/ |
| 31 | + bootstrap.js # JS module that configures test environment |
| 32 | + |
| 33 | +rollup.config.mjs # Rollup config that builds application/library bundles |
| 34 | +rollup-tests.config.mjs # Rollup config that builds test bundle from `build/scripts/test-inputs.js` |
| 35 | + |
| 36 | +build/ # Compiled frontend assets |
| 37 | + scripts/ # Generated JS bundles |
| 38 | + some-app.bundle.js |
| 39 | + some-app.bundle.js.map |
| 40 | + |
| 41 | + styles/ # Generated CSS bundles |
| 42 | + some-app.css |
| 43 | + some-app.css.map |
| 44 | +``` |
| 45 | + |
| 46 | +Compiling frontend assets from source files is handled by tasks defined in |
| 47 | +`gulpfile.mjs`. |
| 48 | + |
| 49 | +To use this package, first add it as a dependency to the project: |
| 50 | + |
| 51 | +```js |
| 52 | +yarn add --dev @hypothesis/frontend-build |
| 53 | +``` |
| 54 | + |
| 55 | +Then use the functions within Gulp tasks. For example: |
| 56 | + |
| 57 | +```js |
| 58 | +import { buildCSS, buildJS, watchCSS, runTests } from '@hypothesis/frontend-build'; |
| 59 | + |
| 60 | +gulp.task('build-js', () => buildJS('rollup.config.mjs')); |
| 61 | +gulp.task('watch-js', () => watchJS('rollup.config.mjs')); |
| 62 | +gulp.task('build-css', () => buildCSS(['src/my-app.scss', 'src/other-app.scss'])); |
| 63 | +gulp.task('watch-css', () => gulp.watch('src/**/*.scss', 'build-css')); |
| 64 | +gulp.task('watch', gulp.parallel('watch-js', 'watch-css')); |
| 65 | +gulp.task('test', () => runTests({ |
| 66 | + bootstrapFile: 'src/tests/bootstrap.js', |
| 67 | + karmaConfig: 'src/karma.config.js', |
| 68 | + rollupConfig: 'rollup-tests.config.mjs', |
| 69 | + testsPattern: '**/*-test.js', |
| 70 | +}); |
| 71 | + |
| 72 | +// Project-specific tasks... |
| 73 | +``` |
| 74 | +
|
| 75 | +## API reference |
| 76 | +
|
| 77 | +This section provides an overview of the functions in this package. See the |
| 78 | +JSDoc comments for individual functions for full details. |
| 79 | +
|
| 80 | +### Building asset bundles |
| 81 | +
|
| 82 | +`buildJS(rollupConfig)` - Build one or more JavaScript bundles defined in a |
| 83 | +Rollup config file. The Rollup config file must be an ES module. |
| 84 | +
|
| 85 | +`watchJS(rollupConfig)` - Same as `buildJS`, but watches for updates to input files |
| 86 | +after building the bundle and rebuilds if they change. |
| 87 | +
|
| 88 | +`buildCSS(inputs)` - Build one or more CSS bundles from CSS or SASS entry points. |
| 89 | +
|
| 90 | +### Running tests |
| 91 | +
|
| 92 | +`runTests(config)` - Build a JavaScript bundle of tests from a set of input files |
| 93 | +and run them in Karma. |
| 94 | +
|
| 95 | +The test bundle is created by first finding all test files that match the |
| 96 | +`testsPattern` argument and generating an entry point, |
| 97 | +`build/scripts/test-inputs.js`, which imports all of the test files. The |
| 98 | +bundle is then built by passing the config specified by `rollupConfig` to |
| 99 | +Rollup. Once the bundle has been generated, Karma is started using the config |
| 100 | +file specified by `karmaConfig`, which should load the test bundle. |
| 101 | +
|
| 102 | +This command supports filtering which tests are run |
| 103 | +by using the `--grep <file pattern>` CLI argument. If the `--watch` CLI flag is |
| 104 | +set, the test runner watches for changes and rebuild and re-runs the tests if |
| 105 | +the input files change. |
| 106 | +
|
| 107 | +### Utilities |
| 108 | +
|
| 109 | +`run(command, args)` - Run a CLI command and forward the output to the terminal. |
0 commit comments