-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically discover .mjs and .cjs configs
- Loading branch information
1 parent
14716ad
commit 7908222
Showing
14 changed files
with
100 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { readdirSync, realpathSync } from 'fs'; | ||
import * as path from 'path'; | ||
import relative from 'require-relative'; | ||
import { handleError } from '../logging'; | ||
|
||
const DEFAULT_CONFIG_BASE = 'rollup.config'; | ||
|
||
export function getConfigPath(commandConfig: any): string { | ||
const cwd = process.cwd(); | ||
if (commandConfig === true) { | ||
return path.join(cwd, findConfigFileNameInCwd()); | ||
} | ||
if (commandConfig.slice(0, 5) === 'node:') { | ||
const pkgName = commandConfig.slice(5); | ||
try { | ||
return relative.resolve(`rollup-config-${pkgName}`, cwd); | ||
} catch (err) { | ||
try { | ||
return relative.resolve(pkgName, cwd); | ||
} catch (err) { | ||
if (err.code === 'MODULE_NOT_FOUND') { | ||
handleError({ | ||
code: 'MISSING_EXTERNAL_CONFIG', | ||
message: `Could not resolve config file "${commandConfig}"` | ||
}); | ||
} | ||
throw err; | ||
} | ||
} | ||
} | ||
return realpathSync(commandConfig); | ||
} | ||
|
||
function findConfigFileNameInCwd(): string { | ||
const filesInWorkingDir = new Set(readdirSync(process.cwd())); | ||
for (const extension of ['mjs', 'cjs']) { | ||
const fileName = `${DEFAULT_CONFIG_BASE}.${extension}`; | ||
if (filesInWorkingDir.has(fileName)) return fileName; | ||
} | ||
return `${DEFAULT_CONFIG_BASE}.js`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
module.exports = { | ||
description: 'does not transpile cjs configs and provides correct __dirname', | ||
// TODO Lukas add auto-discovery | ||
command: 'rollup --config rollup.config.cjs' | ||
command: 'rollup -c' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
console.log('nested'); | ||
console.log(''); | ||
console.log('nested/plugin.js'); | ||
console.log('plugin.js'); | ||
var main = 42; | ||
|
||
export default main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
module.exports = { | ||
description: 'supports native esm as well as CJS plugins when using .mjs in Node 13+', | ||
minNodeVersion: 13, | ||
// TODO Lukas add auto-discovery | ||
command: 'rollup --config rollup.config.mjs' | ||
command: 'rollup -c' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
console.log('nested/plugin.mjs'); | ||
console.log('plugin.mjs'); | ||
var main = 42; | ||
|
||
module.exports = main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { relative } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
export default () => ({ | ||
transform(code) { | ||
return `console.log('${relative(process.cwd(), fileURLToPath(import.meta.url))}');\n${code}`; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { relative } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
export default () => ({ | ||
transform(code) { | ||
return `console.log('${relative(process.cwd(), fileURLToPath(import.meta.url))}');\n${code}`; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
// This is an ESM plugin | ||
// And this is CJS | ||
// This is a CJS plugin | ||
import replace from '@rollup/plugin-replace/dist/rollup-plugin-replace.cjs.js'; | ||
// This is an ESM plugin | ||
import { shebang } from 'rollup-plugin-thatworks'; | ||
import nestedPlugin from './nested/plugin.mjs'; | ||
import plugin from './plugin.mjs'; | ||
|
||
export default { | ||
input: 'main.js', | ||
output: { format: 'cjs' }, | ||
plugins: [shebang(), replace({ ANSWER: 42 })] | ||
plugins: [shebang(), replace({ ANSWER: 42 }), plugin(), nestedPlugin()] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters