Skip to content

Commit

Permalink
feat: better build
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Apr 28, 2023
1 parent c4ef2f6 commit 0949d3f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Expand Up @@ -110,5 +110,3 @@ pnpm-lock.yaml
yarn.lock

/types
/index.cjs
/index.js
19 changes: 9 additions & 10 deletions package.json
Expand Up @@ -2,15 +2,15 @@
"name": "vite-plugin-dynamic-import",
"version": "1.3.0",
"description": "Enhance Vite builtin dynamic import",
"type": "module",
"main": "index.js",
"types": "types",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./index.js",
"require": "./index.cjs",
"types": "./types/index.d.ts"
}
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./*": "./*"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +26,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"acorn": "^8.8.2",
"es-module-lexer": "^1.2.1",
"fast-glob": "^3.2.12"
},
Expand All @@ -45,8 +46,6 @@
"dynamic"
],
"files": [
"types",
"index.cjs",
"index.js"
"dist"
]
}
47 changes: 33 additions & 14 deletions vite.config.ts
Expand Up @@ -5,32 +5,46 @@ import { builtinModules } from 'node:module'
import { defineConfig } from 'vite'
import pkg from './package.json'

const isdev = process.argv.slice(2).includes('--watch')

export default defineConfig({
build: {
emptyOutDir: false,
minify: false,
outDir: '',
target: 'node14',
emptyOutDir: !isdev,
lib: {
entry: path.join(__dirname, 'src/index.ts'),
formats: ['es', 'cjs'],
fileName: format => format === 'cjs' ? '[name].cjs' : '[name].js',
entry: 'src/index.ts',
formats: ['cjs', 'es'],
fileName: format => format === 'es' ? '[name].mjs' : '[name].js',
},
rollupOptions: {
external: [
...builtinModules
.filter(m => !m.startsWith('_'))
.map(m => [m, `node:${m}`])
.flat(),
...Object.keys(pkg.dependencies),
'vite',
...builtinModules,
...builtinModules.map(m => `node:${m}`),
...Object.keys('dependencies' in pkg ? pkg.dependencies as object : {}),
],
output: {
exports: 'named',
},
},
},
plugins: [{
name: 'generate-types',
async closeBundle() {
if (process.env.NODE_ENV === 'test') return

removeTypes()
await generateTypes()
moveTypesToDist()
removeTypes()
},
}],
})

function removeTypes() {
fs.rmSync(path.join(__dirname, 'types'), { recursive: true, force: true })
}

function generateTypes() {
return new Promise(resolve => {
const cp = spawn(
Expand All @@ -46,7 +60,12 @@ function generateTypes() {
})
}

if (process.env.NODE_ENV !== 'test') {
fs.rmSync('types', { recursive: true, force: true })
generateTypes()
function moveTypesToDist() {
const types = path.join(__dirname, 'types')
const dist = path.join(__dirname, 'dist')
const files = fs.readdirSync(types).filter(n => n.endsWith('.d.ts'))
for (const file of files) {
fs.copyFileSync(path.join(types, file), path.join(dist, file))
console.log('[types]', `types/${file} -> dist/${file}`)
}
}

0 comments on commit 0949d3f

Please sign in to comment.