I have a project that include a gulp task for building and packaging the sources and release in a directory called dist
. My goal is publish it as a npm package,
If your project has git you can use small hack. Add next scripts to package.json
"prepublishOnly": "npm run build && cp -r ./lib/* . && rm -rf ./lib",
"postpublish": "git clean -fd",
now when you run publish
command npm involve prepublishOnly
. It builds files and saves them to lib
folder (a build script depend on your project). The next command copies files to root folder and removes lib
. After publish postpublish
script returns the project to a previous state.
Just create a .npmignore
file and add the following to it :
*.*
!dist/*
Here is one more approach which I think is the cleanest. It's all configuration based without needing to move files or specify paths in the build and pack scripts:
package.json
Specify the main file.
{
"main": "lib/index.js",
}
Some additional typescript options:
rootDir
. This directory will have all the source code and it should have an index
file in it (or some other file you can use as main in the package.json
).outDir
. This is where your tsc command will build totsconfig.json
{
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
},
...
}
I have the same desire but I think there is no way of accomplishing this with only using npm tooling. Another script/tool could be used to arrange your package.
Currently I am copying my package.json
into the dist
folder and then running npm pack
inside the dist
folder. I think this essentially provides the desired arrangement of our package.
Here is some relevant reading on this npm design: Why no Directories.lib in Node.
It's also interesting to note that jspm DOES respect the directories.lib
option in package.json
and rearranges the files when resolving the npm package. This all has come about for me because I am wanting to build a common library which can be consumed by jspm or npm/webpack.