2017-05-03 4 views
2

일부 PDF 문서를 프로그래밍 방식으로 인쇄하려고합니다. 그리고 지금 webpack으로 this PDFkit library을 작업하기 위해 몇 시간 동안 노력하고 있습니다.webpack에서 PDFkit 라이브러리를 사용하는 방법은 무엇입니까?

내가 갔어요 형태 :

Can't resolve 'fs' in ..

[BABEL] Note: The code generator has deoptimised the styling of "E:/MyProjects/accountingsystem/node_modules/brotli/dec/dictionary-da 
ta.js" as it exceeds the max of "500KB". 

다음 경고에 다음

fs.readFileSync is not a function

require is not defined - 여기에 붙어 있습니다. 이 모든 오류는 도서관 자체에서 발생합니다.

난 그냥 파일이 - 코드 단지 한 줄에을 app.js입니다 :

const PDFDocument = require('pdfkit');

webpack.config.js는 다음과 같이 최종 보인다 내 :

module.exports = { 
// devtool: 'source-map', 

entry: './src/app.js', 
output: { 
    path: path.resolve (__dirname, "dist"), 
    filename: "bundle.js" 
}, 

// node: { 
//  console: true, 
//  fs: 'empty', 
//  net: 'empty', 
//  tls: 'empty' 
// }, 

// i've added 'target' - following the advice form some github comments. 
target: 'node', 
module : { 
    rules : [ 
     { test: /\.js$/, loader: 'babel-loader' }, 
     { 
      test : /\.html$/, 
      use : [ 'html-loader' ] 
     }, 

     // then i've added this 2 loaders also: 
     { test: /\.json$/, loader: 'json-loader' }, 
     { test: /pdfkit|png-js/, loader: "transform-loader?brfs" } 
    ] 
}, 
plugins:[ 
    new HtmlWebpackPlugin ({ 
      template : `src/app.html` 
    }) 
], 

};

이것은 말 그대로 1 행 앱이며, 지금 몇 시간 동안 벽을 치고 있습니다. 많은 사용자가 fs 코어 모듈과 webpack과 관련된 문제를 겪고있는 것을 보았습니다. 그리고 제가 찾을 수있는 모든 솔루션을 시도했습니다. 얼마나 힘들 수 있습니까? 여기 실제로 무슨 일이 일어나고있는거야? 어떤 통찰력이라도 고맙게 생각합니다. 고마워요.

답변

1

webpack에서 작동하도록 할 수 있습니다.

pdfmake은 webpack에서 작동하는 pdfkit의 래퍼입니다. 당신이 볼 수 있듯이, webpack.config.js 종류의 해키 : 개인적으로

https://github.com/bpampuch/pdfmake/blob/master/webpack.config.js

, 내가 JS 내장 파일을 제공 pdfmake, 작업 끝났다. 이렇게하면 webpack.config를 조정해야합니다. resolve.alias 필드를 사용하여 pdfmake 빌드 된 js 파일을 가리 킵니다.