Skip to content

Commit a49f66b

Browse files
danny-andrewssindresorhus
authored andcommitted
Add recipe for precompiling source files with webpack (#1212)
1 parent 3279336 commit a49f66b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Precompiling source files with webpack
2+
3+
The AVA [readme](https://github.com/avajs/ava#transpiling-imported-modules) mentions precompiling your imported modules as an alternative to runtime compilation, but it doesn't explain how. This recipe shows how to do this using webpack. (This example uses webpack 2.0)
4+
5+
###### webpack.config.js
6+
7+
```js
8+
const nodeExternals = require('webpack-node-externals');
9+
10+
module.exports = {
11+
entry: ['src/tests.js'],
12+
target: 'node',
13+
output: {
14+
path: '_build',
15+
filename: 'tests.js'
16+
},
17+
externals: nodeExternals,
18+
module: {
19+
rules: [{
20+
test: /\.(js|jsx)$/,
21+
use: 'babel-loader'
22+
}]
23+
}
24+
};
25+
```
26+
27+
The important bits are `target: 'node'`, which ignores Node.js-specific `require`s (e.g. `fs`, `path`, etc.) and `externals: nodeModules` which prevents webpack from trying to bundle external Node.js modules which it may choke on.
28+
29+
You can now run `$ ava _build/tests.js` to run the tests contained in this output.

readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ AVA currently only transpiles the tests you ask it to run, as well as test helpe
743743

744744
If you use Babel you can use its [require hook](https://babeljs.io/docs/usage/require/) to transpile imported modules on-the-fly. To add it, [configure it in your `package.json`](#configuration).
745745

746-
You can also transpile your modules in a separate process and refer to the transpiled files rather than the sources from your tests.
746+
You can also transpile your modules in a separate process and refer to the transpiled files rather than the sources from your tests. Example [here](docs/recipes/precompiling-with-webpack.md).
747747

748748
### Promise support
749749

@@ -1137,6 +1137,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
11371137
- [JSPM and SystemJS](docs/recipes/jspm-systemjs.md)
11381138
- [Debugging tests with Chrome DevTools](docs/recipes/debugging-with-chrome-devtools.md)
11391139
- [Debugging tests with WebStorm](docs/recipes/debugging-with-webstorm.md)
1140+
- [Precompiling source files with webpack](docs/recipes/precompiling-with-webpack.md)
11401141

11411142
## Support
11421143

0 commit comments

Comments
 (0)