From 748ad288668fa2ff185c9ea7b8904a5e4988a670 Mon Sep 17 00:00:00 2001 From: Mathieu Dutour Date: Thu, 7 Mar 2024 09:24:48 +0100 Subject: [PATCH] feat: Support custom .parcelrc config (#890) Co-authored-by: L <6723574+louisgv@users.noreply.github.com> --- .../extension-devtools/common-path.ts | 1 + .../features/helpers/create-parcel-bundler.ts | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cli/plasmo/src/features/extension-devtools/common-path.ts b/cli/plasmo/src/features/extension-devtools/common-path.ts index d334c7758..0225ceed2 100644 --- a/cli/plasmo/src/features/extension-devtools/common-path.ts +++ b/cli/plasmo/src/features/extension-devtools/common-path.ts @@ -45,6 +45,7 @@ export const getCommonPath = (projectDirectory = cwd()) => { packageFilePath: resolve(projectDirectory, "package.json"), gitIgnorePath: resolve(projectDirectory, ".gitignore"), assetsDirectory: resolve(projectDirectory, "assets"), + parcelConfig: resolve(projectDirectory, ".parcelrc"), dotPlasmoDirectory, cacheDirectory, diff --git a/cli/plasmo/src/features/helpers/create-parcel-bundler.ts b/cli/plasmo/src/features/helpers/create-parcel-bundler.ts index 3e74df197..f7c03ee7b 100644 --- a/cli/plasmo/src/features/helpers/create-parcel-bundler.ts +++ b/cli/plasmo/src/features/helpers/create-parcel-bundler.ts @@ -1,9 +1,10 @@ import { dirname, join, resolve } from "path" import ParcelFS from "@parcel/fs" import ParcelPM from "@parcel/package-manager" -import { emptyDir, ensureDir, readJson, writeJson } from "fs-extra" +import { emptyDir, ensureDir, exists, readJson, writeJson } from "fs-extra" import { getFlag, hasFlag } from "@plasmo/utils/flags" +import { wLog } from "@plasmo/utils/logging" import { Parcel, type ParcelOptions } from "@plasmohq/parcel-core" @@ -65,7 +66,7 @@ export const createParcelBuilder = async ( const baseConfig = require.resolve("@plasmohq/parcel-config") - const runConfig = join(dirname(baseConfig), "run.json") + let runConfig = join(dirname(baseConfig), "run.json") const configJson = await readJson(baseConfig) @@ -75,6 +76,26 @@ export const createParcelBuilder = async ( await writeJson(runConfig, configJson) + if (await exists(commonPath.parcelConfig)) { + runConfig = commonPath.parcelConfig + + if (isProd) { + const customConfig = await readJson(runConfig) + + if (customConfig.extends !== "@plasmohq/parcel-config") { + wLog( + 'The .parcelrc does not extend "@plasmohq/parcel-config", the result may be unexpected' + ) + } + } + + if (hasFlag("--bundle-buddy")) { + wLog( + 'The "--bundle-buddy" flag does not work with a custom .parcelrc file' + ) + } + } + const engines = { browsers: bundleConfig.manifestVersion === "mv2" &&