gulp - vueify(在 gulp 任务中)与 vue 文件的 babel 转换

标签 gulp ecmascript-6 vue.js browserify vueify

固定。

解决方案是:

  1. 通过.transform(babelify)还有
  2. 确保您所有的.vue -file 脚本元素看起来像 <script type="text/javascript"> -- type 属性必须出现在 自定义属性、lang 属性.. 可能是第一个,甚至是标签上的唯一属性。

认为我的问题是我的 vue 中有 es6。我得到:

[09:33:36] Starting 'public'...

events.js:160
      throw er; // Unhandled 'error' event
      ^
SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "s
tart"

那个错误并没有真正告诉我它是从哪里来的(我的理解是它来自 browserify),但是如果我对公共(public)脚本进行空操作,其余的构建工作。这是该脚本:

gulp.task('public', ['public/styles'], () => {
    mkdirp.sync(dest+'/public/scripts')
    return browserify('public/scripts/demo.js')
        .transform(vueify)
    .bundle()
    .pipe(fs.createWriteStream(dest+'/public/scripts/index.js'))
})

这基本上是从 vueify 的 readme.MD 中复制的。它的依赖public/styles绝对有效。

所以,我想,让我们尝试手动编译它们。我确保我有 babel-core 和 babel-preset-latest。然后:构建文件:

.babelrc

{ "presets": ["latest"]}

vue.config.js

var babel = require("babel-core");

module.exports = {
    customCompilers: {
    // for tags with lang="es6"
    es6: function (content, cb, compiler, filePath) {
      // content:  content extracted from lang="es6" blocks
      // cb:       the callback to call when you're done compiling
      // compiler: the vueify compiler instance
      // filePath: the path for the file being compiled
      //
      // compile some ES6... and when you're done:
      const babelified = babel.transform(content) // what to do with this funky babelified.map ?
      cb(null, babelified.code)
    }
}

这基本上是直接来自 readme.MD,但对于 es6。

然后我更改了我的 vues 以具有该标签,例如:

<template>
    <div class="container">
        <app-header></app-header>
        <div class="row"><div class="col-xs-12">
            <transition name="slide" mode="out-in">
                <router-view></router-view>
            </transition>
        </div></div>
    </div>
</template>

<script lang="es6">
    import Header from './Header.vue'

    export default {
        components: {
            appHeader: Header
        },
        created() {
            this.$store.dispatch('initClasses')
        }
    }
</script>

<style>
    body {
        padding: 5vmin;
    }

    .slide-enter-active { animation: slide-in 200ms ease-out forwards }
    .slide-leave-active { animation: slide-out 200ms ease-out forwards }
    @keyframes slide-in {
        from { transform: translateY(-30vmin); opacity: 0 }
        to { transform: translateY(0px); opacity: 1}
    }
    @keyframes slide-out {
        from { transform: translateY(0px); opacity: 1 }
        to { transform: translateY(-30vmin); opacity: 0}
    }
</style>

问题是,我仍然得到同样的错误。我做错了什么?

回复附录

如果我遵循下面的建议,我的 vue 中的导入语句会出错,在处理我的主要 js 文件 demo.js .

package.json 的开发部门

  "devDependencies": {
    "babel": "^6.5.2",
    "babel-cli": "^6.22.2",
    "babel-plugin-syntax-async-functions": "^6.13.0",
    "babel-plugin-transform-async-to-generator": "^6.22.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-preset-latest": "^6.22.0",
    "babelify": "^7.3.0",
    "browserify": "^14.0.0",
    "gulp": "^3.9.1",
    "gulp-autoprefixer": "^3.1.1",
    "gulp-babel": "^6.1.2",
    "gulp-clean": "^0.3.2",
    "gulp-clean-dest": "^0.2.0",
    "gulp-concat": "^2.6.1",
    "gulp-plumber": "^1.1.0",
    "gulp-rename": "^1.2.2",
    "gulp-rimraf": "^0.2.1",
    "gulp-sass": "^3.1.0",
    "gulp-util": "^3.0.8",
    "gulp-vueify": "0.0.3",
    "mkdirp": "^0.5.1",
    "vueify": "^9.4.0"
  },

几乎固定。

在帮助下,我得到了 babelify 而不是 choking on vues。这实际上非常晦涩但简单,只需摆脱 lang属性并添加 type="text/javascript"每个脚本标签。 (不能只添加该类型规范,它必须是脚本元素中的 第一个 标记)。

它几乎可以工作。现在我得到:

[14:04:32] Starting 'public'...
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Cannot find module './vues/Home.vue' from 'C:\Users\roberto\github\cmst385-accessibility\public\scripts'

那不是主js文件!它在 routes.js

主要我有:

import App from './vues/App.vue'
import {routes} from './routes'

两者都有效。

在我的 route :

import Home from './vues/Home.vue';

即使文件在那里,这也不起作用。

宾果

好吧,我实际上已经将文件命名为“Home..vue”:)

最佳答案

如果没有看到项目,这些东西总是很难调试,但我可以告诉你我是如何设置我的,你可以检查你是否有所有这些东西:

首先,确保您可以在gulpfile 中使用ES6。最简单的方法是将其命名为 gulpfile.babel.js。我猜你已经有了这个设置,因为你正在设法编译你的 styles

现在,确保你已经安装了 babel-latest,我再次确定你已经安装了:

npm install --save-dev babel-preset-latest

我实际上使用了 babel-es2015-preset ,所以如果你发现 latest 是你的问题,只需安装它并将以下内容添加到你的 .babelrc:

{ 
"presets": ["es2015"]
}

现在,确保你已经安装了 babelify:

npm install --save-dev babelify

然后将以下内容添加到package.json:

  "browserify": {
    "transform": [
      "babelify",
      "vueify"
    ]
  }

这意味着您可以摆脱 vue.config.js 因为 browserify 现在应该自动应用转换。

最后在你的 gulpfile 中你可以做:

gulp.task('public', ['public/styles'], () => {
   return browserify({
            entries: 'public/scripts/demo.js',
            debug: true
        })
        .bundle()
        .pipe(source('demo.js')))
        .pipe(gulp.dest('public/scripts'));
});

我从来不需要将 lang 属性添加到 script 标签来让它工作。

关于gulp - vueify(在 gulp 任务中)与 vue 文件的 babel 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005463/

相关文章:

gulp 静默失败 - 没有错误打印到控制台

javascript - 将javascript脚本标签合并为一个

javascript - 在 Node.js 中使用装饰器

javascript - 如何找出使用 Promise.any 解决的 promise ?

vue.js - 我的 Vue 组件的名称是什么?

gulp - 使用 gulp 中的 webpack 2(webpack 2 的 webpack-stream)?

javascript - 使用 gulp 将 typescript 导出提取到 json 文件

javascript - 如何在 Express 中修复此 ES6 promise 链?

javascript - 禁用vue组件交互

vue.js - 在 Vuejs 中,如何在默认路由器 View 之外呈现组件