我们在项目中使用webpack(Node.js,服务器端只有浏览器)并将所有内容捆绑到一个JS文件中。
我安装了一个Node包bhttp
,它取决于errors
包。 errors
包有一些静态文件(CSS和HTML),它们似乎没有与编译/捆绑代码打包在一起。因此,当我运行我的代码时,我收到此错误:
"errorMessage": "ENOENT, no such file or directory '//static/error.css'"
errors
包在代码中加载了两个静态资源/文件:/static/error.css
和/static/error.html
,如下所示:
errorCss = fs.readFileSync(__dirname + '/static/error.css', 'utf8');
errorHtml = fs.readFileSync(__dirname + '/static/error.html', 'utf8');
我的webpack配置文件是:
'use strict';
var path = require('path');
var fs = require('fs');
var webpack = require('webpack');
function readDir(filePath, upload, besePath, result) {
result = result || {};
besePath = besePath || filePath;
fs.readdirSync(filePath).forEach(function (filename) {
var currentPath = path.join(filePath, filename);
var stat = fs.statSync(currentPath);
var dest;
var packageConfig;
if (stat.isDirectory()) {
return readDir(currentPath, upload, besePath, result);
}
if (filename === 'handler.js') {
dest = currentPath.replace(filename, 'index');
try {
fs.accessSync(currentPath.replace(filename, 'config.json'), fs.R_OK);
} catch (e) {
return;
}
packageConfig = require(currentPath.replace(filename, 'config.json'));
if (upload && packageConfig.upload !== true) {
return;
}
dest = dest.replace(path.resolve(besePath + '/'), '');
result[dest] = currentPath;
}
});
return result;
}
module.exports = function (options) {
options = options || {};
var onlyWithUploadFlag = options.upload;
var env = options.env || 'dev';
var distFolder = options.dist || 'dist';
return {
entry: readDir(path.resolve(__dirname, './functions'), onlyWithUploadFlag),
output: {
path: path.join(__dirname, distFolder),
library: '[name]',
libraryTarget: 'commonjs2',
filename: '[name].js'
},
target: 'node',
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015'],
plugins: ['syntax-flow', 'transform-flow-strip-types']
}
},
{
test: /\.json$/,
loader: 'json'
}
]
},
resolve: {
root: [
path.resolve('./')
],
modulesDirectories: ['/', 'common', 'node_modules']
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
BUILD_ENV: '"' + env + '"'
}
})
],
externals: {
'dynamodb-doc': true,
'aws-sdk': true
}
};
};
任何想法我该如何解决这个问题?
(我是webpack的新手,似乎无法弄清楚如何使用bundle打包这些静态文件。)
感谢。