-
Notifications
You must be signed in to change notification settings - Fork 8
[feat] Add true-end plugin #171
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| # True End Plugin <!-- #omit in toc --> | ||
|
|
||
| A custom hook for the true end of a build, cross bundlers. | ||
|
|
||
| ## Hooks | ||
|
|
||
| ### `asyncTrueEnd` | ||
|
|
||
| This hook is called at the very end of the build asynchronously. | ||
|
|
||
| It may execute sooner than `syncTrueEnd` in some contexts: | ||
|
|
||
| - `esbuild` will call `asyncTrueEnd` before `syncTrueEnd`. | ||
| - We use `build.onDispose`, for the latest hook possible in the build. The issue is, it's synchronous only. So we have to use `build.onEnd` for the asynchronous `asyncTrueEnd`, but it's called well before `build.onDispose`. | ||
| - `webpack 4` will only call `syncTrueEnd` if the build has an error. All good otherwise. | ||
|
|
||
| ```typescript | ||
| { | ||
| name: 'my-plugin', | ||
| async asyncTrueEnd() { | ||
| // Do something asynchronous on closure | ||
| await someAsyncOperation(); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### `syncTrueEnd` | ||
|
|
||
| This hook is called at the very end of the build synchronously. | ||
|
|
||
| ```typescript | ||
| { | ||
| name: 'my-plugin', | ||
| syncTrueEnd() { | ||
| // Do something synchronous on closure | ||
| someSyncOperation(); | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or 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,27 @@ | ||
| { | ||
| "name": "@dd/internal-true-end-plugin", | ||
| "packageManager": "yarn@4.0.2", | ||
| "license": "MIT", | ||
| "private": true, | ||
| "author": "Datadog", | ||
| "description": "A custom hook for the true end of a build, cross bundlers.", | ||
| "homepage": "https://github.com/DataDog/build-plugins/tree/main/packages/plugins/true-end#readme", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/DataDog/build-plugins", | ||
| "directory": "packages/plugins/true-end" | ||
| }, | ||
| "exports": { | ||
| ".": "./src/index.ts", | ||
| "./*": "./src/*.ts" | ||
| }, | ||
| "scripts": { | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "dependencies": { | ||
| "@dd/core": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "5.4.3" | ||
| } | ||
| } |
This file contains hidden or 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,45 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2019-Present Datadog, Inc. | ||
|
|
||
| import { BUNDLERS, runBundlers } from '@dd/tests/_jest/helpers/runBundlers'; | ||
|
|
||
| describe('True End', () => { | ||
| // TODO test for multi outputs, failing builds, async hook in esbuild. | ||
| test('Should call true end hook.', async () => { | ||
| const asyncBundlers: string[] = []; | ||
| const syncBundlers: string[] = []; | ||
|
|
||
| const asyncTrueEndHookFn = jest.fn(async (bundler: string) => { | ||
| asyncBundlers.push(bundler); | ||
| }); | ||
| const syncTrueEndHookFn = jest.fn((bundler: string) => { | ||
| syncBundlers.push(bundler); | ||
| }); | ||
|
|
||
| await runBundlers({ | ||
| logLevel: 'none', | ||
| customPlugins: ({ context }) => { | ||
| return [ | ||
| { | ||
| name: 'true-end-plugin', | ||
| async asyncTrueEnd() { | ||
| await asyncTrueEndHookFn(context.bundler.fullName); | ||
| }, | ||
| syncTrueEnd() { | ||
| syncTrueEndHookFn(context.bundler.fullName); | ||
| }, | ||
| }, | ||
| ]; | ||
| }, | ||
| }); | ||
|
|
||
| const bundlerNames = BUNDLERS.map((b) => b.name); | ||
|
|
||
| expect(asyncBundlers).toEqual(bundlerNames); | ||
| expect(syncBundlers).toEqual(bundlerNames); | ||
|
|
||
| expect(asyncTrueEndHookFn).toHaveBeenCalledTimes(BUNDLERS.length); | ||
| expect(syncTrueEndHookFn).toHaveBeenCalledTimes(BUNDLERS.length); | ||
| }); | ||
| }); |
This file contains hidden or 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,64 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2019-Present Datadog, Inc. | ||
|
|
||
| import type { GetInternalPlugins, GetPluginsArg, PluginOptions, PluginName } from '@dd/core/types'; | ||
|
|
||
| export const PLUGIN_NAME: PluginName = 'datadog-true-end-plugin' as const; | ||
|
|
||
| export const getTrueEndPlugins: GetInternalPlugins = (arg: GetPluginsArg) => { | ||
| const { context } = arg; | ||
| const asyncHookFn = async () => { | ||
| await context.asyncHook('asyncTrueEnd'); | ||
| }; | ||
| const syncHookFn = () => { | ||
| context.hook('syncTrueEnd'); | ||
| }; | ||
| const bothHookFns = async () => { | ||
| syncHookFn(); | ||
| await asyncHookFn(); | ||
| }; | ||
|
|
||
| const xpackPlugin: PluginOptions['rspack'] & PluginOptions['webpack'] = (compiler) => { | ||
| if (compiler.hooks.shutdown) { | ||
| // NOTE: rspack prior to 1.2.* will randomly crash on shutdown.tapPromise. | ||
| compiler.hooks.shutdown.tapPromise(PLUGIN_NAME, bothHookFns); | ||
| } else { | ||
| // Webpack 4 only. | ||
| compiler.hooks.done.tapPromise(PLUGIN_NAME, bothHookFns); | ||
| compiler.hooks.failed.tap(PLUGIN_NAME, syncHookFn); | ||
| } | ||
| }; | ||
|
|
||
| const rollupPlugin: PluginOptions['rollup'] & PluginOptions['vite'] = { | ||
| async writeBundle() { | ||
| // TODO: Need to fallback here in case the closeBundle isn't called. | ||
| }, | ||
| async closeBundle() { | ||
| await bothHookFns(); | ||
| }, | ||
| }; | ||
|
|
||
| return [ | ||
| { | ||
| name: PLUGIN_NAME, | ||
| enforce: 'post', | ||
| webpack: xpackPlugin, | ||
| esbuild: { | ||
| setup(build) { | ||
| // NOTE: "onEnd" is the best we can do for esbuild, but it's very far from being the "true end" of the build. | ||
| build.onEnd(async () => { | ||
| await asyncHookFn(); | ||
| }); | ||
| // NOTE: "onDispose" is strictly synchronous. | ||
| build.onDispose(() => { | ||
| syncHookFn(); | ||
| }); | ||
| }, | ||
| }, | ||
| vite: rollupPlugin, | ||
| rollup: rollupPlugin, | ||
| rspack: xpackPlugin, | ||
| }, | ||
| ]; | ||
| }; |
This file contains hidden or 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,10 @@ | ||
| { | ||
| "extends": "../../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "./", | ||
| "rootDir": "./", | ||
| "outDir": "./dist" | ||
| }, | ||
| "include": ["**/*"], | ||
| "exclude": ["dist", "node_modules"] | ||
| } |
This file contains hidden or 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 hidden or 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
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.
I find the name a bit weird (but I don't have a better suggestion :/)
what about
onFullBuild?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 find
onFullBuilda bit misleading too, for instance inrollupthe concept ofbuildEndhappens before thewriteBundle, as-in, rollup (and vite) separate both the build from the write process.Can you elaborate on what you find weird with "true end"?
Maybe we could go with just
end?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.
"true" doesn't really have a definition. And it sounds a bit more like something you'd say when speaking to someone vs something defined in a spec / technical document
Uh oh!
There was an error while loading. Please reload this page.
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.
After some discussion offline, it's difficult to find a term that would really define this hook, cross bundler, so for now, until we get better consensus, we'll stick with
true-end.