So I'm using vite-plugin-lib-inject-css
inside my shared package in monorepo (internal lib), to inject css inside compiled vue js components. This works.
But I also have utils folder with helper functions which is also being built with vite, and this lib it seems, also injects component js files into that helpers file, making circular dep.
Here is my vite config:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import * as path from 'path';
import { libInjectCss, scanEntries } from 'vite-plugin-lib-inject-css';
export default defineConfig({
plugins: [
vue(),
libInjectCss({
formats: ['es'],
entry: {
index: 'src/index.ts', // Don't forget the main entry!
...scanEntries(['src/components']),
},
}),
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
outDir: 'dist/assets',
lib: {
entry: 'src/index.ts',
name: 'shared',
formats: ['es'],
fileName: (format) => `shared.${format}.js`,
},
rollupOptions: {
// Make sure to externalize Vue and include it in your peerDependencies
external: ['vue'],
output: {
// intro: 'import "./style.css";', // SM: if no chunks, this is the easiest way to inject css into one js file
manualChunks(id) {
if (id.includes('components')) {
// Extract the file name without extension
const componentName = path.basename(id).split('.')[0];
return `components/${componentName}`;
}
return 'utils';
},
entryFileNames: `[name].js`,
chunkFileNames: `[name].js`,
assetFileNames: `[name].[ext]`,
globals: {
vue: 'Vue',
},
},
},
},
});
and this is file with those js imports:
import "./HelloWorld.js";
import "./Test.js";
const a = (t) => t.slice(0, 1).toUpperCase() + t.slice(1), i = (t, o) => {
const e = t.__vccOpts || t;
for (const [c, r] of o)
e[c] = r;
return e;
};
export {
i as _,
a as c
};
When manually removing those imports from dist folder, my consuming package can render correctly, otherwise it fails with hmr.ts:204 ReferenceError: Cannot access 'n' before initialization at HelloWorld.js:16:25
Here is also compiled HelloWorld:
import "/@fs/Users/srdjan.medjo/SrleProjects/uar/vtrips.ownerportalfe/packages/shared/dist/assets/HelloWorld.css?t=1707381720026";
import { defineComponent as r, ref as l, openBlock as a, createElementBlock as c, createElementVNode as e, toDisplayString as o } from "/node_modules/.vite/deps/vue.js?v=b631ae9a";
import { _ as n } from "/@fs/Users/srdjan.medjo/SrleProjects/uar/vtrips.ownerportalfe/packages/shared/dist/assets/utils.js?t=1707381720026";
const _ = { class: "h1" }, d = { class: "color" }, m = /* @__PURE__ */ r({
__name: "HelloWorld",
props: {
msg: { default: "default msg value" }
},
setup(p) {
const t = l(34);
return (s, i) => (a(), c("div", null, [
e("h1", _, "This is from shared " + o(t.value), 1),
e("div", d, o(s.msg), 1)
]));
}
}), v = /* @__PURE__ */ n(m, [["__scopeId", "data-v-b1d33612"]]);
export {
v as default
};
Any ideas on how to approach this?