Unusual class title following npm packaging

Currently, I am working on developing a Vue 3 library with TypeScript. We are using Rollup for bundling the library. Everything works as expected within the library itself. However, after packing and installing it in another application, we noticed that the class names are different and issues arise with functionalities like instanceof not working correctly anymore. To investigate this issue, I created a sample function that logs some information. Upon inspecting the logs, I observed that the class name appears strange and completely different in the app.

import { ZodString } from 'zod'

export const logZodString = (zodTypeFromApp: ZodString) => {
  console.log('TypeName', zodTypeFromApp._def.typeName)
  console.log('ZodString.name', ZodString.name)
  console.log('ZodString', ZodString)
}

The function will be called like:

logZodString(z.string())

Logs in the library -> all correct:

TypeName ZodString
ZodString.name ZodString
ZodString [class ZodString extends ZodType] { create: [Function (anonymous)] }

Logs in the app where the library is installed -> unusual class name:

TypeName ZodString
ZodString.name $i
ZodString [class $i extends wt] { create: [Function (anonymous)] }

It seems like the class names or types from Zod are getting an alias name, making it impossible to use instanceof or check for equal type names. Could there be something missing in vite.config.ts?

Here is the build property from vite.config.ts:

 build: {
    target: 'esnext',
    lib: {
      entry: path.resolve(__dirname, 'src/index.ts'),
      name: 'mylib',
      fileName: format => `mylib.${format}.js`,
    },
    rollupOptions: {
      external: ['vue', 'pinia'],
      output: {
        inlineDynamicImports: true,
        globals: {
          vue: 'Vue',
          pinia: 'Pinia',
        },
      },
    },
  }

Answer №1

My issue was resolved by updating the alias and rollupOptions in my vite.config.ts file.

vite.config.ts:

  resolve: {
    alias: {
      pinia: 'pinia/dist/pinia.esm-browser.js',
      zod: 'zod/dist/zod.esm-browser.js',
    },
  },
  build: {
    target: 'esnext',
    lib: {
      entry: path.resolve(__dirname, 'src/index.ts'),
      name: 'mylib',
      fileName: format => `mylib.${format}.js`,
    },
    rollupOptions: {
      external: ['vue', 'pinia', 'zod'],
      output: {
        inlineDynamicImports: true,
        globals: {
          vue: 'Vue',
          pinia: 'Pinia',
          zod: 'Zod',
        },
      },
    },
  },

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Inject the data within Observable<Object> into Observable<Array>

I'm faced with a situation where I have two distinct API endpoints. One endpoint returns a single Card object, while the other endpoint returns an Array of Card objects. My goal is to retrieve the first Card from the single Card endpoint and place it ...

What is the proper way to utilize e.key and e.target.value in TypeScript when coding with React?

I'm struggling to get e.key and e.target.value working with the following code: const handleInputKeyPress = (e: React.KeyboardEvent<HTMLInputElement> ) => { if(e.key ==='Enter') { dfTextQuery(e.target.value) } }; Why is & ...

Are you looking for straightforward dynamic directives that come with dynamic controllers and a scope?

Feeling like I have a simple problem to solve here. Working within the confines of a TypeScript + Angular application. Within a controller, I've got an array of similar directives that I want to utilize. These are essentially the panels strewn throug ...

possibilities for inquiries within the Watson dialogue API

Is there a method to retrieve the potential response choices for a specific inquiry using Watson conversation API? For instance, if I have a chatbot app and need to present users with a menu of options to choose from in certain scenarios. I am currently ...

Using React Material UI to create multiple collapse components

Currently, I am facing an issue where all the collapses in my list are linked to one state for "open." This means that if I open one list, all the other lists also open. I am looking for a way to keep the collapses separate from each other without needing ...

TypeScript generic type and potential absence of a value

Recently, I've been delving into Facebook's immutable library and exploring their TypeScript bindings. Consider this snippet of code: const list: User[] = ...; list.map(user => ...) The lambda parameter user correctly has the type of User. ...

Exploring Excel files using the exceljs library

Using the exceljs module, I am able to read Excel files when they are in the same folder as my application. The code works perfectly in this scenario: var workbook = new Excel.Workbook(); workbook.xlsx.readFile('Data.xls') .then(function() ...

Google's reCAPTCHA issue: systemjs not found

Currently, I am attempting to integrate Google's reCAPTCHA into an Angular application by following a helpful tutorial found here. However, I have encountered a problem as the systemjs.config.js file seems to be missing from my Angular CLI project. An ...

What is the relationship between Typescript references, builds, and Docker?

I am facing a dilemma with my projectA which utilizes a common package that is also needed by my other Nodejs services. I am unsure of the best approach to package this in a Docker file. Ideally, running tsc build would compile both the project and the dep ...

Utilize npm to incorporate external JavaScript libraries into Meteor 1.3

Trying to integrate the OpenSeadragon library into my Meteor app has been a bit challenging. After successfully installing it via npm using meteor npm install openseadragon, I found that the OpenSeadragon docs only offer an example using the script tag. T ...

I am unable to employ filtering in TypeScript

Hey there, I'm trying to filter some JSON data randomly by using this function, but I keep running into an error with my variable called filteredArray. The error message says "Property 'filter' does not exist on type 'Dispatch<SetSta ...

Error message: The provider is not being recognized by react-redux while using NextJS with RTK and

Struggling to integrate Redux RTK into my Next JS 13.4 app has been quite the challenge. No matter how many tutorials I follow, I keep encountering the same error in my provider.ts file. 'use client' import { store } from './store'; imp ...

A comprehensive guide on extracting attachments and inline images from EML files and transforming them into HTML format

Our challenge is parsing .EML files to display full emails with inline images and attachments on a webpage. We have successfully extracted the HTML body using MimeKIT for .net, but we are unsure of how to distinguish between inline images and regular email ...

Creating a custom definition file for TypeScript

Currently, I am facing an issue with a third-party library that provides global functions similar to jQuery ($ .functionName()), but unfortunately there is no definition file available. Due to this, my attempt to write my own file has been unsuccessful as ...

Encountered an issue when executing npm command due to a permission error

My hosting is on a digitalocean droplet and my website is built in Laravel. When I try to run the following command, I encounter the attached error. The command that I am running as a root user is: npm run dev Can anyone help me with this issue? Thank y ...

The CSS files undergo modifications when executing the command "npm run dev"

I've been working on an open-source project where I encountered a bug. Even when there are no images to display, the "Load More" button in the web browser extension still appears. To fix this, I decided to add the class `removeButton` to the button an ...

Vercel encountered a compilation error while trying to process the React code

Initially, I encountered an error with react-icons. Now, I am facing errors with the typewriter effect library. The code works perfectly fine on localhost. However, whenever I attempt to install the typewriter effect using npm i, I encounter the following ...

The self-registration of the module was unsuccessful. A custom build C++ node module compiled with cmake-js was utilized within the Electron app

Currently working on a project where I created a C++ module for Node and compiled it with CMake. However, when I tried to use this module in my main Electron app, it resulted in errors. To demonstrate the issue, I have created a minimal repository here: h ...

Use contextual type when determining the return type of a function, rather than relying solely on

When using Typescript 2.2.2 (with the strictNullChecks option set to true), I encountered an unexpected behavior. Is this a bug or intentional? interface Fn { (value: any): number; } var example1: Fn = function(value) { if (value === -1) { ...

The custom component is not updating the NgIf directive in HTML even though it receives a boolean variable

I am struggling with a custom component that includes an *ngIf in its view to handle a boolean variable, but for some reason the *ngIf directive is not working. Here is the code snippet: Component @Input('title') titleText; @Input('backButt ...