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

通过npm引用flv.js,webpack打包之后包含ES6语法,并且未被压缩 #232

Closed
BeiKeJieDeLiuLangMao opened this issue Nov 16, 2017 · 10 comments

Comments

@BeiKeJieDeLiuLangMao
Copy link

因为WebPack中babel-loader一般的配置是不将/node_modules中的代码进行ES6->ES5转码,
而flv.js的package中"main": "./dist/flv.js",这个文件中包含ES6语法,
这就导致了项目转码之后仍然后ES6语法,并在之后的压缩过程中抛出“语法不支持”的异常,
其最终结果就是打包之后的vendor.js中既包含ES6语法的代码,而且没有进行压缩。
于是我将引用的方式由import flvjs from 'flv.js'变为 import flvjs from 'flv.js/dist/flv.min',这个问题就解决了。

@xqq
Copy link
Contributor

xqq commented Nov 16, 2017

"main": "./dist/flv.js"
这个是过了 babel 后经 browserify 打包出来的,纯 ES5

"module": "./src/flv.js"
这个才是 ES6 源文件入口

@idiotWu
Copy link

idiotWu commented Nov 16, 2017

webpack 2+ 会优先使用 pkg.module 入口,你可能需要在 babel-loader 的配置里加上:

{
  ...
  include: [
    // more accurate regex: /(node_modules\/[^\/]+)(?:.(?!node_modules))*$/
    require.resolve('flv.js').replace(/(node_modules\/[^\/]+).*/, '$1'),
  ],
}

其实比较建议 pkg.module 指向转译后的模块:jsforum/jsforum#5

@BeiKeJieDeLiuLangMao
Copy link
Author

看来是我搞错了,但是我提这个issue的目的也是想让,import引用的内容默认是ES5的代码,换句话说我也希望pkg.module 指向半编译后的模块,要不还会有别人踩到坑。

@jianzhou520
Copy link

被坑了,估计都会被摆一道。建议遵循一般的依赖引入的内容构建

@driver1998
Copy link

被坑了+1 建议issue置顶

@xqq xqq pinned this issue Mar 30, 2021
@xqq
Copy link
Contributor

xqq commented Jul 17, 2021

Should be fixed in new version v1.6.0

@tommytroylin
Copy link

tommytroylin commented Jul 28, 2021

Should be fixed in new version v1.6.0

@xqq

1.6.0 没有修复。
https://unpkg.com/[email protected]/dist/flv.js 第11行就是一个箭头函数

在升级 webpack5 的时候需要额外配置 output.environment
否则打包产物中 webpack 初始化的代码就会直接使用箭头函数等 es6+ 语法

文档
https://webpack.js.org/configuration/output/#outputenvironment

@xqq
Copy link
Contributor

xqq commented Jul 28, 2021

@tommytroylin 感谢指正,稍等

@xqq
Copy link
Contributor

xqq commented Jul 28, 2021

@tommytroylin Fixed in https://github.com/bilibili/flv.js/releases/tag/v1.6.1

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

No branches or pull requests

7 participants
@tommytroylin @xqq @idiotWu @jianzhou520 @BeiKeJieDeLiuLangMao @driver1998 and others