|
2 | 2 | // This product includes software developed at Datadog (https://www.datadoghq.com/).
|
3 | 3 | // Copyright 2019-Present Datadog, Inc.
|
4 | 4 |
|
| 5 | +import { runServer } from '@dd/core/helpers/server'; |
5 | 6 | import type { GlobalContext, GetPlugins, Options } from '@dd/core/types';
|
| 7 | +import { CONFIG_KEY as ERROR_TRACKING } from '@dd/error-tracking-plugin'; |
| 8 | +import { API_PREFIX, CONFIG_KEY, PLUGIN_NAME } from '@dd/synthetics-plugin/constants'; |
| 9 | +import type { BuildStatus, SyntheticsOptions } from '@dd/synthetics-plugin/types'; |
| 10 | +import { validateOptions } from '@dd/synthetics-plugin/validate'; |
6 | 11 | import chalk from 'chalk';
|
7 | 12 |
|
8 |
| -import { CONFIG_KEY, PLUGIN_NAME } from './constants'; |
9 |
| -import type { SyntheticsOptions, SyntheticsOptionsWithDefaults } from './types'; |
10 |
| - |
11 | 13 | export { CONFIG_KEY, PLUGIN_NAME };
|
12 | 14 |
|
13 |
| -export const helpers = { |
14 |
| - // Add the helpers you'd like to expose here. |
15 |
| -}; |
16 |
| - |
17 | 15 | export type types = {
|
18 | 16 | // Add the types you'd like to expose here.
|
19 | 17 | SyntheticsOptions: SyntheticsOptions;
|
20 | 18 | };
|
21 | 19 |
|
22 |
| -// Deal with validation and defaults here. |
23 |
| -export const validateOptions = (config: Options): SyntheticsOptionsWithDefaults => { |
24 |
| - const validatedOptions: SyntheticsOptionsWithDefaults = { |
25 |
| - // We don't want to disable it by default. |
26 |
| - disabled: false, |
27 |
| - ...config[CONFIG_KEY], |
28 |
| - }; |
29 |
| - return validatedOptions; |
30 |
| -}; |
31 |
| - |
32 | 20 | export const getPlugins: GetPlugins = (opts: Options, context: GlobalContext) => {
|
33 | 21 | const log = context.getLogger(PLUGIN_NAME);
|
34 | 22 | // Verify configuration.
|
35 |
| - const options = validateOptions(opts); |
| 23 | + const options = validateOptions(opts, context, log); |
36 | 24 |
|
37 | 25 | if (options.disabled) {
|
38 | 26 | return [];
|
39 | 27 | }
|
40 | 28 |
|
| 29 | + const response: { outDir?: string; publicPath?: string; status: BuildStatus } = { |
| 30 | + publicPath: opts[ERROR_TRACKING]?.sourcemaps?.minifiedPathPrefix, |
| 31 | + status: 'running', |
| 32 | + }; |
| 33 | + const getServerResponse = () => { |
| 34 | + return response; |
| 35 | + }; |
| 36 | + |
| 37 | + if (options.server.run) { |
| 38 | + const port = options.server.port; |
| 39 | + log.info( |
| 40 | + `Starting Synthetics local server on ${chalk.bold.cyan(`http://127.0.0.1:${port}`)}.`, |
| 41 | + ); |
| 42 | + |
| 43 | + const server = runServer({ |
| 44 | + port, |
| 45 | + root: context.bundler.outDir, |
| 46 | + routes: { |
| 47 | + [`/${API_PREFIX}/build-status`]: { |
| 48 | + get: (req, res) => { |
| 49 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 50 | + res.end(JSON.stringify(getServerResponse())); |
| 51 | + }, |
| 52 | + }, |
| 53 | + [`/${API_PREFIX}/kill`]: { |
| 54 | + get: (req, res) => { |
| 55 | + res.writeHead(200, { 'Content-Type': 'text/html' }); |
| 56 | + res.end('ok'); |
| 57 | + // kill kill kill. |
| 58 | + server.close(); |
| 59 | + server.closeAllConnections(); |
| 60 | + server.closeIdleConnections(); |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
41 | 67 | return [
|
42 | 68 | {
|
43 | 69 | name: PLUGIN_NAME,
|
44 |
| - async writeBundle() { |
45 |
| - // Execute code after the bundle is written. |
46 |
| - // https://rollupjs.org/plugin-development/#writebundle |
47 |
| - const { BUILD_PLUGINS_S8S_LOCAL, BUILD_PLUGINS_S8S_PORT } = process.env; |
48 |
| - const runServer = |
49 |
| - !options.disabled && BUILD_PLUGINS_S8S_LOCAL === '1' && BUILD_PLUGINS_S8S_PORT; |
50 |
| - |
51 |
| - if (BUILD_PLUGINS_S8S_LOCAL && !BUILD_PLUGINS_S8S_PORT) { |
52 |
| - log.warn( |
53 |
| - `Synthetics local server port is not set, please use ${chalk.bold.yellow('$BUILD_PLUGINS_S8S_PORT=1234')}.`, |
54 |
| - ); |
55 |
| - } |
56 |
| - |
57 |
| - if (!BUILD_PLUGINS_S8S_LOCAL && BUILD_PLUGINS_S8S_PORT) { |
58 |
| - log.warn( |
59 |
| - `Got server port but Synthetics local server is disabled, please use ${chalk.bold.yellow('$BUILD_PLUGINS_S8S_LOCAL=1')}.`, |
60 |
| - ); |
61 |
| - } |
62 |
| - if (runServer) { |
63 |
| - const port = +BUILD_PLUGINS_S8S_PORT; |
64 |
| - log.info( |
65 |
| - `Starting Synthetics local server on ${chalk.bold.cyan(`http://127.0.0.1:${port}`)}.`, |
66 |
| - ); |
| 70 | + bundlerReport(bundlerReport) { |
| 71 | + response.outDir = bundlerReport.outDir; |
| 72 | + }, |
| 73 | + buildReport(buildReport) { |
| 74 | + if (buildReport.errors.length) { |
| 75 | + response.status = 'fail'; |
67 | 76 | }
|
68 | 77 | },
|
| 78 | + writeBundle() { |
| 79 | + response.status = 'success'; |
| 80 | + }, |
69 | 81 | },
|
70 | 82 | ];
|
71 | 83 | };
|
0 commit comments