From b80ef48688d84fd11c9e3a6491eabb94c0dba18a Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Fri, 15 May 2020 23:07:48 +0800 Subject: [PATCH] refactor: external option --- src/index.ts | 23 ++++++++++++++++------- src/resolve-plugin.ts | 3 ++- src/utils.ts | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/utils.ts diff --git a/src/index.ts b/src/index.ts index 9d8255015..f92bde477 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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({ @@ -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, }, } } diff --git a/src/resolve-plugin.ts b/src/resolve-plugin.ts index c453fca89..b143da899 100644 --- a/src/resolve-plugin.ts +++ b/src/resolve-plugin.ts @@ -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]/ @@ -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 } diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 000000000..379a3cf21 --- /dev/null +++ b/src/utils.ts @@ -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 +} \ No newline at end of file