Node.js 告诉webpack显式包含文件夹
我正在使用serverless.com将我的node js应用程序部署到AWS 我需要包括dirNode.js 告诉webpack显式包含文件夹,node.js,webpack,serverless-framework,aws-serverless,Node.js,Webpack,Serverless Framework,Aws Serverless,我正在使用serverless.com将我的node js应用程序部署到AWS 我需要包括dir/src/email templates/**,其中包括电子邮件模板模块使用的.ejs和.scss文件 例如: import * as EmailTemplate from 'email-templates'; template = EmailTemplate.EmailTemplate('./email-templates/default'); 以下是我的网页的外观: /* eslint-disa
/src/email templates/**
,其中包括电子邮件模板模块使用的.ejs和.scss文件
例如:
import * as EmailTemplate from 'email-templates';
template = EmailTemplate.EmailTemplate('./email-templates/default');
以下是我的网页的外观:
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const isLocal = slsw.lib.webpack.isLocal;
module.exports = {
mode: isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
externals: [nodeExternals(), "aws-sdk"],
devtool: 'source-map',
resolve: {
extensions: [ '.js', '.jsx', '.json', '.ts', '.tsx' ]
},
output: {
libraryTarget: 'commonjs2',
path: path.join(__dirname, '.webpack'),
filename: '[name].js'
},
target: 'node',
module: {
rules: [
{ test: /\.ejs$/, loader: 'ejs-loader?variable=data' },
{
// Include ts, tsx, js, and jsx files.
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: [
{
loader: 'cache-loader',
options: {
cacheDirectory: path.resolve('.webpackCache')
}
},
'babel-loader'
]
},
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
]
},
plugins: [new ForkTsCheckerWebpackPlugin()]
};
当我运行slsdeploy--stage=dev
部署EmailTemplates实例时,找不到该文件夹。以下是错误:
Failed to render email { [Error: ENOENT: no such file or directory, stat './email-templates/notification']
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: './email-templates/notification' }
因此,看起来webpack并没有对其进行打包
是否有明确要求webpack包含文件夹的方法?webpack只需使用require(…)生成引用文件。如果在任何.js文件中都不需要您的文件(.ejs),则不会包含该文件 此外,webpack几乎总是用于将多个.js文件传输或构建到一个缩小或压缩的javascript文件中。ejs文件是静态的,因此不需要缩小或压缩
正如评论所说,在webpack结果之后添加ejs文件作为文件夹上述评论之一,我认为这是您寻找的最“规范的webpack”方式: Cop[y]将已存在的单个文件或整个目录复制到生成目录 安装后:
npm安装复制网页包插件——保存开发
在您的网页中需要它
配置文件:
constcopyplugin=require('copy-webpack-plugin')
并更新配置中的插件属性:
plugins: [
new CopyPlugin({
patterns: [
// ALL .scss files emitted under '<top-level-dist>/email-templates', maintaining sub-folder structure
{
from: 'src/email-templates/**/*.scss',
to: './email-templates',
},
// ALL .ejs files copied under '<top-level-dist>/email-templates', maintaining sub-folder structure
{
from: 'src/email-templates/**/*.ejs',
to: './email-templates',
},
],
}),
new ForkTsCheckerWebpackPlugin()
]
插件:[
新CopyPlugin({
模式:[
//在“/email templates”下发出的所有.scss文件,维护子文件夹结构
{
发件人:'src/email templates/***.scss',
收件人:'./电子邮件模板',
},
//在“/email templates”下复制的所有.ejs文件,维护子文件夹结构
{
发件人:“src/email templates/***.ejs”,
收件人:'./电子邮件模板',
},
],
}),
新的ForkTsCheckerWebpackPlugin()
]
我对webpack不太熟悉,但您不应该使用它来包含文件/目录吗?您希望webpack处理条目中未引用的ejs和sass文件,对吗?那么为什么不为它们添加单独的条目呢?我已经使用文件加载器成功地将它们包括进来,并明确声明const defaultHtml=require('../email templates/default/html.ejs')代码>。这是可行的,但对于我拥有的每个ejs和scss文件,我都必须包含这一行,这并不好。还有其他方法吗?您已经看过了吗?您可以尝试使用glob模式。尝试类似于webpack glob entry
或任何类似的模块。