Webpacker,babel,uglifyjs-webpack-plugin-不转换箭头功能,仅在Vue文件中

时间:2018-12-13 22:38:48

标签: ruby-on-rails vue.js webpack babel vue-loader

运行webpacker 3.5.5(gem和程序包)。这通常可以正常工作,但是在IE11中该应用程序已损坏,因为箭头功能似乎没有被变换。但是,检查缩小的代码后,似乎唯一未转换的箭头功能就在我的vue组件中。

我认为这是因为我的babel类属性插件没有以某种方式应用于我的Vue加载器,但是我一直无法提出解决方案。

这是我的.babelrc

{
  "presets": [
    [
      "env",
      {
        "modules": false,
        "targets": {
          "browsers": [
            "> 1%",
            "IE 11"
          ],
          "uglify": true
        },
        "useBuiltIns": true
      }
    ]
  ],
  "plugins": [
    "syntax-dynamic-import",
    "transform-object-rest-spread",
    [
      "transform-class-properties",
      {
        "spec": true
      }
    ]
  ],
  "env": {
    "test": {
      "presets": ["es2015"]
    }
  }
}

这是我的environment.js文件的全部内容,该文件修改了webpacker附带的webpack环境(vue loader位于底部)。

const { environment } = require('@rails/webpacker');

environment.loaders.append('expose', {
    test: require.resolve('jquery'),
    use: [{
        loader: 'expose-loader',
        options: 'jQuery'
    }]
});

const webpack = require('webpack');

// append some global plugins
environment.plugins.append('Provide', new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    axios: 'axios',
    moment: 'moment-timezone',
    _: 'lodash'
}));

// Necesary configuration for vue-loader v15
const VueLoaderPlugin = require('vue-loader/lib/plugin');
environment.plugins.append(
    'VueLoaderPlugin',
    new VueLoaderPlugin()
);
environment.loaders.append('vue', {
    test: /\.vue$/,
    loader: 'vue-loader'
});

module.exports = environment;

编辑以获取更多信息:这是我的背包中名为“摔跤”的入口点

import 'babel-polyfill';
import 'wrestling';

然后在wrestling.js中...

import './styles/wrestling'

import Rails from 'rails-ujs'
Rails.start();

import wrestlingSetup from './wrestlingSetup'
wrestlingSetup();

WrestlingSetup包含对vue文件的实际引用。我已经缩小了文件,以显示单个vue导入在文件中的外观。其余的基本上都相同。

import Vue from 'vue/dist/vue.esm'

// Redacted a bunch of imports, but they all look like this oen
import WrestlerCreate from './vue/wrestler_create.vue'

export default function() {
    document.addEventListener('DOMContentLoaded', () => {
        axiosSetup();

        const app = new Vue({
            el: '#app',
            components: {
                // Other vue components here that I've removed for simplicity
                WrestlerCreate,
            }
        })
    });
}

这是Vue组件的实际示例

<template>
    <div role="form">
        <!-- other form elements -->
    </div>
</template>

<script>
    export default {
        name: 'wrestler-create',
        props: [

        ],
        // This does not get transformed by babel 
        data() {
            return {
                loading: false,
                error: false,
                errorMessage: "Error, please try again later or contact support.",

                first_name: '',
                last_name: '',
                weight_class: '',
                academic_class: ''
            }
        },
        methods: {
            // removed for simplicity
        }
    }
</script>

1 个答案:

答案 0 :(得分:0)

为澄清起见:

请对数据使用function()。我发现function()给我的麻烦比箭头函数少。

export default {
  data: function() {
     return {
       message: "Hello something!",
       secondMessage: "Hello world!" 
     };
  }
}

如果您确实希望使用箭头功能,可以编写:

export default {
  data: () => {
     return {
       message: "Hello something!",
       secondMessage: "Hello world!" 
     };
  }
}