@vercel/static-build 1.2.1 → 1.3.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.
package/dist/index.js CHANGED
@@ -241716,16 +241716,7 @@ const build = async ({ files, entrypoint, workPath, config, meta = {}, }) => {
241716
241716
  process.env[key] = value;
241717
241717
  }
241718
241718
  if (framework.slug === 'gatsby') {
241719
- const injectedPlugins = await GatsbyUtils.injectPlugins(detectedVersion, entrypointDir);
241720
- if (injectedPlugins) {
241721
- const packageManager = await fs_detectors_1.detectFramework({
241722
- fs: localFileSystemDetector,
241723
- frameworkList: fs_detectors_1.packageManagers,
241724
- });
241725
- if (packageManager === 'pnpm') {
241726
- await build_utils_1.execCommand('pnpm install --lockfile-only');
241727
- }
241728
- }
241719
+ await GatsbyUtils.injectPlugins(detectedVersion, entrypointDir);
241729
241720
  }
241730
241721
  if (process.env.VERCEL_ANALYTICS_ID) {
241731
241722
  const frameworkDirectory = path_1.default.join(workPath, path_1.default.dirname(entrypoint));
@@ -241827,6 +241818,9 @@ const build = async ({ files, entrypoint, workPath, config, meta = {}, }) => {
241827
241818
  }
241828
241819
  }
241829
241820
  }
241821
+ if (framework?.slug === 'gatsby') {
241822
+ await GatsbyUtils.createPluginSymlinks(entrypointDir);
241823
+ }
241830
241824
  let gemHome = undefined;
241831
241825
  const pathList = [];
241832
241826
  if (isNpmInstall || (pkg && (buildCommand || devCommand))) {
@@ -241910,20 +241904,27 @@ const build = async ({ files, entrypoint, workPath, config, meta = {}, }) => {
241910
241904
  if (buildCommand) {
241911
241905
  build_utils_1.debug(`Executing "${buildCommand}"`);
241912
241906
  }
241913
- const found = typeof buildCommand === 'string'
241914
- ? await build_utils_1.execCommand(buildCommand, {
241915
- ...spawnOpts,
241916
- // Yarn v2 PnP mode may be activated, so force
241917
- // "node-modules" linker style
241918
- env: {
241919
- YARN_NODE_LINKER: 'node-modules',
241920
- ...spawnOpts.env,
241921
- },
241922
- cwd: entrypointDir,
241923
- })
241924
- : await build_utils_1.runPackageJsonScript(entrypointDir, ['vercel-build', 'now-build', 'build'], spawnOpts);
241925
- if (!found) {
241926
- throw new Error(`Missing required "${buildCommand || 'vercel-build'}" script in "${entrypoint}"`);
241907
+ try {
241908
+ const found = typeof buildCommand === 'string'
241909
+ ? await build_utils_1.execCommand(buildCommand, {
241910
+ ...spawnOpts,
241911
+ // Yarn v2 PnP mode may be activated, so force
241912
+ // "node-modules" linker style
241913
+ env: {
241914
+ YARN_NODE_LINKER: 'node-modules',
241915
+ ...spawnOpts.env,
241916
+ },
241917
+ cwd: entrypointDir,
241918
+ })
241919
+ : await build_utils_1.runPackageJsonScript(entrypointDir, ['vercel-build', 'now-build', 'build'], spawnOpts);
241920
+ if (!found) {
241921
+ throw new Error(`Missing required "${buildCommand || 'vercel-build'}" script in "${entrypoint}"`);
241922
+ }
241923
+ }
241924
+ finally {
241925
+ if (framework?.slug === 'gatsby') {
241926
+ await GatsbyUtils.cleanupGatsbyFiles(entrypointDir);
241927
+ }
241927
241928
  }
241928
241929
  const outputDirPrefix = path_1.default.join(workPath, path_1.default.dirname(entrypoint));
241929
241930
  distPath =
@@ -242533,22 +242534,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
242533
242534
  return (mod && mod.__esModule) ? mod : { "default": mod };
242534
242535
  };
242535
242536
  Object.defineProperty(exports, "__esModule", ({ value: true }));
242536
- exports.injectPlugins = void 0;
242537
- const fs_1 = __webpack_require__(5747);
242537
+ exports.createPluginSymlinks = exports.cleanupGatsbyFiles = exports.injectPlugins = void 0;
242538
+ const fs_extra_1 = __importDefault(__webpack_require__(6365));
242538
242539
  const path = __importStar(__webpack_require__(5622));
242539
242540
  const semver_1 = __importDefault(__webpack_require__(7013));
242540
- const url_1 = __webpack_require__(8835);
242541
242541
  const _shared_1 = __webpack_require__(54);
242542
- const { VERCEL_CLI_VERSION } = process.env;
242543
242542
  const PLUGINS = [
242544
242543
  '@vercel/gatsby-plugin-vercel-analytics',
242545
242544
  '@vercel/gatsby-plugin-vercel-builder',
242546
242545
  ];
242547
242546
  const GATSBY_CONFIG_FILE = 'gatsby-config';
242548
242547
  const GATSBY_NODE_FILE = 'gatsby-node';
242548
+ const PLUGIN_PATHS = {
242549
+ '@vercel/gatsby-plugin-vercel-analytics': path.dirname(eval('require').resolve(`@vercel/gatsby-plugin-vercel-analytics/package.json`)),
242550
+ '@vercel/gatsby-plugin-vercel-builder': path.dirname(eval('require').resolve(`@vercel/gatsby-plugin-vercel-builder/package.json`)),
242551
+ };
242549
242552
  async function injectPlugins(detectedVersion, dir) {
242550
242553
  const plugins = new Set();
242551
- if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN && detectedVersion) {
242554
+ if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN === '1' && detectedVersion) {
242552
242555
  const version = semver_1.default.coerce(detectedVersion);
242553
242556
  if (version && semver_1.default.satisfies(version, '>=4.0.0')) {
242554
242557
  plugins.add('@vercel/gatsby-plugin-vercel-builder');
@@ -242561,7 +242564,14 @@ async function injectPlugins(detectedVersion, dir) {
242561
242564
  if (plugins.size === 0) {
242562
242565
  return false;
242563
242566
  }
242564
- const ops = [addGatsbyPackage(dir, plugins)];
242567
+ let pluginsStr = 'plugin';
242568
+ if (plugins.size > 1) {
242569
+ pluginsStr += 's';
242570
+ }
242571
+ console.log(`Injecting Gatsby.js ${pluginsStr} ${Array.from(plugins)
242572
+ .map(p => `"${p}"`)
242573
+ .join(', ')}`);
242574
+ const ops = [];
242565
242575
  if (plugins.has('@vercel/gatsby-plugin-vercel-analytics')) {
242566
242576
  ops.push(updateGatsbyConfig(dir, ['@vercel/gatsby-plugin-vercel-analytics']));
242567
242577
  }
@@ -242572,64 +242582,33 @@ async function injectPlugins(detectedVersion, dir) {
242572
242582
  return true;
242573
242583
  }
242574
242584
  exports.injectPlugins = injectPlugins;
242575
- function printInjectingPlugins(plugins, configPath) {
242576
- const pluginsArray = Array.from(plugins);
242577
- let pluginsStr = 'plugin';
242578
- if (pluginsArray.length > 1) {
242579
- pluginsStr += 's';
242580
- }
242581
- console.log(`Injecting Gatsby.js ${pluginsStr} ${pluginsArray
242582
- .map(p => `"${p}"`)
242583
- .join(', ')} to \`${configPath}\``);
242584
- }
242585
- async function addGatsbyPackage(dir, plugins) {
242586
- const pkgJson = (await _shared_1.readPackageJson(dir));
242587
- if (!pkgJson.dependencies) {
242588
- pkgJson.dependencies = {};
242589
- }
242590
- for (const plugin of plugins) {
242591
- if (!pkgJson.dependencies[plugin]) {
242592
- console.log(`Adding "${plugin}" to \`package.json\` "dependencies"`);
242593
- let version = 'latest';
242594
- // Use the tarball URL for E2E tests
242595
- if (VERCEL_CLI_VERSION?.startsWith('https://')) {
242596
- version = new url_1.URL(`./${plugin}.tgz`, VERCEL_CLI_VERSION).href;
242597
- }
242598
- pkgJson.dependencies[plugin] = version;
242599
- }
242600
- }
242601
- await _shared_1.writePackageJson(dir, pkgJson);
242602
- }
242603
242585
  async function updateGatsbyConfig(dir, plugins) {
242604
242586
  const gatsbyConfigPathTs = path.join(dir, `${GATSBY_CONFIG_FILE}.ts`);
242605
242587
  const gatsbyConfigPathMjs = path.join(dir, `${GATSBY_CONFIG_FILE}.mjs`);
242606
242588
  const gatsbyConfigPathJs = path.join(dir, `${GATSBY_CONFIG_FILE}.js`);
242607
242589
  if (await _shared_1.fileExists(gatsbyConfigPathTs)) {
242608
- printInjectingPlugins(plugins, gatsbyConfigPathTs);
242609
242590
  await updateGatsbyConfigTs(gatsbyConfigPathTs, plugins);
242610
242591
  }
242611
242592
  else if (await _shared_1.fileExists(gatsbyConfigPathMjs)) {
242612
- printInjectingPlugins(plugins, gatsbyConfigPathMjs);
242613
242593
  await updateGatsbyConfigMjs(gatsbyConfigPathMjs, plugins);
242614
242594
  }
242595
+ else if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
242596
+ await updateGatsbyConfigJs(gatsbyConfigPathJs, plugins);
242597
+ }
242615
242598
  else {
242616
- printInjectingPlugins(plugins, gatsbyConfigPathJs);
242617
- if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
242618
- await updateGatsbyConfigJs(gatsbyConfigPathJs, plugins);
242619
- }
242620
- else {
242621
- await fs_1.promises.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
242622
- plugins: Array.from(plugins),
242623
- })}`);
242624
- }
242599
+ await fs_extra_1.default.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
242600
+ plugins,
242601
+ })}`);
242625
242602
  }
242626
242603
  }
242604
+ const GENERATED_FILE_COMMENT = `// This file was generated by @vercel/static-build`;
242627
242605
  async function updateGatsbyConfigTs(configPath, plugins) {
242628
242606
  const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
242629
242607
  if (!(await _shared_1.fileExists(renamedPath))) {
242630
- await fs_1.promises.rename(configPath, renamedPath);
242608
+ await fs_extra_1.default.rename(configPath, renamedPath);
242631
242609
  }
242632
- await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.ts.__vercel_builder_backup__.ts";
242610
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242611
+ import userConfig from "./gatsby-config.ts.__vercel_builder_backup__.ts";
242633
242612
  import type { PluginRef } from "gatsby";
242634
242613
 
242635
242614
  const preferDefault = (m: any) => (m && m.default) || m;
@@ -242643,7 +242622,7 @@ if (!vercelConfig.plugins) {
242643
242622
  vercelConfig.plugins = [];
242644
242623
  }
242645
242624
 
242646
- for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
242625
+ for (const plugin of ${JSON.stringify(plugins)}) {
242647
242626
  const hasPlugin = vercelConfig.plugins.find(
242648
242627
  (p: PluginRef) =>
242649
242628
  p && (p === plugin || p.resolve === plugin)
@@ -242661,9 +242640,10 @@ export default vercelConfig;
242661
242640
  async function updateGatsbyConfigMjs(configPath, plugins) {
242662
242641
  const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
242663
242642
  if (!(await _shared_1.fileExists(renamedPath))) {
242664
- await fs_1.promises.rename(configPath, renamedPath);
242643
+ await fs_extra_1.default.rename(configPath, renamedPath);
242665
242644
  }
242666
- await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
242645
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242646
+ import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
242667
242647
 
242668
242648
  const preferDefault = (m) => (m && m.default) || m;
242669
242649
 
@@ -242671,11 +242651,12 @@ const vercelConfig = Object.assign(
242671
242651
  {},
242672
242652
  preferDefault(userConfig)
242673
242653
  );
242654
+
242674
242655
  if (!vercelConfig.plugins) {
242675
242656
  vercelConfig.plugins = [];
242676
242657
  }
242677
242658
 
242678
- for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
242659
+ for (const plugin of ${JSON.stringify(plugins)}) {
242679
242660
  const hasPlugin = vercelConfig.plugins.find(
242680
242661
  (p) => p && (p === plugin || p.resolve === plugin)
242681
242662
  );
@@ -242692,9 +242673,10 @@ export default vercelConfig;
242692
242673
  async function updateGatsbyConfigJs(configPath, plugins) {
242693
242674
  const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
242694
242675
  if (!(await _shared_1.fileExists(renamedPath))) {
242695
- await fs_1.promises.rename(configPath, renamedPath);
242676
+ await fs_extra_1.default.rename(configPath, renamedPath);
242696
242677
  }
242697
- await fs_1.promises.writeFile(configPath, `const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
242678
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242679
+ const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
242698
242680
 
242699
242681
  const preferDefault = m => (m && m.default) || m;
242700
242682
 
@@ -242702,11 +242684,12 @@ const vercelConfig = Object.assign(
242702
242684
  {},
242703
242685
  preferDefault(userConfig)
242704
242686
  );
242687
+
242705
242688
  if (!vercelConfig.plugins) {
242706
242689
  vercelConfig.plugins = [];
242707
242690
  }
242708
242691
 
242709
- for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
242692
+ for (const plugin of ${JSON.stringify(plugins)}) {
242710
242693
  const hasPlugin = vercelConfig.plugins.find(
242711
242694
  (p) => p && (p === plugin || p.resolve === plugin)
242712
242695
  );
@@ -242733,15 +242716,16 @@ async function updateGatsbyNode(dir) {
242733
242716
  await updateGatsbyNodeJs(gatsbyNodePathJs);
242734
242717
  }
242735
242718
  else {
242736
- await fs_1.promises.writeFile(gatsbyNodePathJs, `module.exports = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');`);
242719
+ await fs_extra_1.default.writeFile(gatsbyNodePathJs, `module.exports = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');`);
242737
242720
  }
242738
242721
  }
242739
242722
  async function updateGatsbyNodeTs(configPath) {
242740
242723
  const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
242741
242724
  if (!(await _shared_1.fileExists(renamedPath))) {
242742
- await fs_1.promises.rename(configPath, renamedPath);
242725
+ await fs_extra_1.default.rename(configPath, renamedPath);
242743
242726
  }
242744
- await fs_1.promises.writeFile(configPath, `import type { GatsbyNode } from 'gatsby';
242727
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242728
+ import type { GatsbyNode } from 'gatsby';
242745
242729
  import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
242746
242730
  import * as gatsbyNode from './gatsby-node.ts.__vercel_builder_backup__.ts';
242747
242731
 
@@ -242758,9 +242742,10 @@ export const onPostBuild: GatsbyNode['onPostBuild'] = async (args, options) => {
242758
242742
  async function updateGatsbyNodeMjs(configPath) {
242759
242743
  const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
242760
242744
  if (!(await _shared_1.fileExists(renamedPath))) {
242761
- await fs_1.promises.rename(configPath, renamedPath);
242745
+ await fs_extra_1.default.rename(configPath, renamedPath);
242762
242746
  }
242763
- await fs_1.promises.writeFile(configPath, `import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
242747
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242748
+ import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
242764
242749
  import * as gatsbyNode from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
242765
242750
 
242766
242751
  export * from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
@@ -242776,9 +242761,10 @@ export const onPostBuild = async (args, options) => {
242776
242761
  async function updateGatsbyNodeJs(configPath) {
242777
242762
  const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
242778
242763
  if (!(await _shared_1.fileExists(renamedPath))) {
242779
- await fs_1.promises.rename(configPath, renamedPath);
242764
+ await fs_extra_1.default.rename(configPath, renamedPath);
242780
242765
  }
242781
- await fs_1.promises.writeFile(configPath, `const vercelBuilder = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');
242766
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242767
+ const vercelBuilder = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');
242782
242768
  const gatsbyNode = require('./gatsby-node.js.__vercel_builder_backup__.js');
242783
242769
 
242784
242770
  const origOnPostBuild = gatsbyNode.onPostBuild;
@@ -242793,6 +242779,37 @@ gatsbyNode.onPostBuild = async (args, options) => {
242793
242779
  module.exports = gatsbyNode;
242794
242780
  `);
242795
242781
  }
242782
+ async function cleanupGatsbyFiles(dir) {
242783
+ const backup = '.__vercel_builder_backup__';
242784
+ const fileEndings = ['.js', '.ts', '.mjs'];
242785
+ for (const fileName of [GATSBY_CONFIG_FILE, GATSBY_NODE_FILE]) {
242786
+ for (const fileEnding of fileEndings) {
242787
+ const baseFile = `${fileName}${fileEnding}`;
242788
+ const baseFilePath = path.join(dir, baseFile);
242789
+ const backupFile = `${baseFile}${backup}${fileEnding}`;
242790
+ const backupFilePath = path.join(dir, backupFile);
242791
+ const [baseFileContent, backupFileContent] = await Promise.all([
242792
+ fs_extra_1.default.readFile(baseFilePath, 'utf-8').catch(() => null),
242793
+ fs_extra_1.default.readFile(backupFilePath, 'utf-8').catch(() => null),
242794
+ ]);
242795
+ if (baseFileContent &&
242796
+ baseFileContent.startsWith(GENERATED_FILE_COMMENT)) {
242797
+ await fs_extra_1.default.rm(baseFilePath);
242798
+ }
242799
+ if (backupFileContent) {
242800
+ await fs_extra_1.default.rename(backupFilePath, baseFilePath);
242801
+ }
242802
+ }
242803
+ }
242804
+ }
242805
+ exports.cleanupGatsbyFiles = cleanupGatsbyFiles;
242806
+ async function createPluginSymlinks(dir) {
242807
+ const nodeModulesDir = path.join(dir, 'node_modules');
242808
+ await fs_extra_1.default.ensureDir(path.join(nodeModulesDir, '@vercel'));
242809
+ await Promise.all(PLUGINS.map(name => fs_extra_1.default.remove(path.join(nodeModulesDir, name))));
242810
+ await Promise.all(PLUGINS.map(name => fs_extra_1.default.symlink(PLUGIN_PATHS[name], path.join(nodeModulesDir, name))));
242811
+ }
242812
+ exports.createPluginSymlinks = createPluginSymlinks;
242796
242813
 
242797
242814
 
242798
242815
  /***/ }),
@@ -22,22 +22,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.injectPlugins = void 0;
26
- const fs_1 = require("fs");
25
+ exports.createPluginSymlinks = exports.cleanupGatsbyFiles = exports.injectPlugins = void 0;
26
+ const fs_extra_1 = __importDefault(require("fs-extra"));
27
27
  const path = __importStar(require("path"));
28
28
  const semver_1 = __importDefault(require("semver"));
29
- const url_1 = require("url");
30
29
  const _shared_1 = require("./_shared");
31
- const { VERCEL_CLI_VERSION } = process.env;
32
30
  const PLUGINS = [
33
31
  '@vercel/gatsby-plugin-vercel-analytics',
34
32
  '@vercel/gatsby-plugin-vercel-builder',
35
33
  ];
36
34
  const GATSBY_CONFIG_FILE = 'gatsby-config';
37
35
  const GATSBY_NODE_FILE = 'gatsby-node';
36
+ const PLUGIN_PATHS = {
37
+ '@vercel/gatsby-plugin-vercel-analytics': path.dirname(eval('require').resolve(`@vercel/gatsby-plugin-vercel-analytics/package.json`)),
38
+ '@vercel/gatsby-plugin-vercel-builder': path.dirname(eval('require').resolve(`@vercel/gatsby-plugin-vercel-builder/package.json`)),
39
+ };
38
40
  async function injectPlugins(detectedVersion, dir) {
39
41
  const plugins = new Set();
40
- if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN && detectedVersion) {
42
+ if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN === '1' && detectedVersion) {
41
43
  const version = semver_1.default.coerce(detectedVersion);
42
44
  if (version && semver_1.default.satisfies(version, '>=4.0.0')) {
43
45
  plugins.add('@vercel/gatsby-plugin-vercel-builder');
@@ -50,7 +52,14 @@ async function injectPlugins(detectedVersion, dir) {
50
52
  if (plugins.size === 0) {
51
53
  return false;
52
54
  }
53
- const ops = [addGatsbyPackage(dir, plugins)];
55
+ let pluginsStr = 'plugin';
56
+ if (plugins.size > 1) {
57
+ pluginsStr += 's';
58
+ }
59
+ console.log(`Injecting Gatsby.js ${pluginsStr} ${Array.from(plugins)
60
+ .map(p => `"${p}"`)
61
+ .join(', ')}`);
62
+ const ops = [];
54
63
  if (plugins.has('@vercel/gatsby-plugin-vercel-analytics')) {
55
64
  ops.push(updateGatsbyConfig(dir, ['@vercel/gatsby-plugin-vercel-analytics']));
56
65
  }
@@ -61,64 +70,33 @@ async function injectPlugins(detectedVersion, dir) {
61
70
  return true;
62
71
  }
63
72
  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}\``);
73
- }
74
- async function addGatsbyPackage(dir, plugins) {
75
- const pkgJson = (await _shared_1.readPackageJson(dir));
76
- if (!pkgJson.dependencies) {
77
- pkgJson.dependencies = {};
78
- }
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
73
  async function updateGatsbyConfig(dir, plugins) {
93
74
  const gatsbyConfigPathTs = path.join(dir, `${GATSBY_CONFIG_FILE}.ts`);
94
75
  const gatsbyConfigPathMjs = path.join(dir, `${GATSBY_CONFIG_FILE}.mjs`);
95
76
  const gatsbyConfigPathJs = path.join(dir, `${GATSBY_CONFIG_FILE}.js`);
96
77
  if (await _shared_1.fileExists(gatsbyConfigPathTs)) {
97
- printInjectingPlugins(plugins, gatsbyConfigPathTs);
98
78
  await updateGatsbyConfigTs(gatsbyConfigPathTs, plugins);
99
79
  }
100
80
  else if (await _shared_1.fileExists(gatsbyConfigPathMjs)) {
101
- printInjectingPlugins(plugins, gatsbyConfigPathMjs);
102
81
  await updateGatsbyConfigMjs(gatsbyConfigPathMjs, plugins);
103
82
  }
83
+ else if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
84
+ await updateGatsbyConfigJs(gatsbyConfigPathJs, plugins);
85
+ }
104
86
  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
- }
87
+ await fs_extra_1.default.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
88
+ plugins,
89
+ })}`);
114
90
  }
115
91
  }
92
+ const GENERATED_FILE_COMMENT = `// This file was generated by @vercel/static-build`;
116
93
  async function updateGatsbyConfigTs(configPath, plugins) {
117
94
  const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
118
95
  if (!(await _shared_1.fileExists(renamedPath))) {
119
- await fs_1.promises.rename(configPath, renamedPath);
96
+ await fs_extra_1.default.rename(configPath, renamedPath);
120
97
  }
121
- await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.ts.__vercel_builder_backup__.ts";
98
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
99
+ import userConfig from "./gatsby-config.ts.__vercel_builder_backup__.ts";
122
100
  import type { PluginRef } from "gatsby";
123
101
 
124
102
  const preferDefault = (m: any) => (m && m.default) || m;
@@ -132,7 +110,7 @@ if (!vercelConfig.plugins) {
132
110
  vercelConfig.plugins = [];
133
111
  }
134
112
 
135
- for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
113
+ for (const plugin of ${JSON.stringify(plugins)}) {
136
114
  const hasPlugin = vercelConfig.plugins.find(
137
115
  (p: PluginRef) =>
138
116
  p && (p === plugin || p.resolve === plugin)
@@ -150,9 +128,10 @@ export default vercelConfig;
150
128
  async function updateGatsbyConfigMjs(configPath, plugins) {
151
129
  const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
152
130
  if (!(await _shared_1.fileExists(renamedPath))) {
153
- await fs_1.promises.rename(configPath, renamedPath);
131
+ await fs_extra_1.default.rename(configPath, renamedPath);
154
132
  }
155
- await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
133
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
134
+ import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
156
135
 
157
136
  const preferDefault = (m) => (m && m.default) || m;
158
137
 
@@ -160,11 +139,12 @@ const vercelConfig = Object.assign(
160
139
  {},
161
140
  preferDefault(userConfig)
162
141
  );
142
+
163
143
  if (!vercelConfig.plugins) {
164
144
  vercelConfig.plugins = [];
165
145
  }
166
146
 
167
- for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
147
+ for (const plugin of ${JSON.stringify(plugins)}) {
168
148
  const hasPlugin = vercelConfig.plugins.find(
169
149
  (p) => p && (p === plugin || p.resolve === plugin)
170
150
  );
@@ -181,9 +161,10 @@ export default vercelConfig;
181
161
  async function updateGatsbyConfigJs(configPath, plugins) {
182
162
  const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
183
163
  if (!(await _shared_1.fileExists(renamedPath))) {
184
- await fs_1.promises.rename(configPath, renamedPath);
164
+ await fs_extra_1.default.rename(configPath, renamedPath);
185
165
  }
186
- await fs_1.promises.writeFile(configPath, `const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
166
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
167
+ const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
187
168
 
188
169
  const preferDefault = m => (m && m.default) || m;
189
170
 
@@ -191,11 +172,12 @@ const vercelConfig = Object.assign(
191
172
  {},
192
173
  preferDefault(userConfig)
193
174
  );
175
+
194
176
  if (!vercelConfig.plugins) {
195
177
  vercelConfig.plugins = [];
196
178
  }
197
179
 
198
- for (const plugin of ${JSON.stringify(Array.from(plugins))}) {
180
+ for (const plugin of ${JSON.stringify(plugins)}) {
199
181
  const hasPlugin = vercelConfig.plugins.find(
200
182
  (p) => p && (p === plugin || p.resolve === plugin)
201
183
  );
@@ -222,15 +204,16 @@ async function updateGatsbyNode(dir) {
222
204
  await updateGatsbyNodeJs(gatsbyNodePathJs);
223
205
  }
224
206
  else {
225
- await fs_1.promises.writeFile(gatsbyNodePathJs, `module.exports = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');`);
207
+ await fs_extra_1.default.writeFile(gatsbyNodePathJs, `module.exports = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');`);
226
208
  }
227
209
  }
228
210
  async function updateGatsbyNodeTs(configPath) {
229
211
  const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
230
212
  if (!(await _shared_1.fileExists(renamedPath))) {
231
- await fs_1.promises.rename(configPath, renamedPath);
213
+ await fs_extra_1.default.rename(configPath, renamedPath);
232
214
  }
233
- await fs_1.promises.writeFile(configPath, `import type { GatsbyNode } from 'gatsby';
215
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
216
+ import type { GatsbyNode } from 'gatsby';
234
217
  import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
235
218
  import * as gatsbyNode from './gatsby-node.ts.__vercel_builder_backup__.ts';
236
219
 
@@ -247,9 +230,10 @@ export const onPostBuild: GatsbyNode['onPostBuild'] = async (args, options) => {
247
230
  async function updateGatsbyNodeMjs(configPath) {
248
231
  const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
249
232
  if (!(await _shared_1.fileExists(renamedPath))) {
250
- await fs_1.promises.rename(configPath, renamedPath);
233
+ await fs_extra_1.default.rename(configPath, renamedPath);
251
234
  }
252
- await fs_1.promises.writeFile(configPath, `import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
235
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
236
+ import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
253
237
  import * as gatsbyNode from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
254
238
 
255
239
  export * from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
@@ -265,9 +249,10 @@ export const onPostBuild = async (args, options) => {
265
249
  async function updateGatsbyNodeJs(configPath) {
266
250
  const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
267
251
  if (!(await _shared_1.fileExists(renamedPath))) {
268
- await fs_1.promises.rename(configPath, renamedPath);
252
+ await fs_extra_1.default.rename(configPath, renamedPath);
269
253
  }
270
- await fs_1.promises.writeFile(configPath, `const vercelBuilder = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');
254
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
255
+ const vercelBuilder = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');
271
256
  const gatsbyNode = require('./gatsby-node.js.__vercel_builder_backup__.js');
272
257
 
273
258
  const origOnPostBuild = gatsbyNode.onPostBuild;
@@ -282,3 +267,34 @@ gatsbyNode.onPostBuild = async (args, options) => {
282
267
  module.exports = gatsbyNode;
283
268
  `);
284
269
  }
270
+ async function cleanupGatsbyFiles(dir) {
271
+ const backup = '.__vercel_builder_backup__';
272
+ const fileEndings = ['.js', '.ts', '.mjs'];
273
+ for (const fileName of [GATSBY_CONFIG_FILE, GATSBY_NODE_FILE]) {
274
+ for (const fileEnding of fileEndings) {
275
+ const baseFile = `${fileName}${fileEnding}`;
276
+ const baseFilePath = path.join(dir, baseFile);
277
+ const backupFile = `${baseFile}${backup}${fileEnding}`;
278
+ const backupFilePath = path.join(dir, backupFile);
279
+ const [baseFileContent, backupFileContent] = await Promise.all([
280
+ fs_extra_1.default.readFile(baseFilePath, 'utf-8').catch(() => null),
281
+ fs_extra_1.default.readFile(backupFilePath, 'utf-8').catch(() => null),
282
+ ]);
283
+ if (baseFileContent &&
284
+ baseFileContent.startsWith(GENERATED_FILE_COMMENT)) {
285
+ await fs_extra_1.default.rm(baseFilePath);
286
+ }
287
+ if (backupFileContent) {
288
+ await fs_extra_1.default.rename(backupFilePath, baseFilePath);
289
+ }
290
+ }
291
+ }
292
+ }
293
+ exports.cleanupGatsbyFiles = cleanupGatsbyFiles;
294
+ async function createPluginSymlinks(dir) {
295
+ const nodeModulesDir = path.join(dir, 'node_modules');
296
+ await fs_extra_1.default.ensureDir(path.join(nodeModulesDir, '@vercel'));
297
+ await Promise.all(PLUGINS.map(name => fs_extra_1.default.remove(path.join(nodeModulesDir, name))));
298
+ await Promise.all(PLUGINS.map(name => fs_extra_1.default.symlink(PLUGIN_PATHS[name], path.join(nodeModulesDir, name))));
299
+ }
300
+ exports.createPluginSymlinks = createPluginSymlinks;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/static-build",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
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/gatsby.test.js test/prepare-cache.test.ts",
18
+ "test-unit": "pnpm test test/build.test.ts test/gatsby.test.ts test/prepare-cache.test.ts",
19
19
  "test-integration-once": "pnpm test test/integration-*.test.js"
20
20
  },
21
21
  "jest": {
@@ -28,6 +28,10 @@
28
28
  }
29
29
  }
30
30
  },
31
+ "dependencies": {
32
+ "@vercel/gatsby-plugin-vercel-analytics": "1.0.7",
33
+ "@vercel/gatsby-plugin-vercel-builder": "1.0.2"
34
+ },
31
35
  "devDependencies": {
32
36
  "@types/aws-lambda": "8.10.64",
33
37
  "@types/cross-spawn": "6.0.0",
@@ -41,8 +45,6 @@
41
45
  "@vercel/build-utils": "6.0.0",
42
46
  "@vercel/frameworks": "1.3.0",
43
47
  "@vercel/fs-detectors": "3.7.6",
44
- "@vercel/gatsby-plugin-vercel-analytics": "1.0.7",
45
- "@vercel/gatsby-plugin-vercel-builder": "1.0.1",
46
48
  "@vercel/ncc": "0.24.0",
47
49
  "@vercel/routing-utils": "2.1.8",
48
50
  "@vercel/static-config": "2.0.11",
@@ -58,5 +60,5 @@
58
60
  "ts-morph": "12.0.0",
59
61
  "typescript": "4.3.4"
60
62
  },
61
- "gitHead": "25f6595d3698546ba69c7fe9f601a906167fef3f"
63
+ "gitHead": "9317543c48a039bda0ae91f0819f38f789d98338"
62
64
  }