Babel.js 在编译的pdfjs-dist模块中缺少对象初始化

vsnjm48y  于 2024-01-04  发布在  Babel
关注(0)|答案(1)|浏览(216)

我正在开发一个vscode扩展,需要从提供的PDF中提取文本。该扩展在开发过程中工作正常,但在导出到.vsix后我遇到了问题。深入挖掘后,我发现由于pdfjs-dist使用的vscode is an electron app it will not support .mjs
根据这个答案,我开始使用babel-loader转译pdfjs-dist。
在测试编译模块时,我遇到了一些对象没有初始化的问题。
例如
/node_modules/pdfjs-dist/build/pdf.mjs中,

const GlobalWorkerOptions = Object.create(null);
GlobalWorkerOptions.workerPort = null;
GlobalWorkerOptions.workerSrc = "";

字符串
在编译的pdfjs.bundle.js中,我没有看到此初始化
当我尝试使用模块pdfjs.bundle.js时,这会导致问题

const pdfjsLib = await import('../dist/pdfjs.bundle.js');

pdfjsLib.GlobalWorkerOptions.workerSrc = "pdfjs-dist/build/pdf.worker.mjs";

const pdfData = new Uint8Array(fs.readFileSync(pdfPath));

const pdfDocument = await pdfjsLib.getDocument({ data: pdfData }).promise;


我得到

TypeError: Cannot set properties of undefined (setting 'workerSrc')


注解掉pdfjsLib.GlobalWorkerOptions.workerSrc = "pdfjs-dist/build/pdf.worker.mjs";

TypeError: pdfjsLib.getDocument is not a function

Package 脚本./src/pdfjsWrapper.js

import 'pdfjs-dist';

export default pdfjs-dist;

webpack.js

const path = require('path');

module.exports = {
  mode: 'development',
  target: 'node', // for VS Code extensions
  entry: {
    pdfjs:'./src/pdfjsWrapper.js' 
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    library: {
      name: 'pdfjsLib',
      type: 'umd', // Universal Module Definition
    },
    libraryTarget: 'commonjs2'

  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        
        exclude: (modulePath) => {
            // Exclude 'vscode' module
            if (modulePath.includes('node_modules/vscode')) {
              return true;
            }
  
            // Exclude 'fs' module
            if (modulePath.includes('node_modules/fs')) {
              return true;
            }
  
            // Exclude 'weaviate-ts-client' module
            if (modulePath.includes('node_modules/weaviate-ts-client')) {
              return true;
            }
  
            // Include all other modules
            return false;
          },
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            compact: false // to have optimised code
          }
        }
      }
    ]
  }
};

.babelrc

{
    "presets": ["@babel/preset-env"]
}

zzzyeukh

zzzyeukh1#

我通过将pdfjs-dist降级到3.11.174解决了这个问题

相关问题