简体   繁体   English

如何在Gulp中获得简单的Vue编译?

[英]How do I get a simple Vue compilation going in Gulp?

I already use gulp in my workflow, and I don't currently use webpack or browserify, but it seems that compiling Vue 2.x components requires one or the other, there are no actively-maintained mechanisms for compiling Vue components directly as gulp tasks. 我已经在我的工作流程中使用了gulp,我目前没有使用webpack或browserify,但似乎编译Vue 2.x组件需要一个或另一个,没有主动维护的机制来直接编译Vue组件作为gulp任务。

I've searched and can't seem to find a working reference for simply compiling a directory with *.vue components into a single JavaScript file that I can then link from my pages. 我已经搜索过,似乎无法找到一个工作参考,只需将带有*.vue组件的目录编译成单个JavaScript文件,然后我可以从我的页面链接。 I just want to create and use some components, I'm not creating SPAs. 我只是想创建和使用一些组件,我不是在创建SPA。

Here's what I have: 这就是我所拥有的:

gulpfile.js gulpfile.js

const scriptDestFolder = "\\foo\\";
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const gulp = require("gulp");
const vueify = require('vueify');

gulp.task('vue', function () {
    return browserify({ 
                entries: ['./vuecomponents.js'],
                transform: [vueify]
        })
        .bundle()
        .pipe(source('vue-bundle.js'))
        .pipe(gulp.dest(scriptDestFolder));
});

vuecomponents.js vuecomponents.js

var Vue = require('vue');
var App = require('./vue/app.vue');

The app.vue file is the same as the example here . app.vue文件与此处的示例相同。 I have no intention of actually having an "app" component, I'm just trying to get a sample going, I would replace this with a list of my single-file components. 我无意实际拥有一个“app”组件,我只想尝试一个示例,我会用一个单文件组件列表替换它。

And here's the result: 这是结果:

Error: Parsing file \\blah\vue\app.vue:
'import' and 'export' may only appear at the top level (14:0)

I'm stumped. 我很难过。 I think browserify is trying to parse the raw vue code before compilation, but again, I'm a complete newbie at browserify. 认为 browserify试图在编译之前解析原始的vue代码,但同样,我在browserify上是一个完整的新手。

I actually adapted a plugin for this last year, based on vueify but without browserify. 我实际上在去年改编了一个插件,基于vueify而没有browserify I think it does exactly what you want. 我认为它完全符合您的要求。

You can find it here: https://www.npmjs.com/package/gulp-vueify2 你可以在这里找到它: https//www.npmjs.com/package/gulp-vueify2

var vueify = require('gulp-vueify2');

gulp.task('vueify', function () {
  return gulp.src('components/**/*.vue')
    .pipe(vueify(options))
    .pipe(gulp.dest('./dist'));
});

For people that don't need to use gulp, there are however much more consistent solutions to compile vue components, such as bili for librairies or parceljs for apps. 对于不需要使用gulp的人来说,有更多一致的解决方案来编译vue组件,例如bili for librairies或parceljs for apps。

Last but not least, if you are ready to enforce some conventions, Nuxt is the perfect way to compile your app with minimal config work and optional server-side rendering built-in. 最后但并非最不重要的是,如果您准备执行某些约定, Nuxt是使用最少的配置工作和内置的可选服务器端呈现来编译应用程序的完美方式。

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

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