-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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: support async plugin's pre/post #16862
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,32 @@ export default class PluginPass<Options = object> { | |
file: File; | ||
opts: Partial<Options>; | ||
|
||
// The working directory that Babel's programmatic options are loaded | ||
// relative to. | ||
/** | ||
* The working directory that Babel's programmatic options are loaded | ||
* relative to. | ||
*/ | ||
cwd: string; | ||
|
||
// The absolute path of the file being compiled. | ||
/** The absolute path of the file being compiled. */ | ||
filename: string | void; | ||
|
||
constructor(file: File, key?: string | null, options?: Options) { | ||
/** | ||
* Is Babel executed in async mode or not. | ||
*/ | ||
isAsync: boolean | undefined; | ||
|
||
constructor( | ||
file: File, | ||
key?: string | null, | ||
options?: Options, | ||
isAsync?: boolean, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i've added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only export There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, you can just remove the |
||
) { | ||
this.key = key; | ||
this.file = file; | ||
this.opts = options || {}; | ||
this.cwd = file.opts.cwd; | ||
this.filename = file.opts.filename; | ||
this.isAsync = isAsync; | ||
} | ||
|
||
set(key: unknown, val: unknown) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,6 +200,32 @@ describe("asynchronicity", () => { | |
}); | ||
}); | ||
|
||
describe("PluginPass.isAsync", () => { | ||
nodeGte8("called synchronously", () => { | ||
process.chdir("plugin-pass-is-async"); | ||
|
||
expect(babel.transformSync("")).toMatchObject({ | ||
code: `"sync"`, | ||
}); | ||
}); | ||
|
||
nodeGte8("called asynchronously", async () => { | ||
process.chdir("plugin-pass-is-async"); | ||
|
||
await expect(babel.transformAsync("")).resolves.toMatchObject({ | ||
code: `"async"`, | ||
}); | ||
}); | ||
|
||
nodeGte8("should await inherited .pre", async () => { | ||
process.chdir("plugin-pre-chaining"); | ||
|
||
await expect(babel.transformAsync("")).resolves.toMatchObject({ | ||
code: `"pluginC,pluginB,pluginA"`, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("inherits", () => { | ||
nodeGte8("called synchronously", () => { | ||
process.chdir("plugin-inherits"); | ||
|
@@ -312,7 +338,7 @@ describe("asynchronicity", () => { | |
}); | ||
|
||
describe("misc", () => { | ||
it("unknown preset in config file does not trigget unhandledRejection if caught", async () => { | ||
it("unknown preset in config file does not trigger unhandledRejection if caught", async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed a typo |
||
process.chdir("unknown-preset"); | ||
const handler = jest.fn(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
plugins: ["./plugin"], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = function pluginA({ types: t }) { | ||
return { | ||
visitor: { | ||
Program(path) { | ||
const label = this.isAsync ? "async" : "sync"; | ||
|
||
path.pushContainer("body", t.stringLiteral(label)); | ||
}, | ||
}, | ||
}; | ||
}; |
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.
With
/**
style of comment it's a jsdoc description which could be used as quick documentation in IDE.