Babelify not transforming arrow function in Vue Components - vue.js

I am trying to use Vue Single File Components with a Gulp build system.
Gulp should transform the files with Vueify and Babel, but neither of them are transforming arrow functions.
package.json
{
"dependencies": {
"#babel/cli": "7.4.4",
"#babel/core": "7.4.5",
"#babel/plugin-transform-arrow-functions": "7.2.0",
"#babel/plugin-transform-runtime": "7.4.4",
"#babel/preset-env": "7.5.2",
"#babel/runtime": "7.5.2",
"#vue/babel-preset-app": "3.7.0",
"babelify": "10.0.0",
"browserify": "16.2.3",
"core-js": "3.1.4",
"gulp": "4.0.1",
"gulp-babel": "8.0.0",
"gulp-concat": "2.6.1",
"gulp-jshint": "2.1.0",
"gulp-nodemon": "2.4.2",
"gulp-sass": "4.0.2",
"gulp-sourcemaps": "2.6.5",
"gulp-uglify": "3.0.2",
"gulp-watch": "5.0.1",
"node-sass": "4.11.0",
"regenerator-runtime": "0.13.2",
"uglifyify": "5.0.1",
"vinyl-buffer": "1.0.1",
"vinyl-source-stream": "2.0.0",
"vue": "2.6.10",
"vue-script2": "2.1.0",
"vueify": "9.4.1"
},
"engines": {
"node": "11.12.0",
"npm": "6.9.0"
},
}
gulpfile.js
gulp.task('script:' + pageName, function(done) {
var b = browserify(__dirname + '/public/js/vue/entry/'+pageName+'.js', {
debug: true, standalone: 'Bundle'
});
return b
.transform(vueify)
.transform('babelify', {
presets: ['#babel/preset-env', '#vue/babel-preset-app'],
plugins: ['#babel/plugin-transform-arrow-functions']
})
.bundle()
.pipe(source(pageName + '.min.js'))
.pipe(buffer())
.pipe(gulp.dest(paths.js.output));
done();
});
myPage.vue
<script>
module.exports = {
data: ()=>{
return {};
}
}
</script>
The output inside myPage.min.js
module.exports = {
data: ()=>{
return {};
}
}
Notice the arrow function did not get transformed.

Related

Jest don't mount composition-api component on Vue 2.7 + TS + Vuetify with "vue-test-utils"

I'm trying to run unit test for composition-api component. I'm on Vue 2.7 with TS and the following packages:
"nuxt": "^2.15.8",
"#nuxtjs/composition-api": "^0.33.1",
"#vue/test-utils": "^1.3.0",
"jest": "^28.1.3",
The issue is the following error is thrown on component mount:
SyntaxError: Unexpected token (1:1141)
at Parser.pp$4.raise (node_modules/vue-template-es2015-compiler/buble.js:2757:13)
at Parser.pp.unexpected (node_modules/vue-template-es2015-compiler/buble.js:647:8)
at Parser.pp$3.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:2196:10)
at Parser.<anonymous> (node_modules/vue-template-es2015-compiler/buble.js:6003:24)
at Parser.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:6129:31)
at Parser.pp$3.parseExprSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2047:19)
at Parser.pp$3.parseMaybeUnary (node_modules/vue-template-es2015-compiler/buble.js:2024:17)
at Parser.pp$3.parseExprOps (node_modules/vue-template-es2015-compiler/buble.js:1966:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1949:19)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
The test example:
import { mount, createLocalVue } from '#vue/test-utils';
import CompositionApi from '#nuxtjs/composition-api';
import CustomSelect from '#/components/shared/CustomSelect';
import Vuetify from 'vuetify';
const localVue = createLocalVue();
localVue.use(CompositionApi);
let vuetify;
beforeEach(() => {
jest.clearAllMocks();
vuetify = new Vuetify();
});
describe('test select component', () => {
test('Mounts correclty', () => {
const wrapper = mount(CustomSelect, {
localVue,
vuetify,
});
expect(wrapper.vm).toBeTruthy();
});
});
The config:
// jest.config.js
module.exports = {
globalSetup: '<rootDir>/jest.setup.js',
setupFiles: ['<rootDir>/tests/jest.init.js'],
testEnvironment: 'jsdom',
moduleNameMapper: {
'^#/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js',
'^#nuxtjs/composition-api$':
'#nuxtjs/composition-api/dist/runtime/index.js',
},
moduleFileExtensions: ['js', 'vue', 'json'],
transform: {
'^.+\\.js$': 'babel-jest',
'.*\\.(vue)$': 'vue-jest',
'^.+\\.ts?$': 'ts-jest',
'.+\\.(css|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
},
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!(#nuxtjs/composition-api)/)',
],
snapshotSerializers: ['jest-serializer-vue'],
testMatch: ['<rootDir>/tests/unit/**/*.spec.js'],
};
package.json
{
.....
"test": "jest",
"test:watch": "jest --watch",
.....
},
"engines": {
"npm": "8.11",
"node": "16.16"
},
"dependencies": {
.....
"#babel/eslint-parser": "^7.18.9",
"#nuxt/types": "^2.15.8",
"#nuxt/typescript-build": "^2.1.0",
"#nuxtjs/composition-api": "^0.33.1",
"#nuxtjs/vuetify": "^1.12.3",
"#vueuse/core": "^9.3.1",
"flush-promises": "^1.0.2",
"jest-environment-jsdom": "^28.1.3",
"jest-mock-axios": "^4.7.0-beta",
"nuxt": "^2.15.8",
.....
},
"devDependencies": {
.....
"#babel/core": "^7.19.1",
"#babel/preset-env": "^7.19.1",
"#faker-js/faker": "^7.6.0",
"#vue/server-test-utils": "^1.3.0",
"#vue/test-utils": "^1.3.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^28.1.3",
"eslint": "^8.20.0",
"jest": "^28.1.3",
"jest-cli": "^28.1.3",
"jest-serializer-vue": "^2.0.2",
"jest-transform-stub": "^2.0.0",
"mockdate": "^3.0.2",
"vue-jest": "^3.0.7"
.....
}
}
I've tried all the suggestions from Jest docs, including: specifying "transformIgnorePatterns", specifying "moduleNameMapper", enabling "ECMAScript Modules" but it won't help.
What should be the working setup to test composition-api components on Vue 2.7 + Nuxt + Vuetify?

Jest Nuxt Cannot read property 'child' of undefined

