@vercel/static-build 1.2.0 → 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
@@ -237508,6 +237508,11 @@ exports.frameworks = [
237508
237508
  headers: { 'cache-control': 'public, max-age=31536000, immutable' },
237509
237509
  continue: true,
237510
237510
  },
237511
+ {
237512
+ src: '^/_astro/(.*)$',
237513
+ headers: { 'cache-control': 'public, max-age=31536000, immutable' },
237514
+ continue: true,
237515
+ },
237511
237516
  ],
237512
237517
  },
237513
237518
  {
@@ -241711,16 +241716,7 @@ const build = async ({ files, entrypoint, workPath, config, meta = {}, }) => {
241711
241716
  process.env[key] = value;
241712
241717
  }
241713
241718
  if (framework.slug === 'gatsby') {
241714
- const injectedPlugins = await GatsbyUtils.injectPlugins(detectedVersion, entrypointDir);
241715
- if (injectedPlugins) {
241716
- const packageManager = await fs_detectors_1.detectFramework({
241717
- fs: localFileSystemDetector,
241718
- frameworkList: fs_detectors_1.packageManagers,
241719
- });
241720
- if (packageManager === 'pnpm') {
241721
- await build_utils_1.execCommand('pnpm install --lockfile-only');
241722
- }
241723
- }
241719
+ await GatsbyUtils.injectPlugins(detectedVersion, entrypointDir);
241724
241720
  }
241725
241721
  if (process.env.VERCEL_ANALYTICS_ID) {
241726
241722
  const frameworkDirectory = path_1.default.join(workPath, path_1.default.dirname(entrypoint));
@@ -241822,6 +241818,9 @@ const build = async ({ files, entrypoint, workPath, config, meta = {}, }) => {
241822
241818
  }
241823
241819
  }
241824
241820
  }
241821
+ if (framework?.slug === 'gatsby') {
241822
+ await GatsbyUtils.createPluginSymlinks(entrypointDir);
241823
+ }
241825
241824
  let gemHome = undefined;
241826
241825
  const pathList = [];
241827
241826
  if (isNpmInstall || (pkg && (buildCommand || devCommand))) {
@@ -241905,20 +241904,27 @@ const build = async ({ files, entrypoint, workPath, config, meta = {}, }) => {
241905
241904
  if (buildCommand) {
241906
241905
  build_utils_1.debug(`Executing "${buildCommand}"`);
241907
241906
  }
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}"`);
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
+ }
241922
241928
  }
241923
241929
  const outputDirPrefix = path_1.default.join(workPath, path_1.default.dirname(entrypoint));
241924
241930
  distPath =
@@ -242528,90 +242534,87 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
242528
242534
  return (mod && mod.__esModule) ? mod : { "default": mod };
242529
242535
  };
242530
242536
  Object.defineProperty(exports, "__esModule", ({ value: true }));
242531
- exports.injectPlugins = void 0;
242532
- const fs_1 = __webpack_require__(5747);
242537
+ exports.createPluginSymlinks = exports.cleanupGatsbyFiles = exports.injectPlugins = void 0;
242538
+ const fs_extra_1 = __importDefault(__webpack_require__(6365));
242533
242539
  const path = __importStar(__webpack_require__(5622));
242534
242540
  const semver_1 = __importDefault(__webpack_require__(7013));
242535
242541
  const _shared_1 = __webpack_require__(54);
242536
- const PLUGINS = {
242537
- GATSBY_PLUGIN_VERCEL_ANALYTICS: '@vercel/gatsby-plugin-vercel-analytics',
242538
- GATSBY_PLUGIN_VERCEL_BUILDER: '@vercel/gatsby-plugin-vercel-builder',
242539
- };
242542
+ const PLUGINS = [
242543
+ '@vercel/gatsby-plugin-vercel-analytics',
242544
+ '@vercel/gatsby-plugin-vercel-builder',
242545
+ ];
242540
242546
  const GATSBY_CONFIG_FILE = 'gatsby-config';
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
+ };
242541
242552
  async function injectPlugins(detectedVersion, dir) {
242542
- const pluginsToInject = [];
242543
- if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN && detectedVersion) {
242553
+ const plugins = new Set();
242554
+ if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN === '1' && detectedVersion) {
242544
242555
  const version = semver_1.default.coerce(detectedVersion);
242545
242556
  if (version && semver_1.default.satisfies(version, '>=4.0.0')) {
242546
- pluginsToInject.push(PLUGINS.GATSBY_PLUGIN_VERCEL_BUILDER);
242557
+ plugins.add('@vercel/gatsby-plugin-vercel-builder');
242547
242558
  }
242548
242559
  }
