首页 文章

安装在node_modules中时无法加载vue文件

提问于
浏览
0

我想使用第三方插件,但无法使用我的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您可以通过导入Paginate从'vuejs-paginate / src / components / Paginate'导入库;对于SSR . 有任何问题请重新打开此问题 .

1 回答

  • 1

    您不想进入NPM包内部 .

    当你这样做时:

    import Paginate from 'vuejs-paginate/src/components/Paginate.vue'
    

    注意你依赖于 vuejs-paginate 的内部( src 文件夹存在?)等等 .

    属性方式是 install 包并导入包暴露的内容 .


    例如,要在shell上运行,请运行:

    npm install vuejs-paginate --save
    

    然后在您的代码中,导入和使用如下:

    import Paginate from 'vuejs-paginate'
    Vue.component('paginate', Paginate)
    

    并且WebPack将独立地处理其余的事情(至少关于 vuejs-paginat 本身)来自 vue-loader . 并且,是的, vue-loader 应排除 node_modules ,因为您不希望它处理您导入的每个包的每个soucve .vue 文件 .

相关问题