Skip to content

Commit

Permalink
Allow additional packages to be externalized
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmyersdev committed Jun 29, 2023
1 parent ccc4f59 commit b055c03
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
38 changes: 38 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ interface UserOptions {
deps: boolean,
devDeps: boolean,
except: Array<string | RegExp>,
/**
* Additional dependencies to externalize.
*
* @example
*
* ```ts
* externalizeDeps({
* include: [
* /^unlisted-dep(?:\/.*)?$/,
* ],
* })
* ```
*
* @default []
*/
include: Array<string | RegExp>,
nodeBuiltins: boolean,
optionalDeps: boolean,
peerDeps: boolean,
Expand Down Expand Up @@ -38,6 +54,12 @@ const parseFile = (file: string) => {
* // Or match patterns with regular expressions.
* /^@some\/obscure(?:\/.+)?$/,
* ],
* include: [
* // Match exact values with strings.
* '@some/obscure/dependency',
* // Or match patterns with regular expressions.
* /^@some\/obscure(?:\/.+)?$/,
* ],
* nodeBuiltins: true,
* optionalDeps: true,
* peerDeps: true,
Expand All @@ -52,6 +74,7 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
deps: true,
devDeps: false,
except: [],
include: [],
nodeBuiltins: true,
optionalDeps: true,
peerDeps: true,
Expand Down Expand Up @@ -116,6 +139,7 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
}

const depMatchers = Array.from(externalDeps)

const isException = (id: string) => {
return optionsResolved.except.some((exception) => {
if (typeof exception === 'string') {
Expand All @@ -126,6 +150,16 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
})
}

const isIncluded = (id: string) => {
return optionsResolved.include.some((included) => {
if (typeof included === 'string') {
return included === id
}

return included.test(id)
})
}

return {
build: {
rollupOptions: {
Expand All @@ -134,6 +168,10 @@ export const externalizeDeps = (options: Partial<UserOptions> = {}): Plugin => {
return false
}

if (isIncluded(id)) {
return true
}

return depMatchers.some((depMatcher) => depMatcher.test(id))
},
},
Expand Down
4 changes: 3 additions & 1 deletion test/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ import { defineConfig } from 'vite'
import esbuild from 'esbuild'
// @ts-ignore
import rollup from 'rollup'
// @ts-ignore
import hello from 'unlisted-dep'

console.log(path, path2, resolve, chalk, esbuild, defineConfig, rollup)
console.log(path, path2, resolve, chalk, esbuild, defineConfig, rollup, hello)
3 changes: 3 additions & 0 deletions test/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export default defineConfig({
nodeResolve(),
externalizeDeps({
devDeps: true,
include: [
/^unlisted-dep(?:\/.*)?$/,
],
useFile: './test/test.json',
}),
],
Expand Down

0 comments on commit b055c03

Please sign in to comment.