Skip to content

Commit

Permalink
fix(testing): make Puppeteer an optional dependency (#5145)
Browse files Browse the repository at this point in the history
Currently it seems required to have Puppeteer installed to build a Stencil component. It seems that the compiler imports parts of the testing module to compile a project which causes "/testing/puppeteer/puppeteer-declarations.d.ts" to be loaded.

To solve the type dependency the easiest is to add a Rollup plugin that simply adds a "@ts-ignore" before the import. If Puppeteer is not installed, it will not throw any errors, if it is installed, all types are propagated correctly.

STENCIL-881
fixes #4526
  • Loading branch information
christian-bromann committed Dec 8, 2023
1 parent 3c57875 commit 43cf0dc
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scripts/bundles/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export async function testing(opts: BuildOptions) {
preferConst: true,
}),
prettyMinifyPlugin(opts, getBanner(opts, `Stencil Testing`, true)),
ignorePuppteerDependency(opts),
],
treeshake: {
moduleSideEffects: false,
Expand All @@ -130,3 +131,29 @@ async function copyTestingInternalDts(opts: BuildOptions, inputDir: string) {
},
});
}

/**
* To avoid having user to install puppeteer for building their app (even if they don't use e2e testing),
* we ignore the puppeteer dependency in the generated d.ts file.
* @param opts build options
* @returns void
*/
function ignorePuppteerDependency(opts: BuildOptions) {
return {
name: 'ignorePuppteerDependency',
async buildEnd() {
const typeFilePath = join(opts.output.testingDir, 'puppeteer', 'puppeteer-declarations.d.ts');
const updatedFileContent = (await fs.readFile(typeFilePath, 'utf8'))
.split('\n')
.reduce((lines, line) => {
if (line.endsWith(`from 'puppeteer';`)) {
lines.push('// @ts-ignore - avoid requiring puppeteer as dependency');
}
lines.push(line);
return lines;
}, [] as string[])
.join('\n');
await fs.writeFile(typeFilePath, updatedFileContent);
},
};
}

0 comments on commit 43cf0dc

Please sign in to comment.