I have a problem with fire the Jest tests for the Nuxt/Vue components. Last day i connected all that need for test, and had resolve many errors with jsdom and babel issues, but can't resolve problem with Vue component.
The error is
TypeError: Cannot read property 'child' of undefined
4 | describe('Компонент Counter', () => {
5 | test("renders properly", () => {
> 6 | const wrapper = shallowMount(Greeting); // or mount - it's doesn't metter
| ^
7 | console.log(wrapper);
8 | });
9 |
My comoponent Greeting
<template>
<div> Hello World! </div>
</template>
<script>
export default {
name: 'Greeting'
};
</script>
My Greeting.spec.js
import {mount, shallowMount} from '#vue/test-utils';
import Greeting from '../../components/Greeting.vue';
require('jsdom-global')();
describe('Компонент Counter', () => {
test("renders properly", () => {
const wrapper = shallowMount(Greeting);
console.log(wrapper);
});
});
jest.config.js
module.exports = {
moduleFileExtensions: ["js", "json", "vue"],
watchman: false,
preset: "#nuxt/test-utils",
transform: {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "#vue/vue2-jest"
},
};
package.json
"scripts": {
"test": "jest"
},
"devDependencies": {
"#nuxt/test-utils": "^0.2.2",
"#types/jest": "^27.0.3",
"#vue/cli-plugin-babel": "^4.5.15",
"#vue/test-utils": "^1.3.0",
"#vue/vue2-jest": "^27.0.0-alpha.4",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^8.2.1",
"babel-jest": "^27.4.5",
"eslint": "^4.15.0",
"eslint-friendly-formatter": "^3.0.0",
"eslint-loader": "^1.7.1",
"eslint-plugin-vue": "^4.0.0",
"jest": "^27.4.5",
"jsdom-global": "^3.0.2",
"node-sass": "^4.7.2",
"pug": "^2.0.3",
"pug-plain-loader": "^1.0.0",
"sass-loader": "^6.0.7",
"ts-jest": "^27.1.2",
"vue": "^2.6.14",
"vue-jest": "^3.0.7",
"vue-template-compiler": "^2.6.14",
"vueify": "^9.4.1"
}
So the answer is that Jest have an attribute for working with JSDOM and i don't need an additional requires.
i just add in jest.config.js single line in the bottom
module.exports = {
moduleFileExtensions: ["js", "json", "vue"],
watchman: false,
preset: "#nuxt/test-utils",
transform: {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "#vue/vue2-jest"
},
testEnvironment: 'jsdom'
};

CleanWebPackPlugin Error :CleanwebpackPlugin is not a constructor

Im trying to use this plugin: CleanWebpackPlugin.
But it's not running, i can see the Error: "TypeError: CleanWebPackPlugin is not a constructor"
even if i tried all upgrades from the github page: https://github.com/johnagan/clean-webpack-plugin/issues/106
this is my package.json file:
{
"name": "------",
"version": "1.0.0",
"description": "",
"scripts": {
"dev": "webpack-dev-server",
"build": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/----/--------.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/-----/-----/issues"
},
"homepage": "https://github.com/----/------#readme",
"dependencies": {
"axios": "^0.19.0",
"lazysizes": "^5.1.2",
"lodash": "^4.17.15",
"normalize.css": "^8.0.1",
"react": "^16.11.0",
"react-dom": "^16.11.0"
},
"devDependencies": {
"#babel/core": "^7.7.2",
"#babel/preset-env": "^7.7.1",
"#babel/preset-react": "^7.7.0",
"autoprefixer": "^9.6.5",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^3.2.0",
"cssnano": "^4.1.10",
"fs-extra": "^8.1.0",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.8.0",
"postcss-hexrgba": "^2.0.0",
"postcss-import": "^12.0.1",
"postcss-loader": "^3.0.0",
"postcss-mixins": "^6.2.2",
"postcss-nested": "^4.1.2",
"postcss-simple-vars": "^5.0.2",
"style-loader": "^1.0.0",
"webpack": "^4.41.0",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.9.0"
}
}
This is my webpack.config file
const currentTask = process.env.npm_lifecycle_event
const path= require('path')
const {CleanWebPackPlugin} = require ('clean-webpack-plugin')
const postCSSPlugins = [
require('postcss-import'),
require('postcss-mixins'),
require('postcss-simple-vars'),
require('postcss-nested'),
require('postcss-hexrgba'),
require('autoprefixer')
]
let config = {
entry:'./app/assets/scripts/App.js',
module: {
rules:[
{
test:/\.css$/i,
use: ['style-loader','css-loader?url=false',
{loader:'postcss-loader', options:{plugins: postCSSPlugins}}]
}
]
}
}
if (currentTask == 'dev'){
config.output = {
filename:'bundled.js',
path: path.resolve(__dirname,'app')
}
config.devServer = {
before: function(app,server){
server._watch('./app/**/*.html')
},
contentBase: path.join(__dirname, 'app'),
hot:true,
port:3000,
host:'0.0.0.0'
}
config.mode = 'development'
}
if (currentTask == 'build') {
config.output = {
filename:'[name].[chunkhash].js',
chunkFilename:'[name].[chunkhash].js',
path: path.resolve(__dirname,'dist')
}
config.mode = 'production'
config.optimization = {
splitChunks: {chunks: 'all'}
}
//config.plugins = [new CleanWebPackPlugin({dry:true, cleanAfterEveryBuildPatterns:['dist']})]
}
module.exports = config
I tried
const {CleanWebPackPlugin} = require ('clean-webpack-plugin')
and
const CleanWebPackPlugin = require ('clean-webpack-plugin')
I tried
config.plugins = [new CleanWebPackPlugin()]
and
config.plugins = [new CleanWebPackPlugin(['dist'])]
and
config.plugins = [new CleanWebPackPlugin({dry:true, cleanAfterEveryBuildPatterns:['dist']})]
and finally when delete this line, its working, but not with this plugin...

use async \ await in vue?

I want to use async \ await in Vue.
there is order-list.vue, in short methods this is:
getOrder: function () {
return new Promise(function (resolve, reject) {
setTimeout(() => {
console.log('hello world');
resolve(true)
}, 2000)
});
},
example: async() => {
console.log('start');
for (let i in this.orders) {
await this.getOrder().then(
console.log('loaded ' + i)
)
}
console.log('finish');
}
it's all going to webpack.
When I start webpack, I get this message
ERROR in ./erp/vue/order-list.vue?vue&type=script&lang=js& (./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./erp/vue/order-list.vue?vue&type=script&lang=js&)
Module not found: Error: Can't resolve '#babel/runtime/regenerator' in '/Users/pankovalxndr/Sites/mysite/erp/vue'
# ./erp/vue/order-list.vue?vue&type=script&lang=js& (./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./erp/vue/order-list.vue?vue&type=script&lang=js&) 1:0-61 141:13-32 146:28-47 156:21-40
package.json
What is wrong here?
"devDependencies": {
"#babel/core": "^7.4.3",
"#babel/plugin-transform-runtime": "^7.6.2",
"#babel/preset-env": "^7.4.3",
"autoprefixer": "^8.6.5",
"babel-loader": "^8.0.0-beta.6",
"babel-runtime": "^6.26.0",
"gulp": "git://github.com/gulpjs/gulp.git#6d71a658c61edb3090221579d8f97dbe086ba2ed",
"gulp-cheerio": "^0.6.3",
"gulp-cssnano": "^2.1.2",
"gulp-postcss": "^7.0.1",
"gulp-print": "^5.0.2",
"gulp-rename": "^1.4.0",
"gulp-replace": "^0.6.1",
"gulp-sass": "^3.1.0",
"gulp-svg-sprite": "^1.5.0",
"gulp-svgmin": "^1.2.4",
"gulp-uglify": "^3.0.2",
"gulp-watch": "^5.0.1",
"vue-loader": "^15.7.2",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.5.17",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
}
My .babelrc
What is wrong here?
{
"presets": [
"#babel/preset-env"
],
"plugins": [
[
"#babel/plugin-transform-runtime",
{
"regenerator": true
}
]
]
}
webpack file
What is wrong here?
module: {
rules: [
{
test: /\.vue$/,
use: 'vue-loader'
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
}, {
test: /\.js$/,
use: 'babel-loader'
}
]
},
plugins: [
new VueLoaderPlugin(),
]
If I remove functions with await then everything starts to work well, those things are not specifically, more precisely in some module

Type error during importing vue-formio to my vue template

I have a problem with importing vue-formio to my vue component.
Can't import FormBuilder without errors :(
After:
import { FormBuilder } from 'vue-formio';
I see in the console:
TypeError: Object prototype may only be an Object or null: undefined
Whole error You can see here: see print screen
TypeError: Object prototype may only be an Object or null: undefined
Main import:
if (document.body.classList.contains("hr:controller-requisition:action-edit")) {
require.ensure([], () => {
require("./views/requisition/edit.es6.js").default;
});
}
My formio.es6.js file:
import Vue from "vue";
import formio from "./show.vue";
import { store } from "HRComponets/store/store.es6";
export default new Vue({
el: "#formio-show",
data: {},
store,
render(h) {
return h(formio);
}
});
My vue component file:
Hi Guys, need help with vue-formio
<script>
import { FormBuilder } from 'vue-formio';
export default {
name: "formioShow",
data() {
return {
schema: {},
}
},
components: {
FormBuilder
},
};
</script>
My package.json file:
{
"name": "core",
"version": "1.0.0",
"description": "ortal",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"start": "npm run dev",
"prod": "webpack --config webpack.prod.js",
"dev": "webpack --watch --progress --config webpack.dev.js",
},
"nyc": {
"include": [
"src/**/*.(js|vue)"
],
"instrument": false,
"sourceMap": false
},
"keywords": [
"HR",
"Syrveys"
],
"browserslist": [
"> 2%",
"last 3 versions",
"not ie <= 8"
],
"devDependencies": {
"#vue/test-utils": "^1.0.0-beta.18",
"axios": "^0.18.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"clean-webpack-plugin": "^0.1.19",
"css-loader": "^0.28.11",
"eslint": "^4.19.1",
"expect": "^23.1.0",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"husky": "^1.0.0-rc.8",
"istanbul-instrumenter-loader": "^3.0.1",
"jsdom": "^11.11.0",
"jsdom-global": "^3.0.2",
"less": "^3.0.4",
"less-loader": "^4.1.0",
"mini-css-extract-plugin": "^0.4.0",
"mocha": "^5.2.0",
"mocha-webpack": "^2.0.0-beta.0",
"node-sass": "^4.9.0",
"nyc": "^11.8.0",
"prettier-eslint": "^8.8.1",
"sass-loader": "^7.0.1",
"style-loader": "^0.21.0",
"ts-loader": "^5.1.0",
"typescript": "^3.0.3",
"vue": "^2.5.16",
"vue-loader": "^15.0.10",
"vue-template-compiler": "^2.5.16",
"watchfile-webpack-plugin": "0.0.4",
"webpack": "^4.8.1",
"webpack-cli": "^2.1.3",
"webpack-dev-server": "^3.1.4",
"webpack-merge": "^4.1.2",
"webpack-notifier": "^1.6.0"
},
"dependencies": {
"amcharts3": "github:amcharts/amcharts3",
"eslint-plugin-vue": "^4.7.1",
"formiojs": "^3.5.1",
"jquery": "^3.3.1",
"lodash": "^4.17.10",
"moment-timezone": "^0.5.17",
"npm": "^6.4.1",
"numeral": "^2.0.6",
"vee-validate": "^2.1.0-beta.8",
"vue-formio": "^3.0.0",
"vue-moment": "^4.0.0-0",
"vue-multiselect": "^2.1.0",
"vue-numerals": "0.0.2",
"vue-pdf": "^3.3.1",
"vue-upload-component": "^2.8.9",
"vue-wysiwyg": "^1.7.2",
"vue2-dropzone": "^3.2.2",
"vuelidate": "^0.7.4",
"vuenut": "^0.2.2",
"vuex": "^3.0.1",
"webpack-node-externals": "^1.7.2"
}
}
My webpack config file:
const webpack = require("webpack");
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const config = {
entry: {
core: "./src/AppBundle/VueComponents/main.es6.js",
hr: "./src/HRBundle/VueComponents/main.es6.js",
pm: "./src/PerformanceManagementBundle/VueComponents/main.es6.js"
},
output: {
filename: "[name]-app.js",
path: __dirname + "/web/assets/core/js/",
hotUpdateChunkFilename: "../webpack-hot-reload/hot-update.js",
hotUpdateMainFilename: "../webpack-hot-reload/Updatehot-update.json",
publicPath: "/assets/core/js/"
},
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},
module: {
rules: [
{
test: /\.vue$/,
loader: "vue-loader"
},
{
test: /\.ts$/,
exclude: /node_modules|vue\/src/,
loader: "ts-loader",
options: {
appendTsSuffixTo: [/\.vue$/]
}
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
{
test: /.scss$/,
use: ["style-loader", "css-loader"]
},
{
test: /\.less$/,
use: ["vue-style-loader", "css-loader", "less-loader"]
},
{
test: /\.es6.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader"
}
}
]
},
plugins: [
new VueLoaderPlugin(),
new HTMLWebpackPlugin(),
new webpack.HotModuleReplacementPlugin(),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
],
externals: {
jquery: "jQuery"
},
resolve: {
alias: {
vue: "vue/dist/vue.js",
HRComponets: path.resolve(__dirname, "./src/HRBundle/VueComponents")
},
extensions: ["*", ".js", ".vue", ".json", ".ts"]
}
};
module.exports = config;