[Vue2 + module federation]: Error in dependencies "TypeError: Cannot read properties of undefined (reading 'warn')" - vue.js

I'm trying to create a MicroFrontend in Vue2 using module federation, like Pinia provides a singleton behaviour about stores that could be awesome for use in module federation, the problem is when I inject this an error is prompt ( I make previous test in a unique application and that works )
That is the error:
And the piece of code relative is:
My configuration is the next:
Child1: Who exposed it's components contains this vue.config.js
const { defineConfig } = require('#vue/cli-service')
const {ModuleFederationPlugin} = require("webpack").container;
const deps = require("./package.json");
module.exports = defineConfig({
transpileDependencies: true,
publicPath: 'auto',
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: 'child1',
filename: 'remoteEntry.js',
exposes: {
'./ResultsPage': './src/pages/ResultsPage',
'./Test1Page' : './src/pages/Test1Page',
'./Test2Page' : './src/pages/Test2Page',
'./appStore' : './src/store/appStore.js'
},
shared: require("./package.json").dependencies,
}),
],
}
})
And the package.json:
{
"name": "child2",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#vue/composition-api": "^1.7.0",
"core-js": "^3.8.3",
"pinia": "^2.0.17",
"vue": "^2.6.14",
"vue-router": "^4.1.5"
},
"devDependencies": {
"#babel/core": "^7.12.16",
"#babel/eslint-parser": "^7.12.16",
"#vue/cli-plugin-babel": "~5.0.8",
"#vue/cli-plugin-eslint": "~5.0.8",
"#vue/cli-plugin-router": "~5.0.8",
"#vue/cli-plugin-unit-mocha": "~5.0.8",
"#vue/cli-service": "~5.0.8",
"#vue/test-utils": "^1.1.3",
"chai": "^4.2.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"sass": "^1.32.7",
"sass-loader": "^12.0.0",
"vue-cli-plugin-pinia": "~0.1.4",
"vue-template-compiler": "^2.6.14"
}
}
Parent: This is how works vue.config.js
const { defineConfig } = require("#vue/cli-service");
const { ModuleFederationPlugin } = require("webpack").container;
const ExternalTemplateRemotesPlugin = require("external-remotes-plugin");
const deps = require("./package.json");
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
plugins: [
new ModuleFederationPlugin({
name: "microfrontends-library",
remotes: {
child1: "child1#http://localhost:3013/remoteEntry.js",
//child2: "child2#http://localhost:3014/remoteEntry.js",
},
shared: require('./package.json').dependencies,
})
],
},
});
And its package.json
{
"name": "parent",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
"#vue/composition-api": "^1.7.0",
"core-js": "^3.8.3",
"pinia": "^2.0.17",
"vue": "^2.6.14",
"vue-router": "^4.1.5"
},
"devDependencies": {
"#babel/core": "^7.12.16",
"#babel/eslint-parser": "^7.12.16",
"#vue/cli-plugin-babel": "~5.0.0",
"#vue/cli-plugin-eslint": "~5.0.0",
"#vue/cli-plugin-router": "~5.0.0",
"#vue/cli-plugin-unit-jest": "~5.0.0",
"#vue/cli-service": "~5.0.0",
"#vue/test-utils": "^1.1.3",
"#vue/vue2-jest": "^27.0.0-alpha.2",
"babel-jest": "^27.0.6",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3",
"jest": "^27.0.5",
"sass": "^1.32.7",
"sass-loader": "^12.0.0",
"vue-cli-plugin-pinia": "~0.1.4",
"vue-template-compiler": "^2.6.14"
}
}
In resume: I don't have a HTTP server because I'm making a build of the child and them using a HTTP server for support ( In the future I would like to add CDN to this children's )
All help would be accepted, really I'm in a stuck solution, so any movement to a solution would be great

// vue.config.js
module.exports = {
configureWebpack: {
module: {
rules: [
{
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto"
}
]
}
}
}
I find this solution in answer
Works for me

Related

setting up apollo graphql-tag loader issue in vue js

I am new to graphql and I have been getting this error when trying to use .graphql file inside vue apollo. while using gql tag , it works fine , but while importing graphql file this error is showing up.
i have tried to use graphql loader but it doesnt seems to be working.
Failed to resolve loader: cache-loader
You may need to install it.
ERROR in ./src/client/pages/Clients.vue?vue&type=script&lang=js&
(./node_modules/babel-loader/lib/index.js??clonedRuleSet-
40.use[0]!./node_modules/#vue/cli-service/lib/config/vue-loader-v15-resolve-
compat/vue-loader.js??vue-loader-options!./src/client/pages/Clients.vue?
vue&type=script&lang=js&) 100:15-55
Module not found: Error: Can't resolve 'cache-loader' in '/home/noakash/temp/gym-
client'
# ./src/client/pages/Clients.vue?vue&type=script&lang=js& 1:0-246 1:262-265 1:267- 510 1:267-510
# ./src/client/pages/Clients.vue 2:0-59 3:0-54 3:0-54 9:2-8
# ./src/router/index.js 5:0-50 31:13-20
# ./src/main.js 3:0-30 18:2-8
webpack compiled with 1 error
my Clients.vue file look like this
apollo: {
clients() {
return {
query:require('../../graphql/clients.graphql'),
result({ loading }) {
this.loading = loading;
},
};
},
},
i am using vue-cli
my vue.config.js look like this
const { defineConfig } = require('#vue/cli-service')
module.exports = defineConfig({
configureWebpack:{
module:{
rules:[
{
test: /\.graphql$/,
use: 'graphql-tag/loader'
}
]
}
},
transpileDependencies: [
'vuetify'
],
})
my package.json
{
"name": "gym-client",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"apollo": "vue-cli-service apollo:dev --generate-schema",
"apollo:schema:generate": "vue-cli-service apollo:schema:generate",
"apollo:schema:publish": "vue-cli-service apollo:schema:publish",
"apollo:start": "vue-cli-service apollo:start"
},
"dependencies": {
"apollo-boost": "^0.4.9",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
"apollo-link": "^1.2.14",
"apollo-link-http": "^1.5.17",
"core-js": "^3.25.0",
"graphql": "^15.8.0",
"graphql-tag": "^2.12.6",
"jspdf": "^2.5.1",
"lowdb": "^1.0.0",
"mkdirp": "^0.5.1",
"moment": "^2.29.4",
"regenerator-runtime": "^0.13.9",
"shortid": "^2.2.8",
"vue": "^2.6.14",
"vue-apollo": "^3.1.0",
"vue-router": "^3.5.1",
"vuetify": "^2.6.0",
"vuex": "^3.6.2"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~5.0.0",
"#vue/cli-plugin-router": "~5.0.0",
"#vue/cli-plugin-vuex": "~5.0.0",
"#vue/cli-service": "~5.0.0",
"sass": "~1.32.0",
"sass-loader": "^10.0.0",
"vue-cli-plugin-apollo": "~0.22.2",
"vue-cli-plugin-vuetify": "~2.5.5",
"vue-template-compiler": "^2.6.14",
"vuetify-loader": "^1.7.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}

vue3-electron production build facing webpackJsonp not defined problem

I'm trying to build an electron app from a vue-cli3 project. The vue project itself works fine both in development and production. Everything works as well if I run a dev mode with npm run electron:serve. But the built electron app would give a Uncaught TypeError: Cannot read property 'webpackJsonp' of undefined in each packed js file.
vue.config.js:
module.exports = {
configureWebpack: config => {
const tsLoader = config.module.rules.find((loader) => loader.test === /\.ts$/);
const tsLoaderIndex = config.module.rules.indexOf(tsLoader);
const webWorkerLoader = {
test: /\.worker\.ts$/,
use: [
{
loader: 'worker-loader',
options: {
filename: 'WorkerName.[hash].js',
inline: 'fallback',
}
}
]
}
config.module.rules = [...config.module.rules.slice(0, tsLoaderIndex-1), webWorkerLoader, ...config.module.rules.slice(tsLoaderIndex-1)]
// console.log(config.module.rules);
if (process.env.NODE_ENV === 'production') {
config.mode = 'production'
// pack dependencies separately
let optimization = {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 20000,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name (module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
// npm package names are URL-safe, but some servers don't like # symbols
return `npm.${packageName.replace('#', '')}`
}
}
}
}
}
Object.assign(config, {
optimization
})
}
},
parallel: false,
chainWebpack: config => {
config.output.globalObject('this')
},
pluginOptions: {
'style-resources-loader': {
preProcessor: 'scss',
patterns: []
}
},
css: {
loaderOptions: {
less: {
lessOptions: {
modifyVars: {
'font-size-base': '18px'
},
javascriptEnabled: true
}
}
}
},
devServer: {
host: '127.0.0.1',
port: 8080,
public: 'localhost:8080',
}
}
package.json:
{
"name": "testApp",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
},
"main": "background.js",
"dependencies": {
"#ant-design/icons-vue": "^6.0.1",
"#kyvg/vue3-notification": "^2.3.4",
"ant-design-vue": "^2.2.8",
"bioseq": "^0.1.5",
"core-js": "^3.6.5",
"echarts": "^5.3.3",
"idb-keyval": "^5.1.3",
"jszip": "^3.7.1",
"lodash": "^4.17.21",
"pako": "^1.0.11",
"pdfmake": "^0.2.5",
"vue": "^3.2.4",
"vue3-infinite-scroll-good": "^1.0.2",
"xlsx": "^0.17.2"
},
"devDependencies": {
"#babel/core": "^7.14.6",
"#babel/preset-env": "^7.14.7",
"#babel/preset-react": "^7.14.5",
"#types/electron-devtools-installer": "^2.2.0",
"#types/node": "^16.0.0",
"#types/pako": "^1.0.2",
"#types/pdfmake": "^0.2.1",
"#typescript-eslint/eslint-plugin": "^4.18.0",
"#typescript-eslint/parser": "^4.18.0",
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-plugin-typescript": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.1.4",
"#vue/eslint-config-typescript": "^7.0.0",
"electron": "^13.6.9",
"electron-devtools-installer": "^3.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0",
"less": "^4.1.2",
"less-loader": "^7.3.0",
"style-resources-loader": "^1.4.1",
"typescript": "~4.1.5",
"vue-cli-plugin-electron-builder": "~2.1.1",
"vue-cli-plugin-style-resources-loader": "~0.1.5",
"worker-loader": "^3.0.8"
}
}
There are some instructions that it may be caused by something wrong with the webpack configuration. But I can only find examples in vue-cli2 webpack.prod.conf.js. How do we modify that in vue-cli3?
Find the clue at https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/1706
There was a mistype 'this' in one of my vue templates causing the error. Removing that fixed the problem.

