vite-plugin-dts icon indicating copy to clipboard operation
vite-plugin-dts copied to clipboard

Add option to exclude import paths from transformation

Open sionzee opened this issue 2 years ago • 1 comments

The new feature from https://github.com/qmhc/vite-plugin-dts/issues/73 allows excluding types just from the aliases. In my scenario, I have a monorepo where each project is an individual project and can have other monorepo's projects in dependency. I'm using 'build.rollupOptions.external' to prevent bundling the other projects into the project itself.

I propose to make 'aliasesExclude' less strict and allow it for any import-path match or respecting the value in the build.rollupOptions.external.

Config as example:

build: {
  lib: {
    entry: './index.ts',
      name: 'some-project',
      formats: ['es'],
      fileName: () => 'index.js'
  },
  rollupOptions: {
    external: ['@monorepo/shared']
  }
},
resolve: {
  alias: [
    {
      find: /^\/?@monorepo\/([\w-]+)/,
      replacement: `${path.resolve(__dirname, '../')}/$1/index.ts`
    }
  ]
},

sionzee avatar Jul 26 '22 10:07 sionzee

In the meantime, I have written a workaround:

  beforeWriteFile: (filePath, content) => {
    return {
      filePath,
      content: content.replace(/(import\s+.*?\s+from)\s+["'].*?\.\.\/shared\/index.ts["'];/g, '$1 "@monorepo/shared";')
    }
  }

So it will replace any ../shared/index.ts for @monorepo/shared

sionzee avatar Jul 26 '22 10:07 sionzee