-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
gatsby-parcel-namer-relative-to-cwd
to monorepo & update …
…Parcel to 2.5.0 (#35446) * fix(deps): update parcel to ^2.4.1 * update peerDep * add namer plugin * update versions * add npmignore * update parcel to 2.5.0 * install gatsby-dev-cli@next * deps * slash * update lock file * pin to 2.5.0 * also pin gatsby's parcel packages, to prevent potential for transitive parcel packages version mismatches Co-authored-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
- Loading branch information
1 parent
f7f8ffe
commit 459fab4
Showing
9 changed files
with
557 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [["babel-preset-gatsby-package"]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `@gatsbyjs/parcel-namer-relative-to-cwd` | ||
|
||
Parcel by default is trying to find common/shared directory between entries and output paths are impacted by it. See https://github.com/parcel-bundler/parcel/issues/5476#issuecomment-769058504. | ||
|
||
With these inputs files: | ||
|
||
``` | ||
a.html | ||
sub/b.html | ||
``` | ||
|
||
You get: | ||
|
||
- `parcel build a.html` => `dist/a.html` | ||
- `parcel build sub/b.html` => `dist/b.html` | ||
- `parcel build a.html sub/b.html` => `dist/a.html`, `dist/sub/b.html` | ||
|
||
We can see that `sub/b.html` entry might result in either `dist/b.html` or `dist/sub/b.html` (depending wether `a.html` is entry or not). This makes builds not deterministic, which is very problematic where entries are "optional". | ||
|
||
This namer plugin stabilize output, so inside `distDir` the hierarchy is the same as entry file in relation to current working directory (CWD): | ||
|
||
- `parcel build a.html` => `dist/a.html` | ||
- `parcel build sub/b.html` => `dist/sub/b.html` | ||
- `parcel build a.html sub/b.html` => `dist/a.html`, `dist/sub/b.html` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "@gatsbyjs/parcel-namer-relative-to-cwd", | ||
"main": "lib/index.js", | ||
"version": "1.0.0-next.0", | ||
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-parcel-namer-relative-to-cwd#readme", | ||
"description": "Parcel namer that preserve directory structure to stabilize output and keep the hierarchy.", | ||
"author": "Michal Piechowiak <misiek.piechowiak@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/gatsbyjs/gatsby.git", | ||
"directory": "packages/gatsby-parcel-namer-relative-to-cwd" | ||
}, | ||
"engines": { | ||
"node": ">=14.15.0", | ||
"parcel": "2.x" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@parcel/plugin": "2.5.0", | ||
"@babel/runtime": "^7.18.0", | ||
"gatsby-core-utils": "^3.15.0-next.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.17.10", | ||
"@babel/core": "^7.18.0", | ||
"babel-preset-gatsby-package": "^2.15.0-next.0", | ||
"cross-env": "^7.0.3" | ||
}, | ||
"peerDependencies": { | ||
"@parcel/namer-default": "2.5.0" | ||
}, | ||
"scripts": { | ||
"build": "babel src --out-dir lib/ --ignore \"**/__tests__\" --extensions \".ts\"", | ||
"prepare": "cross-env NODE_ENV=production npm run build", | ||
"watch": "babel -w src --out-dir lib/ --ignore \"**/__tests__\" --extensions \".ts\"" | ||
}, | ||
"files": [ | ||
"lib/index.js" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Namer } from "@parcel/plugin" | ||
import { FilePath, Namer as NamerOpts } from "@parcel/types" | ||
import defaultNamer from "@parcel/namer-default" | ||
import * as path from "path" | ||
import { slash } from "gatsby-core-utils" | ||
|
||
const CONFIG = Symbol.for(`parcel-plugin-config`) | ||
const defaultNamerOpts = defaultNamer[CONFIG] as NamerOpts<unknown> | ||
|
||
export default new Namer({ | ||
async name(opts): Promise<FilePath | null | undefined> { | ||
// @parcel/namer-default will find "most common denominator" directory | ||
// depending on entries and make it a "relative root" for output. | ||
// This means that output is not deterministic based JUST on configuration | ||
// as adding/removing entries can change output directory structure. | ||
// To make it deterministic we add "middleware" namer which will use | ||
// @parcel/namer-default for filename, but (possibly) adjust directory | ||
// structure. | ||
|
||
const relativePathFromDefaultNamer = await defaultNamerOpts.name(opts) | ||
if (relativePathFromDefaultNamer) { | ||
const mainEntry = opts.bundle.getMainEntry() | ||
if (!mainEntry) { | ||
return null | ||
} | ||
|
||
// For now treating CWD as root. For current gatsby use case | ||
// this is enough, for various other projects it might need to be configurable | ||
// or just smarter (for example cover common dirs like `src` or `lib` etc) | ||
const root = slash(process.cwd()) | ||
|
||
const sourceRelativeToRoot = path.posix.relative( | ||
root, | ||
slash(path.dirname(mainEntry.filePath)) | ||
) | ||
|
||
// newPath will be output relative to "distDir". | ||
// we want to preserve directory structure in distDir that we have | ||
// between entries and root | ||
const newPath = path.posix.join( | ||
sourceRelativeToRoot, | ||
path.basename(relativePathFromDefaultNamer) | ||
) | ||
|
||
return newPath | ||
} | ||
|
||
return null | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.