Vue3/TS/Jest/testing-ligrary: can not run tests

I create project through vue cli with options: vue3, TS, JEST and add #testing-library/vue . my package.json looks like this
{
"name": "todo-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
"vue": "^3.0.0",
"vue-class-component": "^8.0.0-0",
"vue-router": "^4.0.0-0",
"vuex": "^4.0.0-0"
},
"devDependencies": {
"#testing-library/vue": "^5.8.2",
"#types/jest": "^24.0.19",
"#typescript-eslint/eslint-plugin": "^4.18.0",
"#typescript-eslint/parser": "^4.18.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-plugin-router": "~4.5.0",
"#vue/cli-plugin-typescript": "~4.5.0",
"#vue/cli-plugin-unit-jest": "~4.5.0",
"#vue/cli-plugin-vuex": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.0.0",
"#vue/eslint-config-standard": "^5.1.2",
"#vue/eslint-config-typescript": "^7.0.0",
"#vue/test-utils": "^2.0.0-0",
"eslint": "^6.7.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^7.0.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",
"typescript": "~4.1.5",
"vue-jest": "^5.0.0-0"
}
}
my jest.config.js is the one I am getting from vue CLI
module.exports = {
preset: '#vue/cli-plugin-unit-jest/presets/typescript',
transform: {
'^.+\\.vue$': 'vue-jest'
}
}
my test is
import { fireEvent, render, waitFor } from '#testing-library/vue';
import ContentBlock from '#/components/ContentBlock.vue';
describe('ContentBlock.spec.vue', () => {
const mockedProps = {
title: 'testTitle',
content: 'testTitle',
btn: 'testBtnText'
}
const getMountedInstance = () => {
return render(ContentBlock, {
props: mockedProps
});
};
it('renders block title, content and button', async (): Promise<void> => {
const { getByTestId } = getMountedInstance();
const [title, content, btn] = await waitFor(() => {
return [
getByTestId('blockTitle'),
getByTestId('blockContent'),
getByTestId('blockBtn'),
];
});
expect(title).toBeDefined();
expect(title).toContain(mockedProps.title);
expect(content).toBeDefined();
expect(content).toContain(mockedProps.content);
expect(btn).toBeDefined();
expect(content).toContain(mockedProps.btn);
});
})
tried node versions: 14, 12
I am getting error:
Test suite failed to run
Cannot find module 'vue-template-compiler' from 'vue-test-utils.js'
However, Jest was able to find:
'./render.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'jsx', 'json', 'vue', 'ts', 'tsx'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
I checked similar questions such as Test suite failed to run Cannot find module 'vue-template-compiler when using jest in vue3 but it did not help me.
ah, I managed to find an answer in https://github.com/testing-library/vue-testing-library/issues/176
install this version of library #testing-library/vue#next such as
yarn add -D #testing-library/vue#next
or
npm i --save-dev #testing-library/vue#next

