Skip to content

Commit e4b27b3

Browse files
ChristopherBiscardiJason Lengstorf
authored and
Jason Lengstorf
committed
fix(themes): package names don't have to match filesystem (#14149)
* fix(themes): package names don't have to match filesystem fixes #14134 As a result of this PR, the package names of themes no longer have to match their location on the filesystem. This means a theme with a `package.json` name of `@something/my-theme` in a yarn workspace (or similar setup) folder named `some-theme` will now work. This does not change the behavior of shadowing in child themes, which still requires that you use the package name to align with how npm packages work when installed. Previously, the location on the filesystem would have needed to be in two directories `@something/my-theme`. There are no breaking changes in this PR. * one more * fix windows tests * really fix windows
1 parent 4f984e8 commit e4b27b3

File tree

2 files changed

+23
-13
lines changed
  • packages/gatsby/src/internal-plugins/webpack-theme-component-shadowing

2 files changed

+23
-13
lines changed

packages/gatsby/src/internal-plugins/webpack-theme-component-shadowing/__tests__/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ describe(`Component Shadowing`, () => {
2424
`a-component`
2525
)
2626
)
27-
).toEqual([`a-theme`])
27+
).toEqual([
28+
{
29+
themeDir: path.join(path.sep, `some`, `place`, `a-theme`),
30+
themeName: `a-theme`,
31+
},
32+
])
2833

2934
expect(
3035
// request to a shadowed component in theme b
@@ -41,7 +46,12 @@ describe(`Component Shadowing`, () => {
4146
`a-component`
4247
)
4348
)
44-
).toEqual([`theme-b`])
49+
).toEqual([
50+
{
51+
themeDir: path.join(path.sep, `some`, `place`, `theme-b`),
52+
themeName: `theme-b`,
53+
},
54+
])
4555
})
4656

4757
it(`can determine if the request path is in the shadow chain for the issuer`, () => {

packages/gatsby/src/internal-plugins/webpack-theme-component-shadowing/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ module.exports = class GatsbyThemeComponentShadowingResolverPlugin {
2626
// `gatsby-theme-blog/src/components/gatsby-theme-something/src/components`
2727
if (matchingThemes.length > 1) {
2828
throw new Error(
29-
`Gatsby can't differentiate between themes ${matchingThemes.join(
30-
` and `
31-
)} for path ${request.path}`
29+
`Gatsby can't differentiate between themes ${matchingThemes
30+
.map(theme => theme.themeName)
31+
.join(` and `)} for path ${request.path}`
3232
)
3333
}
3434

@@ -39,7 +39,7 @@ module.exports = class GatsbyThemeComponentShadowingResolverPlugin {
3939
// theme is the theme package from which we're requiring the relative component
4040
const [theme] = matchingThemes
4141
// get the location of the component relative to src/
42-
const [, component] = request.path.split(path.join(theme, `src`))
42+
const [, component] = request.path.split(path.join(theme.themeDir, `src`))
4343

4444
if (
4545
/**
@@ -77,7 +77,7 @@ module.exports = class GatsbyThemeComponentShadowingResolverPlugin {
7777

7878
// This is the shadowing algorithm.
7979
const builtComponentPath = this.resolveComponentPath({
80-
matchingTheme: theme,
80+
matchingTheme: theme.themeName,
8181
themes: this.themes,
8282
component,
8383
})
@@ -137,13 +137,13 @@ module.exports = class GatsbyThemeComponentShadowingResolverPlugin {
137137

138138
getMatchingThemesForPath(filepath) {
139139
// find out which theme's src/components dir we're requiring from
140-
const allMatchingThemes = this.themes.filter(({ themeName }) =>
141-
filepath.includes(path.join(themeName, `src`))
140+
const allMatchingThemes = this.themes.filter(({ themeDir }) =>
141+
filepath.includes(path.join(themeDir, `src`))
142142
)
143143

144144
// The same theme can be included twice in the themes list causing multiple
145145
// matches. This case should only be counted as a single match for that theme.
146-
return _.uniq(allMatchingThemes.map(({ themeName }) => themeName))
146+
return _.uniqBy(allMatchingThemes, `themeName`)
147147
}
148148

149149
// given a theme name, return all of the possible shadow locations
@@ -168,11 +168,11 @@ module.exports = class GatsbyThemeComponentShadowingResolverPlugin {
168168
const [theme] = matchingThemes
169169

170170
// get the location of the component relative to src/
171-
const [, component] = requestPath.split(path.join(theme, `src`))
171+
const [, component] = requestPath.split(path.join(theme.themeDir, `src`))
172172

173173
// get list of potential shadow locations
174-
const shadowFiles = this.getBaseShadowDirsForThemes(theme)
175-
.concat(path.join(userSiteDir, `src`, theme))
174+
const shadowFiles = this.getBaseShadowDirsForThemes(theme.themeName)
175+
.concat(path.join(userSiteDir, `src`, theme.themeName))
176176
.map(dir => path.join(dir, component))
177177

178178
// if the issuer is requesting a path that is a potential shadow path of itself

0 commit comments

Comments
 (0)