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

fix(run): use correct hook for reading options, handle facade chunks #292

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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