How to configure jest and babel to work with vue?

I'm adding jest to my project and can't configure it properly.
When I run $ jest I'm getting a SyntaxError: Cannot use import statement outside a module.
I've searched for quite a while for a solution but none worked...
Can someone please help me config jest and babel?
Here are my files:
package.json
{
"name": "psyque-frontend",
"version": "1.0.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test": "jest"
},
"dependencies": {
"actioncable-vue": "^1.5.1",
"axios": "^0.19.2",
"chart.js": "^2.9.3",
"material-design-icons-iconfont": "^5.0.1",
"qs": "^6.9.1",
"vue": "^2.6.11",
"vue-axios": "^2.1.5",
"vue-chartjs": "^3.5.0",
"vue-router": "^3.1.6",
"vuetify": "^1.5.21",
"vuetify-datetime-picker": "^1.1.3"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^3.12.1",
"#vue/cli-plugin-eslint": "^3.12.1",
"#vue/cli-service": "^3.12.1",
"#vue/test-utils": "^1.0.0-beta.32",
"babel-eslint": "^10.1.0",
"babel-jest": "^25.1.0",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.2.3",
"jest": "^25.1.0",
"stylus": "^0.54.7",
"stylus-loader": "^3.0.1",
"ts-jest": "^25.2.1",
"vue-cli-plugin-vuetify": "^0.6.3",
"vue-jest": "^3.0.5",
"vue-template-compiler": "^2.6.11",
"vuetify-loader": "^1.4.3"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
jest.config.js
module.exports = {
moduleFileExtensions: ['js', 'json', 'ts', 'vue'],
transform: {
'.*\\.(js|jsx)$': 'babel-jest',
'.*\\.(ts|tsx)?$': 'ts-jest',
'.*\\.(vue)$': 'vue-jest'
},
verbose: true
};
babel.config.js
module.exports = {
presets: [
'#vue/app'
]
};
Footer.test.js
import { mount } from '#vue/test-utils'
import Footer from './Footer'
describe('Footer', () => {
test('is a Vue instance', () => {
const wrapper = mount(Footer);
expect(wrapper.isVueInstance()).toBeTruthy();
})
});
Thank you very much for your support!

How to add UglifyJS to a VueJS project

I need to add UglifyJS to a VueJS CLI 3 project.
My vue.config.js:
var webpack = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
// ...
optimization: {
minimizer: [new UglifyJsPlugin()],
},
publicPath: "./",
plugins: [
// ...
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
]
}
This is the error message I get after trying to run npm run build.
Invalid options in vue.config.js: "optimization" is not allowed.
"plugins" is not allowed
I tried updating all packages and install the webpack UglifyJS plugin.
package.json
{
"name": "app",
"version": "0.1.0",
"private": false,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^2.6.5",
"firebase": "^6.3.0",
"material-icons": "^0.3.1",
"register-service-worker": "^1.6.2",
"vue": "^2.6.10",
"vue-flickity": "^1.1.2",
"vue-router": "^3.0.3",
"vuetify": "^1.5.5",
"vuex": "^3.0.1"
},
"devDependencies": {
"#mdi/font": "^3.8.95",
"#vue/cli-plugin-babel": "^3.8.0",
"#vue/cli-plugin-eslint": "^3.8.0",
"#vue/cli-plugin-pwa": "^3.8.0",
"#vue/cli-service": "^3.8.0",
"#vue/eslint-config-prettier": "^4.0.1",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"material-design-icons-iconfont": "^5.0.1",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"vue-cli-plugin-vuetify": "^0.5.0",
"vue-template-compiler": "^2.6.10",
"vuetify-loader": "^1.0.5",
"webpack": "^4.40.2"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"#vue/prettier"
],
"rules": {"no-console": "off"},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
How to add UglifyJS to the VueJS project?