【发布时间】:2017-10-01 07:23:16
【问题描述】:
我想以编程方式打印一些 pdf 文档。我现在正在尝试几个小时让 this PDFkit library 与 webpack 一起工作。
我已经离开了:
Can't resolve 'fs' in ..
到
fs.readFileSync is not a function
然后到警告
[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".
然后去
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 时遇到问题——我尝试了所有能找到的解决方案。它能有多难?这里实际发生了什么?任何见解都表示赞赏,谢谢。
【问题讨论】:
标签: webpack node-pdfkit