I'm trying to create a custom html element with vuejs and vite. But every time I build the element, process.env.NODE_ENV is added to the js and so using the component outside of vuejs fails. My goal is an element I can use in any generic html file. I've done this before with stenciljs, but not with vuejs and I can't figure out what I'm doing wrong. I have no reference to env in my vue file.
My build command:
"build:simple": "vite build --config ./src/components/simple/vite.simple.config.ts"
vite.simple.config.ts:
import vue from '@vitejs/plugin-vue';
import { defineConfig } from 'vite';
const componentName = 'simple';
const entry = `src/components/${componentName}/index.ts`;
const fileName = `${componentName}`;
const outDir = `./dist/${componentName}`;
const name = `${componentName}`;
export default defineConfig({
plugins: [
vue( {customElement: true} ),
],
logLevel: 'info',
build: {
outDir: outDir,
lib: {
entry: entry,
name: name,
fileName: fileName
},
},
});
My index.ts file:
import { defineCustomElement } from 'vue';
import Simple from "./Simple.vue";
const SimpleElement = defineCustomElement(Simple);
function loader() {
const ceRegistry = window.customElements
if ( ceRegistry === null) {
return;
} else {
if ( typeof(ceRegistry.get('my-simple') ) === 'undefined') {
ceRegistry.define('my-simple', SimpleElement);
}
return;
}
}
const ceRegistry = window.customElements;
if ( ceRegistry !== null) {
if ( typeof(ceRegistry.get('my-simple') ) === 'undefined') {
ceRegistry.define('my-simple', SimpleElement);
}
}
declare module 'vue' {
export interface GlobalComponents {
'SimpleElement': typeof SimpleElement
}
}
export { Simple, SimpleElement, loader }
The only thing at all complicated I'm doing is setting the paths to the various files based on a variable, which I'm doing because I want to re-use the config for different components and then just change the componentName variable each time. Based on https://vuejs.org/guide/extras/web-components, this should do it. But I've got over 100 instances of process.env.NODE_ENV in my js, so it is constantly failing.
I'm using vue 3.3.4, vitejs/plugin-vue 4.4.0, and vite 5.0.10.
Any ideas?
Edit for clarification: The code is sort of duplicated because I wasn't sure if I would need to import the loader function and run it in the consuming html. When I create a custom html element with stencil, I can import the loader function into an angular app and run it to register the component. So I just wasn't sure if I should do all of the registration in the index.ts or create a function and export it.
Here are two examples of the code from simple.js (188 references to process.env.NODE_ENV total):
const K = process.env.NODE_ENV !== "production" ? Object.freeze({}) : {},
ht = process.env.NODE_ENV !== "production" ? Object.freeze([]) : []
And 2 from simple.umd.js (188 references to process.env.NODE_ENV total):
const L=process.env.NODE_ENV!=="production"?Object.freeze({}):{},
ct=process.env.NODE_ENV!=="production"?Object.freeze([]):[]
I took my index.ts file down to just this and I still have 51 references in each of the js files:
import { defineCustomElement } from 'vue';
import Simple from "./Simple.vue";
const SimpleElement = defineCustomElement(Simple);
loaderfunction include the same code as the loader function. You could just call theloaderfunction below... And how can you have instances ofprocess.env.NODE_ENVin your code? Can you clarify a bit more?