Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support all config file extensions (.js,.mjs,...) #3204

Merged
merged 3 commits into from Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 } )
]
};