Skip to content

Commit

Permalink
feat: use node-resolve plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed May 13, 2020
1 parent 8eab2f7 commit 9f182cb
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 30 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
"author": "EGOIST",
"license": "MIT",
"scripts": {
"build": "rm -rf dist && esbuild src/cli.ts --platform=node --outdir=dist --format=cjs --target=es2018 --external:rollup --external:rollup-plugin-esbuild --external:typescript --bundle && chmod +x dist/cli.js",
"build": "rm -rf dist && esbuild src/cli.ts --platform=node --outdir=dist --format=cjs --target=es2018 --external:rollup --external:rollup-plugin-esbuild --external:joycon --external:typescript --bundle && chmod +x dist/cli.js",
"prepublishOnly": "npm run build",
"test": "npm run build && jest"
},
"dependencies": {
"joycon": "^2.2.5",
"rollup": "^2.10.0",
"rollup-plugin-esbuild": "^1.4.1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^11.1.0",
"rollup-plugin-dts": "^1.4.2",
"@rollup/plugin-json": "^4.0.3",
"@rollup/plugin-node-resolve": "^7.1.3",
"@types/fs-extra": "^8.1.0",
"@types/jest": "^25.2.1",
"@types/node": "^13.9.2",
Expand All @@ -31,8 +33,9 @@
"jest": "^26.0.1",
"prettier": "^2.0.5",
"resolve": "^1.17.0",
"rollup-plugin-dts": "^1.4.2",
"rollup-plugin-hashbang": "^2.2.2",
"ts-jest": "^25.5.1",
"typescript": "^3.8.3"
"typescript": "^3.9.2"
}
}
6 changes: 6 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cli
.option('--dts', 'Generate declaration file')
.option('--watch', 'Watch mode')
.option('--define.* <value>', 'Define compile-time constants')
.option('--external <name>', 'Mark specific packages as external (use with --bundle)')
.option('--jsxFactory <jsxFactory>', 'Name of JSX factory function', {
default: 'React.createElement',
})
Expand All @@ -40,6 +41,7 @@ cli
bundle: options.bundle,
outDir: options.outDir,
define: options.define,
external: options.external,
})
if (options.watch) {
const watcher = watch(
Expand All @@ -53,12 +55,16 @@ cli
})
} else {
try {
const startTime = Date.now()

await Promise.all(
rollupConfigs.map(async (config) => {
const result = await rollup(config.inputConfig)
await result.write(config.outputConfig)
})
)
const endTime = Date.now()
console.log(`Done in ${endTime - startTime}ms`)
} catch (error) {
handlError(error)
}
Expand Down
1 change: 1 addition & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import colors from 'colorette'
export function handlError(error: any) {
if (error.frame) {
console.error(colors.red(`Error parsing: ${error.loc.file}:${error.loc.line}:${error.loc.column}`))
console.error(colors.red(error.message))
console.error(colors.dim(error.frame))
} else {
console.error(colors.red(error.stack))
Expand Down
35 changes: 29 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ModuleFormat } from 'rollup'
import { ModuleFormat, InputOptions, OutputOptions } from 'rollup'
import { Target as EsbuildTarget } from 'esbuild'
import hashbangPlugin from 'rollup-plugin-hashbang'
import esbuildPlugin from 'rollup-plugin-esbuild'
import commonjsPlugin from '@rollup/plugin-commonjs'
import jsonPlugin from '@rollup/plugin-json'
import dtsPlugin from 'rollup-plugin-dts'
import { resolvePlugin } from './resolve-plugin'

Expand All @@ -19,27 +20,49 @@ type Options = {
define?: {
[k: string]: string
}
/** Don't bundle these packages */
external?: string[]
}

export async function createRollupConfigs(files: string[], options: Options) {
const getRollupConfig = ({ dts }: { dts?: boolean }) => {
const getRollupConfig = ({
dts,
}: {
dts?: boolean
}): { inputConfig: InputOptions; outputConfig: OutputOptions } => {
return {
inputConfig: {
input: files,
preserveEntrySignatures: 'strict',
onwarn(warning, handler) {
if (
warning.code === 'UNRESOLVED_IMPORT' ||
warning.code === 'CIRCULAR_DEPENDENCY'
) {
return
}
return handler(warning)
},
plugins: [
hashbangPlugin(),
resolvePlugin({ bundle: options.bundle }),
commonjsPlugin(),
jsonPlugin(),
resolvePlugin({ bundle: options.bundle, external: options.external }),
commonjsPlugin({
namedExports: {
// commonjs plugin failed to detect named exports for `resolve`, TODO: report this bug
resolve: Object.keys(require('resolve'))
}
}),
dts && dtsPlugin(),
!dts &&
esbuildPlugin({
target: options.target,
watch: options.watch,
minify: options.minify,
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
define: options.define
define: options.define,
}),
dts && dtsPlugin(),
].filter(Boolean),
},
outputConfig: {
Expand Down
50 changes: 34 additions & 16 deletions src/resolve-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import {builtinModules} from 'module'
import { builtinModules } from 'module'
import { dirname } from 'path'
import { Plugin } from 'rollup'
import r, { Opts as ResolveOpts } from 'resolve'

const resolvePackage = (id: string, options: ResolveOpts): Promise<string> =>
new Promise((resolve, reject) => {
r(id, options, (err, result) => {
if (err) {
return reject(err)
}
resolve(result)
})
})
import JoyCon from 'joycon'
import nodeResolvePlugin from '@rollup/plugin-node-resolve'

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

export const resolvePlugin = ({ bundle }: { bundle?: boolean }): Plugin => {
const joycon = new JoyCon()

export const resolvePlugin = ({
bundle,
external,
}: {
bundle?: boolean
external?: string[]
}): Plugin => {
const nodeResolve = nodeResolvePlugin()

return {
name: 'resolve',
...nodeResolve,

async resolveId(source, importer) {
// Always exclude builtin modules
Expand All @@ -28,8 +29,25 @@ export const resolvePlugin = ({ bundle }: { bundle?: boolean }): Plugin => {
if (bundle) {
const cwd = importer && dirname(importer)
if (cwd && PACKAGE_NAME_RE.test(source)) {
const id = await resolvePackage(source, { basedir: cwd })
return id
// Exclude specified packages
if (external && external.includes(source)) {
return false
}

// Exclude "dependencies" in package.json
const pkg = joycon.loadSync(['package.json'], process.cwd())

const deps = Object.keys((pkg.data && pkg.data.dependencies) || {})

if (deps.includes(source)) {
return false
}
}

const result = await nodeResolve.resolveId!.call(this, source, importer)

if (result !== null) {
return result
}
}

Expand Down
50 changes: 45 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,24 @@
magic-string "^0.25.2"
resolve "^1.11.0"

"@rollup/plugin-json@^4.0.3":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-4.0.3.tgz#747e2c2884c5a0fa00b66c9c0f3f1012cddca534"
integrity sha512-QMUT0HZNf4CX17LMdwaslzlYHUKTYGuuk34yYIgZrNdu+pMEfqMS55gck7HEeHBKXHM4cz5Dg1OVwythDdbbuQ==
dependencies:
"@rollup/pluginutils" "^3.0.8"

"@rollup/plugin-node-resolve@^7.1.3":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-7.1.3.tgz#80de384edfbd7bfc9101164910f86078151a3eca"
integrity sha512-RxtSL3XmdTAE2byxekYLnx+98kEUOrPHF/KRVjLH+DEIHy6kjIw7YINQzn+NXiH/NTrQLAwYs0GWB+csWygA9Q==
dependencies:
"@rollup/pluginutils" "^3.0.8"
"@types/resolve" "0.0.8"
builtin-modules "^3.1.0"
is-module "^1.0.0"
resolve "^1.14.2"

"@rollup/pluginutils@^3.0.10", "@rollup/pluginutils@^3.0.8":
version "3.0.10"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.0.10.tgz#a659b9025920378494cd8f8c59fbf9b3a50d5f12"
Expand Down Expand Up @@ -597,6 +615,13 @@
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.0.0.tgz#dc85454b953178cc6043df5208b9e949b54a3bc4"
integrity sha512-/rM+sWiuOZ5dvuVzV37sUuklsbg+JPOP8d+nNFlo2ZtfpzPiPvh1/gc8liWOLBqe+sR+ZM7guPaIcTt6UZTo7Q==

"@types/resolve@0.0.8":
version "0.0.8"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194"
integrity sha512-auApPaJf3NPfe18hSoJkp8EbZzer2ISk7o8mCC3M9he/a04+gbMF97NkpD2S8riMGvm4BMRI59/SZQSaLTKpsQ==
dependencies:
"@types/node" "*"

"@types/resolve@^1.17.0":
version "1.17.0"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.17.0.tgz#eb25b38e2682f641d33841df162e052d7364eaa8"
Expand Down Expand Up @@ -904,6 +929,11 @@ buffer-from@1.x, buffer-from@^1.0.0:
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==

builtin-modules@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.1.0.tgz#aad97c15131eb76b65b50ef208e7584cd76a7484"
integrity sha512-k0KL0aWZuBt2lrxrcASWDfwOLMnodeQjodT/1SxEQAXsHANgo6ZC/VEaSEHCXt7aSTZ4/4H5LKa+tBXmW7Vtvw==

cac@^6.5.8:
version "6.5.8"
resolved "https://registry.yarnpkg.com/cac/-/cac-6.5.8.tgz#b15d183ee478226f846888be74612ac080533667"
Expand Down Expand Up @@ -1787,6 +1817,11 @@ is-generator-fn@^2.0.0:
resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-2.1.0.tgz#7d140adc389aaf3011a8f2a2a4cfa6faadffb118"
integrity sha512-cTIB4yPYL/Grw0EaSzASzg6bBy9gqCofvWN8okThAYIxKJZC+udlRAmGbM0XLeniEJSs8uEgHPGuHSe1XsOLSQ==

is-module@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-module/-/is-module-1.0.0.tgz#3258fb69f78c14d5b815d664336b4cffb6441591"
integrity sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE=

is-number@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-number/-/is-number-3.0.0.tgz#24fd6201a4782cf50561c810276afc7d12d71195"
Expand Down Expand Up @@ -2291,6 +2326,11 @@ jest@^26.0.1:
import-local "^3.0.2"
jest-cli "^26.0.1"

joycon@^2.2.5:
version "2.2.5"
resolved "https://registry.yarnpkg.com/joycon/-/joycon-2.2.5.tgz#8d4cf4cbb2544d7b7583c216fcdfec19f6be1615"
integrity sha512-YqvUxoOcVPnCp0VU1/56f+iKSdvIRJYPznH22BdXV3xMk75SFXhWeJkZ8C9XxUWt1b5x2X1SxuFygW1U0FmkEQ==

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down Expand Up @@ -3016,7 +3056,7 @@ resolve-url@^0.2.1:
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=

resolve@^1.10.0, resolve@^1.11.0, resolve@^1.17.0, resolve@^1.3.2:
resolve@^1.10.0, resolve@^1.11.0, resolve@^1.14.2, resolve@^1.17.0, resolve@^1.3.2:
version "1.17.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444"
integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==
Expand Down Expand Up @@ -3550,10 +3590,10 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"

typescript@^3.8.3:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==
typescript@^3.9.2:
version "3.9.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.2.tgz#64e9c8e9be6ea583c54607677dd4680a1cf35db9"
integrity sha512-q2ktq4n/uLuNNShyayit+DTobV2ApPEo/6so68JaD5ojvc/6GClBipedB9zNWYxRSAlZXAe405Rlijzl6qDiSw==

union-value@^1.0.0:
version "1.0.1"
Expand Down

0 comments on commit 9f182cb

Please sign in to comment.