242549
242560
  if (process.env.VERCEL_ANALYTICS_ID) {
242550
242561
  process.env.GATSBY_VERCEL_ANALYTICS_ID = process.env.VERCEL_ANALYTICS_ID;
242551
- pluginsToInject.push(PLUGINS.GATSBY_PLUGIN_VERCEL_ANALYTICS);
242562
+ plugins.add('@vercel/gatsby-plugin-vercel-analytics');
242552
242563
  }
242553
- if (pluginsToInject.length === 0) {
242564
+ if (plugins.size === 0) {
242554
242565
  return false;
242555
242566
  }
242556
- await addGatsbyPackage(dir, pluginsToInject);
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 = [];
242575
+ if (plugins.has('@vercel/gatsby-plugin-vercel-analytics')) {
242576
+ ops.push(updateGatsbyConfig(dir, ['@vercel/gatsby-plugin-vercel-analytics']));
242577
+ }
242578
+ if (plugins.has('@vercel/gatsby-plugin-vercel-builder')) {
242579
+ ops.push(updateGatsbyNode(dir));
242580
+ }
242581
+ await Promise.all(ops);
242582
+ return true;
242583
+ }
242584
+ exports.injectPlugins = injectPlugins;
242585
+ async function updateGatsbyConfig(dir, plugins) {
242557
242586
  const gatsbyConfigPathTs = path.join(dir, `${GATSBY_CONFIG_FILE}.ts`);
242558
242587
  const gatsbyConfigPathMjs = path.join(dir, `${GATSBY_CONFIG_FILE}.mjs`);
242559
242588
  const gatsbyConfigPathJs = path.join(dir, `${GATSBY_CONFIG_FILE}.js`);
242560
242589
  if (await _shared_1.fileExists(gatsbyConfigPathTs)) {
242561
- printInjectingPlugins(pluginsToInject, gatsbyConfigPathTs);
242562
- await updateGatsbyTsConfig(gatsbyConfigPathTs, pluginsToInject);
242590
+ await updateGatsbyConfigTs(gatsbyConfigPathTs, plugins);
242563
242591
  }
242564
242592
  else if (await _shared_1.fileExists(gatsbyConfigPathMjs)) {
242565
- printInjectingPlugins(pluginsToInject, gatsbyConfigPathMjs);
242566
- await updateGatsbyMjsConfig(gatsbyConfigPathMjs, pluginsToInject);
242593
+ await updateGatsbyConfigMjs(gatsbyConfigPathMjs, plugins);
242594
+ }
242595
+ else if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
242596
+ await updateGatsbyConfigJs(gatsbyConfigPathJs, plugins);
242567
242597
  }
242568
242598
  else {
242569
- printInjectingPlugins(pluginsToInject, gatsbyConfigPathJs);
242570
- if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
242571
- await updateGatsbyJsConfig(gatsbyConfigPathJs, pluginsToInject);
242572
- }
242573
- else {
242574
- await fs_1.promises.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
242575
- plugins: pluginsToInject,
242576
- })}`);
242577
- }
242599
+ await fs_extra_1.default.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
242600
+ plugins,
242601
+ })}`);
242578
242602
  }
242579
- return true;
242580
242603
  }
242581
- exports.injectPlugins = injectPlugins;
242582
- function printInjectingPlugins(plugins, configPath) {
242583
- let pluginsStr = 'plugin';
242584
- if (plugins.length > 1) {
242585
- pluginsStr += 's';
242604
+ const GENERATED_FILE_COMMENT = `// This file was generated by @vercel/static-build`;
242605
+ async function updateGatsbyConfigTs(configPath, plugins) {
242606
+ const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
242607
+ if (!(await _shared_1.fileExists(renamedPath))) {
242608
+ await fs_extra_1.default.rename(configPath, renamedPath);
242586
242609
  }
242587
- console.log(`Injecting Gatsby.js ${pluginsStr} ${plugins
242588
- .map(p => `"${p}"`)
242589
- .join(', ')} to \`${configPath}\``);
242590
- }
242591
- async function addGatsbyPackage(dir, plugins) {
242592
- const pkgJson = (await _shared_1.readPackageJson(dir));
242593
- if (!pkgJson.dependencies) {
242594
- pkgJson.dependencies = {};
242595
- }
242596
- for (const plugin of plugins) {
242597
- if (!pkgJson.dependencies[plugin]) {
242598
- console.log(`Adding "${plugin}" to \`package.json\` "dependencies"`);
242599
- pkgJson.dependencies[plugin] = 'latest';
242600
- }
242601
- }
242602
- await _shared_1.writePackageJson(dir, pkgJson);
242603
- }
242604
- async function updateGatsbyTsConfig(configPath, plugins) {
242605
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.ts');
242606
- 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";
242607
242612
  import type { PluginRef } from "gatsby";
242608
242613
 
242609
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
242610
242614
  const preferDefault = (m: any) => (m && m.default) || m;
242611
242615
 
242612
242616
  const vercelConfig = Object.assign(
242613
242617
  {},
242614
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
242615
242618
  preferDefault(userConfig)
242616
242619
  );
242617
242620
 
@@ -242634,18 +242637,21 @@ for (const plugin of ${JSON.stringify(plugins)}) {
242634
242637
  export default vercelConfig;
242635
242638
  `);
242636
242639
  }
242637
- async function updateGatsbyMjsConfig(configPath, plugins) {
242638
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.mjs');
242639
- await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
242640
+ async function updateGatsbyConfigMjs(configPath, plugins) {
242641
+ const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
242642
+ if (!(await _shared_1.fileExists(renamedPath))) {
242643
+ await fs_extra_1.default.rename(configPath, renamedPath);
242644
+ }
242645
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242646
+ import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
242640
242647
 
242641
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
242642
242648
  const preferDefault = (m) => (m && m.default) || m;
242643
242649
 
242644
242650
  const vercelConfig = Object.assign(
242645
242651
  {},
242646
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
242647
242652
  preferDefault(userConfig)
242648
242653
  );
242654
+
242649
242655
  if (!vercelConfig.plugins) {
242650
242656
  vercelConfig.plugins = [];
242651
242657
  }
@@ -242664,18 +242670,21 @@ for (const plugin of ${JSON.stringify(plugins)}) {
242664
242670
  export default vercelConfig;
242665
242671
  `);
242666
242672
  }
242667
- async function updateGatsbyJsConfig(configPath, plugins) {
242668
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.js');
242669
- await fs_1.promises.writeFile(configPath, `const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
242673
+ async function updateGatsbyConfigJs(configPath, plugins) {
242674
+ const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
242675
+ if (!(await _shared_1.fileExists(renamedPath))) {
242676
+ await fs_extra_1.default.rename(configPath, renamedPath);
242677
+ }
242678
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242679
+ const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
242670
242680
 
242671
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
242672
242681
  const preferDefault = m => (m && m.default) || m;
242673
242682
 
242674
242683
  const vercelConfig = Object.assign(
242675
242684
  {},
242676
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
242677
242685
  preferDefault(userConfig)
242678
242686
  );
242687
+
242679
242688
  if (!vercelConfig.plugins) {
242680
242689
  vercelConfig.plugins = [];
242681
242690
  }
@@ -242693,6 +242702,114 @@ for (const plugin of ${JSON.stringify(plugins)}) {
242693
242702
  module.exports = vercelConfig;
242694
242703
  `);
242695
242704
  }
242705
+ async function updateGatsbyNode(dir) {
242706
+ const gatsbyNodePathTs = path.join(dir, `${GATSBY_NODE_FILE}.ts`);
242707
+ const gatsbyNodePathMjs = path.join(dir, `${GATSBY_NODE_FILE}.mjs`);
242708
+ const gatsbyNodePathJs = path.join(dir, `${GATSBY_NODE_FILE}.js`);
242709
+ if (await _shared_1.fileExists(gatsbyNodePathTs)) {
242710
+ await updateGatsbyNodeTs(gatsbyNodePathTs);
242711
+ }
242712
+ else if (await _shared_1.fileExists(gatsbyNodePathMjs)) {
242713
+ await updateGatsbyNodeMjs(gatsbyNodePathMjs);
242714
+ }
242715
+ else if (await _shared_1.fileExists(gatsbyNodePathJs)) {
242716
+ await updateGatsbyNodeJs(gatsbyNodePathJs);
242717
+ }
242718
+ else {
242719
+ await fs_extra_1.default.writeFile(gatsbyNodePathJs, `module.exports = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');`);
242720
+ }
242721
+ }
242722
+ async function updateGatsbyNodeTs(configPath) {
242723
+ const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
242724
+ if (!(await _shared_1.fileExists(renamedPath))) {
242725
+ await fs_extra_1.default.rename(configPath, renamedPath);
242726
+ }
242727
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242728
+ import type { GatsbyNode } from 'gatsby';
242729
+ import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
242730
+ import * as gatsbyNode from './gatsby-node.ts.__vercel_builder_backup__.ts';
242731
+
242732
+ export * from './gatsby-node.ts.__vercel_builder_backup__.ts';
242733
+
242734
+ export const onPostBuild: GatsbyNode['onPostBuild'] = async (args, options) => {
242735
+ if (typeof (gatsbyNode as any).onPostBuild === 'function') {
242736
+ await (gatsbyNode as any).onPostBuild(args, options);
242737
+ }
242738
+ await vercelBuilder.onPostBuild(args, options);
242739
+ };
242740
+ `);
242741
+ }
242742
+ async function updateGatsbyNodeMjs(configPath) {
242743
+ const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
242744
+ if (!(await _shared_1.fileExists(renamedPath))) {
242745
+ await fs_extra_1.default.rename(configPath, renamedPath);
242746
+ }
242747
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242748
+ import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
242749
+ import * as gatsbyNode from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
242750
+
242751
+ export * from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
242752
+
242753
+ export const onPostBuild = async (args, options) => {
242754
+ if (typeof gatsbyNode.onPostBuild === 'function') {
242755
+ await gatsbyNode.onPostBuild(args, options);
242756
+ }
242757
+ await vercelBuilder.onPostBuild(args, options);
242758
+ };
242759
+ `);
242760
+ }
242761
+ async function updateGatsbyNodeJs(configPath) {
242762
+ const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
242763
+ if (!(await _shared_1.fileExists(renamedPath))) {
242764
+ await fs_extra_1.default.rename(configPath, renamedPath);
242765
+ }
242766
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
242767
+ const vercelBuilder = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');
242768
+ const gatsbyNode = require('./gatsby-node.js.__vercel_builder_backup__.js');
242769
+
242770
+ const origOnPostBuild = gatsbyNode.onPostBuild;
242771
+
242772
+ gatsbyNode.onPostBuild = async (args, options) => {
242773
+ if (typeof origOnPostBuild === 'function') {
242774
+ await origOnPostBuild(args, options);
242775
+ }
242776
+ await vercelBuilder.onPostBuild(args, options);
242777
+ };
242778
+
242779
+ module.exports = gatsbyNode;
242780
+ `);
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;
242696
242813
 
242697
242814
 
242698
242815
  /***/ }),
@@ -22,90 +22,87 @@ 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
29
  const _shared_1 = require("./_shared");
30
- const PLUGINS = {
31
- GATSBY_PLUGIN_VERCEL_ANALYTICS: '@vercel/gatsby-plugin-vercel-analytics',
32
- GATSBY_PLUGIN_VERCEL_BUILDER: '@vercel/gatsby-plugin-vercel-builder',
33
- };
30
+ const PLUGINS = [
31
+ '@vercel/gatsby-plugin-vercel-analytics',
32
+ '@vercel/gatsby-plugin-vercel-builder',
33
+ ];
34
34
  const GATSBY_CONFIG_FILE = 'gatsby-config';
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
+ };
35
40
  async function injectPlugins(detectedVersion, dir) {
36
- const pluginsToInject = [];
37
- if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN && detectedVersion) {
41
+ const plugins = new Set();
42
+ if (process.env.VERCEL_GATSBY_BUILDER_PLUGIN === '1' && detectedVersion) {
38
43
  const version = semver_1.default.coerce(detectedVersion);
39
44
  if (version && semver_1.default.satisfies(version, '>=4.0.0')) {
40
- pluginsToInject.push(PLUGINS.GATSBY_PLUGIN_VERCEL_BUILDER);
45
+ plugins.add('@vercel/gatsby-plugin-vercel-builder');
41
46
  }
42
47
  }
43
48
  if (process.env.VERCEL_ANALYTICS_ID) {
44
49
  process.env.GATSBY_VERCEL_ANALYTICS_ID = process.env.VERCEL_ANALYTICS_ID;
45
- pluginsToInject.push(PLUGINS.GATSBY_PLUGIN_VERCEL_ANALYTICS);
50
+ plugins.add('@vercel/gatsby-plugin-vercel-analytics');
46
51
  }
47
- if (pluginsToInject.length === 0) {
52
+ if (plugins.size === 0) {
48
53
  return false;
49
54
  }
50
- await addGatsbyPackage(dir, pluginsToInject);
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 = [];
63
+ if (plugins.has('@vercel/gatsby-plugin-vercel-analytics')) {
64
+ ops.push(updateGatsbyConfig(dir, ['@vercel/gatsby-plugin-vercel-analytics']));
65
+ }
66
+ if (plugins.has('@vercel/gatsby-plugin-vercel-builder')) {
67
+ ops.push(updateGatsbyNode(dir));
68
+ }
69
+ await Promise.all(ops);
70
+ return true;
71
+ }
72
+ exports.injectPlugins = injectPlugins;
73
+ async function updateGatsbyConfig(dir, plugins) {
51
74
  const gatsbyConfigPathTs = path.join(dir, `${GATSBY_CONFIG_FILE}.ts`);
52
75
  const gatsbyConfigPathMjs = path.join(dir, `${GATSBY_CONFIG_FILE}.mjs`);
53
76
  const gatsbyConfigPathJs = path.join(dir, `${GATSBY_CONFIG_FILE}.js`);
54
77
  if (await _shared_1.fileExists(gatsbyConfigPathTs)) {
55
- printInjectingPlugins(pluginsToInject, gatsbyConfigPathTs);
56
- await updateGatsbyTsConfig(gatsbyConfigPathTs, pluginsToInject);
78
+ await updateGatsbyConfigTs(gatsbyConfigPathTs, plugins);
57
79
  }
58
80
  else if (await _shared_1.fileExists(gatsbyConfigPathMjs)) {
59
- printInjectingPlugins(pluginsToInject, gatsbyConfigPathMjs);
60
- await updateGatsbyMjsConfig(gatsbyConfigPathMjs, pluginsToInject);
81
+ await updateGatsbyConfigMjs(gatsbyConfigPathMjs, plugins);
61
82
  }
62
- else {
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
- }
83
+ else if (await _shared_1.fileExists(gatsbyConfigPathJs)) {
84
+ await updateGatsbyConfigJs(gatsbyConfigPathJs, plugins);
72
85
  }
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';
86
+ else {
87
+ await fs_extra_1.default.writeFile(gatsbyConfigPathJs, `module.exports = ${JSON.stringify({
88
+ plugins,
89
+ })}`);
80
90
  }
81
- console.log(`Injecting Gatsby.js ${pluginsStr} ${plugins
82
- .map(p => `"${p}"`)
83
- .join(', ')} to \`${configPath}\``);
84
91
  }
