我正在尝试构建Webpack文件,当我需要添加“节点”目标时出现了问题,因为它导致读取某些文件的“ fs”功能出现问题。然后,我将配置分为网络和节点。但是我不确定自己是否做对了。
我目前正在尝试分离这些webpack配置。
我的webpack.config.js:
const VueLoaderPlugin = require('vue-loader/lib/plugin')
var path = require('path');
const webConfig = {
entry: './src/main.js',
mode: 'development',
output: {
path: path.resolve(__dirname, './dist/'),
filename: 'bundle.js'
},
resolve: {
extensions: ['.css','.jpg','.png'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve(__dirname, './src/'),
}
},
module: {
rules: [
{ test: /\.less$/, loader: "style-loader!css-loader!less-loader" },
{ test: /\.png/, loader: "url-loader?limit=100000&mimetype=image/png" },
{ test: /\.gif/, loader: "url-loader?limit=100000&mimetype=image/gif" },
{ test: /\.jpg/, loader: "file-loader" },
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' },
{ test: /\.css$/, use: ['vue-style-loader','css-loader']}
]
}
}
const nodeConfig = {
entry: './src/main.js',
mode: 'development',
output: {
path: path.resolve(__dirname, './dist/'),
filename: 'bundle.node.js'
},
resolve: {
extensions: ['.js', '.vue','.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve(__dirname, './src/'),
}
},
target: 'node',
node: { fs: 'empty' },
module: {
rules: [
{ test: /\.vue$/, loader: 'vue-loader' },
{ test: /\.js$/, loader: 'babel-loader' },
{ test: /\.css$/, use: ['vue-style-loader','css-loader']}
// this will apply to both plain `.css` files
// AND `<style>` blocks in `.vue` files
]
},
plugins: [
new VueLoaderPlugin()
]
}
但是我有这些错误。第一个问题是js文件有问题,即使我没有采取任何措施。 我在做什么错了?
ERROR in ./node_modules/axios/index.js
Module not found: Error: Can't resolve './lib/axios' in 'C:\Users\sdr\Documents\deficarte\node_modules\axios'
@ ./node_modules/axios/index.js 1:17-39
@ ./src/main.js
ERROR in ./node_modules/vue-style-loader/lib/addStylesClient.js
Module not found: Error: Can't resolve './listToStyles' in 'C:\Users\sdr\Documents\deficarte\node_modules\vue-style-loader\lib'
@ ./node_modules/vue-style-loader/lib/addStylesClient.js 7:0-41 57:15-27 69:15-27
@ ./node_modules/bootstrap/dist/css/bootstrap.min.css
答案 0 :(得分:1)
尝试
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
var path = require('path');
const webConfig = {
entry: './src/main.js',
mode: 'development',
output: {
path: path.resolve(__dirname, './dist/'),
filename: 'bundle.js'
},
resolve: {
extensions: ['.css','.jpg','.png','.js','.vue'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve(__dirname, './src/'),
}
},
module: {
rules: [
{ test: /\.less$/, loader: "style-loader!css-loader!less-loader" },
{ test: /\.png/, loader: "url-loader?limit=100000&mimetype=image/png" },
{ test: /\.gif/, loader: "url-loader?limit=100000&mimetype=image/gif" },
{ test: /\.jpg/, loader: "file-loader" },
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' },
{ test: /\.vue$/, loader: 'vue-loader' },
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }, //query: {presets: ['es2015']}
{ test: /\.css$/, use: ['vue-style-loader','css-loader']}
// this will apply to both plain `.css` files
// AND `<style>` blocks in `.vue` files
]
},
plugins: [
new VueLoaderPlugin()
]
}
const nodeConfig = {
entry: './src/main.js',
mode: 'development',
output: {
path: path.resolve(__dirname, './dist/'),
filename: 'bundle.node.js'
},
resolve: {
extensions: ['.js', '.vue','.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve(__dirname, './src/'),
}
},
target: 'node',
node: { fs: 'empty' },
module: {
rules: [
// { test: /\.json/, loader: "raw-loader" },
{ test: /\.vue$/, loader: 'vue-loader' },
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }, //query: {presets: ['es2015']}
{ test: /\.css$/, use: ['vue-style-loader','css-loader']}
// this will apply to both plain `.css` files
// AND `<style>` blocks in `.vue` files
]
},
plugins: [
new VueLoaderPlugin()
]
}
module.exports = [ webConfig, nodeConfig ];
// var webpackConfig = merge(baseWebpackConfig, {
// plugins: [
// // service worker caching
// new SWPrecacheWebpackPlugin({
// cacheId: 'my-vue-app',
// filename: 'src/service-worker.js',
// staticFileGlobs: ['dist/*/.{js,html,css}'],
// minify: true,
// stripPrefix: 'dist/',
// runtimeCaching: [
// {
// urlPattern: /^https:\/\/js\.api\.here\.com\/v3\/3.0\/mapsjs-core\.js\//,
// handler: 'cacheFirst'
// },
// {
// urlPattern: /^https:\/\/(\d+)\.media\.tumblr\.com\//,
// handler: 'cacheFirst'
// },
// ]
// })
// ]
// })