简体   繁体   English

如何使用fabric.js v2创建jspm包

[英]How to create jspm bundle with fabric.js v2

I'm trying to create a javascript bundle with JSPM that includes fabric.js version 2. Alas, performing jspm bundle fails. 我正在尝试使用包含fabric.js版本2的JSPM创建一个JavaScript捆绑包。可惜,执行jspm捆绑包失败。

$ jspm bundle src/main --minify --inject
       Building the bundle tree for src/main...

err  Error on fetch for @empty/lib/jsdom/living/generated/utils.js at file:///Users/dkoerner/projects/JSPMFabricTestCase/@empty/lib/jsdom/living/generated/utils.js
    Loading npm:fabric@2.0.0-rc.4/dist/fabric.js
    Loading npm:fabric@2.0.0-rc.4.js
    Loading src/bootstrap.js
    Loading src/main.js
    Error: ENOENT: no such file or directory, open '/Users/dkoerner/projects/JSPMFabricTestCase/@empty/lib/jsdom/living/generated/utils.js'

I have created a test project ( https://github.com/dkoerner85/JSPMFabricTestCase ) following the jspm and fabric examples and verified that bundle creation works with the current fabric stable release v1.7.22. 我已根据jspm和fabric示例创建了一个测试项目( https://github.com/dkoerner85/JSPMFabricTestCase ),并验证了捆绑包的创建可与当前Fabric稳定版v1.7.22一起使用。 Raising the version to the release candidate v2.0.0-rc4 however breaks bundling. 但是,将版本提高到候选版本v2.0.0-rc4会破坏捆绑。

I am fairly new to javascript app development and hence do not understand the reason why this fails and how to repair it. 我对javascript应用程序开发还很陌生,因此不了解失败的原因以及如何修复。 I am grateful for any pointers or explanation. 感谢您提出任何指示或解释。

npm: v5.5.1 node: v8.9.3 npm:v5.5.1节点:v8.9.3

Solution

Add an override section to your project's package.json : 在项目的package.json添加覆盖部分:

"overrides": {
  "npm:fabric@2.0.0-rc.4": {
    "map": {
      "canvas": "@empty",
      "fs": "@empty",
      "jsdom/lib/jsdom/living/generated/utils": "@empty",
      "jsdom/lib/jsdom/utils": "@empty",
      "jsdom": "@empty",
      "http": "@empty",
      "https": "@empty",
      "xmldom": "@empty",
      "url": "@empty"
    }
  }
}

Insight 洞察力

After reading about jspm and systemJS, I have found a solution to this problem. 阅读有关jspm和systemJS的文章后,我找到了解决此问题的方法。 The critical code is in the fabric.js and package.json files of the fabric package. 关键代码在fabric包的fabric.js和package.json文件中。

dist/fabric.js dist / fabric.js

...
fabric.document = require('jsdom').jsdom(decodeURIComponent('%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3C%2Fhead%3E%3Cbody%3E%3C%2Fbody%3E%3C%2Fhtml%3E'),{ features: {FetchExternalResources: ['img']}});
fabric.jsdomImplForWrapper = require('jsdom/lib/jsdom/living/generated/utils').implForWrapper;
fabric.nodeCanvas = require('jsdom/lib/jsdom/utils').Canvas;
fabric.window = fabric.document.defaultView;
DOMParser = require('xmldom').DOMParser;
...

package.json package.json

...
"browser" : {
   "canvas": false,
   "fs":      false,
   "jsdom":   false,
   "jsdom/lib/jsdom/living/generated/utils": false,
   "jsdom/lib/jsdom/utils": false,
   "http":   false,
   "https":   false,
   "xmldom":  false,
   "url": false
},
...

Now, when installing the package with jspm install npm:fabric@2.0.0-rc.4 , jspm will modify the require statements, resulting in the following code: 现在,当使用jspm install npm:fabric@2.0.0-rc.4软件包时, jspm install npm:fabric@2.0.0-rc.4 ,jspm会修改require语句,得到以下代码:

jspm_packages/npm/fabric@2.0.0-rc.4/dist/fabric.js jspm_packages/npm/fabric@2.0.0-rc.4/dist/fabric.js

...
fabric.document = require('@empty').jsdom(decodeURIComponent('%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3C%2Fhead%3E%3Cbody%3E%3C%2Fbody%3E%3C%2Fhtml%3E'), {features: {FetchExternalResources: ['img']}});
fabric.jsdomImplForWrapper = require('@empty/lib/jsdom/living/generated/utils').implForWrapper;
fabric.nodeCanvas = require('@empty/lib/jsdom/utils').Canvas;
fabric.window = fabric.document.defaultView;
DOMParser = require('@empty').DOMParser;
...

When trying to create a bundle, jspm will stumble over the '@empty/lib/jsdom/living/generated/utils' require statement. 尝试创建捆绑包时,jspm会偶然遇到'@empty/lib/jsdom/living/generated/utils'语句。

Error: ENOENT: no such file or directory, open '/Users/dkoerner/projects/JSPMFabricTestCase/@empty/lib/jsdom/living/generated/utils.js'

jspm treated the jsdom entries first as required by the package.json , thereby failing to recognize the more detailled entry 'jsdom/lib/jsdom/living/generated/utils' . jspm首先根据package.json要求处理jsdom条目,从而无法识别更详细的条目'jsdom/lib/jsdom/living/generated/utils' It is necessary to provide the map in the correct order - long paths before short paths. 必须以正确的顺序提供地图-长路径先于短路径。

This can be achieved with a local override as depicted in the solution above. 如上面的解决方案所示,可以使用局部替代来实现。 Please note that jsdom is ordered below @empty/lib/jsdom/living/generated/utils in the override, as opposed to the original package.json . 请注意,与原始package.json相对, jsdom@empty/lib/jsdom/living/generated/utils位于优先级中。

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

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