Skip to content
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

fix: fixes the yarn Plug and Play issue for plugins #4713

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
64 changes: 0 additions & 64 deletions packages/build/src/plugins/child/logic.js

This file was deleted.

85 changes: 85 additions & 0 deletions packages/build/src/plugins/child/logic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { readdirSync } from 'fs'
import { createRequire } from 'module'
import { pathToFileURL } from 'url'

import { execaNode } from 'execa'

import { ROOT_PACKAGE_JSON } from '../../utils/json.js'
import { DEV_EVENTS, EVENTS } from '../events.js'

import { addTsErrorInfo } from './typescript.js'

const require = createRequire(import.meta.url)

// Require the plugin file and fire its top-level function.
// The returned object is the `logic` which includes all event handlers.
export const getLogic = async function ({ pluginPath, inputs, tsNodeService }) {
const logic = await importLogic(pluginPath, tsNodeService)
const logicA = loadLogic({ logic, inputs })
return logicA
}

const importLogic = async function (pluginPath: string, tsNodeService) {
console.log(readdirSync('/Users/lukasholzer/Sites/tmp/effy.space'))
const b = await import('file:///Users/lukasholzer/Sites/tmp/effy.space/.pnp.cjs')
console.log(b.default.resolveRequest(''))
b.default.setup()

const res = require.resolve('netlify-plugin-inline-source')
console.log(res)
// const c = await execaNode(
// '/Users/lukasholzer/Sites/tmp/effy.space/.yarn/cache/netlify-plugin-inline-source-npm-1.0.4-94542d11f7-edcba2b6aa.zip',
// [],
// {
// stdio: 'inherit',
// },
// )
// console.log(process.cwd())
// const a = await require('netlify-plugin-inline-source')
// console.log(a)
// try {
// // `ts-node` is not available programmatically for pure ES modules yet,
// // which is currently making it impossible for local plugins to use both
// // pure ES modules and TypeScript.
// if (tsNodeService !== undefined) {
// return require(pluginPath)
// }

// // `pluginPath` is an absolute file path but `import()` needs URLs.
// // Converting those with `pathToFileURL()` is needed especially on Windows
// // where the drive letter would not work with `import()`.
// const returnValue = await import(pathToFileURL(pluginPath))
// // Plugins should use named exports, but we still support default exports
// // for backward compatibility with CommonJS
// return returnValue.default === undefined ? returnValue : returnValue.default
// } catch (error) {
// addTsErrorInfo(error, tsNodeService)
// // We must change `error.stack` instead of `error.message` because some
// // errors thrown from `import()` access `error.stack` before throwing.
// // `error.stack` is lazily instantiated by Node.js, so changing
// // `error.message` afterwards would not modify `error.stack`. Therefore, the
// // resulting stack trace, which is printed in the build logs, would not
// // include the additional message prefix.
// error.stack = `Could not import plugin:\n${error.stack}`
// throw error
// }
return {}
}

const loadLogic = function ({ logic, inputs }) {
if (typeof logic !== 'function') {
return logic
}

const metadata = {
events: new Set([...DEV_EVENTS, ...EVENTS]),
version: ROOT_PACKAGE_JSON.version,
}

try {
return logic(inputs, metadata)
} catch (error) {
error.message = `Could not load plugin:\n${error.message}`
throw error
}
}
1 change: 1 addition & 0 deletions packages/build/src/plugins/child/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
console.log('!!!! CHILD \n\n')
import { setInspectColors } from '../../log/colors.js'
import { sendEventToParent, getEventsFromParent } from '../ipc.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import process from 'process'
import { promisify } from 'util'

import type { ExecaChildProcess } from 'execa'
import { pEvent } from 'p-event'
import { v4 as uuidv4 } from 'uuid'

Expand Down Expand Up @@ -34,7 +35,7 @@ export const callChild = async function ({ childProcess, eventName, payload, log
// - child process `exit`
// In the later two cases, we propagate the error.
// We need to make `p-event` listeners are properly cleaned up too.
export const getEventFromChild = async function (childProcess, callId) {
export const getEventFromChild = async function (childProcess: ExecaChildProcess<string>, callId: 'ready' | string) {
if (childProcessHasExited(childProcess)) {
throw getChildExitError('Could not receive event from child process because it already exited.')
}
Expand Down Expand Up @@ -124,6 +125,8 @@ export const sendEventToParent = async function (callId, payload, verbose, error
// Error static properties are not serializable through `child_process`
// (which uses `v8.serialize()` under the hood) so we need to convert from/to
// plain objects.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const serializePayload = function ({ error = {}, error: { name } = {}, ...payload }) {
if (name === undefined) {
return payload
Expand All @@ -133,11 +136,13 @@ const serializePayload = function ({ error = {}, error: { name } = {}, ...payloa
return { ...payload, error: errorA }
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const parsePayload = function ({ error = {}, error: { name } = {}, ...payload }) {
if (name === undefined) {
return payload
}

const errorA = jsonToError(error)
const errorA = jsonToError(error as any)
return { ...payload, error: errorA }
}
17 changes: 17 additions & 0 deletions packages/build/src/plugins/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { dirname } from 'path'
import { PackageJson } from 'read-pkg-up'
import semver from 'semver'

import { Mode } from '../core/types.js'
import { addErrorInfo } from '../error/info.js'
import { installLocalPluginsDependencies } from '../install/local.js'
import { measureDuration } from '../time/main.js'
Expand All @@ -12,6 +13,15 @@ import { getPackageJson } from '../utils/package.js'
import { useManifest } from './manifest/main.js'
import { resolvePluginsPath } from './resolve.js'

export type PluginsOptions = {
packageName: string
pluginPath?: unknown
pinnedVersion: undefined
loadedFrom: undefined | 'auto_install' | string
origin: 'config' | string
inputs: Record<string, unknown>
}

// Load core plugins and user plugins
const tGetPluginsOptions = async function ({
pluginsOptions,
Expand All @@ -28,6 +38,13 @@ const tGetPluginsOptions = async function ({
sendStatus,
testOpts,
featureFlags,
}: {
pluginsOptions: PluginsOptions[]
mode: Mode
buildDir: string
nodePath: string
packageJson: PackageJson
[key: string]: any
}) {
const pluginsOptionsA = await resolvePluginsPath({
pluginsOptions,
Expand Down
Loading