我正在嘗試將 PDF 生成添加到作為 AWS Lambda 函式實作的郵件腳本中。它在本地運行良好,等等。當我用 webpack 4 打包它時,它不起作用。
我的 webpack 配置如下:
'use strict';
const webpack = require('webpack');
const path = require('path');
module.exports = {
node: {
__dirname: true
},
entry: './src/index.js',
output: {
library: 'mylib',
libraryTarget: 'commonjs2'
},
resolve: {
extensions: ['.json', '.jsx', '.js', '.mjs'],
alias: {
'tiny-lru': path.join(
process.cwd(),
'./node_modules/tiny-lru/lib/tiny-lru.cjs.js'
),
// pdfkit: 'pdfkit/js/pdfkit.js',
'unicode-properties': 'unicode-properties/unicode-properties.cjs.js'
}
},
target: 'node',
optimization: {
minimize: true
},
externals: [
'aws-sdk',
'pino-pretty',
'uglify-es',
'uglify-es/package.json'
],
plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/),
new webpack.IgnorePlugin(/^pg-native$/)
],
module: {
rules: [
{
test: /unicode-properties[\/\\]unicode-properties/,
loader: 'transform-loader?brfs'
},
// { test: /pdfkit[/\\]js[/\\]/, loader: 'transform-loader?brfs' },
{ test: /fontkit[\/\\]index.js$/, loader: 'transform-loader?brfs' },
// {
// test: /linebreak[\/\\]src[\/\\]linebreaker.js/,
// loader: 'transform-loader?brfs'
// }
{ test: /\.mjs$/, include: /node_modules/, type: "javascript/auto"}
]
}
};
當我構建這個東西時,它會出錯:
ERROR in ./node_modules/fontkit/dist/module.mjs 7463:26
Module parse failed: Unexpected token (7463:26)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
我能夠為 lambda 構建的方式是通過fontkit外部制作。它編譯,但不起作用。正如預期的那樣,它在運行時出錯:Runtime.ImportModuleError: Error: Cannot find module 'fontkit'".
我也試過{ test: /fontkit[\/\\]index.js$/, loader: 'transform-loader?brfs' }了{ test: /\.mjs$/, include: /node_modules/, type: "javascript/auto"},到目前為止沒有任何區別。
有什么建議嗎?
uj5u.com熱心網友回復:
我最終添加了以下內容:
layers:
Pdfkit:
name: Pdfkit
compatibleRuntimes:
- nodejs16.x
description: Required for Pdfkit
package:
artifact: layer/pdfkit-layer.zip
custom:
并將所有內容安裝在一個 zip 中,同時在 webpack 配置中將這些庫標記為外部。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523269.html
上一篇:重大變化:webpack<5用于包含node.js的polyfills用于“timers-browserify”
下一篇:我正在嘗試使用Python在VisualStudioCode中運行一個簡單的列印腳本,但終端或輸出中沒有顯示任何內容
