Skip to content

Commit

Permalink
refactor: external option
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed May 15, 2020
1 parent 20c2ff4 commit b80ef48
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import jsonPlugin from '@rollup/plugin-json'
import dtsPlugin from 'rollup-plugin-dts'
import { sizePlugin, caches } from './size-plugin'
import { resolvePlugin } from './resolve-plugin'
import { isExternal } from './utils'

type Options = {
bundle?: boolean
Expand Down Expand Up @@ -55,12 +56,20 @@ export async function createRollupConfigs(files: string[], options: Options) {
external: options.external,
dts,
}),
!dts && commonjsPlugin({
namedExports: {
// commonjs plugin failed to detect named exports for `resolve`, TODO: report this bug
resolve: Object.keys(require('resolve')),
},
}),
!dts &&
commonjsPlugin({
namedExports: {
// commonjs plugin failed to detect named exports for `resolve`, TODO: report this bug
resolve: Object.keys(require('resolve')),
},
// @ts-ignore wrong typing in @rollup/plugin-commonjs
ignore:(name: string) => {
if (!options.external) {
return false
}
return isExternal(options.external, name)
},
}),
dts && dtsPlugin(),
!dts &&
esbuildPlugin({
Expand All @@ -78,7 +87,7 @@ export async function createRollupConfigs(files: string[], options: Options) {
dir: options.outDir,
format: options.format,
exports: 'named',
name: options.moduleName
name: options.moduleName,
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/resolve-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { dirname } from 'path'
import { Plugin } from 'rollup'
import JoyCon from 'joycon'
import nodeResolvePlugin from '@rollup/plugin-node-resolve'
import { isExternal } from './utils'

const PACKAGE_NAME_RE = /^[@a-z]/

Expand Down Expand Up @@ -40,7 +41,7 @@ export const resolvePlugin = ({
const cwd = importer && dirname(importer)
if (cwd && PACKAGE_NAME_RE.test(source)) {
// Exclude specified packages
if (external && external.includes(source)) {
if (external && isExternal(external, source, importer)) {
return false
}

Expand Down
39 changes: 39 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// No backslash in path
function slash(input: string) {
return input.replace(/\\/g, '/')
}

export type External = string | RegExp | ((id: string, parentId?: string) => boolean)

export function isExternal(
externals: External | External[],
id: string,
parentId?: string
) {
id = slash(id)

if (!Array.isArray(externals)) {
externals = [externals]
}

for (const external of externals) {
if (
typeof external === 'string' &&
(id === external || id.includes(`/node_modules/${external}/`))
) {
return true
}
if (external instanceof RegExp) {
if (external.test(id)) {
return true
}
}
if (typeof external === 'function') {
if (external(id, parentId)) {
return true
}
}
}

return false
}

0 comments on commit b80ef48

Please sign in to comment.