【问题标题】:Webpack Vue component with css <style> tags fails to build with Module parse failed: Unexpected token带有 css <style> 标签的 Webpack Vue 组件无法使用 Module parse failed: Unexpected token 构建
【发布时间】:2020-09-13 02:47:30
【问题描述】:

从一个干净的 vue 项目开始,我在从 PrimeVue 构建 .vue 组件时遇到了问题。 这些是现成的组件,应该不会构建失败。

每次我尝试构建时,它都失败了,并且似乎在 CSS 样式开头的行指针失败。

ERROR in ./node_modules/primevue/components/slider/Slider.vue?vue&type=style&index=0&lang=css& (./node_modules/vue-loader/lib??vue-loader-options!./node_modules/primevue/components/slider/Slider.vue?vue&type=style&index=0&lang=css&) 340:0
Module parse failed: Unexpected token (340:0)
File was processed with these loaders:
 * ./node_modules/vue-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|
|
> .p-slider {
|   position: relative;
| }
 @ ./node_modules/primevue/components/slider/Slider.vue?vue&type=style&index=0&lang=css& 1:0-119 1:135-138 1:140-256 1:140-256
 @ ./node_modules/primevue/components/slider/Slider.vue
 @ ./node_modules/primevue/slider.js
 @ ./myproject/components/Test.js
 @ ./myproject/components/App.js
 @ ./myproject/main.js

这是我的 webpack 配置文件:

const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');

module.exports = {
    mode: 'development',
    entry: 'main.js',
    output: {
        filename: 'main.bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                use: 'vue-loader'
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin()
    ]
};

是什么导致了这个错误,因为我按照 PrimeVue 文档的说明正确导入了组件。

【问题讨论】:

    标签: vue.js webpack vue-loader primevue


    【解决方案1】:

    在 webpack 配置中设置规则以将 .vue 文件发送到 vue-loader 进行处理是不够的。

    您还需要指定如何处理.css 文件,然后这也将应用于.vue 文件中的标签。如果没有这条规则,它就不会知道如何处理&lt;style&gt; 块,即使您不打算使用其中的 .css 文件部分。

    使用以下内容更新webpack.config.js 的规则部分:

    rules: [
                {
                    test: /\.vue$/,
                    use: 'vue-loader'
                },
                // this will apply to both plain `.css` files
                // AND `<style>` blocks in `.vue` files
                {
                    test: /\.css$/,
                    use: [
                        'vue-style-loader',
                        'css-loader'
                    ]
                }
            ]
    

    还要确保 vue-style-loadercss-loader 安装在 package.json 中。

    更多信息可以在vue-loader文档的manual installation section找到,具体是下的代码示例'A more complete example webpack config will look like this'

    【讨论】:

      【解决方案2】:

      我强烈建议缓存 .vue 文件,因为它们会减慢您在大型项目中的构建时间。

      // snippet from https://github.com/unic/darvin-webpack-boilerplate/blob/master/webpack/settings/javascript-vue/index.js
      
      const {VueLoaderPlugin} = require('vue-loader');
      const webpack = require('webpack');
      
      const path = require('path');
      const ROOT_PATH = process.cwd();
      
      const CACHE_PATH = path.join(ROOT_PATH, 'tmp/cache');
      
      const VUE_VERSION = require('vue/package.json').version;
      const VUE_LOADER_VERSION = require('vue-loader/package.json').version;
      
      const dev = {
      module: {
          rules: [
              {
                  test: /\.vue$/,
                  loader: 'vue-loader',
                  options: {
                      cacheDirectory: path.join(CACHE_PATH, 'vue-loader'),
                      cacheIdentifier: [
                          process.env.NODE_ENV || 'development',
                          webpack.version,
                          VUE_VERSION,
                          VUE_LOADER_VERSION,
                      ].join('|'),
                  },
              }
          ]
      },
      plugins: [
          new VueLoaderPlugin(),
      ],
      resolve: {
              alias: {
                  'vue$': 'vue/dist/vue.esm.js'
              },
              extensions: ['.js', '.vue', '.json'],
          },
      };
      

      【讨论】:

      • 虽然与问题无关,但了解一下还是有用的,谢谢分享@tfr
      • 我已经解决了这个错误,但现在出现了一个新错误,您可以看看,看看您是否有任何提示? stackoverflow.com/questions/62021441/…
      猜你喜欢
      • 2019-02-13
      • 2022-12-03
      • 2021-12-03
      • 2020-07-24
      • 2021-09-25
      • 1970-01-01
      • 2017-05-14
      • 2020-05-09
      • 2018-05-10
      相关资源
      最近更新 更多