Skip to content

Commit

Permalink
Handle loading symlinked config files (#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 17, 2020
1 parent c512305 commit 952c37f
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 10 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# rollup changelog

## 2.27.1
*2020-09-17*

### Bug Fixes
* Do not fail when using ES module imports in symlinked config files (#3783)

### Pull Requests
* [#3783](https://github.com/rollup/rollup/pull/3783): Handle loading symlinked config files (@lukastaegert)

## 2.27.0
*2020-09-16*

Expand Down
8 changes: 4 additions & 4 deletions cli/run/getConfigPath.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { readdirSync, realpathSync } from 'fs';
import { readdirSync } 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 {
export function getConfigPath(commandConfig: string | true): string {
const cwd = process.cwd();
if (commandConfig === true) {
return path.join(cwd, findConfigFileNameInCwd());
return path.resolve(findConfigFileNameInCwd());
}
if (commandConfig.slice(0, 5) === 'node:') {
const pkgName = commandConfig.slice(5);
Expand All @@ -28,7 +28,7 @@ export function getConfigPath(commandConfig: any): string {
}
}
}
return realpathSync(commandConfig);
return path.resolve(commandConfig);
}

function findConfigFileNameInCwd(): string {
Expand Down
14 changes: 8 additions & 6 deletions cli/run/loadConfigFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bold } from 'colorette';
import * as fs from 'fs';
import * as path from 'path';
import { pathToFileURL } from 'url';
import * as rollup from '../../src/node-entry';
Expand Down Expand Up @@ -82,16 +83,17 @@ async function getDefaultFromTranspiledConfigFile(
}

async function loadConfigFromBundledFile(fileName: string, bundledCode: string) {
const extension = path.extname(fileName);
const resolvedFileName = fs.realpathSync(fileName);
const extension = path.extname(resolvedFileName);
const defaultLoader = require.extensions[extension]!;
require.extensions[extension] = (module: NodeModule, filename: string) => {
if (filename === fileName) {
(module as NodeModuleWithCompile)._compile(bundledCode, filename);
require.extensions[extension] = (module: NodeModule, requiredFileName: string) => {
if (requiredFileName === resolvedFileName) {
(module as NodeModuleWithCompile)._compile(bundledCode, requiredFileName);
} else {
defaultLoader(module, filename);
defaultLoader(module, requiredFileName);
}
};
delete require.cache[fileName];
delete require.cache[resolvedFileName];
try {
const config = getDefaultFromCjs(require(fileName));
require.extensions[extension] = defaultLoader;
Expand Down
5 changes: 5 additions & 0 deletions test/cli/samples/symlinked-config/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
description: 'loads a symlinked config file',
command: 'rollup -c',
execute: true
};
1 change: 1 addition & 0 deletions test/cli/samples/symlinked-config/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert.equal(ANSWER, 42);
1 change: 1 addition & 0 deletions test/cli/samples/symlinked-config/rollup.config.js
11 changes: 11 additions & 0 deletions test/cli/samples/symlinked-config/rollup.config.symlinked
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import replace from '@rollup/plugin-replace';

export default {
input: 'main.js',
output: {
format: 'cjs'
},
plugins: [
replace({ 'ANSWER': 42 })
]
};
5 changes: 5 additions & 0 deletions test/cli/samples/symlinked-named-config/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
description: 'loads a symlinked config file with the given name',
command: 'rollup --config my.rollup.config.js',
execute: true
};
1 change: 1 addition & 0 deletions test/cli/samples/symlinked-named-config/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert.equal(ANSWER, 42);
11 changes: 11 additions & 0 deletions test/cli/samples/symlinked-named-config/rollup.config.symlinked
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import replace from '@rollup/plugin-replace';

export default {
input: 'main.js',
output: {
format: 'cjs'
},
plugins: [
replace({ 'ANSWER': 42 })
]
};

0 comments on commit 952c37f

Please sign in to comment.