简体   繁体   English

为什么 vue-loader 和 webpack-4 不起作用?

[英]Why vue-loader & webpack-4 is not working?

I'm trying to run my own webpack-4 + vue app.我正在尝试运行我自己的 webpack-4 + vue 应用程序。 with this package.json.使用这个 package.json。 I hope if I achieve this I can integrate it with an existing ASP.Net-Core app我希望如果我实现了这一点,我可以将它与现有的 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 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>

I'm getting the following error:我收到以下错误:

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

Running this command node_modules\\.bin\\webpack --config webpack.config.js --mode development it should be able to work but it doesn't.运行这个命令node_modules\\.bin\\webpack --config webpack.config.js --mode development它应该能够工作,但它没有。

These are specifications:这些是规格:

  1. node: v8.11.2节点:v8.11.2
  2. webpack 4.17.2网络包 4.17.2
  3. vuejs 2.5.17 Vuejs 2.5.17

Anything else you want to know, please tell me.你还有什么想知道的,请告诉我。

For anyone else that runs into this issue, newer versions of vue-loader need to be loaded in the webpack plugins section:对于遇到此问题的任何其他人,需要在 webpack 插件部分加载较新版本的 vue-loader:

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

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

It looks like you don't have a rule configured for .vue files: you have one for .vue.html files but not for just .vue files.看起来您没有为.vue文件配置rule :您有一个用于.vue.html文件的规则,但不仅仅是.vue文件。 You can just change your existing rule by removing the \\.html in order to make it work.您可以通过删除\\.html来更改现有规则以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM