-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
loadConfigFile.js
52 lines (46 loc) · 1.47 KB
/
loadConfigFile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import path from 'path';
import chalk from 'chalk';
import * as rollup from 'rollup';
import batchWarnings from './batchWarnings.js';
import relativeId from '../../../src/utils/relativeId.js';
import { handleError, stderr } from '../logging.js';
export default function loadConfigFile (configFile, silent) {
const warnings = batchWarnings();
return rollup.rollup({
entry: configFile,
external: id => {
return (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5,id.length) === '.json';
},
onwarn: warnings.add
})
.then( bundle => {
if ( !silent && warnings.count > 0 ) {
stderr( chalk.bold( `loaded ${relativeId( configFile )} with warnings` ) );
warnings.flush();
}
return bundle.generate({
format: 'cjs'
});
})
.then( ({ code }) => {
// temporarily override require
const defaultLoader = require.extensions[ '.js' ];
require.extensions[ '.js' ] = ( m, filename ) => {
if ( filename === configFile ) {
m._compile( code, filename );
} else {
defaultLoader( m, filename );
}
};
const configs = require( configFile );
if ( Object.keys( configs ).length === 0 ) {
handleError({
code: 'MISSING_CONFIG',
message: 'Config file must export an options object, or an array of options objects',
url: 'https://github.com/rollup/rollup/wiki/Command-Line-Interface#using-a-config-file'
});
}
require.extensions[ '.js' ] = defaultLoader;
return Array.isArray( configs ) ? configs : [configs];
});
}