简体   繁体   English

转译 JS 代码库的最佳实践:包含在 git 中还是在安装过程中生成?

[英]Best practices with transpiled JS codebase: included in git or generated during installation process?

I'm creating a JS library (using Babel and Webpack) for:我正在创建一个 JS 库(使用 Babel 和 Webpack):

  • node.js节点.js
  • the browser.浏览器。

Is it recommended to include the transpiled files from Babel and webpack in the codebase?是否建议在代码库中包含来自 Babel 和 webpack 的转译文件? Since, at least for the backend side, another project installing that library might not have Babel (particularly to avoid issue like SyntaxError: Cannot use import statement outside a module )因为,至少在后端方面,安装该库的另一个项目可能没有 Babel(特别是为了避免诸如SyntaxError: Cannot use import statement outside a module

I was thinking of generating theses distributions file during the NPM installation process, but I don't think it's a good idea to move babel and webpack from devDependencies to dependencies in the packages.json .我想在NPM安装过程中产生的论文分布的文件,但我不认为这是一个好主意,此举巴贝尔的WebPack从devDependenciesdependenciespackages.json

The project structure is looking at this:项目结构是这样看的:

project-directory
|--dist
|  |--index.js
|--node_modules
|--src
|  |--index.js
|--package.json
|--.babelrc

After playing with npm publish , I think I found the right way.在玩过npm publish ,我想我找到了正确的方法。

The codebase should not contain the dist/ directory (excluded in .gitignore ).代码库不应包含dist/目录(排除.gitignore )。

When publishing, npm publish will create a package containing your dist/ directory (like git, you have a .npmignore to exclude elements you don't want in your package).发布时, npm publish将创建一个包含您的dist/目录的包(如 git,您有一个.npmignore来排除包中不需要的元素)。

In the packages.json , the main property should point to the main transpiled .js file.packages.jsonmain属性应该指向主要的转译的.js 文件。 Adding a prepack script will ensure that your transpiled code is up to date before publishing a new package.添加prepack脚本将确保您transpiled代码是最新发布的新包之前。

{
  "main": "dist/index.js",
  "module": "src/index.js",
  "scripts": {
    "prepack": "npm run build",
    "build": "babel src --out-dir dist"
  },
  "devDependencies": {
    "@babel/cli": "^7.7.5",
    "@babel/core": "^7.7.5",
    "@babel/node": "^7.7.4",
    "@babel/preset-env": "^7.7.6"
  }
}

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

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