Skip to content

Commit 7c6f519

Browse files
feat(babel): add typings (#462)
* 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
1 parent e548fdd commit 7c6f519

File tree

4 files changed

+299
-3
lines changed

4 files changed

+299
-3
lines changed

packages/babel/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
"lint:js": "eslint --fix --cache src test",
2626
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
2727
"prebuild": "del-cli dist",
28-
"prepublishOnly": "pnpm run lint && pnpm run test && pnpm run build",
28+
"prepublishOnly": "pnpm run lint && pnpm run test && pnpm run build && pnpm run test:ts",
2929
"pretest": "pnpm run build",
30-
"test": "ava"
30+
"test": "ava",
31+
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
3132
},
3233
"files": [
3334
"dist",
35+
"types",
3436
"README.md",
3537
"LICENSE"
3638
],
@@ -44,6 +46,7 @@
4446
],
4547
"peerDependencies": {
4648
"@babel/core": "^7.0.0",
49+
"@types/babel__core": "^7.1.9",
4750
"rollup": "^1.20.0||^2.0.0"
4851
},
4952
"dependencies": {
@@ -58,6 +61,7 @@
5861
"@babel/plugin-transform-runtime": "^7.7.4",
5962
"@babel/preset-env": "^7.9.0",
6063
"@rollup/plugin-json": "^4.0.0",
64+
"@types/babel__core": "^7.1.9",
6165
"rollup": "^2.0.0",
6266
"source-map": "^0.6.1"
6367
},
@@ -73,5 +77,11 @@
7377
"Bogdan Chadkin <[email protected]>",
7478
"Mateusz Burzyński <[email protected]> (https://github.com/Andarist)"
7579
],
76-
"module": "dist/index.es.js"
80+
"module": "dist/index.es.js",
81+
"peerDependenciesMeta": {
82+
"@types/babel__core": {
83+
"optional": true
84+
}
85+
},
86+
"types": "types/index.d.ts"
7787
}

packages/babel/test/types.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/** eslint-disable @typescript-eslint/no-unused-vars */
2+
3+
import babelPlugin, {
4+
babel,
5+
getBabelInputPlugin,
6+
getBabelOutputPlugin,
7+
createBabelInputPluginFactory,
8+
createBabelOutputPluginFactory
9+
} from '../types';
10+
11+
const rollupConfig: import('rollup').RollupOptions = {
12+
input: 'main.js',
13+
output: {
14+
file: 'bundle.js',
15+
format: 'iife'
16+
},
17+
plugins: [
18+
babelPlugin({
19+
include: 'node_modules/**',
20+
exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/],
21+
extensions: ['.js', '.coffee'],
22+
babelHelpers: 'runtime',
23+
skipPreflightCheck: true,
24+
babelrc: false,
25+
plugins: []
26+
}),
27+
babel({
28+
include: 'node_modules/**',
29+
exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/],
30+
extensions: ['.js', '.coffee'],
31+
babelHelpers: 'runtime',
32+
skipPreflightCheck: true,
33+
babelrc: false,
34+
plugins: []
35+
}),
36+
getBabelInputPlugin({
37+
include: 'node_modules/**',
38+
exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/],
39+
extensions: ['.js', '.coffee'],
40+
babelHelpers: 'runtime',
41+
skipPreflightCheck: true,
42+
babelrc: false,
43+
plugins: []
44+
}),
45+
getBabelOutputPlugin({
46+
allowAllFormats: true,
47+
babelrc: false,
48+
plugins: []
49+
})
50+
]
51+
};
52+
53+
export default rollupConfig;
54+
55+
createBabelInputPluginFactory((babelCore) => {
56+
function myPlugin() {
57+
return {
58+
name: `input-${babelCore.version}`,
59+
visitor: {}
60+
};
61+
}
62+
63+
return {
64+
// Passed the plugin options.
65+
options({ opt1, opt2, ...pluginOptions }) {
66+
return {
67+
// Pull out any custom options that the plugin might have.
68+
customOptions: { opt1, opt2 },
69+
70+
// Pass the options back with the two custom options removed.
71+
pluginOptions
72+
};
73+
},
74+
75+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
76+
config(cfg, { code, customOptions }) {
77+
if (cfg.hasFilesystemConfig()) {
78+
// Use the normal config
79+
return cfg.options;
80+
}
81+
82+
return {
83+
...cfg.options,
84+
plugins: [
85+
...(cfg.options.plugins || []),
86+
87+
// Include a custom plugin in the options.
88+
myPlugin
89+
]
90+
};
91+
},
92+
93+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
94+
result(result, { code, customOptions, config, transformOptions }) {
95+
return {
96+
...result,
97+
code: `${result.code}\n// Generated by some custom plugin`
98+
};
99+
}
100+
};
101+
});
102+
103+
createBabelOutputPluginFactory((babelCore) => {
104+
function myPlugin() {
105+
return {
106+
name: `output-${babelCore.version}`,
107+
visitor: {}
108+
};
109+
}
110+
111+
return {
112+
// Passed the plugin options.
113+
options({ opt1, opt2, ...pluginOptions }) {
114+
return {
115+
// Pull out any custom options that the plugin might have.
116+
customOptions: { opt1, opt2 },
117+
118+
// Pass the options back with the two custom options removed.
119+
pluginOptions
120+
};
121+
},
122+
123+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
124+
config(cfg, { code, customOptions }) {
125+
if (cfg.hasFilesystemConfig()) {
126+
// Use the normal config
127+
return cfg.options;
128+
}
129+
130+
return {
131+
...cfg.options,
132+
plugins: [
133+
...(cfg.options.plugins || []),
134+
135+
// Include a custom plugin in the options.
136+
myPlugin
137+
]
138+
};
139+
},
140+
141+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
142+
result(result, { code, customOptions, config, transformOptions }) {
143+
return {
144+
...result,
145+
code: `${result.code}\n// Generated by some custom plugin`
146+
};
147+
}
148+
};
149+
});

