feat: build main, preload, react-ts
This commit is contained in:
		
							parent
							
								
									7b92b312d6
								
							
						
					
					
						commit
						0fb526481d
					
				| 
						 | 
					@ -1,7 +1,13 @@
 | 
				
			||||||
 | 
					import { join } from 'path'
 | 
				
			||||||
import { defineConfig } from 'vite'
 | 
					import { defineConfig } from 'vite'
 | 
				
			||||||
import react from '@vitejs/plugin-react'
 | 
					import react from '@vitejs/plugin-react'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// https://vitejs.dev/config/
 | 
					// https://vitejs.dev/config/
 | 
				
			||||||
export default defineConfig({
 | 
					export default defineConfig({
 | 
				
			||||||
  plugins: [react()]
 | 
					  plugins: [react()],
 | 
				
			||||||
 | 
					  base: './',
 | 
				
			||||||
 | 
					  build: {
 | 
				
			||||||
 | 
					    minify: false,
 | 
				
			||||||
 | 
					    outDir: join(process.cwd(), 'dist/react-ts'),
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,11 @@
 | 
				
			||||||
import { join } from 'path'
 | 
					import { join, relative } from 'path'
 | 
				
			||||||
import { readdirSync } from 'fs'
 | 
					import { readdirSync } from 'fs'
 | 
				
			||||||
import { OutputOptions, rollup, RollupOptions, RollupOutput } from 'rollup'
 | 
					import { OutputOptions, rollup, RollupOptions, RollupOutput, RollupError } from 'rollup'
 | 
				
			||||||
 | 
					import { build as viteBuild2 } from 'vite'
 | 
				
			||||||
import { optionsFactory } from './rollup.config'
 | 
					import { optionsFactory } from './rollup.config'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export type BuildResult = [RollupError | null, RollupOutput | null]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const TAG = '[build.ts]'
 | 
					const TAG = '[build.ts]'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function mainOptions() {
 | 
					function mainOptions() {
 | 
				
			||||||
| 
						 | 
					@ -26,10 +29,32 @@ function preloadOptions() {
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function doBuild(options: RollupOptions): Promise<[Error | null, RollupOutput | null]> {
 | 
					// build main、preload
 | 
				
			||||||
 | 
					async function rollupBuild(options: RollupOptions): Promise<BuildResult> {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    const build = await rollup(options)
 | 
					    const build = await rollup(options)
 | 
				
			||||||
    const output = await build.write(options.output as OutputOptions)
 | 
					    const optOutput = (options.output || {}) as OutputOptions
 | 
				
			||||||
 | 
					    const output = await build.write(optOutput)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    output.output.forEach(out => {
 | 
				
			||||||
 | 
					      const relativePath = relative(__dirname, optOutput.dir as string)
 | 
				
			||||||
 | 
					      console.log(TAG, `Build successful - ${join(relativePath, out.fileName)}`)
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return [null, output]
 | 
				
			||||||
 | 
					  } catch (error: any) {
 | 
				
			||||||
 | 
					    console.error(TAG, 'Build failed:\n', error)
 | 
				
			||||||
 | 
					    return [error, null]
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// build react-ts
 | 
				
			||||||
 | 
					async function buildReactTs(): Promise<BuildResult> {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    const output = await viteBuild2({
 | 
				
			||||||
 | 
					      root: join(__dirname, '../react-ts'),
 | 
				
			||||||
 | 
					      configFile: join(__dirname, '../react-ts/vite.config.ts'),
 | 
				
			||||||
 | 
					    }) as RollupOutput
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return [null, output]
 | 
					    return [null, output]
 | 
				
			||||||
  } catch (error: any) {
 | 
					  } catch (error: any) {
 | 
				
			||||||
| 
						 | 
					@ -37,16 +62,17 @@ async function doBuild(options: RollupOptions): Promise<[Error | null, RollupOut
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// build
 | 
					; (async () => {
 | 
				
			||||||
[mainOptions(), preloadOptions()].forEach(options => {
 | 
					
 | 
				
			||||||
  doBuild(options)
 | 
					  console.log(TAG, 'Build with rollup.')
 | 
				
			||||||
    .then(([err, output]) => {
 | 
					  try {
 | 
				
			||||||
      if (err) {
 | 
					    await Promise.all([
 | 
				
			||||||
        console.error(err)
 | 
					      rollupBuild(mainOptions()),
 | 
				
			||||||
        process.exit(1)
 | 
					      rollupBuild(preloadOptions()),
 | 
				
			||||||
      }
 | 
					    ])
 | 
				
			||||||
      output?.output.forEach(out => {
 | 
					    await buildReactTs()
 | 
				
			||||||
        console.log(TAG, `Build successed -- ${(out as any).facadeModuleId}`)
 | 
					  } catch (error) {
 | 
				
			||||||
      })
 | 
					    console.error(TAG, error)
 | 
				
			||||||
    })
 | 
					    process.exit(1)
 | 
				
			||||||
})
 | 
					  }
 | 
				
			||||||
 | 
					})();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue