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

WIP - TS local plugins #34814

Closed
wants to merge 1 commit into from
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
40 changes: 30 additions & 10 deletions packages/gatsby/src/bootstrap/load-plugins/load.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from "lodash"
import { slash, createRequireFromPath } from "gatsby-core-utils"
import fs from "fs"
import { ensureDirSync } from "fs-extra"
import path from "path"
import crypto from "crypto"
import glob from "glob"
Expand All @@ -19,7 +20,10 @@ import {
ISiteConfig,
} from "./types"
import { PackageJson } from "../../.."
import { COMPILED_CACHE_DIR } from "../../utils/parcel/compile-gatsby-files"
import {
compileGatsbyFiles,
COMPILED_CACHE_DIR,
} from "../../utils/parcel/compile-gatsby-files"

const GATSBY_CLOUD_PLUGIN_NAME = `gatsby-plugin-gatsby-cloud`
const TYPESCRIPT_PLUGIN_NAME = `gatsby-plugin-typescript`
Expand Down Expand Up @@ -79,18 +83,34 @@ export function resolvePlugin(

if (existsSync(resolvedPath)) {
if (existsSync(`${resolvedPath}/package.json`)) {
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
) as PackageJson
const name = packageJSON.name || pluginName
warnOnIncompatiblePeerDependency(name, packageJSON)
let packageJSON
let name

const compiledDir = `${rootDir}/${COMPILED_CACHE_DIR}/${pluginName}`

ensureDirSync(compiledDir)

// TODO - This is a proof of concept, everything will be refactored

compileGatsbyFiles(resolvedPath, compiledDir)
.then(() => {
packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
) as PackageJson
name = packageJSON.name || pluginName
warnOnIncompatiblePeerDependency(name, packageJSON)
})
.catch(error => {
console.error(`CAUGHT ERROR:`, error)
})

return {
resolve: resolvedPath,
resolveCompiled: compiledDir,
name,
id: createPluginId(name),
version:
packageJSON.version || createFileContentHash(resolvedPath, `**`),
packageJSON?.version || createFileContentHash(resolvedPath, `**`),
}
} else {
// Make package.json a requirement for local plugins too
Expand Down Expand Up @@ -340,12 +360,12 @@ export function loadPlugins(
}

// Add the site's default "plugin" i.e. gatsby-x files in root of site.
const compiledPath = `${path.join(process.cwd(), COMPILED_CACHE_DIR)}`
plugins.push({
resolve: slash(compiledPath),
resolve: slash(process.cwd()),
resolveCompiled: `${path.join(process.cwd(), COMPILED_CACHE_DIR)}`,
id: createPluginId(`default-site-plugin`),
name: `default-site-plugin`,
version: createFileContentHash(compiledPath, `gatsby-*`),
version: createFileContentHash(slash(process.cwd()), `gatsby-*`),
pluginOptions: {
plugins: [],
},
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/bootstrap/load-plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export interface IPluginInfo {
/** The absolute path to the plugin */
resolve: string

/** The absolute path to the compiled plugin directory, if there is one */
resolveCompiled?: string

/** The plugin name */
name: string

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,11 @@ exports.sourceNodes = ({
flattenedPlugins.forEach(plugin => {
plugin.pluginFilepath = plugin.resolve

// Since `default-site-plugin` is a proxy for gatsby-* files that are compiled and live in .cache/compiled,
// use program.directory to access package.json since it is not compiled and lives in the site root
const dir =
plugin.name === `default-site-plugin` ? program.directory : plugin.resolve

createNode({
...plugin,
packageJson: transformPackageJson(require(`${dir}/package.json`)),
packageJson: transformPackageJson(
require(`${plugin.resolve}/package.json`)
),
parent: null,
children: [],
internal: {
Expand Down
6 changes: 1 addition & 5 deletions packages/gatsby/src/schema/graphql-engine/print-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ function renderQueryEnginePlugins(): string {
}

function relativePluginPath(resolve: string): string {
const relativePath = slash(
return slash(
path.relative(path.dirname(schemaCustomizationPluginsPath), resolve)
)

// TODO: This is quite hacky, better idea anyone?
if (relativePath.startsWith(`.`)) return relativePath
return relativePath.replace(`compiled`, `..`)
}

function render(
Expand Down
12 changes: 8 additions & 4 deletions packages/gatsby/src/services/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,22 +533,26 @@ export async function initialize({
if (env === `ssr` && plugin.skipSSR === true) return undefined

const envAPIs = plugin[`${env}APIs`]
const dir =
plugin.name === `default-site-plugin` ? program.directory : plugin.resolve

// Always include gatsby-browser.js files if they exist as they're
// a handy place to include global styles and other global imports.
try {
if (env === `browser`) {
const modulePath = path.join(dir, `gatsby-${env}`)
const modulePath = path.join(
plugin?.compiledResolve || plugin.resolve,
`gatsby-${env}`
)
return slash(resolveModule(modulePath) as string)
}
} catch (e) {
// ignore
}

if (envAPIs && Array.isArray(envAPIs) && envAPIs.length > 0) {
const modulePath = path.join(dir, `gatsby-${env}`)
const modulePath = path.join(
plugin?.compiledResolve || plugin.resolve,
`gatsby-${env}`
)
return slash(resolveModule(modulePath) as string)
}
return undefined
Expand Down
15 changes: 9 additions & 6 deletions packages/gatsby/src/utils/parcel/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import reporter from "gatsby-cli/lib/reporter"

export const COMPILED_CACHE_DIR = `.cache/compiled`

export function constructBundler(dir: string): Parcel {
export function constructBundler(entryDir: string, distDir?: string): Parcel {
return new Parcel({
entries: `${dir}/gatsby-+(node|config).{ts,tsx,js}`,
entries: `${entryDir}/gatsby-+(node|config).{ts,tsx,js}`,
defaultConfig: `gatsby-parcel-config`,
mode: `production`,
targets: {
default: {
distDir: `${dir}/${COMPILED_CACHE_DIR}`,
distDir: distDir || `${entryDir}/${COMPILED_CACHE_DIR}`,
outputFormat: `commonjs`,
includeNodeModules: false,
sourceMap: false,
Expand All @@ -19,16 +19,19 @@ export function constructBundler(dir: string): Parcel {
},
},
},
cacheDir: `${dir}/.cache/.parcel-cache`,
cacheDir: `${entryDir}/.cache/.parcel-cache`,
})
}

/**
* Compiles known gatsby-* files (e.g. `gatsby-config`, `gatsby-node`)
* and stores them in `.cache/compiled` relative to the site root.
*/
export async function compileGatsbyFiles(dir: string): Promise<void> {
const bundler = constructBundler(dir)
export async function compileGatsbyFiles(
entryDir: string,
distDir?: string
): Promise<void> {
const bundler = constructBundler(entryDir, distDir)

const activity = reporter.activityTimer(`compile gatsby files`)
activity.start()
Expand Down