Skip to content

Commit

Permalink
Merge pull request #179 from adamburgess/mjs-config
Browse files Browse the repository at this point in the history
Add support for config.rollup.mjs/ESM configs
  • Loading branch information
PepsRyuu authored Feb 19, 2021
2 parents d074d1b + 9bdb6eb commit 900cb71
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
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;
}
};

0 comments on commit 900cb71

Please sign in to comment.