85
- async function addGatsbyPackage(dir, plugins) {
86
- const pkgJson = (await _shared_1.readPackageJson(dir));
87
- if (!pkgJson.dependencies) {
88
- pkgJson.dependencies = {};
89
- }
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
- }
92
+ const GENERATED_FILE_COMMENT = `// This file was generated by @vercel/static-build`;
93
+ async function updateGatsbyConfigTs(configPath, plugins) {
94
+ const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
95
+ if (!(await _shared_1.fileExists(renamedPath))) {
96
+ await fs_extra_1.default.rename(configPath, renamedPath);
95
97
  }
96
- await _shared_1.writePackageJson(dir, pkgJson);
97
- }
98
- async function updateGatsbyTsConfig(configPath, plugins) {
99
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.ts');
100
- 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";
101
100
  import type { PluginRef } from "gatsby";
102
101
 
103
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
104
102
  const preferDefault = (m: any) => (m && m.default) || m;
105
103
 
106
104
  const vercelConfig = Object.assign(
107
105
  {},
108
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
109
106
  preferDefault(userConfig)
110
107
  );
111
108
 
@@ -128,18 +125,21 @@ for (const plugin of ${JSON.stringify(plugins)}) {
128
125
  export default vercelConfig;
129
126
  `);
130
127
  }
131
- async function updateGatsbyMjsConfig(configPath, plugins) {
132
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.mjs');
133
- await fs_1.promises.writeFile(configPath, `import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
128
+ async function updateGatsbyConfigMjs(configPath, plugins) {
129
+ const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
130
+ if (!(await _shared_1.fileExists(renamedPath))) {
131
+ await fs_extra_1.default.rename(configPath, renamedPath);
132
+ }
133
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
134
+ import userConfig from "./gatsby-config.mjs.__vercel_builder_backup__.mjs";
134
135
 
135
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
136
136
  const preferDefault = (m) => (m && m.default) || m;
137
137
 
138
138
  const vercelConfig = Object.assign(
139
139
  {},
140
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
141
140
  preferDefault(userConfig)
142
141
  );
142
+
143
143
  if (!vercelConfig.plugins) {
144
144
  vercelConfig.plugins = [];
145
145
  }
@@ -158,18 +158,21 @@ for (const plugin of ${JSON.stringify(plugins)}) {
158
158
  export default vercelConfig;
159
159
  `);
160
160
  }
161
- async function updateGatsbyJsConfig(configPath, plugins) {
162
- await fs_1.promises.rename(configPath, configPath + '.__vercel_builder_backup__.js');
163
- await fs_1.promises.writeFile(configPath, `const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
161
+ async function updateGatsbyConfigJs(configPath, plugins) {
162
+ const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
163
+ if (!(await _shared_1.fileExists(renamedPath))) {
164
+ await fs_extra_1.default.rename(configPath, renamedPath);
165
+ }
166
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
167
+ const userConfig = require("./gatsby-config.js.__vercel_builder_backup__.js");
164
168
 
165
- // https://github.com/gatsbyjs/gatsby/blob/354003fb2908e02ff12109ca3a02978a5a6e608c/packages/gatsby/src/bootstrap/prefer-default.ts
166
169
  const preferDefault = m => (m && m.default) || m;
167
170
 
168
171
  const vercelConfig = Object.assign(
169
172
  {},
170
- // https://github.com/gatsbyjs/gatsby/blob/a6ecfb2b01d761e8a3612b8ea132c698659923d9/packages/gatsby/src/services/initialize.ts#L113-L117
171
173
  preferDefault(userConfig)
172
174
  );
175
+
173
176
  if (!vercelConfig.plugins) {
174
177
  vercelConfig.plugins = [];
175
178
  }
@@ -187,3 +190,111 @@ for (const plugin of ${JSON.stringify(plugins)}) {
187
190
  module.exports = vercelConfig;
188
191
  `);
189
192
  }
193
+ async function updateGatsbyNode(dir) {
194
+ const gatsbyNodePathTs = path.join(dir, `${GATSBY_NODE_FILE}.ts`);
195
+ const gatsbyNodePathMjs = path.join(dir, `${GATSBY_NODE_FILE}.mjs`);
196
+ const gatsbyNodePathJs = path.join(dir, `${GATSBY_NODE_FILE}.js`);
197
+ if (await _shared_1.fileExists(gatsbyNodePathTs)) {
198
+ await updateGatsbyNodeTs(gatsbyNodePathTs);
199
+ }
200
+ else if (await _shared_1.fileExists(gatsbyNodePathMjs)) {
201
+ await updateGatsbyNodeMjs(gatsbyNodePathMjs);
202
+ }
203
+ else if (await _shared_1.fileExists(gatsbyNodePathJs)) {
204
+ await updateGatsbyNodeJs(gatsbyNodePathJs);
205
+ }
206
+ else {
207
+ await fs_extra_1.default.writeFile(gatsbyNodePathJs, `module.exports = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');`);
208
+ }
209
+ }
210
+ async function updateGatsbyNodeTs(configPath) {
211
+ const renamedPath = `${configPath}.__vercel_builder_backup__.ts`;
212
+ if (!(await _shared_1.fileExists(renamedPath))) {
213
+ await fs_extra_1.default.rename(configPath, renamedPath);
214
+ }
215
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
216
+ import type { GatsbyNode } from 'gatsby';
217
+ import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
218
+ import * as gatsbyNode from './gatsby-node.ts.__vercel_builder_backup__.ts';
219
+
220
+ export * from './gatsby-node.ts.__vercel_builder_backup__.ts';
221
+
222
+ export const onPostBuild: GatsbyNode['onPostBuild'] = async (args, options) => {
223
+ if (typeof (gatsbyNode as any).onPostBuild === 'function') {
224
+ await (gatsbyNode as any).onPostBuild(args, options);
225
+ }
226
+ await vercelBuilder.onPostBuild(args, options);
227
+ };
228
+ `);
229
+ }
230
+ async function updateGatsbyNodeMjs(configPath) {
231
+ const renamedPath = `${configPath}.__vercel_builder_backup__.mjs`;
232
+ if (!(await _shared_1.fileExists(renamedPath))) {
233
+ await fs_extra_1.default.rename(configPath, renamedPath);
234
+ }
235
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
236
+ import * as vercelBuilder from '@vercel/gatsby-plugin-vercel-builder/gatsby-node.js';
237
+ import * as gatsbyNode from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
238
+
239
+ export * from './gatsby-node.mjs.__vercel_builder_backup__.mjs';
240
+
241
+ export const onPostBuild = async (args, options) => {
242
+ if (typeof gatsbyNode.onPostBuild === 'function') {
243
+ await gatsbyNode.onPostBuild(args, options);
244
+ }
245
+ await vercelBuilder.onPostBuild(args, options);
246
+ };
247
+ `);
248
+ }
249
+ async function updateGatsbyNodeJs(configPath) {
250
+ const renamedPath = `${configPath}.__vercel_builder_backup__.js`;
251
+ if (!(await _shared_1.fileExists(renamedPath))) {
252
+ await fs_extra_1.default.rename(configPath, renamedPath);
253
+ }
254
+ await fs_extra_1.default.writeFile(configPath, `${GENERATED_FILE_COMMENT}
255
+ const vercelBuilder = require('@vercel/gatsby-plugin-vercel-builder/gatsby-node.js');
256
+ const gatsbyNode = require('./gatsby-node.js.__vercel_builder_backup__.js');
257
+
258
+ const origOnPostBuild = gatsbyNode.onPostBuild;
259
+
260
+ gatsbyNode.onPostBuild = async (args, options) => {
261
+ if (typeof origOnPostBuild === 'function') {
262
+ await origOnPostBuild(args, options);
263
+ }
264
+ await vercelBuilder.onPostBuild(args, options);
265
+ };
266
+
267
+ module.exports = gatsbyNode;
268
+ `);
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.0",
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/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",
@@ -38,9 +42,9 @@
38
42
  "@types/node-fetch": "2.5.4",
39
43
  "@types/promise-timeout": "1.3.0",
40
44
  "@types/semver": "7.3.13",
41
- "@vercel/build-utils": "5.9.0",
42
- "@vercel/frameworks": "1.2.4",
43
- "@vercel/fs-detectors": "3.7.5",
45
+ "@vercel/build-utils": "6.0.0",
46
+ "@vercel/frameworks": "1.3.0",
47
+ "@vercel/fs-detectors": "3.7.6",
44
48
  "@vercel/ncc": "0.24.0",
45
49
  "@vercel/routing-utils": "2.1.8",
46
50
  "@vercel/static-config": "2.0.11",
@@ -56,5 +60,5 @@
56
60
  "ts-morph": "12.0.0",
57
61
  "typescript": "4.3.4"
58
62
  },
59
- "gitHead": "92f5b6e0c9be947a68b617d282fc39144f530813"
63
+ "gitHead": "9317543c48a039bda0ae91f0819f38f789d98338"
60
64
  }