Skip to content

Commit

Permalink
fix(run): cover missing cases (#292)
Browse files Browse the repository at this point in the history
Ref #241 (comment)

- take input from latest options when build is started
- check facade id instead of modules list
  • Loading branch information
TrySound committed Apr 1, 2020
1 parent 5bcc533 commit d7259f6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
22 changes: 6 additions & 16 deletions packages/run/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
return {
name: 'run',

options(options) {
buildStart(options) {
let inputs = options.input!;

if (typeof inputs === 'string') {
Expand All @@ -32,7 +32,6 @@ export default function run(opts: RollupRunOptions = {}): Plugin {
}

input = path.resolve(inputs[0]);
return options;
},

generateBundle(_outputOptions, _bundle, isWrite) {
Expand All @@ -43,23 +42,14 @@ export default function run(opts: RollupRunOptions = {}): Plugin {

writeBundle(outputOptions, bundle) {
const dir = outputOptions.dir || path.dirname(outputOptions.file!);

let dest: string | undefined;
for (const fileName of Object.keys(bundle)) {
const entryFileName = Object.keys(bundle).find((fileName) => {
const chunk = bundle[fileName] as RenderedChunk;
return chunk.isEntry && chunk.facadeModuleId === input;
});

// eslint-disable-next-line no-continue
if (!chunk.isEntry) continue;

if (chunk.isEntry && chunk.modules[input]) {
dest = path.join(dir, fileName);
break;
}
}

if (dest) {
if (entryFileName) {
if (proc) proc.kill();
proc = fork(dest, args, forkOptions);
proc = fork(path.join(dir, entryFileName), args, forkOptions);
} else {
this.error(`@rollup/plugin-run could not find output chunk`);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/run/test/fixtures/facade-entry/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import log from './library';

log(0);
6 changes: 6 additions & 0 deletions packages/run/test/fixtures/facade-entry/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import log from './library';

log(0);
(async () => {
await import('./dynamic');
})();
2 changes: 2 additions & 0 deletions packages/run/test/fixtures/facade-entry/library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const log = (value) => console.log(value);
export default log;
28 changes: 27 additions & 1 deletion packages/run/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ test('builds the bundle and forks a child process', async (t) => {
t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], {}));
});

test('takes input from the latest options', async (t) => {
const bundle = await rollup({
input: 'incorrect',
plugins: [
run(),
{
options(options) {
options.input = input;
return options;
}
}
]
});
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'),
plugins: [run()]
});
const outputDir = join(cwd, 'output');
await bundle.write({ dir: outputDir, format: 'cjs' });
t.true(mockChildProcess.calledWithExactly(join(outputDir, 'index.js'), [], {}));
});

test('allows pass-through options for child_process.fork', async (t) => {
const forkOptions = {
cwd,
Expand All @@ -48,7 +75,6 @@ test('allows pass-through options for child_process.fork', async (t) => {
input,
plugins: [run(forkOptions)]
});

await bundle.write(outputOptions);
t.true(mockChildProcess.calledWithExactly(outputOptions.file, [], forkOptions));
});
Expand Down

0 comments on commit d7259f6

Please sign in to comment.