|
1 | 1 | /**
|
2 |
| - * Copyright 2020 Google Inc. All rights reserved. |
| 2 | + * Copyright 2017 Google Inc. All rights reserved. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -// api.ts has to use module.exports as it's also consumed by DocLint which runs |
18 |
| -// on Node. |
19 |
| -// eslint-disable-next-line @typescript-eslint/no-var-requires |
20 |
| -const api = require('./api'); |
| 17 | +import { initializePuppeteer } from './initialize'; |
| 18 | +import * as path from 'path'; |
21 | 19 |
|
22 |
| -import { helper } from './common/helper'; |
23 |
| -import { Page } from './common/Page'; |
24 |
| -import { Puppeteer } from './common/Puppeteer'; |
| 20 | +const puppeteer = initializePuppeteer({ |
| 21 | + packageJson: require(path.join(__dirname, '..', 'package.json')), |
| 22 | + rootDirectory: path.join(__dirname, '..'), |
| 23 | +}); |
25 | 24 |
|
26 |
| -interface InitOptions { |
27 |
| - packageJson: { |
28 |
| - puppeteer: { |
29 |
| - chromium_revision: string; |
30 |
| - firefox_revision: string; |
31 |
| - }; |
32 |
| - name: string; |
33 |
| - }; |
34 |
| - rootDirectory: string; |
35 |
| -} |
36 |
| - |
37 |
| -export const initializePuppeteer = (options: InitOptions): Puppeteer => { |
38 |
| - const { packageJson, rootDirectory } = options; |
39 |
| - |
40 |
| - for (const className in api) { |
41 |
| - if (typeof api[className] === 'function') |
42 |
| - helper.installAsyncStackHooks(api[className]); |
43 |
| - } |
44 |
| - |
45 |
| - // Expose alias for deprecated method. |
46 |
| - // @ts-expect-error emulateMedia does not exist error |
47 |
| - Page.prototype.emulateMedia = Page.prototype.emulateMediaType; |
48 |
| - |
49 |
| - let preferredRevision = packageJson.puppeteer.chromium_revision; |
50 |
| - const isPuppeteerCore = packageJson.name === 'puppeteer-core'; |
51 |
| - // puppeteer-core ignores environment variables |
52 |
| - const product = isPuppeteerCore |
53 |
| - ? undefined |
54 |
| - : process.env.PUPPETEER_PRODUCT || |
55 |
| - process.env.npm_config_puppeteer_product || |
56 |
| - process.env.npm_package_config_puppeteer_product; |
57 |
| - if (!isPuppeteerCore && product === 'firefox') |
58 |
| - preferredRevision = packageJson.puppeteer.firefox_revision; |
59 |
| - |
60 |
| - const puppeteer = new Puppeteer( |
61 |
| - rootDirectory, |
62 |
| - preferredRevision, |
63 |
| - isPuppeteerCore, |
64 |
| - product |
65 |
| - ); |
66 |
| - |
67 |
| - // The introspection in `Helper.installAsyncStackHooks` references `Puppeteer._launcher` |
68 |
| - // before the Puppeteer ctor is called, such that an invalid Launcher is selected at import, |
69 |
| - // so we reset it. |
70 |
| - puppeteer._lazyLauncher = undefined; |
71 |
| - return puppeteer; |
72 |
| -}; |
| 25 | +/* |
| 26 | + * Has to be CJS here rather than ESM such that the output file ends with |
| 27 | + * module.exports = puppeteer. |
| 28 | + * |
| 29 | + * If this was export default puppeteer the output would be: |
| 30 | + * exports.default = puppeteer |
| 31 | + * And therefore consuming via require('puppeteer') would break / require the user |
| 32 | + * to access require('puppeteer').default; |
| 33 | + */ |
| 34 | +module.exports = puppeteer; |
0 commit comments