Skip to content

Commit

Permalink
feat(babel): add typings (#462)
Browse files Browse the repository at this point in the history
* feat(babel): add typings

* refactor(babel): typings PR feedback

* chore(babel): update lockfile

* refactor(babel): named export + dev dep

* refactor(babel): typing PR feedback round 2

* test(babel): remove old options

* chore(babel): order peerDeps

* fix(babel): typing typos

* refactor(babel): consistent typing names
  • Loading branch information
AndrewLeedham committed Jul 7, 2020
1 parent e548fdd commit 7c6f519
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/babel/package.json
Expand Up @@ -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"
],
Expand All @@ -44,6 +46,7 @@
],
"peerDependencies": {
"@babel/core": "^7.0.0",
"@types/babel__core": "^7.1.9",
"rollup": "^1.20.0||^2.0.0"
},
"dependencies": {
Expand All @@ -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"
},
Expand All @@ -73,5 +77,11 @@
"Bogdan Chadkin <trysound@yandex.ru>",
"Mateusz Burzyński <mateuszburzynski@gmail.com> (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"
}
149 changes: 149 additions & 0 deletions 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`
};
}
};
});
106 changes: 106 additions & 0 deletions 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<babelCore.TransformOptions, 'include' | 'exclude'> {
/**
* 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<babelCore.TransformOptions, 'include' | 'exclude'> {
/**
* Use with other formats than UMD/IIFE.
* @default false
*/
allowAllFormats?: boolean;
}

export type RollupBabelCustomInputPluginOptions = (
options: RollupBabelInputPluginOptions & Record<string, any>
) => {
customOptions: Record<string, any>;
pluginOptions: RollupBabelInputPluginOptions;
};
export type RollupBabelCustomOutputPluginOptions = (
options: RollupBabelOutputPluginOptions & Record<string, any>
) => {
customOptions: Record<string, any>;
pluginOptions: RollupBabelOutputPluginOptions;
};
export type RollupBabelCustomPluginConfig = (
cfg: babelCore.PartialConfig,
options: { code: string; customOptions: Record<string, any> }
) => babelCore.TransformOptions;
export type RollupBabelCustomPluginResult = (
result: babelCore.BabelFileResult,
options: {
code: string;
customOptions: Record<string, any>;
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;
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c6f519

Please sign in to comment.