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

feat(run): use resolved input if file exists #336

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/run/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"devDependencies": {
"@rollup/plugin-typescript": "^3.0.0",
"@rollup/plugin-virtual": "^2.0.1",
"del": "^5.1.0",
"rollup": "^2.0.0",
"sinon": "8.0.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/run/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pkg from './package.json';
export default {
input: 'src/index.ts',
plugins: [typescript()],
external: Object.keys(pkg.dependencies).concat(['path', 'child_process']),
external: Object.keys(pkg.dependencies).concat(['path', 'child_process', 'fs']),
output: [
{ format: 'cjs', file: pkg.main },
{ format: 'esm', file: pkg.module }
Expand Down
9 changes: 8 additions & 1 deletion packages/run/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChildProcess, fork } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';

import { Plugin, RenderedChunk } from 'rollup';

Expand Down Expand Up @@ -31,7 +32,13 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
throw new Error(`@rollup/plugin-run only works with a single entry point`);
}

input = path.resolve(inputs[0]);
// eslint-disable-next-line prefer-destructuring
input = inputs[0];

const resolvedInputPath = path.resolve(input);
if (fs.existsSync(resolvedInputPath)) {
input = resolvedInputPath;
}
},

generateBundle(_outputOptions, _bundle, isWrite) {
Expand Down
16 changes: 16 additions & 0 deletions packages/run/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const sinon = require('sinon');

const run = require('../');

const virtual = require('../../virtual');

const cwd = join(__dirname, 'fixtures/');
const file = join(cwd, 'output/bundle.js');
const input = join(cwd, 'input.js');
Expand Down Expand Up @@ -55,6 +57,20 @@ test('takes input from the latest options', async (t) => {
t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {}));
});

test('uses resolved input path if it refers to an existing file', async (t) => {
const bundle = await rollup({
input: 'entry',
plugins: [
virtual({
entry: 'console.log("No real path example")'
}),
run()
]
});
await bundle.write(outputOptions);
t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {}));
});

test('checks entry point facade module', async (t) => {
const bundle = await rollup({
input: join(cwd, 'facade-entry/index.js'),
Expand Down
12 changes: 6 additions & 6 deletions packages/virtual/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/
import virtual from '@rollup/plugin-virtual';

export default {
entry: 'src/entry.js',
input: 'src/input.js',
// ...
plugins: [
virtual({
batman: `export default 'na na na na na'`,
'src/robin.js': `export default 'batmannnnn'`
batman: `export default 'na na na na na';`,
'src/robin.js': `export default 'batmannnnn';`
})
]
};
Expand All @@ -73,9 +73,9 @@ export default {
plugins: [
virtual({
entry: `
import batman from 'batcave';
console.log(batman);
`
import batman from 'batcave';
console.log(batman);
`
})
]
};
Expand Down
43 changes: 43 additions & 0 deletions packages/virtual/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
const PREFIX = `\0virtual:`;

export default function virtual(modules) {
const virtualInputIds = [];
const resolvedIds = new Map();

Object.keys(modules).forEach((id) => {
Expand All @@ -13,7 +14,49 @@ export default function virtual(modules) {
return {
name: 'virtual',

options(options) {
let updatedInput = options.input;

if (typeof updatedInput === 'string' && updatedInput in modules) {
const virtualId = PREFIX + updatedInput;
virtualInputIds.push(virtualId);
updatedInput = virtualId;
}

if (typeof updatedInput === 'object') {
if (Array.isArray(updatedInput)) {
updatedInput = updatedInput.map((id) => {
if (id in modules) {
const virtualId = PREFIX + id;
virtualInputIds.push(virtualId);
return virtualId;
}
return id;
});
} else {
updatedInput = Object.keys(updatedInput).reduce((nextUpdatedInput, key) => {
let id = key;
if (id in modules) {
const virtualId = PREFIX + id;
virtualInputIds.push(virtualId);
id = virtualId;
}
// eslint-disable-next-line no-param-reassign
nextUpdatedInput[id] = updatedInput[key];
return nextUpdatedInput;
}, {});
}
}

return {
...options,
input: updatedInput
};
},

resolveId(id, importer) {
if (virtualInputIds.includes(id)) return id;

if (id in modules) return PREFIX + id;

if (importer) {
Expand Down
32 changes: 32 additions & 0 deletions packages/virtual/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,35 @@ test('loads an absolute path from memory', (t) => {
t.is(resolved, `\0virtual:${path.resolve('src/foo.js')}`);
t.is(plugin.load(resolved), 'export default 42');
});

test('from memory input entry options are prefixed immediately', (t) => {
const plugin = virtual({
'foo.js': 'export default 42',
'src/foo.js': 'export default 42'
});

const singleInputOption = {
input: 'foo.js'
};

const multipleInputOption = {
input: ['foo.js', 'src/foo.js']
};

const mappedInputOption = {
input: {
'foo.js': 'lib/foo.js',
'src/foo.js': 'lib/index.js'
}
};

t.is(plugin.options(singleInputOption).input, '\0virtual:foo.js');
t.deepEqual(plugin.options(multipleInputOption).input, [
'\0virtual:foo.js',
'\0virtual:src/foo.js'
]);
t.deepEqual(plugin.options(mappedInputOption).input, {
'\0virtual:foo.js': 'lib/foo.js',
'\0virtual:src/foo.js': 'lib/index.js'
});
});
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.