Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Last active October 7, 2019 18:16
Show Gist options
  • Save jmakeig/a34436dbda60908acf47d9f19e266ce5 to your computer and use it in GitHub Desktop.
Save jmakeig/a34436dbda60908acf47d9f19e266ce5 to your computer and use it in GitHub Desktop.
Bundle TypeScript tests for the browser and Node using Rollup, including resolving reported stacktraces with sourcemaps
import multiEntry from 'rollup-plugin-multi-entry';
import nodeResolve from 'rollup-plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
export default {
input: 'src/**/*.test.ts',
output: [
/* Command-line in Node
```shell
rollup -c test.config.js && \
tsc && \
node -r source-map-support/register dist/tests.js`
```
*/
{
format: 'cjs',
file: 'dist/tests.js',
sourcemap: true
},
/* Browser console
See HTML harness below.
*/
{
format: 'iife',
file: 'dist/tests-browser.js',
/*
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Tests</title>
<script
type="application/javascript"
src="//unpkg.com/zora@3.0.3/dist/bundle/zora.js"
></script>
<script
type="application/javascript"
src="//unpkg.com/xstate@4.7.0-rc3/dist/xstate.js"
></script>
<script
type="application/javascript"
src="//unpkg.com/source-map-support@0.5.13/browser-source-map-support.js"
></script>
<script type="application/javascript">
sourceMapSupport.install();
</script>
<script type="application/javascript" src="./tests-browser.js"></script>
</head>
<body>
Check out the console…
</body>
</html>
```
*/
globals: { xstate: 'XState', zora: 'zora' },
sourcemap: true
}
],
/* Don’t transpile these into the bundle (for testing, at least)
- In CommonJS output, these will end up as explicit `require`
statements that Node can resolve at runtime.
- For browsers, `<script>` elements will expose global variables. See
`output[1].globals` above.
*/
external: ['xstate', 'zora'],
plugins: [
multiEntry(),
nodeResolve(/*{ modulesOnly: true }*/),
typescript({
typescript: require('typescript')
})
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment