简体   繁体   English

我的软件包的NPM构建阶段在安装过程中未执行

[英]NPM build phase of my package not executed during install

I am developing a package to use web workers in a generic way but I am finding some issues when trying to add it as a dependency for another project. 我正在开发一个用于以通用方式使用Web Worker的程序包,但是在尝试将其添加为另一个项目的依赖项时发现了一些问题。

Normally I would expect that having a build script section of my package.json when doing install that it would be automatically called generating the output of the rollup.config.js . 通常,我希望在install时在package.json中有一个构建脚本部分,它将被自动调用以生成rollup.config.js的输出。 But it does not seem to execute anything. 但是它似乎什么也没执行。 Do I have any misunderstanding on how npm build should be working? 我对npm build应该如何工作有任何误解?

If not, there be any other colliding script in package.json that is causing it not to work in the next file example: 如果不是,则在下一个文件示例中, package.json中存在任何其他冲突脚本,导致该脚本不起作用:

{
 "name": "web-threads",
  "version": "1.0.5",
  "description": "generic threads using web workers for the web",
  "main": "dist/web-threads.js",
  "scripts": {
    "build": "rollup -c",
    "test": "jest",
    "test:dev": "jest --watchAll test/unit",
    "test:int": "jest test/integration",
    "test:cov": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
    "push": "yarn test && git push",
    "deploy:major": "yarn version --major",
    "deploy:minor": "yarn version --minor",
    "deploy:patch": "yarn version --patch",
    "deploy:push": "git push && git push --tags",
    "preversion": "yarn test"
  },
  "keywords": [""],
  "repository": "",
  "author": "",
  "license": "MIT",
  "private": false,
  "devDependencies": {
    "babel-jest": "23.4.2",
    "babel-preset-env": "1.7.0",
    "babel-preset-stage-0": "6.24.1",
    "coveralls": "3.0.2",
    "faker": "4.1.0",
    "jest": "23.5.0",
    "jest-puppeteer": "3.3.1",
    "puppeteer": "1.7.0",
    "rollup": "0.65.0",
    "rollup-plugin-babel": "3.0.7",
    "rollup-plugin-uglify": "4.0.0",
    "uglify-es": "3.3.9"
  },
  "babel": {
    "presets": ["env","stage-0"]
  },
  "jest": {
    "testMatch": [
      "**/test/**/*-test.js"
    ],
    "transform": {
      "^.+\\.jsx|.js?$": "babel-jest"
    }
  }
}

I also moved the dependencies to not be devDependencies but it didn't help solving the issue. 我也将依赖关系改为不是devDependencies但这并没有帮助解决问题。

NPM build documentation: https://docs.npmjs.com/cli/build NPM构建文档: https : //docs.npmjs.com/cli/build

You could try adding a postinstall script. 您可以尝试添加postinstall脚本。 As documented in the npm docs npm docs中所述

postinstall: Run AFTER the package is installed. 安装后:在安装软件包后运行。

So the answer of @Olian04 send me in the right direction and dig a bit on the documentation. 因此,@ Olian04的答案将正确的信息发送给我,并在文档上做了一些介绍。 Indeed I had a misunderstanding regarding build as it is actually not a script but just a hook to the process stage. 确实,我对构建有一个误解,因为它实际上不是script而仅仅是过程阶段的一个hook

So seems the correct way of solving the compilation required in packages is a different process run by prepare . 因此,解决包中所需编译问题的正确方法似乎是由prepare运行的不同过程。 This is a script that documentation defines as: 这是一个脚本,文档定义为:

For build steps that are not platform-specific, such as compiling CoffeeScript or other languages to JavaScript, use the prepare script to do this, and make the required package a devDependency. 对于不是特定于平台的构建步骤(例如,将CoffeeScript或其他语言编译为JavaScript),请使用prepare脚本执行此操作,并将所需的程序包设为devDependency。

The prepare script will be run before publishing so that users can consume the functionality without requiring them to compile it themselves. 准备脚本将在发布之前运行,以便用户可以使用该功能而无需他们自己对其进行编译。 In dev mode (ie, locally running npm install), it'll run this script as well, so that you can test it easily. 在开发模式下(即在本地运行npm install),它也会运行此脚本,以便您可以轻松对其进行测试。

Example: 例:

{ "name": "web-threads",
  "description": "a delightfully fruity coffee varietal",
  "version": "1.2.3",
  "devDependencies": {
    "coffee-script": "~1.6.3"
  },
  "scripts": {
    "prepare": "coffee -o lib/ -c src/waza.coffee"
  },
  "main": "lib/waza.js"
}

As a summary use postinstall for things that need to happen locally to the installing computer/platform (but it will require all dependencies to be satisfied). 作为摘要,对于需要在安装计算机/平台本地进行的事情,请使用postinstall (但需要满足所有依赖性)。 Use prepare for process that are not platform dependant this will not require the user to have all the tools to trnspile the package and you will also not polute your repository. 使用不依赖平台的prepare过程,这将不需要用户拥有所有工具来打包程序包,并且您也不会污染存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM