Skip to content

Commit 2579a87

Browse files
committed
chore: vite 配置优化
1 parent b543e1f commit 2579a87

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

package-lock.json

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"prepare": "husky install",
77
"dev": "vite build --watch",
88
"build": "vite build",
9-
"lint": "eslint --ext=.cjs,.mjs,.ts,.tsx src/ test/ && tsc --project tsconfig.json --noEmit",
9+
"lint:es": "eslint --ext=.cjs,.mjs,.ts,.tsx src/ test/",
10+
"lint:type": " tsc --project tsconfig.json",
11+
"lint": "npm run lint:es && npm run lint:type",
1012
"test": "vitest run",
1113
"test:coverage": "vitest run --coverage"
1214
},
@@ -16,17 +18,17 @@
1618
"engineStrict": true,
1719
"sideEffects": false,
1820
"type": "module",
19-
"module": "dist/index.js",
21+
"module": "dist/index.mjs",
2022
"main": "dist/index.cjs",
2123
"types": "dist/index.d.ts",
2224
"exports": {
2325
".": {
24-
"import": "./dist/index.js",
26+
"import": "./dist/index.mjs",
2527
"require": "./dist/index.cjs",
2628
"types": "./dist/index.d.ts"
2729
},
2830
"./helpers": {
29-
"import": "./dist/helpers.js",
31+
"import": "./dist/helpers.mjs",
3032
"require": "./dist/helpers.cjs",
3133
"types": "./dist/helpers.d.ts"
3234
},
@@ -77,10 +79,10 @@
7779
"eslint-plugin-prettier": "^4.2.1",
7880
"husky": "^8.0.3",
7981
"lint-staged": "^13.2.1",
80-
"rollup-plugin-node-externals": "^5.1.2",
8182
"typescript": "^5.0.4",
8283
"vite": "^4.2.1",
8384
"vite-plugin-dts": "^2.2.0",
85+
"vite-plugin-node-externals": "^0.0.1",
8486
"vitest": "^0.30.1"
8587
}
8688
}

vite.config.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
import { createRequire } from 'node:module';
2-
import externals from 'rollup-plugin-node-externals';
3-
1+
import path from 'path';
2+
import nodeExternals from 'vite-plugin-node-externals';
43
import dts from 'vite-plugin-dts';
5-
// https://cn.vitest.dev/config/
64
import { defineConfig } from 'vitest/config';
7-
8-
const require = createRequire(import.meta.url);
9-
10-
const pkg = require('./package.json');
5+
import pkg from './package.json';
116

127
// https://vitejs.dev/config/
138
export default defineConfig({
149
plugins: [
10+
nodeExternals(),
1511
dts({
1612
insertTypesEntry: true,
1713
}),
18-
{
19-
...externals(),
20-
enforce: 'pre',
21-
},
2214
],
2315
define: {
2416
'process.env.PKG_NAME': JSON.stringify(pkg.name),
@@ -28,9 +20,14 @@ export default defineConfig({
2820
lib: {
2921
entry: ['src/index.ts', 'src/helpers.ts'],
3022
formats: ['es', 'cjs'],
23+
fileName(format, entryName) {
24+
const ext = format === 'es' ? '.mjs' : '.cjs';
25+
const basename = path.basename(entryName, '.ts');
26+
return `${basename}${ext}`;
27+
},
3128
},
3229
modulePreload: false,
33-
sourcemap: false,
30+
sourcemap: true,
3431
minify: false,
3532
copyPublicDir: false,
3633
outDir: 'dist',

0 commit comments

Comments
 (0)