New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(babel): add typings #462
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0e2960a
feat(babel): add typings
AndrewLeedham 8d3c197
refactor(babel): typings PR feedback
AndrewLeedham a3436ec
chore(babel): update lockfile
AndrewLeedham 1523383
refactor(babel): named export + dev dep
AndrewLeedham 3f17f7c
refactor(babel): typing PR feedback round 2
AndrewLeedham a17d506
test(babel): remove old options
AndrewLeedham c9eb724
chore(babel): order peerDeps
AndrewLeedham 14131fa
fix(babel): typing typos
AndrewLeedham 18c732b
refactor(babel): consistent typing names
AndrewLeedham a639c34
Merge remote-tracking branch 'upstream/master'
AndrewLeedham File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` | ||
}; | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TS is not my strongest suit so take my comment with a grain of salt - maybe instead of accepting
& Record<string, any>
here this should be parametrized with a generic?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think the
customOptions
should be a 1-1 mapping, as that would limit the custom plugin, so they could not remove or add options. I think this way the user could build stronger on top if necassary.