Sending the distribution the NPM
To know what you're sending to npm:
$ npm pack
it's part of the npm script that publishes. Then you can open the zip file and see what is sending to npm.
In his example this contains the dirs test
and src
and we don't need to send those (and neither the code of conduct file).
This can be configured in package.json
where we can tell npm which files matter to the registry.
"files": [
"dist"
],
Readme.md and license (and package.json) is included by default already. So this will only include the dist
directory.
We need to add some more configs:
###main
entry in package.json
This tells NodeJS where to look for when require
ing your package. In the beginning it was in src/index.js
but now after the transpilation, it's actually in dist/index.js
.
We need to update this.
"main": "dist/index.js",
(package.json)
###About adding module.exports = mainExport
In the end of index.js
:
import uniqueRandomArray from 'unique-random-array'
const starWarsNames = require('./starwars-names.json')
const mainExport = {
all: starWarsNames,
random: uniqueRandomArray(starWarsNames),
}
// this ↴
export default mainExport
module.exports = mainExport // for CommonJS compatibility
kcd.im/tears to see explanation.
Q about instrumentation
It (NYC) instruments all the required modules (except from node_modules). So he's testing his src files not the dist ones. Even though the users will use the dist files, so you have to have faith in Babel that it does not add bugs in your code.
In general, he says that
you should test your source files and trust your build tools
Of course you can also use integration tests. They are more valuable but harder to set up...