vite-plugin-esm2cjs
This commit is contained in:
		
							parent
							
								
									8795fb23f2
								
							
						
					
					
						commit
						8e702586d5
					
				| 
						 | 
				
			
			@ -1,9 +1,16 @@
 | 
			
		|||
import { builtinModules } from 'module'
 | 
			
		||||
import { defineConfig } from 'vite'
 | 
			
		||||
import esm2cjs from '../../scripts/vite-plugin-esm2cjs'
 | 
			
		||||
import pkg from '../../package.json'
 | 
			
		||||
 | 
			
		||||
export default defineConfig({
 | 
			
		||||
  root: __dirname,
 | 
			
		||||
  plugins: [
 | 
			
		||||
    esm2cjs([
 | 
			
		||||
      'execa',
 | 
			
		||||
      'node-fetch',
 | 
			
		||||
    ]),
 | 
			
		||||
  ],
 | 
			
		||||
  build: {
 | 
			
		||||
    outDir: '../../dist/main',
 | 
			
		||||
    lib: {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,40 @@
 | 
			
		|||
import { builtinModules } from 'module'
 | 
			
		||||
import { Plugin, build } from 'vite'
 | 
			
		||||
import resolve from 'vite-plugin-resolve'
 | 
			
		||||
 | 
			
		||||
export default function esm2cjs(modules: string[]): Plugin {
 | 
			
		||||
  return resolve({
 | 
			
		||||
    ...modules.reduce((memo, moduleId) => Object.assign(memo, {
 | 
			
		||||
      async [moduleId](args) {
 | 
			
		||||
        await build({
 | 
			
		||||
          plugins: [
 | 
			
		||||
            {
 | 
			
		||||
              name: 'vite-plugin[node:mod-to-mod]',
 | 
			
		||||
              enforce: 'pre',
 | 
			
		||||
              resolveId(source) {
 | 
			
		||||
                if (source.startsWith('node:')) {
 | 
			
		||||
                  return source.replace('node:', '')
 | 
			
		||||
                }
 | 
			
		||||
              },
 | 
			
		||||
            }
 | 
			
		||||
          ],
 | 
			
		||||
          build: {
 | 
			
		||||
            outDir: args.dir,
 | 
			
		||||
            minify: false,
 | 
			
		||||
            emptyOutDir: false,
 | 
			
		||||
            lib: {
 | 
			
		||||
              entry: require.resolve(moduleId),
 | 
			
		||||
              formats: ['cjs'],
 | 
			
		||||
              fileName: () => `${moduleId}.js`,
 | 
			
		||||
            },
 | 
			
		||||
            rollupOptions: {
 | 
			
		||||
              external: [
 | 
			
		||||
                ...builtinModules,
 | 
			
		||||
              ],
 | 
			
		||||
            },
 | 
			
		||||
          },
 | 
			
		||||
        })
 | 
			
		||||
      },
 | 
			
		||||
    } as Parameters<typeof resolve>[0]), {}),
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue