Skip to content

Commit

Permalink
fix: types error caoxiemeihao#9
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Feb 18, 2023
1 parent f7b21c5 commit abf84f8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"name": "nuxt-electron",
"version": "0.4.0",
"description": "Nuxt Integration with Electron",
"main": "index.mjs",
"types": "types",
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.js"
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"repository": {
Expand All @@ -17,9 +18,9 @@
"author": "草鞋没号 <[email protected]>",
"license": "MIT",
"scripts": {
"build": "npm run types && vite build",
"build": "vite build",
"dev": "vite build --watch",
"types": "rm -rf types && tsc -p tsconfig.build.json",
"types": "tsc -p tsconfig.build.json",
"prepublishOnly": "npm run build"
},
"peerDependencies": {
Expand All @@ -36,9 +37,8 @@
},
"files": [
"electron-env.d.ts",
"types",
"index.mjs",
"index.js"
"dist",
"types"
],
"keywords": [
"nuxt",
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
startup,
} from 'vite-electron-plugin'

// Fix tsc build error
import { NuxtModule } from '@nuxt/schema'

export interface ElectronOptions extends Partial<Configuration> { }

export default defineNuxtModule<ElectronOptions>({
Expand Down Expand Up @@ -67,4 +70,4 @@ export default defineNuxtModule<ElectronOptions>({
}
})
}
}) as any
})
47 changes: 43 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import fs from 'node:fs'
import path from 'node:path'
import { spawn } from 'node:child_process'
import { builtinModules } from 'node:module'
import { defineConfig } from 'vite'
import { builtinModules } from 'module'
import pkg from './package.json'

export default defineConfig({
build: {
minify: false,
emptyOutDir: false,
outDir: '',
lib: {
entry: 'src/index.ts',
formats: ['cjs', 'es'],
fileName: format => format === 'es' ? '[name].mjs' : '[name].js',
fileName: format => format === 'es' ? '[name].mjs' : '[name].cjs',
},
rollupOptions: {
external: [
Expand All @@ -28,4 +29,42 @@ export default defineConfig({
},
},
},
plugins: [{
name: 'generate-types',
async closeBundle() {
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(
process.platform === 'win32' ? 'npm.cmd' : 'npm',
['run', 'types'],
{ stdio: 'inherit' },
)
cp.on('exit', code => {
!code && console.log('[types]', 'declaration generated')
resolve(code)
})
cp.on('error', process.exit)
})
}

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 abf84f8

Please sign in to comment.