Skip to content

Commit d993c75

Browse files
rexxarsbjoerge
authored andcommitted
[plugin-loader] Allow overriding config parts (#134)
1 parent 4cfbfe5 commit d993c75

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

packages/@sanity/plugin-loader/loader.js

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ function registerLoader(options) {
7474

7575
const configMatch = request.match(configMatcher)
7676
if (configMatch) {
77+
const configOverrides = overrides && overrides[request]
78+
if (configOverrides && configOverrides.length > 0) {
79+
require.cache[request] = getModule(request, configOverrides[0])
80+
return request
81+
}
82+
7783
const configFor = configMatch[1]
7884
if (configFor === 'sanity') {
7985
const sanityConfig = require(path.join(basePath, 'sanity.json'))

packages/@sanity/plugin-loader/test/loader.test.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ test('overrides *must* be arrays', t => {
1818
t.throws(() => pluginLoader({overrides}), /array/)
1919
})
2020

21-
test('should be able to override a role with multiple fulfillers', t => {
22-
const role = 'part:foo/bar/baz'
21+
test('should be able to override a part with multiple fulfillers', t => {
22+
const part = 'part:foo/bar/baz'
2323
const func1 = () => '1'
2424
const func2 = () => '2'
2525

26-
const overrides = {[role]: [func1, func2]}
26+
const overrides = {[part]: [func1, func2]}
2727
pluginLoader({overrides})
28-
t.is(require(role), func1)
29-
t.is(require(`all:${role}`), overrides[role])
28+
t.is(require(part), func1)
29+
t.is(require(`all:${part}`), overrides[part])
3030
})
3131

3232
test('should pass unmocked requests onto the default resolver', t => {
@@ -43,7 +43,7 @@ test('should be able to resolve an actual filesystem structure', t => {
4343
t.is(getBar(), 'bar')
4444
})
4545

46-
test('should be able to require all fulfillers of a role', t => {
46+
test('should be able to require all fulfillers of a part', t => {
4747
const start = Date.now()
4848
pluginLoader({basePath: path.join(__dirname, 'fixture')})
4949

@@ -56,7 +56,7 @@ test('should be able to require all fulfillers of a role', t => {
5656
})
5757
})
5858

59-
test('should be able to include the sanity debug role', t => {
59+
test('should be able to include the sanity debug part', t => {
6060
pluginLoader({basePath: path.join(__dirname, 'fixture')})
6161
const debug = require('sanity:debug')
6262
t.is(debug.plugins[0].name, 'date')
@@ -99,23 +99,23 @@ test('should be able to load CSS files through PostCSS', t => {
9999
t.is(styles.zebra, 'datepicker__zebra___1_qke')
100100
})
101101

102-
test('should resolve correctly when using optional role requires (?-postfix)', t => {
102+
test('should resolve correctly when using optional part requires (?-postfix)', t => {
103103
pluginLoader({basePath: path.join(__dirname, 'fixture')})
104104

105105
const getBar = require('part:base/bar?')
106106
t.is(require.resolve('part:base/bar?'), path.join(__dirname, 'fixture', 'getBar.js'))
107107
t.is(getBar(), 'bar')
108108
})
109109

110-
test('should resolve correctly when overriding and using optional role requires', t => {
110+
test('should resolve correctly when overriding and using optional part requires', t => {
111111
const overrides = {'part:base/bar': ['moo']}
112112
pluginLoader({basePath: path.join(__dirname, 'fixture'), overrides})
113113

114114
const bar = require('part:base/bar?')
115115
t.is(bar, 'moo')
116116
})
117117

118-
test('should return undefined when using optional role requires on an unfulfilled role', t => {
118+
test('should return undefined when using optional part requires on an unfulfilled part', t => {
119119
pluginLoader({basePath: path.join(__dirname, 'fixture')})
120120

121121
const result = require('part:not/existant?')
@@ -128,3 +128,11 @@ test('should resolve parts that point to a path which is a directory containing
128128
const result = require('part:base/indexpart')
129129
t.is(result(), 'index value')
130130
})
131+
132+
test('should be able to override config parts', t => {
133+
const overrides = {'config:sanity': [{api: {projectId: 'heisann'}}]}
134+
pluginLoader({overrides})
135+
136+
const config = require('config:sanity')
137+
t.is(config.api.projectId, 'heisann')
138+
})

0 commit comments

Comments
 (0)