webpack 在TypeScript项目中导入Canvg v4

hfwmuf9z  于 2024-01-08  发布在  Webpack
关注(0)|答案(1)|浏览(181)

我有一个旧的TypeScript项目,具有以下tsconfig:

  1. {
  2. "compilerOptions": {
  3. "baseUrl": "./src",
  4. "outDir": "build/dist",
  5. "module": "esnext",
  6. "target": "es5",
  7. "lib": ["es7", "dom"],
  8. ...

字符串
当我定期导入它(即import { Canvg } from 'canvg';)时,编译器会抱怨库中的ES6语法:

  1. Failed to compile.
  2. ./node_modules/canvg/dist/index.js
  3. Module parse failed: Unexpected token (3822:16)
  4. You may need an appropriate loader to handle this file type.
  5. | const dyY = Math.cos(-rotation) * dy;
  6. | segment.p0 = {
  7. | ...p0,
  8. | x: p0.x + dyX,
  9. | y: p0.y + dyY


我试图将库作为静态文件独立导入,但我只能找到v3,并且无法为我需要的文件工作,而latest v4 demo工作得很好。
有什么办法能让我脱身吗?谢谢!

8iwquhpp

8iwquhpp1#

我最终使用这个Webpack配置生成了v4单个bundle文件:

  1. const path = require('path');
  2. module.exports = {
  3. entry: './index.js', // Adjust this path to your module's entry point
  4. output: {
  5. path: path.resolve(__dirname, 'dist'),
  6. filename: 'canvg.js',
  7. library: 'Canvg', // This is the name you'll use to access the module from the window object
  8. libraryTarget: 'window',
  9. },
  10. };

字符串
并公开了我需要的函数:

  1. import { Canvg } from 'canvg';
  2. export const fromString = Canvg.fromString;


另外,v3对我不起作用的原因是我没有在从画布中提取图像之前等待渲染。

  1. await v.render();

展开查看全部

相关问题