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 2 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
22 changes: 13 additions & 9 deletions cli/run/loadConfigFile.ts
Expand Up @@ -39,14 +39,18 @@ export default function loadConfigFile(
})
.then(({ output: [{ code }] }: RollupOutput) => {
// temporarily override require
const defaultLoader = require.extensions['.js'];
require.extensions['.js'] = (module: NodeModule, filename: string) => {
if (filename === configFile) {
(module as NodeModuleWithCompile)._compile(code, filename);
} else {
defaultLoader(module, filename);
}
};
const extensions = ['.js','.mjs'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work nicely for '.js' and '.mjs' but not other extensions, and is slightly complicated. How about this: As we KNOW the name of the config file, how about using path.extname to extract the extension of the config file and just modify the loader for this extension?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes perfect sense

const defaultLoaders = extensions.map(e => {
const defaultLoader = require.extensions[e];
require.extensions[e] = (module: NodeModule, filename: string) => {
if (filename === configFile) {
(module as NodeModuleWithCompile)._compile(code, filename);
} else {
defaultLoader(module, filename);
}
};
return defaultLoader;
});

delete require.cache[configFile];

Expand All @@ -67,7 +71,7 @@ export default function loadConfigFile(
});
}

require.extensions['.js'] = defaultLoader;
extensions.forEach((extension,index) => require.extensions[extension] = defaultLoaders[index]);

return Array.isArray(configs) ? configs : [configs];
});
Expand Down
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 } )
]
};