packages/babel/types/index.d.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { Plugin } from 'rollup';
2+
import { FilterPattern } from '@rollup/pluginutils';
3+
import * as babelCore from '@babel/core';
4+
5+
export interface RollupBabelInputPluginOptions
6+
extends Omit<babelCore.TransformOptions, 'include' | 'exclude'> {
7+
/**
8+
* 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.
9+
* @default undefined;
10+
*/
11+
include?: FilterPattern;
12+
/**
13+
* 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.
14+
* @default undefined;
15+
*/
16+
exclude?: FilterPattern;
17+
/**
18+
* 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.
19+
* @default ['.js', '.jsx', '.es6', '.es', '.mjs']
20+
*/
21+
extensions?: string[];
22+
/**
23+
* 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.
24+
* @default 'bundled'
25+
*/
26+
babelHelpers?: 'bundled' | 'runtime' | 'inline' | 'external';
27+
/**
28+
* 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.
29+
* @default false
30+
*/
31+
skipPreflightCheck?: boolean;
32+
}
33+
34+
export interface RollupBabelOutputPluginOptions
35+
extends Omit<babelCore.TransformOptions, 'include' | 'exclude'> {
36+
/**
37+
* Use with other formats than UMD/IIFE.
38+
* @default false
39+
*/
40+
allowAllFormats?: boolean;
41+
}
42+
43+
export type RollupBabelCustomInputPluginOptions = (
44+
options: RollupBabelInputPluginOptions & Record<string, any>
45+
) => {
46+
customOptions: Record<string, any>;
47+
pluginOptions: RollupBabelInputPluginOptions;
48+
};
49+
export type RollupBabelCustomOutputPluginOptions = (
50+
options: RollupBabelOutputPluginOptions & Record<string, any>
51+
) => {
52+
customOptions: Record<string, any>;
53+
pluginOptions: RollupBabelOutputPluginOptions;
54+
};
55+
export type RollupBabelCustomPluginConfig = (
56+
cfg: babelCore.PartialConfig,
57+
options: { code: string; customOptions: Record<string, any> }
58+
) => babelCore.TransformOptions;
59+
export type RollupBabelCustomPluginResult = (
60+
result: babelCore.BabelFileResult,
61+
options: {
62+
code: string;
63+
customOptions: Record<string, any>;
64+
config: babelCore.PartialConfig;
65+
transformOptions: babelCore.TransformOptions;
66+
}
67+
) => babelCore.BabelFileResult;
68+
export interface RollupBabelCustomInputPlugin {
69+
options?: RollupBabelCustomInputPluginOptions;
70+
config?: RollupBabelCustomPluginConfig;
71+
result?: RollupBabelCustomPluginResult;
72+
}
73+
export interface RollupBabelCustomOutputPlugin {
74+
options?: RollupBabelCustomOutputPluginOptions;
75+
config?: RollupBabelCustomPluginConfig;
76+
result?: RollupBabelCustomPluginResult;
77+
}
78+
export type RollupBabelCustomInputPluginBuilder = (
79+
babel: typeof babelCore
80+
) => RollupBabelCustomInputPlugin;
81+
export type RollupBabelCustomOutputPluginBuilder = (
82+
babel: typeof babelCore
83+
) => RollupBabelCustomOutputPlugin;
84+
85+
/**
86+
* A Rollup plugin for seamless integration between Rollup and Babel.
87+
* @param options - Plugin options.
88+
* @returns Plugin instance.
89+
*/
90+
export function getBabelInputPlugin(options?: RollupBabelInputPluginOptions): Plugin;
91+
export function getBabelOutputPlugin(options?: RollupBabelOutputPluginOptions): Plugin;
92+
93+
export function createBabelInputPluginFactory(
94+
customCallback?: RollupBabelCustomInputPluginBuilder
95+
): typeof getBabelInputPlugin;
96+
export function createBabelOutputPluginFactory(
97+
customCallback?: RollupBabelCustomOutputPluginBuilder
98+
): typeof getBabelOutputPlugin;
99+
100+
/**
101+
* A Rollup plugin for seamless integration between Rollup and Babel.
102+
* @param options - Plugin options.
103+
* @returns Plugin instance.
104+
*/
105+
export function babel(options?: RollupBabelInputPluginOptions): Plugin;
106+
export default babel;

pnpm-lock.yaml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)