简体   繁体   English

将 Webpack UMD 转换为原生 ES 模块

[英]Convert Webpack UMD to Native ES Module

I'm trying to work strictly using native ES Modules without transpiling my own code but often times I will find a third party library that is packaged with Webpack and babel as a UMD which seems to be the most common format these days.我正在尝试严格使用原生 ES 模块而不转译我自己的代码,但经常我会找到一个第三方库,它与 Webpack 和 babel 一起打包为 UMD,这似乎是当今最常见的格式。

This doesn't import so well这不太好导入

import { mat4 } from 'https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix.js'

Results in结果在

SyntaxError: The requested module does not provide an export named 'mat4'语法错误:请求的模块不提供名为“mat4”的导出

I don't think UMD should really be called universal.我不认为 UMD 真的应该被称为通用的。

Sure I could import the src directly but then I have to take on whatever babel configuration they may have.当然我可以直接导入 src 但是我必须接受他们可能拥有的任何 babel 配置。 I'm trying to avoid transpiling any of my code and I just want to transpile all UMD node_modules to use within my code.我试图避免转译我的任何代码,我只想转译所有 UMD node_modules以在我的代码中使用。

What I'm looking for is the opposite of this: https://www.npmjs.com/package/babel-plugin-transform-es2015-modules-umd我正在寻找的与此相反: https : //www.npmjs.com/package/babel-plugin-transform-es2015-modules-umd

I want to convert from UMD to ES.我想从 UMD 转换为 ES。 This way I could trust that the library has transpiled away anything non standard and I can import it as normal.这样我就可以相信图书馆已经转译了任何非标准的东西,我可以像往常一样导入它。 I've searched the web but I didn't find anything.我在网上搜索过,但没有找到任何东西。 I tried the commonjs to es modules plugin but it didn't work because the export declarations weren't at the top level.我尝试了 commonjs 到 es modules 插件,但它没有用,因为导出声明不在顶层。

Does anyone know if there is a plugin that does this or can anyone provide some ideas on how they deal with this type of scenario?有谁知道是否有一个插件可以做到这一点,或者任何人都可以提供一些关于他们如何处理这种情况的想法?

After 2 years of struggling with dealing with third party libraries when writing ESM code I think the folks over at snowpack.dev have a really nice solution.在编写 ESM 代码时与第三方库打交道 2 年之后,我认为snowpack.dev 上的人们有一个非常好的解决方案。

So I am going to leave this as an answer for anyone else that comes across this.因此,我将把它作为其他遇到此问题的人的答案。

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

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