Skip to content

Commit

Permalink
Add CommonJS exports
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmyersdev committed Oct 10, 2022
1 parent c3da8fa commit 2ccf195
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 142 deletions.
48 changes: 0 additions & 48 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed .yarn/cache/add-npm-2.0.6-c96b78d852-e2d23d4049.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@
"vite-plugin"
],
"type": "module",
"main": "./dist/index.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"build": "tsc",
"build": "vite build && tsc",
"test": "vite build -c test/vite.config.ts"
},
"packageManager": "[email protected]",
"devDependencies": {
"@rollup/plugin-node-resolve": "^13.3.0",
"@types/node": "^18.7.13",
"add": "^2.0.6",
"typescript": "^4.8.2",
"vite": "^3.0.9",
"vite-plugin-inspect": "^0.6.0",
"yarn": "^1.22.19"
},
"peerDependencies": {
"vite": "^2.0.0 || ^3.0.0"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"chalk": "^5.0.1"
}
}
116 changes: 59 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,64 +31,66 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
return {
name: 'vite-plugin-externalize-deps',
config: (_config, _env) => {
if (existsSync(optionsResolved.useFile)) {
const externalDeps = new Set<RegExp>()
const {
dependencies = {},
devDependencies = {},
optionalDependencies = {},
peerDependencies = {},
} = parseFile(optionsResolved.useFile)

if (optionsResolved.deps) {
Object.keys(dependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

if (optionsResolved.devDeps) {
Object.keys(devDependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

if (optionsResolved.nodeBuiltins) {
builtinModules.forEach((builtinModule) => {
const builtinMatcher = new RegExp(`^(?:node:)?${builtinModule}$`)

externalDeps.add(builtinMatcher)
})
}

if (optionsResolved.optionalDeps) {
Object.keys(optionalDependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

if (optionsResolved.peerDeps) {
Object.keys(peerDependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

return {
build: {
rollupOptions: {
external: [
...externalDeps.values(),
],
},
if (!existsSync(optionsResolved.useFile)) {
throw new Error(`[vite-plugin-externalize-deps] The file specified for useFile (${optionsResolved.useFile}) does not exist.`)
}

const externalDeps = new Set<RegExp>()
const {
dependencies = {},
devDependencies = {},
optionalDependencies = {},
peerDependencies = {},
} = parseFile(optionsResolved.useFile)

if (optionsResolved.deps) {
Object.keys(dependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

if (optionsResolved.devDeps) {
Object.keys(devDependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

if (optionsResolved.nodeBuiltins) {
builtinModules.forEach((builtinModule) => {
const builtinMatcher = new RegExp(`^(?:node:)?${builtinModule}$`)

externalDeps.add(builtinMatcher)
})
}

if (optionsResolved.optionalDeps) {
Object.keys(optionalDependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

if (optionsResolved.peerDeps) {
Object.keys(peerDependencies).forEach((dep) => {
const depMatcher = new RegExp(`^${dep}(?:/.+)?$`)

externalDeps.add(depMatcher)
})
}

return {
build: {
rollupOptions: {
external: [
...externalDeps.values(),
],
},
}
},
}
},
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"compilerOptions": {
"alwaysStrict": true,
"declaration": true,
"emitDeclarationOnly": true,
"esModuleInterop": true,
"module": "ESNext",
"moduleResolution": "Node",
"noImplicitReturns": false,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./dist",
Expand Down
34 changes: 34 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { defineConfig } from 'vite'
import { externalizeDeps } from 'vite-plugin-externalize-deps'

// https://vitejs.dev/config/
export default defineConfig({
build: {
lib: {
entry: './src/index.ts',
fileName: 'index',
formats: [],
},
rollupOptions: {
external: [/^node:.*$/],
output: [
{
esModule: true,
exports: 'named',
format: 'es',
},
{
exports: 'named',
format: 'cjs',
inlineDynamicImports: true,
interop: 'auto',
},
],
},
sourcemap: true,
target: 'esnext',
},
plugins: [
externalizeDeps(),
],
})
32 changes: 2 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@ __metadata:
version: 6
cacheKey: 8

"#ansi-styles@npm:[email protected]":
version: 6.1.0
resolution: "ansi-styles@npm:6.1.0"
checksum: 7a7f8528c07a9d20c3a92bccd2b6bc3bb4d26e5cb775c02826921477377bd495d615d61f710d56216344b6238d1d11ef2b0348e146c5b128715578bfb3217229
languageName: node
linkType: hard

"#supports-color@npm:[email protected]":
version: 9.2.2
resolution: "supports-color@npm:9.2.2"
checksum: 976d84877402fc38c1d43b1fde20b0a8dc0283273f21cfebe4ff7507d27543cdfbeec7db108a96b82d694465f06d64e8577562b05d0520b41710088e0a33cc50
languageName: node
linkType: hard

"@esbuild/linux-loong64@npm:0.14.54":
version: 0.14.54
resolution: "@esbuild/linux-loong64@npm:0.14.54"
Expand Down Expand Up @@ -136,13 +122,6 @@ __metadata:
languageName: node
linkType: hard

"add@npm:^2.0.6":
version: 2.0.6
resolution: "add@npm:2.0.6"
checksum: e2d23d40494565dfed4acd65e478570c444db5ac6c053551ed429c39ea0f2c99d83df63e7befec936df601827d2254d06a2fb6f7dcfd2022e810b25eab818b8c
languageName: node
linkType: hard

"agent-base@npm:6, agent-base@npm:^6.0.2":
version: 6.0.2
resolution: "agent-base@npm:6.0.2"
Expand Down Expand Up @@ -256,13 +235,6 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^5.0.1":
version: 5.0.1
resolution: "chalk@npm:5.0.1"
checksum: 7b45300372b908f0471fbf7389ce2f5de8d85bb949026fd51a1b95b10d0ed32c7ed5aab36dd5e9d2bf3191867909b4404cef75c5f4d2d1daeeacd301dd280b76
languageName: node
linkType: hard

"chownr@npm:^2.0.0":
version: 2.0.0
resolution: "chownr@npm:2.0.0"
Expand Down Expand Up @@ -1412,12 +1384,12 @@ __metadata:
dependencies:
"@rollup/plugin-node-resolve": ^13.3.0
"@types/node": ^18.7.13
add: ^2.0.6
chalk: ^5.0.1
typescript: ^4.8.2
vite: ^3.0.9
vite-plugin-inspect: ^0.6.0
yarn: ^1.22.19
peerDependencies:
vite: ^2.0.0 || ^3.0.0
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 2ccf195

Please sign in to comment.