How to npm publish specific folder but as package root

后端 未结 10 1201
后悔当初
后悔当初 2020-12-13 01:10

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,

相关标签:
10条回答
  • 2020-12-13 01:56

    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.

    0 讨论(0)
  • 2020-12-13 01:58

    Just create a .npmignore file and add the following to it :

    *.*
    !dist/*
    
    0 讨论(0)
  • 2020-12-13 01:59

    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:

    • Specify the 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).
    • Specify the outDir. This is where your tsc command will build to

    tsconfig.json

    {
        "compilerOptions": {
            "rootDir": "src",
            "outDir": "lib",
        },
        ...
    
    }
    
    0 讨论(0)
  • 2020-12-13 02:11

    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.

    Alternative Solution

    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.

    0 讨论(0)
提交回复
热议问题