Im trying to publish locally a webpack on Vite to test micro front ends, but when I run my host app, it doesn't find the remoteEntry.js, and thats because when I try to access my remoteEntry.js, it doesn't exist. Does anyone know why?
This is my vite.conf on the remote
import { defineConfig } from "vite";
import vue from "#vitejs/plugin-vue";
// https://vitejs.dev/config/
// vite.config.js
import federation from "#originjs/vite-plugin-federation";
export default defineConfig({
server: {
port: 8080,
},
plugins: [
vue(),
federation({
name: "myLib",
filename: "remoteEntry.js",
// Modules to expose
exposes: {
"./Counter": "./src/components/Counter.vue",
},
remotes: {},
shared: ["vue"],
}),
],
});
And this is the config on the remote side:
import { defineConfig } from 'vite'
import vue from '#vitejs/plugin-vue'
import federation from "#originjs/vite-plugin-federation";
export default defineConfig({
server: {
port: 8081,
},
plugins: [
vue(),
federation({
name: "myApp",
remotes: {
myLib: "http://localhost:8080/assets/remoteEntry.js",
},
shared: ["vue"],
}),
],
});
When i try to access my dependencies on the host side, this error pops on the console:
Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:8080/assets/remoteEntry.js
Thank you very much
Related
I'm having an issue when attempting to import a webpack-built Node package into my NextJS project.
In this scenario, I have two packages my-site and toolkit. I am trying to import a function, myFunction into my my-site package from my toolkit package.
In a file in my my-site package, I have the following code:
import { myFunction} from "#myorganization/toolkit";
myFunction();
And upon visiting the relevant page of my Next site in the browser, I receive this error:
TypeError: _myorganization_toolkit__WEBPACK_IMPORTED_MODULE_3__.myFunction is not a function
My webpack config in the toolkit package is as follows:
import path from "path";
const config = {
mode: "development",
entry: "./ts/main.ts",
devtool: "source-map",
experiments: {
outputModule: true,
},
output: {
path: path.resolve("./js"),
filename: "main.js",
globalObject: "this",
library: {
type: "module",
}
},
module: {
rules: [
{
test: /\.ts(x)?$/,
loader: "ts-loader",
exclude: /node_modules/,
},
]
},
resolve: {
extensions: [
".tsx",
".ts",
".js",
],
},
};
export default config;
Oddly enough, if I log the value of myFunction to the console:
import { myFunction} from "#myorganization/toolkit";
console.log("myFunction: ", myFunction);
myFunction();
The stdout of the Next server correctly reports that it is a function, but the browser console reports it as undefined.
This leads me to believe that the issue has something to do with NextJS not 'forwarding' on the code from toolkit to the browser, but I am not sure how to fix this, nor am I even certain that that is actually what is going on.
How do I fix this issue?
I'm trying to solve the Vite build error I get:
RollupError: Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
The file name reported with this error points to
my web worker code, so I assumed that this setting belongs to the worker section in vite.config.ts:
import { defineConfig } from "vite";
import preact from "#preact/preset-vite";
import basicSsl from "#vitejs/plugin-basic-ssl";
import { NodeGlobalsPolyfillPlugin } from "#esbuild-plugins/node-globals-polyfill";
import { NodeModulesPolyfillPlugin } from "#esbuild-plugins/node-modules-polyfill";
import rollupNodePolyFill from "rollup-plugin-node-polyfills";
export default defineConfig({
plugins: [
preact(),
basicSsl(),
],
server: {
port: 3001,
https: true,
},
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: "globalThis",
},
// Enable esbuild polyfill plugins
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
}),
NodeModulesPolyfillPlugin(),
],
},
},
worker: {
rollupOptions: {
output: {
format: "esm",
},
},
},
build: {
rollupOptions: {
plugins: [
// Enable rollup polyfills plugin
// used during production bundling
rollupNodePolyFill(),
],
output: {
format: "esm",
},
},
},
});
Additionally, I set the output format in the build rollup options. However, neither of the two settings are applied and I still get the said error.
What is the correct way to change the rollup output format setting in Vite?
The worker output format must be specified directly in the worker config key, not its rollup options:
import { defineConfig } from "vite";
import preact from "#preact/preset-vite";
import basicSsl from "#vitejs/plugin-basic-ssl";
import { NodeGlobalsPolyfillPlugin } from "#esbuild-plugins/node-globals-polyfill";
import { NodeModulesPolyfillPlugin } from "#esbuild-plugins/node-modules-polyfill";
import rollupNodePolyFill from "rollup-plugin-node-polyfills";
export default defineConfig({
plugins: [
preact(),
basicSsl(),
],
server: {
port: 3001,
https: true,
},
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: "globalThis",
},
// Enable esbuild polyfill plugins
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
}),
NodeModulesPolyfillPlugin(),
],
},
},
worker: {
format: "es",
},
build: {
rollupOptions: {
plugins: [
// Enable rollup polyfills plugin
// used during production bundling
rollupNodePolyFill(),
],
output: {
format: "esm",
},
},
},
});
I'm doing a microfrontend project using vite plugin federation. The mfe childs uses i18n and pinia.
After compiling and importing the mfe childs into de host, it doesn't work because it can't resolve de dependencies (i18n and pinia).
Error with i18n:
Uncaught TypeError: _ctx.$t is not a function
Error with pinia:
Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?
const pinia = createPinia()
app.use(pinia)
To resolve it, it's not enough installing these dependencies into the host, i also need to config i18n with the same json structure and pinia with the same stores structures.
Due to this, i'll have some duplicated files in different repositories so i think it's not the best way to handle this.
Do you have any workaround idea?
(mfe-child) vite.config.ts
export default defineConfig({
plugins: [
vue(),
federation({
name: "mfe-boilerplate",
filename: "remoteEntry.js",
exposes: {
"./ExampleView": "./src/modules/example-module/views/ExampleView.vue",
},
shared: ["vue", "vue-i18n", "pinia"],
}),
vueI18n({
include: path.resolve(__dirname, "./src/locales/**"),
compositionOnly: true,
}),
],
build: {
minify: false,
}
});
(mfe-host) vite.config.ts
export default defineConfig({
plugins: [
vue(),
federation({
name: "host-app",
remotes: {
mfe_boilerplate: "http://localhost:4174/assets/remoteEntry.js",
},
shared: ["vue", "vue-i18n", "pinia"],
}),
],
});
Additional config to make i18n work:
(mfe-host) main.ts
const i18n = createI18n({
locale: "en",
messages: { // this is the same JSON structure and content from mfe-child
en: {
"mfe-child-page-title": "Title from host"
},
},
});
app.use(i18n);
I have some package on the node modules that have a package.json
{
"sideEffects": false,
"module": "./index.js",
"main": "../node/{moduleName}/index.js",
"types": "./index.d.ts"
}
When vite trying to build it throw the error
[commonjs--resolver] Failed to resolve entry for package "/drone/src/node_modules/{module}/core/node_modules/#mui/material/LinearProgress". The package may have incorrect main/module/exports specified in its package.json: ENOENT: no such file or directory, lstat '/drone/src/node_modules/{module}/core/node_modules/#mui/material/LinearProgress/index.js'
error during build:
My vite config is
import { defineConfig } from 'vite';
import RubyPlugin from 'vite-plugin-ruby';
export default defineConfig({
build: {
assetsInlineLimit: 2048,
rollupOptions: {
external: ['react', 'react-dom'],
output:{
globals: {
react: 'React',
'react-dom': 'ReactDom'
},
manualChunks(id) {
if (id.includes('node_modules')) {
return id.toString().split('node_modules/')[1].split('/')[0].toString();
}
}
}
}
},
plugins: [RubyPlugin()],
});
I find this Solution, but I can't remove it from the package.json
How can I solve this problem ?
If you are using the vite bundler, you can resolve it with an alias using https://vitejs.dev/config/shared-options.html#resolve-alias
Ex:
resolve: {
alias: [
{
find: /#mui\/material/,
replacement: path.resolve(__dirname, 'node_modules', '#mui', 'material'),
},
],
},
I'm migrating my Vue plugin from Vue CLI to Vitejs.
With vue-cli-service build I generate three files: index.common.js, index.umd.js and index.umd.min.js
In package.json I refer to these files with:
"main": "dist/index.common.js",
"unpkg": "dist/index.umd.min.js",
But now migrating to ViteJS npm run build creates js files with random strings index.25e1eb44.js.
How do I use unpkg with ViteJS in package.json?
By reading other code, I found a good solution:
import { defineConfig } from 'vite';
import vue from '#vitejs/plugin-vue';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';
const path = require('path');
// https://vitejs.dev/config/
export default defineConfig({
build: {
lib: {
entry: path.resolve(__dirname, 'src/index.js'),
name: 'VueResponsiveVideoBackgroundPlayer',
fileName: 'vue-responsive-video-background-player',
},
rollupOptions: {
external: ['vue'],
output: {
// Provide global variables to use in the UMD build
// Add external deps here
globals: {
vue: 'Vue',
},
},
},
},
plugins: [
vue(),
cssInjectedByJsPlugin(),
],
});
For more, read here: https://vitejs.dev/config/build-options.html#build-commonjsoptions