【发布时间】:2018-08-30 22:16:02
【问题描述】:
我想使用第 3 方插件,但无法使用我的 webpack 配置加载它,因为 vue-loader 配置不正确(我认为) 导入项目文件夹中我自己的组件是可行的。
import Paginate from 'vuejs-paginate/src/components/Paginate.vue'
当尝试从包中导入 .vue 文件时,我收到此错误:
(function (exports, require, module, __filename, __dirname) { <template>
SyntaxError: Unexpected token <
所以我认为如果我导入 node_modules 文件夹中的包,vue-loader 不起作用。
这是我的 webpack 配置:
const path = require('path')
const webpack = require('webpack')
const vueConfig = require('./vue-loader.config')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const nodeExternals = require('webpack-node-externals')
const isProd = process.env.NODE_ENV === 'production'
module.exports = {
devtool: isProd
? false
: '#cheap-module-source-map',
output: {
path: path.resolve(__dirname, '../dist'),
publicPath: '/dist/',
filename: '[name].[chunkhash].js'
},
resolve: {
alias: {
'public': path.resolve(__dirname, '../public')
}
},
module: {
noParse: /es6-promise\.js$/, // avoid webpack shimming process
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules\/,
query: {
plugins: ['transform-runtime']
}
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url-loader',
options: {
limit: 10000,
name: '[name].[ext]?[hash]'
}
},
{
test: /\.css$/,
use: isProd
? ExtractTextPlugin.extract({
use: 'css-loader?minimize',
fallback: 'vue-style-loader'
})
: ['vue-style-loader', 'css-loader']
}
]
},
performance: {
maxEntrypointSize: 300000,
hints: isProd ? 'warning' : false
},
plugins: isProd
? [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false }
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new ExtractTextPlugin({
filename: 'common.[chunkhash].css'
})
]
: [
new FriendlyErrorsPlugin()
]
}
vue-loader.config.js
module.exports = {
extractCSS: process.env.NODE_ENV === 'production',
preserveWhitespace: false,
postcss: [
require('autoprefixer')({
browsers: ['last 3 versions']
})
]
}
node_modules 被排除在外,但不应该只为 babel-loader 排除,而不是 vue 加载器本身吗?
----编辑
我正在使用SSR,作者本人在此issue中声明
@johnRivs 非常感谢。 @sbefort 您可以通过以下方式导入库 从'vuejs-paginate/src/components/Paginate'导入分页;为了 固态硬盘。有任何问题请重新打开此问题。
【问题讨论】: