Skip to content

Commit

Permalink
support all config file extensions (.js,.mjs,...) (#3204)
Browse files Browse the repository at this point in the history
* feat: support *.mjs as config file extension

see #3189

* docs: add *.mjs cli example

* fix: overwrite loder for the extension of the actual config file
  • Loading branch information
arlac77 authored and lukastaegert committed Oct 31, 2019
1 parent 53fb6fe commit 9c85d2a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cli/run/loadConfigFile.ts
Expand Up @@ -39,8 +39,9 @@ export default function loadConfigFile(
})
.then(({ output: [{ code }] }: RollupOutput) => {
// temporarily override require
const defaultLoader = require.extensions['.js'];
require.extensions['.js'] = (module: NodeModule, filename: string) => {
const extension = path.extname(configFile);
const defaultLoader = require.extensions[extension];
require.extensions[extension] = (module: NodeModule, filename: string) => {
if (filename === configFile) {
(module as NodeModuleWithCompile)._compile(code, filename);
} else {
Expand All @@ -67,9 +68,9 @@ export default function loadConfigFile(
});
}

require.extensions['.js'] = defaultLoader;
require.extensions[extension] = defaultLoader;

return Array.isArray(configs) ? configs : [configs];
});
});
}
}
3 changes: 3 additions & 0 deletions docs/01-command-line-reference.md
Expand Up @@ -164,6 +164,9 @@ $ rollup --config

# alternatively, specify a custom config file location
$ rollup --config my.config.js

# .js and .mjs are supported
$ rollup --config my.config.mjs
```

You can also export a function that returns any of the above configuration formats. This function will be passed the current command line arguments so that you can dynamically adapt your configuration to respect e.g. [`--silent`](guide/en/#--silent). You can even define your own command line options if you prefix them with `config`:
Expand Down
5 changes: 5 additions & 0 deletions test/cli/samples/config-mjs/_config.js
@@ -0,0 +1,5 @@
module.exports = {
description: 'uses config file (.mjs)',
command: 'rollup --config rollup.config.mjs',
execute: true
};
1 change: 1 addition & 0 deletions test/cli/samples/config-mjs/main.js
@@ -0,0 +1 @@
assert.equal( ANSWER, 42 );
11 changes: 11 additions & 0 deletions test/cli/samples/config-mjs/rollup.config.mjs
@@ -0,0 +1,11 @@
import replace from 'rollup-plugin-replace';

export default {
input: 'main.js',
output: {
format: 'cjs'
},
plugins: [
replace( { 'ANSWER': 42 } )
]
};

0 comments on commit 9c85d2a

Please sign in to comment.