@@ -2,9 +2,9 @@ import path from 'path'
2
2
import jsYaml from 'js-yaml'
3
3
import parseJson from 'parse-json'
4
4
import createDebug from 'debug'
5
- import loadPlugin from 'load-plugin'
5
+ import { resolvePlugin } from 'load-plugin'
6
6
import isPlainObj from 'is-plain-obj'
7
- import fault from 'fault'
7
+ import { fault } from 'fault'
8
8
import { FindUp } from './find-up.js'
9
9
10
10
const debug = createDebug ( 'unified-engine:configuration' )
@@ -81,26 +81,26 @@ async function create(buf, filePath) {
81
81
const fn = ( filePath && loaders [ path . extname ( filePath ) ] ) || defaultLoader
82
82
const options = { prefix : this . pluginPrefix , cwd : this . cwd }
83
83
const result = { settings : { } , plugins : [ ] }
84
- let contents
84
+ let value
85
85
86
86
if ( filePath ) {
87
- contents = await Reflect . apply ( fn , this , arguments )
87
+ value = await fn . call ( this , buf , filePath )
88
88
}
89
89
90
- if ( this . configTransform && contents !== undefined ) {
91
- contents = this . configTransform ( contents , filePath )
90
+ if ( this . configTransform && value !== undefined ) {
91
+ value = this . configTransform ( value , filePath )
92
92
}
93
93
94
94
// Exit if we did find a `package.json`, but it does not have configuration.
95
95
if (
96
96
buf &&
97
- contents === undefined &&
97
+ value === undefined &&
98
98
path . basename ( filePath ) === 'package.json'
99
99
) {
100
100
return
101
101
}
102
102
103
- if ( contents === undefined ) {
103
+ if ( value === undefined ) {
104
104
if ( this . defaultConfig ) {
105
105
await merge (
106
106
result ,
@@ -111,7 +111,7 @@ async function create(buf, filePath) {
111
111
} else {
112
112
await merge (
113
113
result ,
114
- contents ,
114
+ value ,
115
115
Object . assign ( { } , options , { root : path . dirname ( filePath ) } )
116
116
)
117
117
}
@@ -126,7 +126,7 @@ function loadScriptOrModule(_, filePath) {
126
126
}
127
127
128
128
function loadYaml ( buf , filePath ) {
129
- return jsYaml . safeLoad ( buf , { filename : path . basename ( filePath ) } )
129
+ return jsYaml . load ( buf , { filename : path . basename ( filePath ) } )
130
130
}
131
131
132
132
function loadJson ( buf , filePath ) {
@@ -201,34 +201,37 @@ async function merge(target, raw, options) {
201
201
}
202
202
203
203
async function addModule ( id , value ) {
204
- let fp = loadPlugin . resolve ( id , { cwd : options . root , prefix : options . prefix } )
205
- let result
206
-
207
- if ( fp ) {
208
- result = await loadFromAbsolutePath ( fp , options . root )
209
-
210
- try {
211
- if ( typeof result === 'function' ) {
212
- addPlugin ( result , value )
213
- } else {
214
- await merge (
215
- target ,
216
- result ,
217
- Object . assign ( { } , options , { root : path . dirname ( fp ) } )
218
- )
219
- }
220
- } catch {
221
- throw fault (
222
- 'Error: Expected preset or plugin, not %s, at `%s`' ,
204
+ let fp
205
+
206
+ try {
207
+ fp = await resolvePlugin ( id , {
208
+ cwd : options . root ,
209
+ prefix : options . prefix
210
+ } )
211
+ } catch ( error ) {
212
+ addPlugin ( ( ) => {
213
+ throw fault ( 'Could not find module `%s`\n%s' , id , error . stack )
214
+ } , value )
215
+ return
216
+ }
217
+
218
+ const result = await loadFromAbsolutePath ( fp , options . root )
219
+
220
+ try {
221
+ if ( typeof result === 'function' ) {
222
+ addPlugin ( result , value )
223
+ } else {
224
+ await merge (
225
+ target ,
223
226
result ,
224
- path . relative ( options . root , fp )
227
+ Object . assign ( { } , options , { root : path . dirname ( fp ) } )
225
228
)
226
229
}
227
- } else {
228
- fp = path . relative ( options . cwd , path . resolve ( options . root , id ) )
229
- addPlugin (
230
- failingModule ( fp , new Error ( 'Could not find module `' + id + '`' ) ) ,
231
- value
230
+ } catch {
231
+ throw fault (
232
+ 'Error: Expected preset or plugin, not %s, at `%s`' ,
233
+ result ,
234
+ path . relative ( options . root , fp )
232
235
)
233
236
}
234
237
}
@@ -262,26 +265,13 @@ function find(entries, plugin) {
262
265
}
263
266
}
264
267
265
- function failingModule ( id , error ) {
266
- const cache = failingModule . cache || ( failingModule . cache = { } )
267
- const submodule = own . call ( cache , id ) ? cache [ id ] : ( cache [ id ] = fail )
268
- return submodule
269
- function fail ( ) {
270
- throw error
271
- }
272
- }
273
-
274
268
async function loadFromAbsolutePath ( fp , base ) {
275
269
let result
276
270
277
271
try {
278
272
result = ( await import ( fp ) ) . default
279
273
} catch ( error ) {
280
- throw fault (
281
- 'Cannot parse script `%s`\n%s' ,
282
- path . relative ( base , fp ) ,
283
- error . stack
284
- )
274
+ throw fault ( 'Cannot import `%s`\n%s' , path . relative ( base , fp ) , error . stack )
285
275
}
286
276
287
277
if ( result && typeof result === 'object' && result . __esModule ) {
0 commit comments