@vercel/static-build 1.1.7 → 1.2.1

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,155 +18,267 @@ 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"));
29
+ const url_1 = require("url");
25
30
  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
- ],
34
- };
31
+ const { VERCEL_CLI_VERSION } = process.env;
32
+ const PLUGINS = [
33
+ '@vercel/gatsby-plugin-vercel-analytics',
34
+ '@vercel/gatsby-plugin-vercel-builder',
35
+ ];
35
36
  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`);
42
- const gatsbyConfigPathTs = path.join(dir, `${GATSBY_CONFIG_FILE}.ts`);
43
- 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);
37
+ const GATSBY_NODE_FILE = 'gatsby-node';
38
+ async function injectPlugins(detectedVersion, dir) {
39
+ const plugins = new Set();
40
+ if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN && detectedVersion) {
41
+ const version = semver_1.default.coerce(detectedVersion);
42
+ if (version && semver_1.default.satisfies(version, '>=4.0.0')) {
43
+ plugins.add('@vercel/gatsby-plugin-vercel-builder');
44
+ }
47
45
  }
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);
46
+ if (process.env.VERCEL_ANALYTICS_ID) {
47
+ process.env.GATSBY_VERCEL_ANALYTICS_ID = process.env.VERCEL_ANALYTICS_ID;
48
+ plugins.add('@vercel/gatsby-plugin-vercel-analytics');
52
49
  }
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);
50
+ if (plugins.size === 0) {
51
+ return false;
57
52
  }
58
- else {
59
- await fs_1.promises.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify(DEFAULT_CONFIG)}`);
53
+ const ops = [addGatsbyPackage(dir, plugins)];
54
+ if (plugins.has('@vercel/gatsby-plugin-vercel-analytics')) {
55
+ ops.push(updateGatsbyConfig(dir, ['@vercel/gatsby-plugin-vercel-analytics']));
60
56
  }
57
+ if (plugins.has('@vercel/gatsby-plugin-vercel-builder')) {
58
+ ops.push(updateGatsbyNode(dir));
59
+ }
60
+ await Promise.all(ops);
61
+ return true;
62
+ }
63
+ exports.injectPlugins = injectPlugins;
64
+ function printInjectingPlugins(plugins, configPath) {
65
+ const pluginsArray = Array.from(plugins);
66
+ let pluginsStr = 'plugin';
67
+ if (pluginsArray.length > 1) {
68
+ pluginsStr += 's';
69
+ }
70
+ console.log(`Injecting Gatsby.js ${pluginsStr} ${pluginsArray
71
+ .map(p => `"${p}"`)
72
+ .join(', ')} to \`${configPath}\``);
61
73
  }
62
- exports.injectVercelAnalyticsPlugin = injectVercelAnalyticsPlugin;
63
- async function addGatsbyPackage(dir) {
74
+ async function addGatsbyPackage(dir, plugins) {
64
75
  const pkgJson = (await _shared_1.readPackageJson(dir));
65
76
  if (!pkgJson.dependencies) {
66
77
  pkgJson.dependencies = {};
67
78
  }
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);
79
+ for (const plugin of plugins) {
80
+ if (!pkgJson.dependencies[plugin]) {
81
+ console.log(`Adding "${plugin}" to \`package.json\` "dependencies"`);
82
+ let version = 'latest';
83
+ // Use the tarball URL for E2E tests
84
+ if (VERCEL_CLI_VERSION?.startsWith('https://')) {
85
+ version = new url_1.URL(`./${plugin}.tgz`, VERCEL_CLI_VERSION).href;
86
+ }
87
+ pkgJson.dependencies[plugin] = version;
88
+ }
89
+ }
90
+ await _shared_1.writePackageJson(dir, pkgJson);
91
+ }
92
+ async function updateGatsbyConfig(dir, plugins) {
93
+ const gatsbyConfigPathTs = path.join(dir, `${GATSBY_CONFIG_FILE}.ts`);
94
+ const gatsbyConfigPathMjs = path.join(dir, `${GATSBY_CONFIG_FILE}.mjs`);
95
+ const gatsbyConfigPathJs = path.join(dir, `${GATSBY_CONFIG_FILE}.js`);
96
+ if (await _shared_1.fileExists(gatsbyConfigPathTs)) {
97
+ printInjectingPlugins(plugins, gatsbyConfigPathTs);
98
+ await updateGatsbyConfigTs(gatsbyConfigPathTs, plugins);
99
+ }
100
+ else if (await _shared_1.fileExists(gatsbyConfigPathMjs)) {
101
+ printInjectingPlugins(plugins, gatsbyConfigPathMjs);
102
+ await updateGatsbyConfigMjs(gatsbyConfigPathMjs, plugins);
103
+ }
104
+ else {
105
+ printInjectingPlugins(plugins, gatsbyConfigPathJs);
106
+ if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
107
+ await updateGatsbyConfigJs(gatsbyConfigPathJs, plugins);
108
+ }
109
+ else {
110
+ await fs_1.promises.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
111
+ plugins: Array.from(plugins),
112
+ })}`);
113
+ }
72
114
  }
73
115
  }
74
- async function updateGatsbyTsConfig(configPath) {
75
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.ts');
116
+ async function updateGatsbyConfigTs(configPath, plugins) {
117
+ const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
118
+ if (!(await _shared_1.fileExists(renamedPath))) {
119
+ await fs_1.promises.rename(configPath, renamedPath);
120
+ }
76
121
  await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.ts.__vercel_builder_backup__.ts";
77
122
  import type { PluginRef } from "gatsby";
78
123
 
79
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
80
124
  const preferDefault = (m: any) => (m && m.default) || m;
81
125
 
82
126
  const vercelConfig = Object.assign(
83
127
  {},
84
-
85
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
86
128
  preferDefault(userConfig)
87
129
  );
130
+
88
131
  if (!vercelConfig.plugins) {
89
132
  vercelConfig.plugins = [];
90
133
  }
91
134
 
92
- const hasPlugin = vercelConfig.plugins.find(
93
- (p: PluginRef) =>
94
- p && (p === "${GATSBY_PLUGIN_PACKAGE_NAME}" || p.resolve === "${GATSBY_PLUGIN_PACKAGE_NAME}")
95
- );
135
+ for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
136
+ const hasPlugin = vercelConfig.plugins.find(
137
+ (p: PluginRef) =>
138
+ p && (p === plugin || p.resolve === plugin)
139
+ );
96
140
 
97
- if (!hasPlugin) {
98
- vercelConfig.plugins = vercelConfig.plugins.slice();
99
- vercelConfig.plugins.push({
100
- resolve: "${GATSBY_PLUGIN_PACKAGE_NAME}",
101
- options: {},
102
- });
141
+ if (!hasPlugin) {
142
+ vercelConfig.plugins = vercelConfig.plugins.slice();
143
+ vercelConfig.plugins.push(plugin);
144
+ }
103
145
  }
104
146
 
105
147
  export default vercelConfig;
106
148
  `);
107
149
  }
108
- async function updateGatsbyMjsConfig(configPath) {
109
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.mjs');
150
+ async function updateGatsbyConfigMjs(configPath, plugins) {
151
+ const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
152
+ if (!(await _shared_1.fileExists(renamedPath))) {
153
+ await fs_1.promises.rename(configPath, renamedPath);
154
+ }
110
155
  await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
111
156
 
112
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
113
157
  const preferDefault = (m) => (m && m.default) || m;
114
158
 
115
159
  const vercelConfig = Object.assign(
116
160
  {},
117
-
118
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
119
161
  preferDefault(userConfig)
120
162
  );
121
163
  if (!vercelConfig.plugins) {
122
164
  vercelConfig.plugins = [];
123
165
  }
124
166
 
125
- const hasPlugin = vercelConfig.plugins.find(
126
- (p) =>
127
- p && (p === "${GATSBY_PLUGIN_PACKAGE_NAME}" || p.resolve === "${GATSBY_PLUGIN_PACKAGE_NAME}")
128
- );
167
+ for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
168
+ const hasPlugin = vercelConfig.plugins.find(
169
+ (p) => p && (p === plugin || p.resolve === plugin)
170
+ );
129
171
 
130
- if (!hasPlugin) {
131
- vercelConfig.plugins = vercelConfig.plugins.slice();
132
- vercelConfig.plugins.push({
133
- resolve: "${GATSBY_PLUGIN_PACKAGE_NAME}",
134
- options: {},
135
- });
172
+ if (!hasPlugin) {
173
+ vercelConfig.plugins = vercelConfig.plugins.slice();
174
+ vercelConfig.plugins.push(plugin);
175
+ }
136
176
  }
137
177
 
138
178
  export default vercelConfig;
139
179
  `);
140
180
  }
141
- async function updateGatsbyJsConfig(configPath) {
142
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.js');
181
+ async function updateGatsbyConfigJs(configPath, plugins) {
182
+ const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
183
+ if (!(await _shared_1.fileExists(renamedPath))) {
184
+ await fs_1.promises.rename(configPath, renamedPath);
185
+ }
143
186
  await fs_1.promises.writeFile(configPath, `const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
144
187
 
145
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
146
188
  const preferDefault = m => (m && m.default) || m;
147
189
 
148
190
  const vercelConfig = Object.assign(
149
191
  {},
150
-
151
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
152
192
  preferDefault(userConfig)
153
193
  );
154
194
  if (!vercelConfig.plugins) {
155
195
  vercelConfig.plugins = [];
156
196
  }
157
197
 
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
- }
198
+ for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
199
+ const hasPlugin = vercelConfig.plugins.find(
200
+ (p) => p && (p === plugin || p.resolve === plugin)
201
+ );
169
202
 
203
+ if (!hasPlugin) {
204
+ vercelConfig.plugins = vercelConfig.plugins.slice();
205
+ vercelConfig.plugins.push(plugin);
206
+ }
207
+ }
170
208
  module.exports = vercelConfig;
171
209
  `);
172
210
  }
211
+ async function updateGatsbyNode(dir) {
212
+ const gatsbyNodePathTs = path.join(dir, `${GATSBY_NODE_FILE}.ts`);
213
+ const gatsbyNodePathMjs = path.join(dir, `${GATSBY_NODE_FILE}.mjs`);
214
+ const gatsbyNodePathJs = path.join(dir, `${GATSBY_NODE_FILE}.js`);
215
+ if (await _shared_1.fileExists(gatsbyNodePathTs)) {
216
+ await updateGatsbyNodeTs(gatsbyNodePathTs);
217
+ }
218
+ else if (await _shared_1.fileExists(gatsbyNodePathMjs)) {
219
+ await updateGatsbyNodeMjs(gatsbyNodePathMjs);
220
+ }
221
+ else if (await _shared_1.fileExists(gatsbyNodePathJs)) {
222
+ await updateGatsbyNodeJs(gatsbyNodePathJs);
223
+ }
224
+ else {
225
+ await fs_1.promises.writeFile(gatsbyNodePathJs, `module.exports = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');`);
226
+ }
227
+ }
228
+ async function updateGatsbyNodeTs(configPath) {
229
+ const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
230
+ if (!(await _shared_1.fileExists(renamedPath))) {
231
+ await fs_1.promises.rename(configPath, renamedPath);
232
+ }
233
+ await fs_1.promises.writeFile(configPath, `import type { GatsbyNode } from 'gatsby';
234
+ import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
235
+ import * as gatsbyNode from './gatsby-node.ts.__vercel_builder_backup__.ts';
236
+
237
+ export * from './gatsby-node.ts.__vercel_builder_backup__.ts';
238
+
239
+ export const onPostBuild: GatsbyNode['onPostBuild'] = async (args, options) => {
240
+ if (typeof (gatsbyNode as any).onPostBuild === 'function') {
241
+ await (gatsbyNode as any).onPostBuild(args, options);
242
+ }
243
+ await vercelBuilder.onPostBuild(args, options);
244
+ };
245
+ `);
246
+ }
247
+ async function updateGatsbyNodeMjs(configPath) {
248
+ const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
249
+ if (!(await _shared_1.fileExists(renamedPath))) {
250
+ await fs_1.promises.rename(configPath, renamedPath);
251
+ }
252
+ await fs_1.promises.writeFile(configPath, `import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
253
+ import * as gatsbyNode from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
254
+
255
+ export * from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
256
+
257
+ export const onPostBuild = async (args, options) => {
258
+ if (typeof gatsbyNode.onPostBuild === 'function') {
259
+ await gatsbyNode.onPostBuild(args, options);
260
+ }
261
+ await vercelBuilder.onPostBuild(args, options);
262
+ };
263
+ `);
264
+ }
265
+ async function updateGatsbyNodeJs(configPath) {
266
+ const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
267
+ if (!(await _shared_1.fileExists(renamedPath))) {
268
+ await fs_1.promises.rename(configPath, renamedPath);
269
+ }
270
+ await fs_1.promises.writeFile(configPath, `const vercelBuilder = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');
271
+ const gatsbyNode = require('./gatsby-node.js.__vercel_builder_backup__.js');
272
+
273
+ const origOnPostBuild = gatsbyNode.onPostBuild;
274
+
275
+ gatsbyNode.onPostBuild = async (args, options) => {
276
+ if (typeof origOnPostBuild === 'function') {
277
+ await origOnPostBuild(args, options);
278
+ }
279
+ await vercelBuilder.onPostBuild(args, options);
280
+ };
281
+
282
+ module.exports = gatsbyNode;
283
+ `);
284
+ }
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.1",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index",
6
6
  "homepage": "https://vercel.com/docs/build-step",
@@ -15,7 +15,7 @@
15
15
  "scripts": {
16
16
  "build": "node build",
17
17
  "test": "jest --env node --verbose --bail --runInBand",
18
- "test-unit": "pnpm test test/build.test.ts test/prepare-cache.test.ts",
18
+ "test-unit": "pnpm test test/build.test.ts test/gatsby.test.js test/prepare-cache.test.ts",
19
19
  "test-integration-once": "pnpm test test/integration-*.test.js"
20
20
  },
21
21
  "jest": {
@@ -37,8 +37,12 @@
37
37
  "@types/node": "14.18.33",
38
38
  "@types/node-fetch": "2.5.4",
39
39
  "@types/promise-timeout": "1.3.0",
40
- "@vercel/build-utils": "5.9.0",
41
- "@vercel/frameworks": "1.2.4",
40
+ "@types/semver": "7.3.13",
41
+ "@vercel/build-utils": "6.0.0",
42
+ "@vercel/frameworks": "1.3.0",
43
+ "@vercel/fs-detectors": "3.7.6",
44
+ "@vercel/gatsby-plugin-vercel-analytics": "1.0.7",
45
+ "@vercel/gatsby-plugin-vercel-builder": "1.0.1",
42
46
  "@vercel/ncc": "0.24.0",
43
47
  "@vercel/routing-utils": "2.1.8",
44
48
  "@vercel/static-config": "2.0.11",
@@ -49,9 +53,10 @@
49
53
  "ms": "2.1.2",
50
54
  "node-fetch": "2.6.7",
51
55
  "rc9": "1.2.0",
56
+ "semver": "7.3.8",
52
57
  "tree-kill": "1.2.2",
53
58
  "ts-morph": "12.0.0",
54
59
  "typescript": "4.3.4"
55
60
  },
56
- "gitHead": "4ccdcde463560dc44da89edf52523419fc56ab62"
61
+ "gitHead": "25f6595d3698546ba69c7fe9f601a906167fef3f"
57
62
  }