@vercel/static-build 1.1.7 → 1.2.0

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.
@@ -27,7 +27,7 @@ exports.readPackageJson = readPackageJson;
27
27
  * Write package.json
28
28
  */
29
29
  async function writePackageJson(workPath, packageJson) {
30
- await fs_1.promises.writeFile(path_1.default.join(workPath, 'package.json'), JSON.stringify(packageJson, null, 2));
30
+ await fs_1.promises.writeFile(path_1.default.join(workPath, 'package.json'), `${JSON.stringify(packageJson, null, 2)}\n`);
31
31
  }
32
32
  exports.writePackageJson = writePackageJson;
33
33
  function isObjectEmpty(object) {
@@ -18,60 +18,84 @@ var __importStar = (this && this.__importStar) || function (mod) {
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
21
24
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.injectVercelAnalyticsPlugin = void 0;
25
+ exports.injectPlugins = void 0;
23
26
  const fs_1 = require("fs");
24
27
  const path = __importStar(require("path"));
28
+ const semver_1 = __importDefault(require("semver"));
25
29
  const _shared_1 = require("./_shared");
26
- const GATSBY_PLUGIN_PACKAGE_NAME = '@vercel/gatsby-plugin-vercel-analytics';
27
- const DEFAULT_CONFIG = {
28
- plugins: [
29
- {
30
- resolve: GATSBY_PLUGIN_PACKAGE_NAME,
31
- options: {},
32
- },
33
- ],
30
+ const PLUGINS = {
31
+ GATSBY_PLUGIN_VERCEL_ANALYTICS: '@vercel/gatsby-plugin-vercel-analytics',
32
+ GATSBY_PLUGIN_VERCEL_BUILDER: '@vercel/gatsby-plugin-vercel-builder',
34
33
  };
35
34
  const GATSBY_CONFIG_FILE = 'gatsby-config';
36
- async function injectVercelAnalyticsPlugin(dir) {
37
- // Gatsby requires a special variable name for environment variables to be
38
- // exposed to the client-side JavaScript bundles:
39
- process.env.GATSBY_VERCEL_ANALYTICS_ID = process.env.VERCEL_ANALYTICS_ID;
40
- const gatsbyConfigPathJs = path.join(dir, `${GATSBY_CONFIG_FILE}.js`);
41
- const gatsbyConfigPathMjs = path.join(dir, `${GATSBY_CONFIG_FILE}.mjs`);
35
+ async function injectPlugins(detectedVersion, dir) {
36
+ const pluginsToInject = [];
37
+ if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN && detectedVersion) {
38
+ const version = semver_1.default.coerce(detectedVersion);
39
+ if (version && semver_1.default.satisfies(version, '>=4.0.0')) {
40
+ pluginsToInject.push(PLUGINS.GATSBY_PLUGIN_VERCEL_BUILDER);
41
+ }
42
+ }
43
+ if (process.env.VERCEL_ANALYTICS_ID) {
44
+ process.env.GATSBY_VERCEL_ANALYTICS_ID = process.env.VERCEL_ANALYTICS_ID;
45
+ pluginsToInject.push(PLUGINS.GATSBY_PLUGIN_VERCEL_ANALYTICS);
46
+ }
47
+ if (pluginsToInject.length === 0) {
48
+ return false;
49
+ }
50
+ await addGatsbyPackage(dir, pluginsToInject);
42
51
  const gatsbyConfigPathTs = path.join(dir, `${GATSBY_CONFIG_FILE}.ts`);
52
+ const gatsbyConfigPathMjs = path.join(dir, `${GATSBY_CONFIG_FILE}.mjs`);
53
+ const gatsbyConfigPathJs = path.join(dir, `${GATSBY_CONFIG_FILE}.js`);
43
54
  if (await _shared_1.fileExists(gatsbyConfigPathTs)) {
44
- console.log(`Injecting Gatsby.js analytics plugin "${GATSBY_PLUGIN_PACKAGE_NAME}" to \`${gatsbyConfigPathTs}\``);
45
- await addGatsbyPackage(dir);
46
- return updateGatsbyTsConfig(gatsbyConfigPathTs);
55
+ printInjectingPlugins(pluginsToInject, gatsbyConfigPathTs);
56
+ await updateGatsbyTsConfig(gatsbyConfigPathTs, pluginsToInject);
47
57
  }
48
- if (await _shared_1.fileExists(gatsbyConfigPathMjs)) {
49
- console.log(`Injecting Gatsby.js analytics plugin "${GATSBY_PLUGIN_PACKAGE_NAME}" to \`${gatsbyConfigPathMjs}\``);
50
- await addGatsbyPackage(dir);
51
- return updateGatsbyMjsConfig(gatsbyConfigPathMjs);
52
- }
53
- console.log(`Injecting Gatsby.js analytics plugin "${GATSBY_PLUGIN_PACKAGE_NAME}" to \`${gatsbyConfigPathJs}\``);
54
- await addGatsbyPackage(dir);
55
- if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
56
- await updateGatsbyJsConfig(gatsbyConfigPathJs);
58
+ else if (await _shared_1.fileExists(gatsbyConfigPathMjs)) {
59
+ printInjectingPlugins(pluginsToInject, gatsbyConfigPathMjs);
60
+ await updateGatsbyMjsConfig(gatsbyConfigPathMjs, pluginsToInject);
57
61
  }
58
62
  else {
59
- await fs_1.promises.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify(DEFAULT_CONFIG)}`);
63
+ printInjectingPlugins(pluginsToInject, gatsbyConfigPathJs);
64
+ if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
65
+ await updateGatsbyJsConfig(gatsbyConfigPathJs, pluginsToInject);
66
+ }
67
+ else {
68
+ await fs_1.promises.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
69
+ plugins: pluginsToInject,
70
+ })}`);
71
+ }
72
+ }
73
+ return true;
74
+ }
75
+ exports.injectPlugins = injectPlugins;
76
+ function printInjectingPlugins(plugins, configPath) {
77
+ let pluginsStr = 'plugin';
78
+ if (plugins.length > 1) {
79
+ pluginsStr += 's';
60
80
  }
81
+ console.log(`Injecting Gatsby.js ${pluginsStr} ${plugins
82
+ .map(p => `"${p}"`)
83
+ .join(', ')} to \`${configPath}\``);
61
84
  }
