diff --git a/packages/babel/package.json b/packages/babel/package.json index 5633cb3dd..1bf843c32 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" ], @@ -44,6 +46,7 @@ ], "peerDependencies": { "@babel/core": "^7.0.0", + "@types/babel__core": "^7.1.9", "rollup": "^1.20.0||^2.0.0" }, "dependencies": { @@ -58,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.9", "rollup": "^2.0.0", "source-map": "^0.6.1" }, @@ -73,5 +77,11 @@ "Bogdan Chadkin ", "Mateusz BurzyƄski (https://github.com/Andarist)" ], - "module": "dist/index.es.js" + "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 new file mode 100644 index 000000000..4c9027d69 --- /dev/null +++ b/packages/babel/test/types.ts @@ -0,0 +1,149 @@ +/** eslint-disable @typescript-eslint/no-unused-vars */ + +import babelPlugin, { + babel, + getBabelInputPlugin, + getBabelOutputPlugin, + createBabelInputPluginFactory, + createBabelOutputPluginFactory +} from '../types'; + +const rollupConfig: import('rollup').RollupOptions = { + input: 'main.js', + output: { + file: 'bundle.js', + 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/], + extensions: ['.js', '.coffee'], + babelHelpers: 'runtime', + 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({ + allowAllFormats: true, + babelrc: false, + plugins: [] + }) + ] +}; + +export default rollupConfig; + +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 new file mode 100644 index 000000000..602aea31b --- /dev/null +++ b/packages/babel/types/index.d.ts @@ -0,0 +1,106 @@ +import { Plugin } from 'rollup'; +import { FilterPattern } from '@rollup/pluginutils'; +import * as babelCore from '@babel/core'; + +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. + * @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' | '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 + */ + skipPreflightCheck?: boolean; +} + +export interface RollupBabelOutputPluginOptions + extends Omit { + /** + * Use with other formats than UMD/IIFE. + * @default false + */ + allowAllFormats?: boolean; +} + +export type RollupBabelCustomInputPluginOptions = ( + options: RollupBabelInputPluginOptions & Record +) => { + customOptions: Record; + pluginOptions: RollupBabelInputPluginOptions; +}; +export type RollupBabelCustomOutputPluginOptions = ( + options: RollupBabelOutputPluginOptions & Record +) => { + customOptions: Record; + pluginOptions: RollupBabelOutputPluginOptions; +}; +export type RollupBabelCustomPluginConfig = ( + cfg: babelCore.PartialConfig, + options: { code: string; customOptions: Record } +) => babelCore.TransformOptions; +export type RollupBabelCustomPluginResult = ( + result: babelCore.BabelFileResult, + options: { + code: string; + customOptions: Record; + config: babelCore.PartialConfig; + transformOptions: babelCore.TransformOptions; + } +) => babelCore.BabelFileResult; +export interface RollupBabelCustomInputPlugin { + options?: RollupBabelCustomInputPluginOptions; + config?: RollupBabelCustomPluginConfig; + result?: RollupBabelCustomPluginResult; +} +export interface RollupBabelCustomOutputPlugin { + options?: RollupBabelCustomOutputPluginOptions; + config?: RollupBabelCustomPluginConfig; + result?: RollupBabelCustomPluginResult; +} +export type RollupBabelCustomInputPluginBuilder = ( + babel: typeof babelCore +) => RollupBabelCustomInputPlugin; +export type RollupBabelCustomOutputPluginBuilder = ( + babel: typeof babelCore +) => RollupBabelCustomOutputPlugin; + +/** + * 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?: RollupBabelCustomInputPluginBuilder +): typeof getBabelInputPlugin; +export function createBabelOutputPluginFactory( + customCallback?: RollupBabelCustomOutputPluginBuilder +): typeof getBabelOutputPlugin; + +/** + * A Rollup plugin for seamless integration between Rollup and Babel. + * @param options - Plugin options. + * @returns Plugin instance. + */ +export function babel(options?: RollupBabelInputPluginOptions): Plugin; +export default babel; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 85112b3e6..73e1298a1 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.9 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.9 rollup: ^2.0.0 source-map: ^0.6.1 packages/beep: @@ -2453,6 +2455,35 @@ packages: node: '>=6' resolution: integrity: sha512-XIB2XbzHTN6ieIjfIMV9hlVcfPU26s2vafYWQcZHWXHOxiaRZYEDKEwdl129Zyg50+foYV2jCgtrqSA6qNuNSA== + /@types/babel__core/7.1.9: + 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-sY2RsIJ5rpER1u3/aQ8OFSI7qGIy8o1NEEbgb2UaJcvOtXOMpd39ko723NBpjQFg9SIX7TXtjejZVGeIMLhoOw== + /@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