javascript - Gulp/Webpack 不转译 Vue 文件

标签 javascript webpack vue.js babeljs

当我从命令行运行 Webpack 时,一切都运行良好:生成的包已正确转译/丑化,并且在浏览器中运行良好。但是,当我尝试从 Gulp 调用 Webpack 时(我使用的是 Gulp@4,FWIW),我总是收到此错误:

stream.js:74
      throw er; // Unhandled stream error in pipe.
      ^
Error: bundle.js from UglifyJs
Unexpected token: name (Vue) [bundle.js:47,8]

我从带有 .babelrc 的 Babel 开始,手动将 Webpack-stream 指向 Webpack 2^.X.X,甚至在 reading this 之后给了 Buble 一个机会。 。有什么建议吗?

package.json

"dependencies": {
  "body-parser": "^1.16.0",
  "csv-parse": "^1.2.0",
  "express": "^4.14.1",
  "express-handlebars": "^3.0.0",
  "knex": "^0.12.7",
  "marked": "^0.3.6",
  "sass": "^0.5.0",
  "sqlite3": "^3.1.8"
},
"devDependencies": {
  "browser-sync": "^2.18.8",
  "buble": "^0.15.2",
  "buble-loader": "^0.4.1",
  "css-loader": "^0.27.3",
  "eslint": "^3.14.1",
  "eslint-plugin-html": "^2.0.1",
  "eslint-plugin-vue": "^2.0.1",
  "gulp": "github:gulpjs/gulp#4.0",
  "gulp-nodemon": "^2.2.1",
  "gulp-sass": "^3.1.0",
  "gulp-uglify": "^2.1.2",
  "gulp-webpack": "^1.5.0",
  "jsdoc-to-markdown": "^3.0.0",
  "uglify-js": "^2.8.14",
  "vue": "^2.2.4",
  "vue-loader": "^11.2.0",
  "vue-template-compiler": "^2.2.4",
  "webpack": "^2.3.1",
  "webpack-stream": "^3.2.0"
}

webpack.config.js

const path = require('path');

const webpack = require('webpack');

module.exports = {
  entry: path.resolve(__dirname, 'src/views/vue.js'),
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'src/public/js')
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.js$/,
        loader: 'buble-loader'
      }
    ]
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin()
  ]
};

gulpfile.js

const path        = require('path');
const browserSync = require('browser-sync');
const gulp        = require('gulp');
const nodemon     = require('gulp-nodemon');
const uglify      = require('gulp-uglify');
const webpack     = require('webpack-stream');

const BROWSER_SYNC_DELAY    = 1000;
const NODEMON_RESTART_DELAY = 5000;

gulp.task('compile-js', (done) => {
  // Use webpack to transpile, bundle, and uglify front-end JS
  gulp.src(path.resolve(__dirname, 'src/views/vue.js'))
    .pipe(webpack(require(path.resolve(__dirname, 'webpack.config.js'))))
    .pipe(gulp.dest(path.resolve(__dirname, 'src/public/js')));

  done();
});

vue.js

import Vue from 'vue';
import App from './index.vue';

new Vue({
  el: '#app',
  render: (createElement) => {
    return createElement(App);
  }
});

index.vue

<template>
  <p>{{ message }}</p>
</template>

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

最佳答案

我对 vue 和 webpack 没有太多经验,因为我自己也在学习过程中。也许您可能希望将 vue-loader 应用于 js 测试,因为您要将 vue 组件发送到 bundle.js。

仅供引用,欢迎指正。

关于javascript - Gulp/Webpack 不转译 Vue 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42964474/

相关文章:

javascript - 从提交时点击 'next'按钮现在如何防止这种情况?

javascript - 如何在 JS 的 setInterval 中等待?

c# - 根据两个字段记住密码

reactjs - React Storybook 不显示 SVG 图标

node.js - 无法使用 webpack 加载 Node 原生插件

javascript - ""包围的 html 标签未呈现

javascript - 如何获取不同行中的显示字段

javascript - 制作对象时数组键不会被插值

angular - 未捕获错误 : Unexpected directive 'MyComboBox' imported by the module 'AppModule' . 请添加 @NgModule 注解

laravel - 无法使用 Vuetify 和 Laravel 读取未定义的属性 't'