62
- exports.injectVercelAnalyticsPlugin = injectVercelAnalyticsPlugin;
63
- async function addGatsbyPackage(dir) {
85
+ async function addGatsbyPackage(dir, plugins) {
64
86
  const pkgJson = (await _shared_1.readPackageJson(dir));
65
87
  if (!pkgJson.dependencies) {
66
88
  pkgJson.dependencies = {};
67
89
  }
68
- if (!pkgJson.dependencies[GATSBY_PLUGIN_PACKAGE_NAME]) {
69
- console.log(`Adding "${GATSBY_PLUGIN_PACKAGE_NAME}" to \`package.json\` "dependencies"`);
70
- pkgJson.dependencies[GATSBY_PLUGIN_PACKAGE_NAME] = 'latest';
71
- await _shared_1.writePackageJson(dir, pkgJson);
90
+ for (const plugin of plugins) {
91
+ if (!pkgJson.dependencies[plugin]) {
92
+ console.log(`Adding "${plugin}" to \`package.json\` "dependencies"`);
93
+ pkgJson.dependencies[plugin] = 'latest';
94
+ }
72
95
  }
96
+ await _shared_1.writePackageJson(dir, pkgJson);
73
97
  }
74
- async function updateGatsbyTsConfig(configPath) {
98
+ async function updateGatsbyTsConfig(configPath, plugins) {
75
99
  await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.ts');
76
100
  await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.ts.__vercel_builder_backup__.ts";
77
101
  import type { PluginRef } from "gatsby";
@@ -81,31 +105,30 @@ const preferDefault = (m: any) => (m && m.default) || m;
81
105
 
82
106
  const vercelConfig = Object.assign(
83
107
  {},
84
-
85
108
  // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
86
109
  preferDefault(userConfig)
87
110
  );
111
+
88
112
  if (!vercelConfig.plugins) {
89
113
  vercelConfig.plugins = [];
90
114
  }
91
115
 
92
- const hasPlugin = vercelConfig.plugins.find(
93
- (p: PluginRef) =>
94
- p && (p === "${GATSBY_PLUGIN_PACKAGE_NAME}" || p.resolve === "${GATSBY_PLUGIN_PACKAGE_NAME}")
95
- );
116
+ for (const plugin of ${JSON.stringify(plugins)}) {
117
+ const hasPlugin = vercelConfig.plugins.find(
118
+ (p: PluginRef) =>
119
+ p && (p === plugin || p.resolve === plugin)
120
+ );
96
121
 
97
- if (!hasPlugin) {
98
- vercelConfig.plugins = vercelConfig.plugins.slice();
99
- vercelConfig.plugins.push({
100
- resolve: "${GATSBY_PLUGIN_PACKAGE_NAME}",
101
- options: {},
102
- });
122
+ if (!hasPlugin) {
123
+ vercelConfig.plugins = vercelConfig.plugins.slice();
124
+ vercelConfig.plugins.push(plugin);
125
+ }
103
126
  }
104
127
 
105
128
  export default vercelConfig;
106
129
  `);
107
130
  }
108
- async function updateGatsbyMjsConfig(configPath) {
131
+ async function updateGatsbyMjsConfig(configPath, plugins) {
109
132
  await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.mjs');
110
133
  await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
111
134
 
@@ -114,7 +137,6 @@ const preferDefault = (m) => (m && m.default) || m;
114
137
 
115
138
  const vercelConfig = Object.assign(
116
139
  {},
117
-
118
140
  // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
119
141
  preferDefault(userConfig)
120
142
  );
@@ -122,23 +144,21 @@ if (!vercelConfig.plugins) {
122
144
  vercelConfig.plugins = [];
123
145
  }
124
146
 
125
- const hasPlugin = vercelConfig.plugins.find(
126
- (p) =>
127
- p && (p === "${GATSBY_PLUGIN_PACKAGE_NAME}" || p.resolve === "${GATSBY_PLUGIN_PACKAGE_NAME}")
128
- );
147
+ for (const plugin of ${JSON.stringify(plugins)}) {
148
+ const hasPlugin = vercelConfig.plugins.find(
149
+ (p) => p && (p === plugin || p.resolve === plugin)
150
+ );
129
151
 
130
- if (!hasPlugin) {
131
- vercelConfig.plugins = vercelConfig.plugins.slice();
132
- vercelConfig.plugins.push({
133
- resolve: "${GATSBY_PLUGIN_PACKAGE_NAME}",
134
- options: {},
135
- });
152
+ if (!hasPlugin) {
153
+ vercelConfig.plugins = vercelConfig.plugins.slice();
154
+ vercelConfig.plugins.push(plugin);
155
+ }
136
156
  }
137
157
 
138
158
  export default vercelConfig;
139
159
  `);
140
160
  }
141
- async function updateGatsbyJsConfig(configPath) {
161
+ async function updateGatsbyJsConfig(configPath, plugins) {
142
162
  await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.js');
143
163
  await fs_1.promises.writeFile(configPath, `const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
144
164
 
@@ -147,7 +167,6 @@ const preferDefault = m => (m && m.default) || m;
147
167
 
148
168
  const vercelConfig = Object.assign(
149
169
  {},
150
-
151
170
  // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
152
171
  preferDefault(userConfig)
153
172
  );
@@ -155,18 +174,16 @@ if (!vercelConfig.plugins) {
155
174
  vercelConfig.plugins = [];
156
175
  }
157
176
 
158
- const hasPlugin = vercelConfig.plugins.find(
159
- (p) =>
160
- p && (p === "${GATSBY_PLUGIN_PACKAGE_NAME}" || p.resolve === "${GATSBY_PLUGIN_PACKAGE_NAME}")
161
- );
162
- if (!hasPlugin) {
163
- vercelConfig.plugins = vercelConfig.plugins.slice();
164
- vercelConfig.plugins.push({
165
- resolve: "${GATSBY_PLUGIN_PACKAGE_NAME}",
166
- options: {},
167
- });
168
- }
177
+ for (const plugin of ${JSON.stringify(plugins)}) {
178
+ const hasPlugin = vercelConfig.plugins.find(
179
+ (p) => p && (p === plugin || p.resolve === plugin)
180
+ );
169
181
 
182
+ if (!hasPlugin) {
183
+ vercelConfig.plugins = vercelConfig.plugins.slice();
184
+ vercelConfig.plugins.push(plugin);
185
+ }
186
+ }
170
187
  module.exports = vercelConfig;
171
188
  `);
172
189
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/static-build",
3
- "version": "1.1.7",
3
+ "version": "1.2.0",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index",
6
6
  "homepage": "https://vercel.com/docs/build-step",
@@ -37,8 +37,10 @@
37
37
  "@types/node": "14.18.33",
38
38
  "@types/node-fetch": "2.5.4",
39
39
  "@types/promise-timeout": "1.3.0",
40
+ "@types/semver": "7.3.13",
40
41
  "@vercel/build-utils": "5.9.0",
41
42
  "@vercel/frameworks": "1.2.4",
43
+ "@vercel/fs-detectors": "3.7.5",
42
44
  "@vercel/ncc": "0.24.0",
43
45
  "@vercel/routing-utils": "2.1.8",
44
46
  "@vercel/static-config": "2.0.11",
@@ -49,9 +51,10 @@
49
51
  "ms": "2.1.2",
50
52
  "node-fetch": "2.6.7",
51
53
  "rc9": "1.2.0",
54
+ "semver": "7.3.8",
52
55
  "tree-kill": "1.2.2",
53
56
  "ts-morph": "12.0.0",
54
57
  "typescript": "4.3.4"
55
58
  },
56
- "gitHead": "4ccdcde463560dc44da89edf52523419fc56ab62"
59
+ "gitHead": "92f5b6e0c9be947a68b617d282fc39144f530813"
57
60
  }