简体   繁体   English

Webpack Fabric external 正在解析为 undefined

[英]Webpack Fabric external is resolving to undefined

I'm setting up a project (typescript, webpack) with a couple of js libraries, configured as externals in webpack.我正在使用几个 js 库设置一个项目(打字稿、webpack),在 webpack 中配置为外部。 They should not be part of the bundle, instead provided by script tags within the html.它们不应该是包的一部分,而是由 html 中的脚本标签提供。

But when trying to use them in my class, they resolve to undefined.但是当试图在我的课堂上使用它们时,它们解析为未定义。

Fabric configured as an external in webpack is resolving to undefined在 webpack 中配置为外部的 Fabric 解析为 undefined

An error occurs when trying to set up the fabric js library as an external within a (typescript + webpack ) project.尝试将结构 js 库设置为 (typescript + webpack) 项目中的外部时发生错误。 Fabric should not be bundled in the output file since it will be the responsibility of the consumer to provide (eg. through a browser script tag). Fabric 不应捆绑在输出文件中,因为它是消费者提供的责任(例如,通过浏览器脚本标签)。

Note: jQuery initially had an issue (as an external) but is now resolved, and works as expected.注意:jQuery 最初有一个问题(作为外部),但现在已解决,并按预期工作。 Fabric on the other hand does not.另一方面,织物没有。

fabric has been configured as an external so that it will not be included in the webpack bundle. fabric 已配置为外部,因此它不会包含在 webpack 包中。

Here's how...就是这样...

  1. Added as an external within the webpack.config.js在 webpack.config.js 中作为外部添加
...
  externals: {
    jquery: 'jQuery',
    fabric: 'fabric',
  },
...
  1. Installed the declaration files for both libraries安装了两个库的声明文件
npm install @types/jquery -D
npm install @types/fabric -D
  1. Added the libraries in public folder and index.html (since they must not be part of the app bundle)在 public 文件夹和 index.html 中添加了库(因为它们不能是应用程序包的一部分)
  <script src="js/lib/jquery.min.js"></script>
  <script src="js/lib/fabric.min.js"></script>
  1. Created a class App.ts, imported and implemented instances of these two libraries.创建了一个类App.ts,导入并实现了这两个库的实例。 (see App.ts) (见 App.ts)
import { fabric } from "fabric";
import $ from 'jquery';

fabric resolves to undefined within the class App.ts with the error:结构在类 App.ts 中解析为 undefined 并出现错误:

TypeError: Cannot read property 'Canvas' of undefined

Please don't recommend ProvidePlugin or installing Babel.请不要推荐 ProvidePlugin 或安装 Babel。

More about webpack "externals": https://webpack.js.org/configuration/externals/更多关于 webpack“externals”: https ://webpack.js.org/configuration/externals/


Update #1更新 #1

jQuery is now working as an external library. jQuery 现在作为一个外部库工作。 I was not referencing the actual jquery global "jQuery" in the externals setup.我没有在外部设置中引用实际的 jquery 全局“jQuery”。 I had "JQuery" (with a capital J).我有“JQuery”(大写 J)。 That's now resolved and jquery is working.现在已经解决了,jquery 正在工作。 Thanks @Aluan谢谢@Aluan

Fabric on the other hand seems to be a different issue altogether.另一方面,面料似乎完全是一个不同的问题。

What you're looking for is called shimming .您要查找的内容称为shimming Webpack docs cover this extensively here: https://webpack.js.org/guides/shimming/ Webpack 文档在这里广泛涵盖了这一点: https ://webpack.js.org/guides/shimming/

Edit to add example:编辑以添加示例:

In your webpack.config.js plugins array:在你的webpack.config.js plugins数组中:

    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        })
    ]

EDIT: I pulled down your code and got it working.编辑:我拉下你的代码并让它工作。 Here are the steps:以下是步骤:

  1. ts-loader chokes on shims, so use babel's @babel/preset-typescript -- otherwise you'll need to find a way to tell the ts compiler to ignore them. ts-loader阻塞在垫片上,所以使用 babel 的@babel/preset-typescript —— 否则你需要找到一种方法来告诉 ts 编译器忽略它们。 This will get you started:这将使您开始:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-typescript core-js@3
  1. In your root, create a file called .babelrc and add the following:在您的根目录中,创建一个名为.babelrc的文件并添加以下内容:
{
    "presets": [
        "@babel/preset-typescript",
      [
        "@babel/env",
        {
          "targets": {
            "edge": "17",
            "firefox": "60",
            "chrome": "67",
            "safari": "11.1"
          },
          "useBuiltIns": "usage",
          "corejs": "3"
        }
      ]
    ]
  }
  1. Add this to your webpack.config.js :将此添加到您的webpack.config.js
  plugins: [
    new ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      fabric: "fabric"
    })
  ]
  1. Also update ts-loader , changing it to babel-loader .同时更新ts-loader ,将其更改为babel-loader

  2. Now in your code, you'll need to prefix your shimmed libraries with window :现在在您的代码中,您需要使用window前缀您的垫片库:

    constructor(private readonly selector: string, canvasHeight: number, canvasWidth: number) {
      
      window.$(`#${selector}`).replaceWith(`<canvas id="${selector}" height=${canvasHeight} width=${canvasWidth}> </canvas>`);

      this.canvas = new window.fabric.Canvas(`${selector}`, { selection: false });

    }

It turns out that the issue with fabric is from fabric itself!原来,面料的问题出在面料本身! The reason fabric is resolving to undefined (when being configured as an external on webpack) is related to the way that fabric exposes its library for consumption. Fabric 解析为 undefined(当在 webpack 上配置为外部时)的原因与 Fabric 公开其库以供使用的方式有关。 It's an issue they need to fix.这是他们需要解决的问题。

I've added an issue on the official fabric github page我在官方的fabric github页面上添加了一个问题

But there is a quick solution for us.但是我们有一个快速的解决方案。 Just import using CommonJS like this:只需像这样使用 CommonJS 导入:

const fabric = require('fabric');

Now it works!现在它起作用了!

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

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