【问题标题】:Why vue-loader & webpack-4 is not working?为什么 vue-loader & webpack-4 不工作?
【发布时间】:2019-02-12 00:32:58
【问题描述】:

我正在尝试运行我自己的 webpack-4 + vue 应用程序。有了这个 package.json。我希望如果我实现了这一点,我可以将它与现有的 ASP.Net-Core 应用程序集成

{
  "name": "client-app",
  "version": "1.0.0",
  "description": "Proyecto con WebPack 4 y Vue 2",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --config webpack.config.vendor.js --mode development",
    "prod": "webpack --mode production"
  },
  "keywords": [
    "webpack-4",
    "vue-2",
    "vuetify"
  ],
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^9.1.5",
    "awesome-typescript-loader": "^5.2.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "base64-font-loader": "0.0.4",
    "css-loader": "^1.0.0",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "file-loader": "^2.0.0",
    "html-webpack-plugin": "^3.2.0",
    "i": "^0.3.6",
    "material-design-icons-iconfont": "^3.0.3",
    "mini-css-extract-plugin": "^0.4.2",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^7.1.0",
    "typescript": "^2.7.2",
    "url-loader": "^1.1.1",
    "vue-loader": "^15.4.1",
    "vue-class-component": "^6.2.0",
    "vue-style-loader": "^4.1.2",
    "style-loader": "^0.23.0",
    "vue-template-compiler": "^2.5.17",
    "webpack-md5-hash": "0.0.6",
    "webpack": "^4.17.2",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.7",
    "webpack-hot-middleware": "^2.23.1"
  },
  "dependencies": {
    "vue": "^2.5.17",
    "vue-router": "^3.0.1",
    "vuetify": "^1.2.3",
    "vuex": "^3.0.1",
    "vuex-class": "^0.3.1"
  }
}

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const bundleOutputDir = 'dist';
const { VueLoaderPlugin } = require('vue-loader')

module.exports = (env) => {
    const isDevBuild = !(env && env.prod);

    return [{
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: ['.js'] },
        entry: { 'main': './src/index.js' },
        module: {
            rules: [
                { test: /\.vue\.html$/, include: /ClientApp/, loader: 'vue-loader', options: { loaders: { js: 'awesome-typescript-loader?silent=true', loader: "unicode-loader" } } },
                // { test: /\.ts$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' },
                { test: /\.css$/, use: isDevBuild ? ['style-loader', 'css-loader'] : ExtractTextPlugin.extract({ use: 'css-loader?minimize' }) },
                { test: /\.(png|jpg|jpeg|gif|svg|woff2|woff)$/, include: /ClientApp/, use: 'url-loader?limit=25000' }
            ]
        },
        output: {
            path: path.join(__dirname, bundleOutputDir),
            filename: '[name].js',
            publicPath: 'dist/'
        },
        plugins: [
            new VueLoaderPlugin(),
            // new CheckerPlugin(),
            new webpack.DefinePlugin({
                'process.env': {
                    NODE_ENV: (isDevBuild ? 'development' : 'production')
                }
            })/* ,
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./dist/vendor-manifest.json')
            }) */
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(bundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
                // Plugins that apply in production builds only
                new webpack.optimize.UglifyJsPlugin(),
                new ExtractTextPlugin('site.css')
            ])
    }];
};

App.vue

<template>
  <div class="example">{{ msg }}</div>
</template>

<script>
export default {
  data () {
    return {
      msg: 'Hello world!'
    }
  }
}
</script>

<style>
.example {
  color: red;
}
</style>

我收到以下错误:

ERROR in ./src/components/App.vue 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> <template>
|   <div class="example">{{ msg }}</div>
| </template>
@ ./src/index.js 2:0-47 11:18-30

运行这个命令node_modules\.bin\webpack --config webpack.config.js --mode development它应该可以工作,但它没有。

这些是规格:

  1. 节点:v8.11.2
  2. webpack 4.17.2
  3. vuejs 2.5.17

还有什么想知道的,请告诉我。

【问题讨论】:

    标签: node.js vue.js webpack


    【解决方案1】:

    对于遇到此问题的其他任何人,需要在 webpack 插件部分加载较新版本的 vue-loader:

    // Required for vue-loader v15
    const VueLoaderPlugin = require('vue-loader/lib/plugin')
    environment.plugins.append(
      'VueLoaderPlugin',
      new VueLoaderPlugin()
    )
    

    来自https://github.com/rails/webpacker/issues/1453#issuecomment-412291197

    【讨论】:

      【解决方案2】:

      您似乎没有为.vue 文件配置rule:您为.vue.html 文件配置了一个rule,但不仅仅是.vue 文件。您可以通过删除 \.html 来更改现有规则以使其正常工作。

      【讨论】:

        猜你喜欢
        • 2019-02-05
        • 2019-05-04
        • 2017-07-15
        • 2016-02-28
        • 2021-05-27
        • 2017-08-10
        • 2018-02-11
        • 1970-01-01
        • 2016-05-20
        相关资源
        最近更新 更多