From 0e2960a758f6cc0dcb2caccc92b7f125fa8d10ae Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Fri, 19 Jun 2020 16:54:25 +0100 Subject: [PATCH 1/9] feat(babel): add typings --- packages/babel/package.json | 10 ++- packages/babel/test/types.ts | 22 ++++++ packages/babel/types/index.d.ts | 38 +++++++++++ pnpm-lock.yaml | 114 ++++++++++++++++++++++++-------- 4 files changed, 153 insertions(+), 31 deletions(-) create mode 100644 packages/babel/test/types.ts create mode 100644 packages/babel/types/index.d.ts diff --git a/packages/babel/package.json b/packages/babel/package.json index 2d14902c5..62056a224 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -25,12 +25,14 @@ "lint:js": "eslint --fix --cache src test", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", - "prepublishOnly": "pnpm run lint && pnpm run test && pnpm run build", + "prepublishOnly": "pnpm run lint && pnpm run test && pnpm run build && pnpm run test:ts", "pretest": "pnpm run build", - "test": "ava" + "test": "ava", + "test:ts": "tsc types/index.d.ts test/types.ts --noEmit" }, "files": [ "dist", + "types", "README.md", "LICENSE" ], @@ -58,6 +60,7 @@ "@babel/plugin-transform-runtime": "^7.7.4", "@babel/preset-env": "^7.9.0", "@rollup/plugin-json": "^4.0.0", + "@types/babel__core": "^7.1.8", "rollup": "^2.0.0", "source-map": "^0.6.1" }, @@ -73,5 +76,6 @@ "Bogdan Chadkin ", "Mateusz Burzyński (https://github.com/Andarist)" ], - "module": "dist/index.es.js" + "module": "dist/index.es.js", + "types": "types/index.d.ts" } diff --git a/packages/babel/test/types.ts b/packages/babel/test/types.ts new file mode 100644 index 000000000..dbbe39b82 --- /dev/null +++ b/packages/babel/test/types.ts @@ -0,0 +1,22 @@ +import babel from '../types'; + +const config: import('rollup').RollupOptions = { + input: 'main.js', + output: { + file: 'bundle.js', + format: 'iife' + }, + plugins: [ + babel({ + include: 'node_modules/**', + exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], + extensions: ['.js', '.coffee'], + babelHelpers: 'runtime', + skipPreflightCheck: true, + babelrc: false, + plugins: [] + }) + ] +}; + +export default config; diff --git a/packages/babel/types/index.d.ts b/packages/babel/types/index.d.ts new file mode 100644 index 000000000..35604b765 --- /dev/null +++ b/packages/babel/types/index.d.ts @@ -0,0 +1,38 @@ +import { Plugin } from 'rollup'; +import { FilterPattern } from '@rollup/pluginutils'; +import { TransformOptions } from '@babel/core'; + +interface RollupBabelOptions extends Omit { + /** + * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there. + * @default undefined; + */ + include?: FilterPattern; + /** + * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. When relaying on Babel configuration files you can only exclude additional files with this option, you cannot override what you have configured for Babel itself. + * @default undefined; + */ + exclude?: FilterPattern; + /** + * An array of file extensions that Babel should transpile. If you want to transpile TypeScript files with this plugin it's essential to include .ts and .tsx in this option. + * @default ['.js', '.jsx', '.es6', '.es', '.mjs'] + */ + extensions?: string[]; + /** + * It is recommended to configure this option explicitly (even if with its default value) so an informed decision is taken on how those babel helpers are inserted into the code. + * @default 'bundled' + */ + babelHelpers?: 'bundled' | 'runtime' | 'inline' | 'extends'; + /** + * Before transpiling your input files this plugin also transpile a short piece of code for each input file. This is used to validate some misconfiguration errors, but for sufficiently big projects it can slow your build times so if you are confident about your configuration then you might disable those checks with this option. + * @default false + */ + skipPreflightCheck?: boolean; +} + +/** + * A Rollup plugin for seamless integration between Rollup and Babel. + * @param options - Plugin options. + * @returns Plugin instance. + */ +export default function babel(options?: RollupBabelOptions): Plugin; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 624cfd217..3e05db0d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -81,7 +81,7 @@ importers: packages/babel: dependencies: '@babel/helper-module-imports': 7.8.3 - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 devDependencies: '@babel/core': 7.8.3 '@babel/plugin-external-helpers': 7.8.3_@babel+core@7.8.3 @@ -89,7 +89,8 @@ importers: '@babel/plugin-syntax-dynamic-import': 7.8.3_@babel+core@7.8.3 '@babel/plugin-transform-runtime': 7.9.0_@babel+core@7.8.3 '@babel/preset-env': 7.9.5_@babel+core@7.8.3 - '@rollup/plugin-json': 4.0.1_rollup@2.2.0 + '@rollup/plugin-json': 4.0.3_rollup@2.2.0 + '@types/babel__core': 7.1.8 rollup: 2.2.0 source-map: 0.6.1 specifiers: @@ -102,6 +103,7 @@ importers: '@babel/preset-env': ^7.9.0 '@rollup/plugin-json': ^4.0.0 '@rollup/pluginutils': ^3.0.8 + '@types/babel__core': ^7.1.8 rollup: ^2.0.0 source-map: ^0.6.1 packages/beep: @@ -113,7 +115,7 @@ importers: strip-ansi: ^5.2.0 packages/buble: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 '@types/buble': 0.19.2 buble: 0.20.0 devDependencies: @@ -133,7 +135,7 @@ importers: typescript: ^3.7.4 packages/commonjs: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.7.2 + '@rollup/pluginutils': 3.0.10_rollup@2.7.2 commondir: 1.0.1 estree-walker: 1.0.1 glob: 7.1.6 @@ -144,7 +146,7 @@ importers: '@babel/core': 7.9.0 '@babel/preset-env': 7.9.5_@babel+core@7.9.0 '@babel/register': 7.9.0_@babel+core@7.9.0 - '@rollup/plugin-json': 4.0.1_rollup@2.7.2 + '@rollup/plugin-json': 4.0.3_rollup@2.7.2 '@rollup/plugin-node-resolve': 7.1.1_rollup@2.7.2 acorn: 7.1.1 locate-character: 2.0.5 @@ -182,7 +184,7 @@ importers: packages/data-uri: devDependencies: '@rollup/plugin-typescript': 3.0.0_rollup@2.2.0+typescript@3.7.5 - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 rollup: 2.2.0 typescript: 3.7.5 specifiers: @@ -192,7 +194,7 @@ importers: typescript: ^3.7.4 packages/dsv: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 d3-dsv: 1.2.0 tosource: 1.0.0 devDependencies: @@ -213,7 +215,7 @@ importers: rollup-plugin-postcss: ^2.0.3 packages/image: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 mini-svg-data-uri: 1.1.3 devDependencies: '@rollup/plugin-buble': 0.21.0_rollup@2.2.0 @@ -225,7 +227,7 @@ importers: rollup: ^2.0.0 packages/inject: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 estree-walker: 1.0.1 magic-string: 0.25.6 devDependencies: @@ -247,7 +249,7 @@ importers: typescript: ^3.7.4 packages/json: dependencies: - '@rollup/pluginutils': 3.0.8 + '@rollup/pluginutils': 3.0.10 devDependencies: '@rollup/plugin-buble': 0.21.0 '@rollup/plugin-node-resolve': 7.1.1 @@ -342,7 +344,7 @@ importers: typescript: ^3.7.5 packages/replace: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 magic-string: 0.25.6 devDependencies: '@rollup/plugin-buble': 0.21.0_rollup@2.2.0 @@ -376,7 +378,7 @@ importers: sinon: 8.0.4 packages/strip: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 estree-walker: 1.0.1 magic-string: 0.25.6 devDependencies: @@ -402,7 +404,7 @@ importers: sucrase: ^3.10.1 packages/typescript: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 resolve: 1.15.0 devDependencies: '@rollup/plugin-buble': 0.21.0_rollup@2.2.0 @@ -424,7 +426,7 @@ importers: typescript: ^3.7.4 packages/url: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 make-dir: 3.0.0 mime: 2.4.4 devDependencies: @@ -466,7 +468,7 @@ importers: source-map: ^0.7.3 packages/yaml: dependencies: - '@rollup/pluginutils': 3.0.8_rollup@2.2.0 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 js-yaml: 3.13.1 tosource: 1.0.0 devDependencies: @@ -2128,28 +2130,28 @@ packages: rollup: ^1.20.0 resolution: integrity: sha512-MPYGZr0qdbV5zZj8/2AuomVpnRVXRU5XKXb3HVniwRoRCreGlf5kOE081isNWeiLIi6IYkwTX9zE0/c7V8g81g== - /@rollup/plugin-json/4.0.1_rollup@2.2.0: + /@rollup/plugin-json/4.0.3_rollup@2.12.0: dependencies: - rollup: 2.2.0 - rollup-pluginutils: 2.8.2 + '@rollup/pluginutils': 3.0.10_rollup@2.12.0 + rollup: 2.12.0 dev: true peerDependencies: - rollup: ^1.20.0 + rollup: ^1.20.0 || ^2.0.0 resolution: - integrity: sha512-soxllkhOGgchswBAAaTe7X9G80U2tjjHvXv0sBrriLJcC/89PkP59iTrKPOfbz3SjX088mKDmMhAscuyLz8ZSg== - /@rollup/plugin-json/4.0.1_rollup@2.7.2: + integrity: sha512-QMUT0HZNf4CX17LMdwaslzlYHUKTYGuuk34yYIgZrNdu+pMEfqMS55gck7HEeHBKXHM4cz5Dg1OVwythDdbbuQ== + /@rollup/plugin-json/4.0.3_rollup@2.2.0: dependencies: - rollup: 2.7.2 - rollup-pluginutils: 2.8.2 + '@rollup/pluginutils': 3.0.10_rollup@2.2.0 + rollup: 2.2.0 dev: true peerDependencies: - rollup: ^1.20.0 + rollup: ^1.20.0 || ^2.0.0 resolution: - integrity: sha512-soxllkhOGgchswBAAaTe7X9G80U2tjjHvXv0sBrriLJcC/89PkP59iTrKPOfbz3SjX088mKDmMhAscuyLz8ZSg== - /@rollup/plugin-json/4.0.3_rollup@2.12.0: + integrity: sha512-QMUT0HZNf4CX17LMdwaslzlYHUKTYGuuk34yYIgZrNdu+pMEfqMS55gck7HEeHBKXHM4cz5Dg1OVwythDdbbuQ== + /@rollup/plugin-json/4.0.3_rollup@2.7.2: dependencies: - '@rollup/pluginutils': 3.0.10_rollup@2.12.0 - rollup: 2.12.0 + '@rollup/pluginutils': 3.0.10_rollup@2.7.2 + rollup: 2.7.2 dev: true peerDependencies: rollup: ^1.20.0 || ^2.0.0 @@ -2272,6 +2274,18 @@ packages: typescript: '>=2.1.0' resolution: integrity: sha512-KYZCn1Iw9hZWkeEPqPs5YjlmvSjR7UdezVca8z0e8rm/29wU24UD9Y4IZHhnc9tm749hzsgBTiOUxA85gfShEQ== + /@rollup/pluginutils/3.0.10: + dependencies: + '@types/estree': 0.0.39 + estree-walker: 1.0.1 + picomatch: 2.2.2 + dev: false + engines: + node: '>= 8.0.0' + peerDependencies: + rollup: ^1.20.0||^2.0.0 + resolution: + integrity: sha512-d44M7t+PjmMrASHbhgpSbVgtL6EFyX7J4mYxwQ/c5eoaE6N2VgCgEcWVzNnwycIloti+/MpwFr8qfw+nRw00sw== /@rollup/pluginutils/3.0.10_rollup@2.12.0: dependencies: '@types/estree': 0.0.39 @@ -2296,9 +2310,22 @@ packages: rollup: ^1.20.0||^2.0.0 resolution: integrity: sha512-d44M7t+PjmMrASHbhgpSbVgtL6EFyX7J4mYxwQ/c5eoaE6N2VgCgEcWVzNnwycIloti+/MpwFr8qfw+nRw00sw== + /@rollup/pluginutils/3.0.10_rollup@2.7.2: + dependencies: + '@types/estree': 0.0.39 + estree-walker: 1.0.1 + picomatch: 2.2.2 + rollup: 2.7.2 + engines: + node: '>= 8.0.0' + peerDependencies: + rollup: ^1.20.0||^2.0.0 + resolution: + integrity: sha512-d44M7t+PjmMrASHbhgpSbVgtL6EFyX7J4mYxwQ/c5eoaE6N2VgCgEcWVzNnwycIloti+/MpwFr8qfw+nRw00sw== /@rollup/pluginutils/3.0.8: dependencies: estree-walker: 1.0.1 + dev: true engines: node: '>= 8.0.0' peerDependencies: @@ -2309,6 +2336,7 @@ packages: dependencies: estree-walker: 1.0.1 rollup: 2.2.0 + dev: true engines: node: '>= 8.0.0' peerDependencies: @@ -2319,6 +2347,7 @@ packages: dependencies: estree-walker: 1.0.1 rollup: 2.7.2 + dev: true engines: node: '>= 8.0.0' peerDependencies: @@ -2372,6 +2401,35 @@ packages: node: '>=6' resolution: integrity: sha512-XIB2XbzHTN6ieIjfIMV9hlVcfPU26s2vafYWQcZHWXHOxiaRZYEDKEwdl129Zyg50+foYV2jCgtrqSA6qNuNSA== + /@types/babel__core/7.1.8: + dependencies: + '@babel/parser': 7.9.4 + '@babel/types': 7.9.5 + '@types/babel__generator': 7.6.1 + '@types/babel__template': 7.0.2 + '@types/babel__traverse': 7.0.12 + dev: true + resolution: + integrity: sha512-KXBiQG2OXvaPWFPDS1rD8yV9vO0OuWIqAEqLsbfX0oU2REN5KuoMnZ1gClWcBhO5I3n6oTVAmrMufOvRqdmFTQ== + /@types/babel__generator/7.6.1: + dependencies: + '@babel/types': 7.9.5 + dev: true + resolution: + integrity: sha512-bBKm+2VPJcMRVwNhxKu8W+5/zT7pwNEqeokFOmbvVSqGzFneNxYcEBro9Ac7/N9tlsaPYnZLK8J1LWKkMsLAew== + /@types/babel__template/7.0.2: + dependencies: + '@babel/parser': 7.9.4 + '@babel/types': 7.9.5 + dev: true + resolution: + integrity: sha512-/K6zCpeW7Imzgab2bLkLEbz0+1JlFSrUMdw7KoIIu+IUdu51GWaBZpd3y1VXGVXzynvGa4DaIaxNZHiON3GXUg== + /@types/babel__traverse/7.0.12: + dependencies: + '@babel/types': 7.9.5 + dev: true + resolution: + integrity: sha512-t4CoEokHTfcyfb4hUaF9oOHu9RmmNWnm1CP0YmMqOOfClKascOmvlEM736vlqeScuGvBDsHkf8R2INd4DWreQA== /@types/buble/0.19.2: dependencies: magic-string: 0.25.7 From 8d3c1974c3c122e7db6d558a7ed857ff019b4c78 Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Sat, 20 Jun 2020 18:22:54 +0100 Subject: [PATCH 2/9] refactor(babel): typings PR feedback --- packages/babel/package.json | 9 ++- packages/babel/test/types.ts | 129 +++++++++++++++++++++++++++++++- packages/babel/types/index.d.ts | 98 +++++++++++++++++++++++- 3 files changed, 228 insertions(+), 8 deletions(-) diff --git a/packages/babel/package.json b/packages/babel/package.json index 62056a224..bb2dde682 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -46,7 +46,8 @@ ], "peerDependencies": { "@babel/core": "^7.0.0", - "rollup": "^1.20.0||^2.0.0" + "rollup": "^1.20.0||^2.0.0", + "@types/babel__core": "^7.1.8" }, "dependencies": { "@babel/helper-module-imports": "^7.7.4", @@ -60,7 +61,6 @@ "@babel/plugin-transform-runtime": "^7.7.4", "@babel/preset-env": "^7.9.0", "@rollup/plugin-json": "^4.0.0", - "@types/babel__core": "^7.1.8", "rollup": "^2.0.0", "source-map": "^0.6.1" }, @@ -77,5 +77,10 @@ "Mateusz Burzyński (https://github.com/Andarist)" ], "module": "dist/index.es.js", + "peerDependenciesMeta": { + "@types/babel__core": { + "optional": true + } + }, "types": "types/index.d.ts" } diff --git a/packages/babel/test/types.ts b/packages/babel/test/types.ts index dbbe39b82..c1c688e97 100644 --- a/packages/babel/test/types.ts +++ b/packages/babel/test/types.ts @@ -1,6 +1,13 @@ -import babel from '../types'; +/** eslint-disable @typescript-eslint/no-unused-vars */ -const config: import('rollup').RollupOptions = { +import babel, { + getBabelInputPlugin, + getBabelOutputPlugin, + createBabelInputPluginFactory, + createBabelOutputPluginFactory +} from '../types'; + +const rollipConfig: import('rollup').RollupOptions = { input: 'main.js', output: { file: 'bundle.js', @@ -15,8 +22,124 @@ const config: import('rollup').RollupOptions = { skipPreflightCheck: true, babelrc: false, plugins: [] + }), + getBabelInputPlugin({ + include: 'node_modules/**', + exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], + extensions: ['.js', '.coffee'], + babelHelpers: 'runtime', + skipPreflightCheck: true, + babelrc: false, + plugins: [] + }), + getBabelOutputPlugin({ + include: 'node_modules/**', + exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], + extensions: ['.js', '.coffee'], + runtimeHelpers: true, + externalHelpers: true, + externalHelpersWhitelist: ['example'], + allowAllFormats: true, + babelrc: false, + plugins: [] }) ] }; -export default config; +export default rollipConfig; + +createBabelInputPluginFactory((babelCore) => { + function myPlugin() { + return { + name: `input-${babelCore.version}`, + visitor: {} + }; + } + + return { + // Passed the plugin options. + options({ opt1, opt2, ...pluginOptions }) { + return { + // Pull out any custom options that the plugin might have. + customOptions: { opt1, opt2 }, + + // Pass the options back with the two custom options removed. + pluginOptions + }; + }, + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + config(cfg, { code, customOptions }) { + if (cfg.hasFilesystemConfig()) { + // Use the normal config + return cfg.options; + } + + return { + ...cfg.options, + plugins: [ + ...(cfg.options.plugins || []), + + // Include a custom plugin in the options. + myPlugin + ] + }; + }, + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + result(result, { code, customOptions, config, transformOptions }) { + return { + ...result, + code: `${result.code}\n// Generated by some custom plugin` + }; + } + }; +}); + +createBabelOutputPluginFactory((babelCore) => { + function myPlugin() { + return { + name: `output-${babelCore.version}`, + visitor: {} + }; + } + + return { + // Passed the plugin options. + options({ opt1, opt2, ...pluginOptions }) { + return { + // Pull out any custom options that the plugin might have. + customOptions: { opt1, opt2 }, + + // Pass the options back with the two custom options removed. + pluginOptions + }; + }, + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + config(cfg, { code, customOptions }) { + if (cfg.hasFilesystemConfig()) { + // Use the normal config + return cfg.options; + } + + return { + ...cfg.options, + plugins: [ + ...(cfg.options.plugins || []), + + // Include a custom plugin in the options. + myPlugin + ] + }; + }, + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + result(result, { code, customOptions, config, transformOptions }) { + return { + ...result, + code: `${result.code}\n// Generated by some custom plugin` + }; + } + }; +}); diff --git a/packages/babel/types/index.d.ts b/packages/babel/types/index.d.ts index 35604b765..fa5fc497d 100644 --- a/packages/babel/types/index.d.ts +++ b/packages/babel/types/index.d.ts @@ -1,8 +1,9 @@ import { Plugin } from 'rollup'; import { FilterPattern } from '@rollup/pluginutils'; -import { TransformOptions } from '@babel/core'; +import * as babelCore from '@babel/core'; -interface RollupBabelOptions extends Omit { +interface RollupBabelInputPluginOptions + extends Omit { /** * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there. * @default undefined; @@ -30,9 +31,100 @@ interface RollupBabelOptions extends Omit { + /** + * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there. + * @default undefined; + */ + include?: FilterPattern; + /** + * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. When relaying on Babel configuration files you can only exclude additional files with this option, you cannot override what you have configured for Babel itself. + * @default undefined; + */ + exclude?: FilterPattern; + /** + * An array of file extensions that Babel should transpile. If you want to transpile TypeScript files with this plugin it's essential to include .ts and .tsx in this option. + * @default ['.js', '.jsx', '.es6', '.es', '.mjs'] + */ + extensions?: string[]; + /** + * A boolean value indicating whether to bundle in the Babel helpers. + * @default false + */ + externalHelpers?: boolean; + /** + * An array which gives explicit control over which babelHelper functions are allowed in the bundle. + * @default undefined + */ + externalHelpersWhitelist?: string[]; + /** + * @default false + */ + runtimeHelpers?: boolean; + /** + * Use with other formats than UMD/IIFE. + * @default false + */ + allowAllFormats?: boolean; +} + +type CustomInputPluginOptions = ( + options: RollupBabelInputPluginOptions & Record +) => { + customOptions: Record; + pluginOptions: RollupBabelInputPluginOptions; +}; +type CustomOutputPluginOptions = ( + options: RollupBabelOutputPluginOptions & Record +) => { + customOptions: Record; + pluginOptions: RollupBabelOutputPluginOptions; +}; +type CustomPluginConfig = ( + cfg: babelCore.PartialConfig, + options: { code: string; customOptions: Record } +) => babelCore.TransformOptions; +type CustomPluginResult = ( + result: babelCore.BabelFileResult, + options: { + code: string; + customOptions: Record; + config: babelCore.PartialConfig; + transformOptions: babelCore.TransformOptions; + } +) => babelCore.BabelFileResult; +interface CustomInputPlugin { + options?: CustomInputPluginOptions; + config?: CustomPluginConfig; + result?: CustomPluginResult; +} +interface CustomOutputPlugin { + options?: CustomOutputPluginOptions; + config?: CustomPluginConfig; + result?: CustomPluginResult; +} +type CustomInputPluginBuilder = (babel: typeof babelCore) => CustomInputPlugin; +type CustomOutputPluginBuilder = (babel: typeof babelCore) => CustomOutputPlugin; + +/** + * A Rollup plugin for seamless integration between Rollup and Babel. + * @param options - Plugin options. + * @returns Plugin instance. + */ +export function getBabelInputPlugin(options?: RollupBabelInputPluginOptions): Plugin; +export function getBabelOutputPlugin(options?: RollupBabelOutputPluginOptions): Plugin; + +export function createBabelInputPluginFactory( + customCallback?: CustomInputPluginBuilder +): typeof getBabelInputPlugin; +export function createBabelOutputPluginFactory( + customCallback?: CustomOutputPluginBuilder +): typeof getBabelOutputPlugin; + /** * A Rollup plugin for seamless integration between Rollup and Babel. * @param options - Plugin options. * @returns Plugin instance. */ -export default function babel(options?: RollupBabelOptions): Plugin; +export default function babel(options?: RollupBabelInputPluginOptions): Plugin; From a3436ec5dd930bf961655f12bb28962aaf3be4fc Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Sat, 20 Jun 2020 18:26:32 +0100 Subject: [PATCH 3/9] chore(babel): update lockfile --- pnpm-lock.yaml | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3e05db0d6..8715294ce 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,7 +90,6 @@ importers: '@babel/plugin-transform-runtime': 7.9.0_@babel+core@7.8.3 '@babel/preset-env': 7.9.5_@babel+core@7.8.3 '@rollup/plugin-json': 4.0.3_rollup@2.2.0 - '@types/babel__core': 7.1.8 rollup: 2.2.0 source-map: 0.6.1 specifiers: @@ -103,7 +102,6 @@ importers: '@babel/preset-env': ^7.9.0 '@rollup/plugin-json': ^4.0.0 '@rollup/pluginutils': ^3.0.8 - '@types/babel__core': ^7.1.8 rollup: ^2.0.0 source-map: ^0.6.1 packages/beep: @@ -2401,35 +2399,6 @@ packages: node: '>=6' resolution: integrity: sha512-XIB2XbzHTN6ieIjfIMV9hlVcfPU26s2vafYWQcZHWXHOxiaRZYEDKEwdl129Zyg50+foYV2jCgtrqSA6qNuNSA== - /@types/babel__core/7.1.8: - dependencies: - '@babel/parser': 7.9.4 - '@babel/types': 7.9.5 - '@types/babel__generator': 7.6.1 - '@types/babel__template': 7.0.2 - '@types/babel__traverse': 7.0.12 - dev: true - resolution: - integrity: sha512-KXBiQG2OXvaPWFPDS1rD8yV9vO0OuWIqAEqLsbfX0oU2REN5KuoMnZ1gClWcBhO5I3n6oTVAmrMufOvRqdmFTQ== - /@types/babel__generator/7.6.1: - dependencies: - '@babel/types': 7.9.5 - dev: true - resolution: - integrity: sha512-bBKm+2VPJcMRVwNhxKu8W+5/zT7pwNEqeokFOmbvVSqGzFneNxYcEBro9Ac7/N9tlsaPYnZLK8J1LWKkMsLAew== - /@types/babel__template/7.0.2: - dependencies: - '@babel/parser': 7.9.4 - '@babel/types': 7.9.5 - dev: true - resolution: - integrity: sha512-/K6zCpeW7Imzgab2bLkLEbz0+1JlFSrUMdw7KoIIu+IUdu51GWaBZpd3y1VXGVXzynvGa4DaIaxNZHiON3GXUg== - /@types/babel__traverse/7.0.12: - dependencies: - '@babel/types': 7.9.5 - dev: true - resolution: - integrity: sha512-t4CoEokHTfcyfb4hUaF9oOHu9RmmNWnm1CP0YmMqOOfClKascOmvlEM736vlqeScuGvBDsHkf8R2INd4DWreQA== /@types/buble/0.19.2: dependencies: magic-string: 0.25.7 From 1523383fdf69a11480bd8094232d065c4ea44126 Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Sat, 20 Jun 2020 19:05:48 +0100 Subject: [PATCH 4/9] refactor(babel): named export + dev dep --- packages/babel/package.json | 1 + packages/babel/test/types.ts | 12 +++++++++++- packages/babel/types/index.d.ts | 3 ++- pnpm-lock.yaml | 31 +++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/babel/package.json b/packages/babel/package.json index bb2dde682..97235674d 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -61,6 +61,7 @@ "@babel/plugin-transform-runtime": "^7.7.4", "@babel/preset-env": "^7.9.0", "@rollup/plugin-json": "^4.0.0", + "@types/babel__core": "^7.1.8", "rollup": "^2.0.0", "source-map": "^0.6.1" }, diff --git a/packages/babel/test/types.ts b/packages/babel/test/types.ts index c1c688e97..7e1190600 100644 --- a/packages/babel/test/types.ts +++ b/packages/babel/test/types.ts @@ -1,6 +1,7 @@ /** eslint-disable @typescript-eslint/no-unused-vars */ -import babel, { +import babelPlugin, { + babel, getBabelInputPlugin, getBabelOutputPlugin, createBabelInputPluginFactory, @@ -14,6 +15,15 @@ const rollipConfig: import('rollup').RollupOptions = { format: 'iife' }, plugins: [ + babelPlugin({ + include: 'node_modules/**', + exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], + extensions: ['.js', '.coffee'], + babelHelpers: 'runtime', + skipPreflightCheck: true, + babelrc: false, + plugins: [] + }), babel({ include: 'node_modules/**', exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], diff --git a/packages/babel/types/index.d.ts b/packages/babel/types/index.d.ts index fa5fc497d..c723d36d1 100644 --- a/packages/babel/types/index.d.ts +++ b/packages/babel/types/index.d.ts @@ -127,4 +127,5 @@ export function createBabelOutputPluginFactory( * @param options - Plugin options. * @returns Plugin instance. */ -export default function babel(options?: RollupBabelInputPluginOptions): Plugin; +export function babel(options?: RollupBabelInputPluginOptions): Plugin; +export default babel; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8715294ce..3e05db0d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,6 +90,7 @@ importers: '@babel/plugin-transform-runtime': 7.9.0_@babel+core@7.8.3 '@babel/preset-env': 7.9.5_@babel+core@7.8.3 '@rollup/plugin-json': 4.0.3_rollup@2.2.0 + '@types/babel__core': 7.1.8 rollup: 2.2.0 source-map: 0.6.1 specifiers: @@ -102,6 +103,7 @@ importers: '@babel/preset-env': ^7.9.0 '@rollup/plugin-json': ^4.0.0 '@rollup/pluginutils': ^3.0.8 + '@types/babel__core': ^7.1.8 rollup: ^2.0.0 source-map: ^0.6.1 packages/beep: @@ -2399,6 +2401,35 @@ packages: node: '>=6' resolution: integrity: sha512-XIB2XbzHTN6ieIjfIMV9hlVcfPU26s2vafYWQcZHWXHOxiaRZYEDKEwdl129Zyg50+foYV2jCgtrqSA6qNuNSA== + /@types/babel__core/7.1.8: + dependencies: + '@babel/parser': 7.9.4 + '@babel/types': 7.9.5 + '@types/babel__generator': 7.6.1 + '@types/babel__template': 7.0.2 + '@types/babel__traverse': 7.0.12 + dev: true + resolution: + integrity: sha512-KXBiQG2OXvaPWFPDS1rD8yV9vO0OuWIqAEqLsbfX0oU2REN5KuoMnZ1gClWcBhO5I3n6oTVAmrMufOvRqdmFTQ== + /@types/babel__generator/7.6.1: + dependencies: + '@babel/types': 7.9.5 + dev: true + resolution: + integrity: sha512-bBKm+2VPJcMRVwNhxKu8W+5/zT7pwNEqeokFOmbvVSqGzFneNxYcEBro9Ac7/N9tlsaPYnZLK8J1LWKkMsLAew== + /@types/babel__template/7.0.2: + dependencies: + '@babel/parser': 7.9.4 + '@babel/types': 7.9.5 + dev: true + resolution: + integrity: sha512-/K6zCpeW7Imzgab2bLkLEbz0+1JlFSrUMdw7KoIIu+IUdu51GWaBZpd3y1VXGVXzynvGa4DaIaxNZHiON3GXUg== + /@types/babel__traverse/7.0.12: + dependencies: + '@babel/types': 7.9.5 + dev: true + resolution: + integrity: sha512-t4CoEokHTfcyfb4hUaF9oOHu9RmmNWnm1CP0YmMqOOfClKascOmvlEM736vlqeScuGvBDsHkf8R2INd4DWreQA== /@types/buble/0.19.2: dependencies: magic-string: 0.25.7 From 3f17f7c6c6330d75f394ca6abfb4d2b074617a23 Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Sun, 21 Jun 2020 11:38:39 +0100 Subject: [PATCH 5/9] refactor(babel): typing PR feedback round 2 --- packages/babel/package.json | 4 +-- packages/babel/types/index.d.ts | 49 +++++++-------------------------- pnpm-lock.yaml | 8 +++--- 3 files changed, 16 insertions(+), 45 deletions(-) diff --git a/packages/babel/package.json b/packages/babel/package.json index 97235674d..49dce6865 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -47,7 +47,7 @@ "peerDependencies": { "@babel/core": "^7.0.0", "rollup": "^1.20.0||^2.0.0", - "@types/babel__core": "^7.1.8" + "@types/babel__core": "^7.1.9" }, "dependencies": { "@babel/helper-module-imports": "^7.7.4", @@ -61,7 +61,7 @@ "@babel/plugin-transform-runtime": "^7.7.4", "@babel/preset-env": "^7.9.0", "@rollup/plugin-json": "^4.0.0", - "@types/babel__core": "^7.1.8", + "@types/babel__core": "^7.1.9", "rollup": "^2.0.0", "source-map": "^0.6.1" }, diff --git a/packages/babel/types/index.d.ts b/packages/babel/types/index.d.ts index c723d36d1..90f102f6a 100644 --- a/packages/babel/types/index.d.ts +++ b/packages/babel/types/index.d.ts @@ -2,7 +2,7 @@ import { Plugin } from 'rollup'; import { FilterPattern } from '@rollup/pluginutils'; import * as babelCore from '@babel/core'; -interface RollupBabelInputPluginOptions +export interface RollupBabelInputPluginOptions extends Omit { /** * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there. @@ -31,37 +31,8 @@ interface RollupBabelInputPluginOptions skipPreflightCheck?: boolean; } -interface RollupBabelOutputPluginOptions +export interface RollupBabelOutputPluginOptions extends Omit { - /** - * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there. - * @default undefined; - */ - include?: FilterPattern; - /** - * A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. When relaying on Babel configuration files you can only exclude additional files with this option, you cannot override what you have configured for Babel itself. - * @default undefined; - */ - exclude?: FilterPattern; - /** - * An array of file extensions that Babel should transpile. If you want to transpile TypeScript files with this plugin it's essential to include .ts and .tsx in this option. - * @default ['.js', '.jsx', '.es6', '.es', '.mjs'] - */ - extensions?: string[]; - /** - * A boolean value indicating whether to bundle in the Babel helpers. - * @default false - */ - externalHelpers?: boolean; - /** - * An array which gives explicit control over which babelHelper functions are allowed in the bundle. - * @default undefined - */ - externalHelpersWhitelist?: string[]; - /** - * @default false - */ - runtimeHelpers?: boolean; /** * Use with other formats than UMD/IIFE. * @default false @@ -69,23 +40,23 @@ interface RollupBabelOutputPluginOptions allowAllFormats?: boolean; } -type CustomInputPluginOptions = ( +export type CustomInputPluginOptions = ( options: RollupBabelInputPluginOptions & Record ) => { customOptions: Record; pluginOptions: RollupBabelInputPluginOptions; }; -type CustomOutputPluginOptions = ( +export type CustomOutputPluginOptions = ( options: RollupBabelOutputPluginOptions & Record ) => { customOptions: Record; pluginOptions: RollupBabelOutputPluginOptions; }; -type CustomPluginConfig = ( +export type CustomPluginConfig = ( cfg: babelCore.PartialConfig, options: { code: string; customOptions: Record } ) => babelCore.TransformOptions; -type CustomPluginResult = ( +export type CustomPluginResult = ( result: babelCore.BabelFileResult, options: { code: string; @@ -94,18 +65,18 @@ type CustomPluginResult = ( transformOptions: babelCore.TransformOptions; } ) => babelCore.BabelFileResult; -interface CustomInputPlugin { +export interface CustomInputPlugin { options?: CustomInputPluginOptions; config?: CustomPluginConfig; result?: CustomPluginResult; } -interface CustomOutputPlugin { +export interface CustomOutputPlugin { options?: CustomOutputPluginOptions; config?: CustomPluginConfig; result?: CustomPluginResult; } -type CustomInputPluginBuilder = (babel: typeof babelCore) => CustomInputPlugin; -type CustomOutputPluginBuilder = (babel: typeof babelCore) => CustomOutputPlugin; +export type CustomInputPluginBuilder = (babel: typeof babelCore) => CustomInputPlugin; +export type CustomOutputPluginBuilder = (babel: typeof babelCore) => CustomOutputPlugin; /** * A Rollup plugin for seamless integration between Rollup and Babel. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3e05db0d6..541aca982 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,7 +90,7 @@ importers: '@babel/plugin-transform-runtime': 7.9.0_@babel+core@7.8.3 '@babel/preset-env': 7.9.5_@babel+core@7.8.3 '@rollup/plugin-json': 4.0.3_rollup@2.2.0 - '@types/babel__core': 7.1.8 + '@types/babel__core': 7.1.9 rollup: 2.2.0 source-map: 0.6.1 specifiers: @@ -103,7 +103,7 @@ importers: '@babel/preset-env': ^7.9.0 '@rollup/plugin-json': ^4.0.0 '@rollup/pluginutils': ^3.0.8 - '@types/babel__core': ^7.1.8 + '@types/babel__core': ^7.1.9 rollup: ^2.0.0 source-map: ^0.6.1 packages/beep: @@ -2401,7 +2401,7 @@ packages: node: '>=6' resolution: integrity: sha512-XIB2XbzHTN6ieIjfIMV9hlVcfPU26s2vafYWQcZHWXHOxiaRZYEDKEwdl129Zyg50+foYV2jCgtrqSA6qNuNSA== - /@types/babel__core/7.1.8: + /@types/babel__core/7.1.9: dependencies: '@babel/parser': 7.9.4 '@babel/types': 7.9.5 @@ -2410,7 +2410,7 @@ packages: '@types/babel__traverse': 7.0.12 dev: true resolution: - integrity: sha512-KXBiQG2OXvaPWFPDS1rD8yV9vO0OuWIqAEqLsbfX0oU2REN5KuoMnZ1gClWcBhO5I3n6oTVAmrMufOvRqdmFTQ== + integrity: sha512-sY2RsIJ5rpER1u3/aQ8OFSI7qGIy8o1NEEbgb2UaJcvOtXOMpd39ko723NBpjQFg9SIX7TXtjejZVGeIMLhoOw== /@types/babel__generator/7.6.1: dependencies: '@babel/types': 7.9.5 From a17d5065e980c7558859c1d7dcf92a45b992d43f Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Sun, 21 Jun 2020 11:40:18 +0100 Subject: [PATCH 6/9] test(babel): remove old options --- packages/babel/test/types.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/babel/test/types.ts b/packages/babel/test/types.ts index 7e1190600..d3db7d536 100644 --- a/packages/babel/test/types.ts +++ b/packages/babel/test/types.ts @@ -43,12 +43,6 @@ const rollipConfig: import('rollup').RollupOptions = { plugins: [] }), getBabelOutputPlugin({ - include: 'node_modules/**', - exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], - extensions: ['.js', '.coffee'], - runtimeHelpers: true, - externalHelpers: true, - externalHelpersWhitelist: ['example'], allowAllFormats: true, babelrc: false, plugins: [] From c9eb7242fb15116875ebb48e444976267e4f8573 Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Tue, 7 Jul 2020 11:54:23 +0100 Subject: [PATCH 7/9] chore(babel): order peerDeps --- packages/babel/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel/package.json b/packages/babel/package.json index 49dce6865..f0e6a932b 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -46,8 +46,8 @@ ], "peerDependencies": { "@babel/core": "^7.0.0", - "rollup": "^1.20.0||^2.0.0", - "@types/babel__core": "^7.1.9" + "@types/babel__core": "^7.1.9", + "rollup": "^1.20.0||^2.0.0" }, "dependencies": { "@babel/helper-module-imports": "^7.7.4", From 14131fa078d1408ecd9da57686466b140939ada4 Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Tue, 7 Jul 2020 11:56:27 +0100 Subject: [PATCH 8/9] fix(babel): typing typos --- packages/babel/test/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel/test/types.ts b/packages/babel/test/types.ts index d3db7d536..4c9027d69 100644 --- a/packages/babel/test/types.ts +++ b/packages/babel/test/types.ts @@ -8,7 +8,7 @@ import babelPlugin, { createBabelOutputPluginFactory } from '../types'; -const rollipConfig: import('rollup').RollupOptions = { +const rollupConfig: import('rollup').RollupOptions = { input: 'main.js', output: { file: 'bundle.js', @@ -50,7 +50,7 @@ const rollipConfig: import('rollup').RollupOptions = { ] }; -export default rollipConfig; +export default rollupConfig; createBabelInputPluginFactory((babelCore) => { function myPlugin() { From 18c732bbb45fc1be674ffaf648f988c17682a605 Mon Sep 17 00:00:00 2001 From: AndrewLeedham Date: Tue, 7 Jul 2020 11:57:16 +0100 Subject: [PATCH 9/9] refactor(babel): consistent typing names --- packages/babel/types/index.d.ts | 38 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/babel/types/index.d.ts b/packages/babel/types/index.d.ts index 90f102f6a..602aea31b 100644 --- a/packages/babel/types/index.d.ts +++ b/packages/babel/types/index.d.ts @@ -23,7 +23,7 @@ export interface RollupBabelInputPluginOptions * It is recommended to configure this option explicitly (even if with its default value) so an informed decision is taken on how those babel helpers are inserted into the code. * @default 'bundled' */ - babelHelpers?: 'bundled' | 'runtime' | 'inline' | 'extends'; + babelHelpers?: 'bundled' | 'runtime' | 'inline' | 'external'; /** * Before transpiling your input files this plugin also transpile a short piece of code for each input file. This is used to validate some misconfiguration errors, but for sufficiently big projects it can slow your build times so if you are confident about your configuration then you might disable those checks with this option. * @default false @@ -40,23 +40,23 @@ export interface RollupBabelOutputPluginOptions allowAllFormats?: boolean; } -export type CustomInputPluginOptions = ( +export type RollupBabelCustomInputPluginOptions = ( options: RollupBabelInputPluginOptions & Record ) => { customOptions: Record; pluginOptions: RollupBabelInputPluginOptions; }; -export type CustomOutputPluginOptions = ( +export type RollupBabelCustomOutputPluginOptions = ( options: RollupBabelOutputPluginOptions & Record ) => { customOptions: Record; pluginOptions: RollupBabelOutputPluginOptions; }; -export type CustomPluginConfig = ( +export type RollupBabelCustomPluginConfig = ( cfg: babelCore.PartialConfig, options: { code: string; customOptions: Record } ) => babelCore.TransformOptions; -export type CustomPluginResult = ( +export type RollupBabelCustomPluginResult = ( result: babelCore.BabelFileResult, options: { code: string; @@ -65,18 +65,22 @@ export type CustomPluginResult = ( transformOptions: babelCore.TransformOptions; } ) => babelCore.BabelFileResult; -export interface CustomInputPlugin { - options?: CustomInputPluginOptions; - config?: CustomPluginConfig; - result?: CustomPluginResult; +export interface RollupBabelCustomInputPlugin { + options?: RollupBabelCustomInputPluginOptions; + config?: RollupBabelCustomPluginConfig; + result?: RollupBabelCustomPluginResult; } -export interface CustomOutputPlugin { - options?: CustomOutputPluginOptions; - config?: CustomPluginConfig; - result?: CustomPluginResult; +export interface RollupBabelCustomOutputPlugin { + options?: RollupBabelCustomOutputPluginOptions; + config?: RollupBabelCustomPluginConfig; + result?: RollupBabelCustomPluginResult; } -export type CustomInputPluginBuilder = (babel: typeof babelCore) => CustomInputPlugin; -export type CustomOutputPluginBuilder = (babel: typeof babelCore) => CustomOutputPlugin; +export type RollupBabelCustomInputPluginBuilder = ( + babel: typeof babelCore +) => RollupBabelCustomInputPlugin; +export type RollupBabelCustomOutputPluginBuilder = ( + babel: typeof babelCore +) => RollupBabelCustomOutputPlugin; /** * A Rollup plugin for seamless integration between Rollup and Babel. @@ -87,10 +91,10 @@ export function getBabelInputPlugin(options?: RollupBabelInputPluginOptions): Pl export function getBabelOutputPlugin(options?: RollupBabelOutputPluginOptions): Plugin; export function createBabelInputPluginFactory( - customCallback?: CustomInputPluginBuilder + customCallback?: RollupBabelCustomInputPluginBuilder ): typeof getBabelInputPlugin; export function createBabelOutputPluginFactory( - customCallback?: CustomOutputPluginBuilder + customCallback?: RollupBabelCustomOutputPluginBuilder ): typeof getBabelOutputPlugin; /**