Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel 7 转码的正确姿势 #4

Open
deepfunc opened this issue Aug 16, 2019 · 13 comments
Open

Babel 7 转码的正确姿势 #4

deepfunc opened this issue Aug 16, 2019 · 13 comments
Labels

Comments

@deepfunc
Copy link
Owner

如果你对 本篇 有任何的疑问和建议,欢迎在这里提出来。

@jiaming743
Copy link

请问babel7如何配置编译提供给别人用的库,我有一个库,之前转码很正常,昨天更新了一下包,就出问题了和项目的polyfill冲突了,想请教一下详细的配置去编译库

@deepfunc
Copy link
Owner Author

请参考我的建议,你的库只用 runtime 和 plugin-transform-runtime 来转码,不要用 polyfill 就行了

@jiaming743
Copy link

库的babel相关依赖现在是这样的,除了@babel/register是配合测试库用的,其他都是我这两天尝试编译安装上的,针对这个配置,您觉得应该移除哪些依赖,以及更新哪些配置呢?

devDependencies

  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/plugin-transform-runtime": "^7.5.5",
    "@babel/preset-env": "^7.5.5",
    "@babel/register": "^7.4.4",
  }

dependencies

{
    "@babel/runtime": "^7.5.5",
    "core-js": "^3.1.4"
 }

.babelrc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "entry",
        "corejs": "3"
      }
    ]
  ]
}

另外,我是否需要在我的库的入口文件内引入

import 'core-js/stable';
import 'regenerator-runtime/runtime';

库发布前会使用babel -d lib/ src/命令进行转码

感谢

@deepfunc
Copy link
Owner Author

现在工作有点忙,我一会儿会回复你。@jiaming743

@jiaming743
Copy link

@deepfunc 好的 谢谢

@jiaming743
Copy link

库项目地址

我一般会使用babel把src转码至lib,并使用browserify打包一个包含所有依赖的UMD版供浏览器端通过script标签直接引用使用

@deepfunc
Copy link
Owner Author

package.json

"dependencies": {
    "@babel/runtime": "..."
  },
"devDependencies": {
    "@babel/cli": "...",
    "@babel/core": "...",
    "@babel/preset-env": "...",
    "@babel/plugin-transform-runtime": "..."
  },

.babelrc

{
  "presets": [
    ["@babel/preset-env"]
  ],
  "plugins": ["@babel/plugin-transform-runtime"]
}

依赖就这样就可以了,如果你需要用 state-x,参考我那篇文章的做法。

import 'core-js/stable';
import 'regenerator-runtime/runtime';

这个不要用,这个是放在业务项目里面用的。如果你发布的是库的话,不要加这个,不然有全局污染。
也就是说,你的业务项目引用了你发布的库,然后在业务项目里面加上上面这个就行了。
@jiaming743

@jiaming743
Copy link

对于Array.prototype.includes之类的实例方法,transform-runtime好像不会做处理,这种情况是否需要考虑呢? @deepfunc

@deepfunc
Copy link
Owner Author

这个就是要交给业务项目处理呀,所以业务项目加入 polyfill 就可以了。因为你的库肯定是要给业务项目使用的。库一加就全局污染了

@jiaming743
Copy link

了解了,谢谢!

@deepfunc
Copy link
Owner Author

不客气,觉得有用的话给个 star 哈,谢谢~

@jiaming743
Copy link

babelrc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "entry",
        "corejs": "3"
      }
    ]
  ]
}

另外问下这种配置是否会产生全局污染,好像是自动按需引入的

@deepfunc
Copy link
Owner Author

这个都是在业务项目里面用的,库里不需要,我上面有写到。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants