@hubspot/ui-extensions-dev-server 0.5.0 → 0.6.1-next.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli/logger.js CHANGED
@@ -1,19 +1,19 @@
1
1
  const { cyan, red, yellow, magenta } = require('console-log-colors');
2
2
 
3
- function info(message) {
4
- console.log(cyan(message));
3
+ function info(message, ...data) {
4
+ console.log(cyan(message), ...data);
5
5
  }
6
6
 
7
- function error(message) {
8
- console.error(red(message));
7
+ function error(message, ...data) {
8
+ console.error(red(message), ...data);
9
9
  }
10
10
 
11
- function warn(message) {
12
- console.info(yellow(message));
11
+ function warn(message, ...data) {
12
+ console.info(yellow(message), ...data);
13
13
  }
14
14
 
15
- function debug(message) {
16
- console.debug(magenta(message));
15
+ function debug(message, ...data) {
16
+ console.debug(magenta(message), ...data);
17
17
  }
18
18
 
19
19
  module.exports = { info, error, warn, debug };
package/cli/run.js CHANGED
@@ -15,7 +15,7 @@ const { loadConfigByPath, loadExtensionConfig } = require('./config');
15
15
 
16
16
  // eslint-disable-next-line no-floating-promise/no-floating-promise
17
17
  (async () => {
18
- const { DEV_MODE, BUILD_MODE, extension, help } = parseArgs();
18
+ const { DEV_MODE, BUILD_MODE, extension, help, alpha } = parseArgs();
19
19
 
20
20
  if (help || !(DEV_MODE || BUILD_MODE)) {
21
21
  showHelp(OUTPUT_DIR);
@@ -31,6 +31,7 @@ const { loadConfigByPath, loadExtensionConfig } = require('./config');
31
31
  allExtensionsConfig[path.join(extensionPath, extension)];
32
32
  }
33
33
  await DevModeInterface.setup({
34
+ alpha,
34
35
  promptUser: inquirer.createPromptModule(),
35
36
  components: {
36
37
  [appConfig.name]: {
package/cli/utils.js CHANGED
@@ -14,6 +14,7 @@ function parseArgs() {
14
14
  const optionDefinitions = [
15
15
  { name: 'port', alias: 'p', type: Number },
16
16
  { name: 'extension', alias: 'e', type: String },
17
+ { name: 'alpha', type: Boolean },
17
18
  { name: 'help', alias: 'h', type: Boolean },
18
19
  ];
19
20
 
@@ -48,6 +49,10 @@ function showHelp(OUTPUT_DIR) {
48
49
  description:
49
50
  'The extension entrypoint file to build or start local development for',
50
51
  },
52
+ {
53
+ name: 'alpha',
54
+ description: 'Run app functions locally.',
55
+ },
51
56
  {
52
57
  name: 'help',
53
58
  alias: 'h',
@@ -55,6 +55,11 @@ class DevModeInterface {
55
55
  }
56
56
 
57
57
  async setup({
58
+ // This flag is used for whether to enable local serverless on the dev server pre-INBOUND.
59
+ // If the user runs `hs project dev --local-all`, the flag is set to true.
60
+ // By INBOUND, we will remove the flag and make the dev server handle both extensions
61
+ // and app functions locally.
62
+ alpha = false,
58
63
  debug = false,
59
64
  accountId,
60
65
  httpClient,
@@ -63,6 +68,7 @@ class DevModeInterface {
63
68
  extensionConfig,
64
69
  logger,
65
70
  }) {
71
+ this.alpha = alpha;
66
72
  this.debug = debug;
67
73
  this.accountId = accountId;
68
74
  this.httpClient = httpClient;
@@ -105,6 +111,7 @@ class DevModeInterface {
105
111
  app: { path: appPath },
106
112
  accountId: this.accountId,
107
113
  httpClient: this.httpClient,
114
+ enabled: !!this.alpha,
108
115
  };
109
116
 
110
117
  this.shutdown = await startDevMode({
package/lib/build.js CHANGED
@@ -4,6 +4,7 @@ const manifestPlugin = require('./plugins/manifestPlugin');
4
4
  const path = require('path');
5
5
  const { getUrlSafeFileName } = require('./utils');
6
6
  const { loadConfig } = require('../cli/config');
7
+ const codeInjectionPlugin = require('./plugins/codeInjectionPlugin');
7
8
 
8
9
  const allowedExtensions = ['.js', '.ts', '.tsx', '.jsx'];
9
10
  const extensionErrorBaseMessage = `Supported file extensions are [${allowedExtensions.join(
@@ -20,14 +21,7 @@ async function buildAllExtensions({ outputDir, logger = console }) {
20
21
  file: data.module.file,
21
22
  outputDir,
22
23
  emptyOutDir: i === 0,
23
- plugins: {
24
- rollup: [
25
- manifestPlugin({
26
- output: getUrlSafeFileName(data.module.file),
27
- logger,
28
- }),
29
- ],
30
- },
24
+ logger,
31
25
  });
32
26
  }
33
27
  }
@@ -57,7 +51,10 @@ async function buildSingleExtension({
57
51
  },
58
52
  rollupOptions: {
59
53
  ...ROLLUP_OPTIONS,
60
- plugins: [manifestPlugin({ output, logger })],
54
+ plugins: [
55
+ manifestPlugin({ minify, output, logger }),
56
+ codeInjectionPlugin({ file }),
57
+ ],
61
58
  },
62
59
  outDir: outputDir,
63
60
  emptyOutDir,
@@ -78,16 +75,12 @@ async function remoteBuild(
78
75
  throw new Error(`${extensionErrorBaseMessage} ${fileInfo.ext}`);
79
76
  }
80
77
 
81
- const output = getUrlSafeFileName(entryPoint);
82
78
  await buildSingleExtension({
83
79
  file: entryPoint,
84
- outputFileName: output,
85
80
  outputDir,
86
- plugins: {
87
- rollup: [manifestPlugin({ minify: true, output, logger })],
88
- },
89
81
  minify: true,
90
82
  root,
83
+ logger
91
84
  });
92
85
  }
93
86
 
package/lib/dev.js CHANGED
@@ -75,15 +75,16 @@ async function startDevMode({
75
75
  logger,
76
76
  root
77
77
  );
78
- return startDevServer(
78
+
79
+ return startDevServer({
79
80
  outputDir,
80
81
  expressPort,
81
82
  webSocketPort,
82
83
  baseMessage,
83
84
  viteDevServer,
84
85
  functionsConfig,
85
- logger
86
- );
86
+ logger,
87
+ });
87
88
  }
88
89
 
89
90
  module.exports = {
@@ -1,8 +1,5 @@
1
1
  const path = require('path');
2
- const {
3
- EXTENSIONS_MESSAGE_VERSION,
4
- SERVER_CAPABILITIES,
5
- } = require('./constants');
2
+ const { EXTENSIONS_MESSAGE_VERSION } = require('./constants');
6
3
  const { loadManifest } = require('./utils');
7
4
 
8
5
  class ExtensionsService {
@@ -10,15 +7,25 @@ class ExtensionsService {
10
7
  this.endpoint = '/extensions';
11
8
  }
12
9
 
13
- add(server, webSocketPort, outputDir, baseMessage) {
10
+ add(server, webSocketPort, outputDir, baseMessage, capabilities) {
14
11
  server.get(
15
12
  this.endpoint,
16
- this.generateExtensionsHandler(baseMessage, webSocketPort, outputDir)
13
+ this.generateExtensionsHandler(
14
+ baseMessage,
15
+ webSocketPort,
16
+ outputDir,
17
+ capabilities
18
+ )
17
19
  );
18
20
  return [this.endpoint];
19
21
  }
20
22
 
21
- generateExtensionsHandler(baseMessage, webSocketPort, outputDir) {
23
+ generateExtensionsHandler(
24
+ baseMessage,
25
+ webSocketPort,
26
+ outputDir,
27
+ capabilities = []
28
+ ) {
22
29
  return function extensionsHandler(_req, res) {
23
30
  try {
24
31
  const output = path.parse(baseMessage.callback).name;
@@ -26,7 +33,7 @@ class ExtensionsService {
26
33
  const response = {
27
34
  websocket: `ws://localhost:${webSocketPort}`,
28
35
  version: EXTENSIONS_MESSAGE_VERSION,
29
- capabilities: SERVER_CAPABILITIES,
36
+ capabilities,
30
37
  extensions: [
31
38
  {
32
39
  ...baseMessage,
@@ -0,0 +1,27 @@
1
+ const path = require('path');
2
+
3
+ function codeInjectionPlugin() {
4
+ return {
5
+ name: 'ui-extensions-code-injection-plugin',
6
+ enforce: 'post', // run after default rollup plugins
7
+ transform(code, file) {
8
+ const { dir } = path.parse(file);
9
+
10
+ if (dir.includes('node_modules')) {
11
+ return { code, map: null }; // Not the file we care about, return the same code
12
+ }
13
+
14
+ // Update the code to import the self script which houses our overrides
15
+ // This needs to be the first line in the source file so that the overrides get hoisted
16
+ // to the top of the generated source file
17
+ const updatedCode = `import "@hubspot/ui-extensions-dev-server/self"; ${code}`;
18
+
19
+ // Return the updated source code. We don't need to include the new code in the
20
+ // sourcemap because we don't want it to show up in the users code when they
21
+ // view the source mapped code in the browser
22
+ return { code: updatedCode, map: null };
23
+ },
24
+ };
25
+ }
26
+
27
+ module.exports = codeInjectionPlugin;
@@ -2,6 +2,7 @@ const { ROLLUP_OPTIONS, WEBSOCKET_MESSAGE_VERSION } = require('../constants');
2
2
  const { build } = require('vite');
3
3
  const manifestPlugin = require('./manifestPlugin');
4
4
  const { stripAnsiColorCodes } = require('../utils');
5
+ const codeInjectionPlugin = require('./codeInjectionPlugin');
5
6
 
6
7
  function devBuildPlugin(options = {}) {
7
8
  const { extensionConfig, outputDir, baseMessage, logger } = options;
@@ -43,6 +44,13 @@ function devBuildPlugin(options = {}) {
43
44
  process.env.NODE_ENV || 'development'
44
45
  ),
45
46
  },
47
+ esbuild: {
48
+ tsconfigRaw: {
49
+ compilerOptions: {
50
+ preserveValueImports: true,
51
+ },
52
+ },
53
+ },
46
54
  build: {
47
55
  lib: {
48
56
  entry: extensionConfig.data.module.file,
@@ -58,6 +66,7 @@ function devBuildPlugin(options = {}) {
58
66
  output: extensionConfig.output,
59
67
  logger,
60
68
  }),
69
+ codeInjectionPlugin({ file: extensionConfig.data.module.file }),
61
70
  ],
62
71
  output: {
63
72
  ...ROLLUP_OPTIONS.output,
@@ -72,6 +81,7 @@ function devBuildPlugin(options = {}) {
72
81
  });
73
82
  return true;
74
83
  } catch (error) {
84
+ logger.error(error);
75
85
  handleBuildError(error, server);
76
86
  return false;
77
87
  }
package/lib/self.js ADDED
@@ -0,0 +1,4 @@
1
+ /* eslint-disable hubspot-dev/no-confusing-browser-globals */
2
+
3
+ // Set methods on the self object to undefined so they are not available within the worker code
4
+ self.importScripts = undefined;
package/lib/server.js CHANGED
@@ -1,36 +1,42 @@
1
1
  const express = require('express');
2
2
  const cors = require('cors');
3
+ const { SERVER_CAPABILITIES } = require('./constants');
3
4
  const extensionsService = require('./extensionsService');
4
5
  const {
5
6
  AppFunctionExecutionService,
6
7
  } = require('@hubspot/app-functions-dev-server');
7
8
 
8
- function startDevServer(
9
+ function startDevServer({
9
10
  outputDir,
10
11
  expressPort,
11
12
  webSocketPort,
12
13
  baseMessage,
13
14
  viteDevServer,
14
15
  functionsConfig,
15
- logger
16
- ) {
16
+ logger,
17
+ }) {
17
18
  const app = express();
18
19
 
19
20
  // Setup middleware
20
21
  app.use(cors());
21
22
  app.use(express.static(outputDir));
22
23
 
23
- app.use(
24
- '/api/crm-extensibility/execution/internal/v3',
25
- AppFunctionExecutionService(functionsConfig)
26
- );
27
- logger.info('Serving app functions locally');
24
+ const capabilities = functionsConfig.enabled ? SERVER_CAPABILITIES : [];
25
+
26
+ if (functionsConfig.enabled) {
27
+ app.use(
28
+ '/api/crm-extensibility/execution/internal/v3',
29
+ AppFunctionExecutionService({ ...functionsConfig, logger })
30
+ );
31
+ logger.info('Serving app functions locally');
32
+ }
28
33
 
29
34
  const endpointsAdded = extensionsService.add(
30
35
  app,
31
36
  webSocketPort,
32
37
  outputDir,
33
- baseMessage
38
+ baseMessage,
39
+ capabilities
34
40
  );
35
41
 
36
42
  endpointsAdded.forEach(endpoint => {
package/lib/utils.js CHANGED
@@ -8,8 +8,11 @@ function getUrlSafeFileName(filePath) {
8
8
  }
9
9
 
10
10
  // Strips ANSI color codes out of strings because we don't want to pass them to the browser
11
- function stripAnsiColorCodes(string) {
12
- return string.replace(
11
+ function stripAnsiColorCodes(stringWithColorCodes) {
12
+ if (!stringWithColorCodes) {
13
+ return null;
14
+ }
15
+ return stringWithColorCodes.replace(
13
16
  // eslint-disable-next-line no-control-regex
14
17
  /[\u001b][[]*([0-9]{1,4};?)*[m]/g,
15
18
  ''
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hubspot/ui-extensions-dev-server",
3
- "version": "0.5.0",
3
+ "version": "0.6.1-next.9+85660802",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,6 +10,10 @@
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
+ "exports": {
14
+ ".": "./index.js",
15
+ "./self": "./lib/self.js"
16
+ },
13
17
  "files": [
14
18
  "cli/config.js",
15
19
  "cli/logger.js",
@@ -21,6 +25,7 @@
21
25
  "lib/dev.js",
22
26
  "lib/extensionsService.js",
23
27
  "lib/DevModeInterface.js",
28
+ "lib/self.js",
24
29
  "lib/server.js",
25
30
  "lib/utils.js",
26
31
  "index.js",
@@ -28,7 +33,7 @@
28
33
  ],
29
34
  "license": "MIT",
30
35
  "dependencies": {
31
- "@hubspot/app-functions-dev-server": "^0.5.0",
36
+ "@hubspot/app-functions-dev-server": "^0.6.0",
32
37
  "command-line-args": "^5.2.1",
33
38
  "command-line-usage": "^7.0.1",
34
39
  "console-log-colors": "^0.4.0",
@@ -62,5 +67,5 @@
62
67
  "optional": true
63
68
  }
64
69
  },
65
- "gitHead": "d11a7bb73cf91ed44279b041f17778833948839f"
70
+ "gitHead": "85660802fede329b3da761adabb509cb9f376d3e"
66
71
  }