Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recipe for precompiling source files with webpack #1212

Merged
merged 9 commits into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/recipes/precompiling-with-webpack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Precompiling source files with webpack

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a link to this recipe in the AVA readme at https://github.com/avajs/ava#transpiling-imported-modules


###### webpack.config.js

```js
const nodeExternals = require('webpack-node-externals');

module.exports = {
entry: ['src/tests.js'],
target: 'node',
output: {
path: '_build',
filename: 'tests.js'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As someone who didn't know about this feature, I'm asking questions from a first time user standpoint that I think are important to address:

  • I would love to see an example of what this file looks like. Could a code block be added above to show this? Is it just a list of t.test? Or better yet is there a repo for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a file with the contents of all the transpiled test code and all of its dependencies. It's really ugly and wouldn't really help to see.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entry: ['src/tests.js'] can be a list of t.test or, better yet, require or import statements pulling in the tests you want to run, because you probably don't want all your tests in one file, unless your project is tiny. As the name suggests, it's the entry point that Webpack will look at to determine what to compile.

The output.filename will contain the compiled code.

},
externals: nodeExternals,
module: {
rules: [{
test: /\.(js|jsx)$/,
use: 'babel-loader'
}]
}
};
```

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 libraries which it may choke on.

You can now run `$ ava _build/tests.js` to run the tests contained in this output.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ AVA currently only transpiles the tests you ask it to run, as well as test helpe

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).

You can also transpile your modules in a separate process and refer to the transpiled files rather than the sources from your tests.
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).

### Promise support

Expand Down Expand Up @@ -1120,6 +1120,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
- [JSPM and SystemJS](docs/recipes/jspm-systemjs.md)
- [Debugging tests with Chrome DevTools](docs/recipes/debugging-with-chrome-devtools.md)
- [Debugging tests with WebStorm](docs/recipes/debugging-with-webstorm.md)
- [Precompiling code with Webpack](docs/recipes/precompiling-with-webpack.md)

## Support

Expand Down