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

Add support for config.rollup.mjs/ESM configs #179

Merged
merged 1 commit into from
Feb 19, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@ if (!process.env.NOLLUP) {
}

let path = require('path');
const fs = require('fs');
const devServer = require('./dev-server')

// from rollup/cli/run/getConfigPath
function findConfigFileNameInCwd() {
const filesInWorkingDir = new Set(fs.readdirSync(process.cwd()));
for (const extension of ['mjs', 'cjs']) {
const fileName = `rollup.config.${extension}`;
if (filesInWorkingDir.has(fileName)) return fileName;
}
return `rollup.config.js`;
}

let options = {
config: path.normalize(process.cwd() + '/rollup.config.js'),
config: path.normalize(process.cwd() + '/' + findConfigFileNameInCwd()),
contentBase: './',
historyApiFallback: false,
hot: false,
Expand Down
40 changes: 35 additions & 5 deletions lib/impl/ConfigLoader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
// @ts-check
let nollup = require('../index');
let path = require('path');
let url = require('url');

module.exports = class ConfigLoader {
/**
* Uses compiler to compile rollup.config.js
* or dynamic import to directly load rollup.config.mjs
*
* @param {string} filepath
* @return {Promise<object>}
*/
static async load(filepath) {
let config = filepath.endsWith('.mjs') ?
await ConfigLoader.loadESM(filepath) :
await ConfigLoader.loadCJS(filepath);

// When function, resolve
return (typeof config === 'function')
? config()
: config;
}

/**
* Uses compiler to compile rollup.config.js file.
* This allows config file to use ESM, but compiles to CJS
Expand All @@ -11,7 +30,7 @@ module.exports = class ConfigLoader {
* @param {string} filepath
* @return {Promise<object>}
*/
static async load (filepath) {
static async loadCJS(filepath) {
// If it isn't relative, it's probably a NodeJS import
// so mark it as external so the require call persists.
let bundle = await nollup({
Expand Down Expand Up @@ -43,9 +62,20 @@ module.exports = class ConfigLoader {
config = config.default || config;
require.extensions['.js'] = defaultLoader;

// When function, resolve
return (typeof config === 'function')
? await config()
: config;
return config;
}

/**
* Directly imports rollup.config.mjs
*
* @param {string} filepath
* @return {Promise<object>}
*/
static async loadESM(filepath) {
if(!filepath.startsWith('/') && !filepath.startsWith('./') && !filepath.startsWith('file://')) {
// import needs a URL, not a path. (mainly for Windows)
filepath = url.pathToFileURL(filepath).toString();
}
return (await import(filepath)).default;
}
};