Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix/no peer deps test #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pieh wants to merge 7 commits into main from fix/no-peer-deps-test
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions plugin/package-lock.json
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions plugin/package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
"prepare": "npm run build"
},
"dependencies": {
"@gatsbyjs/reach-router": "^2.0.0",
"@netlify/functions": "^1.6.0",
"@netlify/ipx": "^1.4.6",
"abortcontroller-polyfill": "^1.7.3",
"chalk": "^4.1.2",
"co-body": "^6.1.0",
"common-tags": "^1.8.2",
"cookie": "^0.6.0",
"download": "^8.0.0",
"etag": "^1.8.1",
Expand All @@ -61,7 +63,6 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@gatsbyjs/reach-router": "^2.0.0",
"@netlify/build": "^29.27.0",
"@types/chance": "^1.1.3",
"@types/fs-extra": "^9.0.12",
Expand All @@ -75,9 +76,5 @@
"rimraf": "^5.0.0",
"tmp-promise": "^3.0.3",
"typescript": "^5.0.0"
},
"peerDependencies": {
"@gatsbyjs/reach-router": "*",
"common-tags": "^1.8.2"
}
}
102 changes: 86 additions & 16 deletions plugin/src/helpers/functions.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,13 +1,89 @@
/* eslint-disable max-lines */
import { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
import { copy, copyFile, ensureDir, existsSync, rm, writeFile } from 'fs-extra'
import { resolve, join, relative } from 'pathe'
import { copy, ensureDir, existsSync, rm, writeFile, readFile } from 'fs-extra'
import { resolve, join, relative, dirname } from 'pathe'

import { makeApiHandler, makeHandler } from '../templates/handlers'

import { getGatsbyRoot } from './config'

export type FunctionList = Array<'API' | 'SSR' | 'DSG'>

/**
* Adjust package imports in functions we produce to be relative. Those imported packages should always be dependencies
* of `@netlify/plugin-gatsby` and we can't rely on those imports being resolvable by accident (i.e. npm hoisting deps
* of this plugin in root node_modules)
*/
export const adjustRequiresToRelative = (
template: string,
outputLocation: string,
): string =>
// built files use CJS so targeting require here despite source files using ESM
template.replace(/require\(["'`]([^"'`]+)["'`]\)/g, (match, request) => {
if (request.startsWith('.')) {
return match
}

let absolutePath
try {
absolutePath = dirname(require.resolve(`${request}/package.json`))
} catch {}

if (!absolutePath) {
absolutePath = require.resolve(request)
}

if (absolutePath === request) {
// for builtins path will be the same as request
return match
}
const relativePath = `./${relative(dirname(outputLocation), absolutePath)}`
return `require('${relativePath}')`
})

const adjustFilesRequiresToRelative = async (
filesToAdjustRequires: Set<string>,
) => {
for (const file of filesToAdjustRequires) {
await writeFile(
file,
adjustRequiresToRelative(await readFile(file, 'utf8'), file),
)
}
}

const writeFileWithRelativeRequires = (
outputPath: string,
source: string,
): Promise<void> =>
writeFile(outputPath, adjustRequiresToRelative(source, outputPath))

const copyWithRelativeRequires = async (
src: string,
dest: string,
): Promise<void> => {
const filesToAdjustRequires = new Set<string>()
await copy(src, dest, {
filter: (_filterSrc, filterDest) => {
if (/\.[cm]?js$/.test(filterDest)) {
filesToAdjustRequires.add(filterDest)
}
return true
},
})
await adjustFilesRequiresToRelative(filesToAdjustRequires)
}

const writeApiFunction = async ({ appDir, functionDir }) => {
const source = makeApiHandler(appDir)
// This is to ensure we're copying from the compiled js, not ts source
await copyWithRelativeRequires(
join(__dirname, '..', '..', 'lib', 'templates', 'api'),
functionDir,
)
await writeFileWithRelativeRequires(join(functionDir, '__api.js'), source)
}

const writeFunction = async ({
renderMode,
handlerName,
Expand All @@ -16,23 +92,16 @@ const writeFunction = async ({
}) => {
const source = makeHandler(appDir, renderMode)
await ensureDir(join(functionsSrc, handlerName))
await writeFile(join(functionsSrc, handlerName, `${handlerName}.js`), source)
await copyFile(
await writeFileWithRelativeRequires(
join(functionsSrc, handlerName, `${handlerName}.js`),
source,
)
await copyWithRelativeRequires(
join(__dirname, '..', '..', 'lib', 'templates', 'utils.js'),
join(functionsSrc, handlerName, 'utils.js'),
)
}

const writeApiFunction = async ({ appDir, functionDir }) => {
const source = makeApiHandler(appDir)
// This is to ensure we're copying from the compiled js, not ts source
await copy(
join(__dirname, '..', '..', 'lib', 'templates', 'api'),
functionDir,
)
await writeFile(join(functionDir, '__api.js'), source)
}

export const writeFunctions = async ({
constants,
netlifyConfig,
Expand Down Expand Up @@ -92,13 +161,13 @@ export const setupImageCdn = async ({

await ensureDir(constants.INTERNAL_FUNCTIONS_SRC)

await copyFile(
await copyWithRelativeRequires(
join(__dirname, '..', '..', 'src', 'templates', 'ipx.ts'),
join(constants.INTERNAL_FUNCTIONS_SRC, '_ipx.ts'),
)

if (NETLIFY_IMAGE_CDN === `true`) {
await copyFile(
await copyWithRelativeRequires(
join(__dirname, '..', '..', 'src', 'templates', 'image.ts'),
join(constants.INTERNAL_FUNCTIONS_SRC, '__image.ts'),
)
Expand Down Expand Up @@ -169,3 +238,4 @@ export const deleteFunctions = async ({
}
}
}
/* eslint-enable max-lines */
10 changes: 1 addition & 9 deletions plugin/src/templates/api/gatsbyFunction.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { existsSync } from 'fs'
import path from 'path'
import process from 'process'

import {
match as reachRouterMatch,
matchPath as reachRouterMatchPath,
} from '@gatsbyjs/reach-router'
import { match as reachMatch } from '@gatsbyjs/reach-router'
import { HandlerEvent } from '@netlify/functions'
import bodyParser from 'co-body'
import multer from 'multer'
Expand All @@ -16,11 +13,6 @@ import {
AugmentedGatsbyFunctionRequest,
} from './utils'

/**
* Depending on the version of '@gatsbyjs/reach-router' installed, the 'match' method may not be defined.
* This check ensures that this continues to work as expected between v1 and v2 of the package.
*/
const reachMatch = reachRouterMatch || reachRouterMatchPath
const parseForm = multer().any()
type MulterReq = Parameters<typeof parseForm>[0]
type MulterRes = Parameters<typeof parseForm>[1]
Expand Down
3 changes: 1 addition & 2 deletions plugin/src/templates/api/utils.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
HandlerContext,
} from '@netlify/functions'
import cookie from 'cookie'
import type { GatsbyFunctionResponse } from 'gatsby'
import { GatsbyFunctionRequest } from 'gatsby'
import type { GatsbyFunctionRequest, GatsbyFunctionResponse } from 'gatsby'
import fetch, { Headers } from 'node-fetch'
import statuses from 'statuses'

Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /