diff --git a/package.json b/package.json index 8b42a9ddd..02d99c431 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,15 @@ }, "dependencies": { "rollup": "^2.8.2", - "rollup-plugin-esbuild": "^1.2.0" + "rollup-plugin-esbuild": "^1.3.1" }, "devDependencies": { + "@rollup/plugin-commonjs": "^11.1.0", "@types/node": "^13.9.2", + "@types/resolve": "^1.17.0", "cac": "^6.5.8", + "colorette": "^1.1.0", + "resolve": "^1.17.0", "rollup-plugin-hashbang": "^2.2.2", "typescript": "^3.8.3" } diff --git a/src/cli.ts b/src/cli.ts index 679357ff7..69e2cbc50 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -2,6 +2,7 @@ import { readFileSync } from 'fs' import { join } from 'path' import { cac } from 'cac' +import { handlError } from './errors' const cli = cac('tsup') @@ -15,22 +16,43 @@ cli .option('--target ', 'Bundle target, "es20XX" or "esnext"', { default: 'es2017', }) + .option('--watch', 'Watch mode') .action(async (files: string[], options) => { - const { rollup } = await import('rollup') + const { rollup, watch } = await import('rollup') const { default: hashbangPlugin } = await import('rollup-plugin-hashbang') const { default: esbuildPlugin } = await import('rollup-plugin-esbuild') + const { default: commonjsPlugin } = await import('@rollup/plugin-commonjs') + const { resolvePlugin } = await import('./resolve-plugin') - const result = await rollup({ + const inputOptions = { input: files, plugins: [ hashbangPlugin(), + resolvePlugin(), + commonjsPlugin(), esbuildPlugin({ minify: options.minify, target: options.target }), ], - }) - await result.write({ + } + const outputOptions = { dir: options.outDir, format: options.format, - }) + } + if (options.watch) { + const watcher = watch({ + ...inputOptions, + output: outputOptions, + }) + watcher.on('event', (event) => { + console.log(event) + }) + } else { + try { + const result = await rollup(inputOptions) + await result.write(outputOptions) + } catch (error) { + handlError(error) + } + } }) cli.help() @@ -38,4 +60,8 @@ cli.help() const pkgPath = join(__dirname, '../package.json') cli.version(JSON.parse(readFileSync(pkgPath, 'utf8')).version) -cli.parse() +try { + cli.parse() +} catch (error) { + handlError(error) +} diff --git a/src/errors.ts b/src/errors.ts new file mode 100644 index 000000000..a5dcf3338 --- /dev/null +++ b/src/errors.ts @@ -0,0 +1,11 @@ +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.dim(error.frame)) + } else { + console.error(colors.red(error.stack)) + } + process.exitCode = 1 +} \ No newline at end of file diff --git a/src/resolve-plugin.ts b/src/resolve-plugin.ts new file mode 100644 index 000000000..4aa0c9bbb --- /dev/null +++ b/src/resolve-plugin.ts @@ -0,0 +1,36 @@ +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 => + new Promise((resolve, reject) => { + r(id, options, (err, result) => { + if (err) { + return reject(err) + } + resolve(result) + }) + }) + +const PACKAGE_NAME_RE = /^[@a-z]/ + +export const resolvePlugin = (): Plugin => { + return { + name: 'resolve', + + async resolveId(source, importer) { + // Always exclude builtin modules + if (builtinModules.includes(source)) { + return false + } + + const cwd = importer && dirname(importer) + if (cwd && PACKAGE_NAME_RE.test(source)) { + const id = await resolvePackage(source, { basedir: cwd }) + return id + } + return null + }, + } +} diff --git a/yarn.lock b/yarn.lock index 8d43bfbd6..c24bfb3d5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,26 +2,135 @@ # yarn lockfile v1 +"@rollup/plugin-commonjs@^11.1.0": + version "11.1.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-11.1.0.tgz#60636c7a722f54b41e419e1709df05c7234557ef" + integrity sha512-Ycr12N3ZPN96Fw2STurD21jMqzKwL9QuFhms3SD7KKRK7oaXUsBU9Zt0jL/rOPHiPYisI21/rXGO3jr9BnLHUA== + dependencies: + "@rollup/pluginutils" "^3.0.8" + commondir "^1.0.1" + estree-walker "^1.0.1" + glob "^7.1.2" + is-reference "^1.1.2" + magic-string "^0.25.2" + resolve "^1.11.0" + +"@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" + integrity sha512-d44M7t+PjmMrASHbhgpSbVgtL6EFyX7J4mYxwQ/c5eoaE6N2VgCgEcWVzNnwycIloti+/MpwFr8qfw+nRw00sw== + dependencies: + "@types/estree" "0.0.39" + estree-walker "^1.0.1" + picomatch "^2.2.2" + +"@types/estree@0.0.39": + version "0.0.39" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" + integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw== + +"@types/node@*": + version "13.13.5" + resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.5.tgz#96ec3b0afafd64a4ccea9107b75bf8489f0e5765" + integrity sha512-3ySmiBYJPqgjiHA7oEaIo2Rzz0HrOZ7yrNO5HWyaE5q0lQ3BppDZ3N53Miz8bw2I7gh1/zir2MGVZBvpb1zq9g== + "@types/node@^13.9.2": version "13.9.2" resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.2.tgz#ace1880c03594cc3e80206d96847157d8e7fa349" integrity sha512-bnoqK579sAYrQbp73wwglccjJ4sfRdKU7WNEZ5FW4K2U6Kc0/eZ5kvXG0JKsEKFB50zrFmfFt52/cvBbZa7eXg== +"@types/resolve@^1.17.0": + version "1.17.0" + resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.17.0.tgz#eb25b38e2682f641d33841df162e052d7364eaa8" + integrity sha512-CKOBcXAHvoGEjYsg6Bkc/kLPqTmtnUEGrWnJJC6LNnk+D36uc1GAHcNnwK2g58x/JWjGlqGfIao9rsJk7LePWw== + dependencies: + "@types/node" "*" + +balanced-match@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" + integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= + +brace-expansion@^1.1.7: + version "1.1.11" + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" + integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA== + dependencies: + balanced-match "^1.0.0" + concat-map "0.0.1" + cac@^6.5.8: version "6.5.8" resolved "https://registry.yarnpkg.com/cac/-/cac-6.5.8.tgz#b15d183ee478226f846888be74612ac080533667" integrity sha512-jLv2+ps4T2HRVR1k4UlQZoAFvliAhf5LVR0yjPjIaIr/Cw99p/I7CXIEkXtw5q+AkYk4NCFJcF5ErmELSyrZnw== +colorette@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.1.0.tgz#1f943e5a357fac10b4e0f5aaef3b14cdc1af6ec7" + integrity sha512-6S062WDQUXi6hOfkO/sBPVwE5ASXY4G2+b4atvhJfSsuUUhIaUKlkjLe9692Ipyt5/a+IPF5aVTu3V5gvXq5cg== + +commondir@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" + integrity sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs= + +concat-map@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= + esbuild@^0.2.9: version "0.2.10" resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.2.10.tgz#28ad1d39a032ae4dfe78b5eb344ed5062e70a4c2" integrity sha512-Drg5gA+NF+Dc1Qd2J/Jjvpmtv2PQqeyEjIP6ipY6bz1sUOHKK3/R+v7oitr7/M8rNrI9PrUYPSR5M6hb2edoog== +estree-walker@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700" + integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg== + +fs.realpath@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= + fsevents@~2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.1.2.tgz#4c0a1fb34bc68e543b4b82a9ec392bfbda840805" integrity sha512-R4wDiBwZ0KzpgOWetKDug1FZcYhqYnUYKtfZYt4mD5SBz76q0KR4Q9o7GIPamsVPGmW3EYPPJ0dOOjvx32ldZA== +glob@^7.1.2: + version "7.1.6" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6" + integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + +inflight@^1.0.4: + version "1.0.6" + resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk= + dependencies: + once "^1.3.0" + wrappy "1" + +inherits@2: + version "2.0.4" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" + integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== + +is-reference@^1.1.2: + version "1.1.4" + resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.1.4.tgz#3f95849886ddb70256a3e6d062b1a68c13c51427" + integrity sha512-uJA/CDPO3Tao3GTrxYn6AwkM4nUPJiGGYu5+cB8qbC7WGFlrKZbiRo7SFKxUAEpFUfiHofWCXBUNhvYJMh+6zw== + dependencies: + "@types/estree" "0.0.39" + magic-string@^0.22.4: version "0.22.5" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.22.5.tgz#8e9cf5afddf44385c1da5bc2a6a0dbd10b03657e" @@ -29,11 +138,55 @@ magic-string@^0.22.4: dependencies: vlq "^0.2.2" -rollup-plugin-esbuild@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/rollup-plugin-esbuild/-/rollup-plugin-esbuild-1.2.0.tgz#370f13b3031bb6f968be4f8bafee7ddc3bf22080" - integrity sha512-1wFivEfmSW2zYEi+TTYn+l6w9iSSrYc2ioFoD+jTzL4SycqmtiiB1RlyFVFVCGA3oNxddNdKZggBYNm1NQPNCg== +magic-string@^0.25.2: + version "0.25.7" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" + integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== + dependencies: + sourcemap-codec "^1.4.4" + +minimatch@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" + integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== + dependencies: + brace-expansion "^1.1.7" + +once@^1.3.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" + integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E= + dependencies: + wrappy "1" + +path-is-absolute@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18= + +path-parse@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c" + integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw== + +picomatch@^2.2.2: + version "2.2.2" + resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad" + integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg== + +resolve@^1.11.0, resolve@^1.17.0: + version "1.17.0" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444" + integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w== dependencies: + path-parse "^1.0.6" + +rollup-plugin-esbuild@^1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/rollup-plugin-esbuild/-/rollup-plugin-esbuild-1.3.1.tgz#f87c25bec618c940a44ecc95ce52f61eb544b494" + integrity sha512-wsb0fXwBsNThSzgdJuom7UtZD1hMoe+/K4YHNz+XGxp4I8SXW+bFEgUvpl6snnFOnee8p+ozxK3BDR1z/rmjRg== + dependencies: + "@rollup/pluginutils" "^3.0.10" esbuild "^0.2.9" rollup-plugin-hashbang@^2.2.2: @@ -50,6 +203,11 @@ rollup@^2.8.2: optionalDependencies: fsevents "~2.1.2" +sourcemap-codec@^1.4.4: + version "1.4.8" + resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4" + integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA== + typescript@^3.8.3: version "3.8.3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061" @@ -59,3 +217,8 @@ vlq@^0.2.2: version "0.2.3" resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26" integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow== + +wrappy@1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" + integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=