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

Support Puppeteer out of the box #416

Merged
merged 14 commits into from
Aug 18, 2022
18 changes: 18 additions & 0 deletions examples/node-puppeteer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const puppeteer = require('puppeteer');

(async () => {
console.log("Starting Puppeteer");
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox'],
});
console.log("Creating Page");
const page = await browser.newPage();
console.log("Navigating to hackernews");
await page.goto('https://news.ycombinator.com', {
waitUntil: 'networkidle2',
});

await browser.close();
console.log("Hello from puppeteer");
})();
15 changes: 15 additions & 0 deletions examples/node-puppeteer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "puppeteer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"puppeteer": "^16.1.1"
}
}
31 changes: 21 additions & 10 deletions src/providers/node/mod.rs
Original file line number Diff line number Diff line change
@@ -64,10 +64,26 @@ impl Provider for NodeProvider {
fn get_build_plan(&self, app: &App, env: &Environment) -> Result<Option<BuildPlan>> {
// Setup
let mut setup = Phase::setup(Some(NodeProvider::get_nix_packages(app, env)?));
if NodeProvider::uses_canvas(app) {
if NodeProvider::uses_node_dependency(app, "canvas") {
setup.add_pkgs_libs(vec!["libuuid".to_string(), "libGL".to_string()]);
}

if NodeProvider::uses_node_dependency(app, "puppeteer") {
setup.add_apt_pkgs(vec![
"libnss3".to_string(),
"libatk1.0-0".to_string(),
"libatk-bridge2.0-0".to_string(),
"libcups2".to_string(),
"libgbm1".to_string(),
"libasound2".to_string(),
"libpangocairo-1.0-0".to_string(),
"libxss1".to_string(),
"libgtk-3-0".to_string(),
"libxshmfence1".to_string(),
"libglu1".to_string(),
]);
Comment on lines +72 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any docs or a comment that can be added explaining wtf these are all for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're random chromium things. I frankly couldn't tell you.

}

// Install
let mut install = Phase::install(Some(NodeProvider::get_install_command(app)));
install.add_cache_directory(NodeProvider::get_package_manager_cache_dir(app));
@@ -306,15 +322,10 @@ impl NodeProvider {
Ok(pkgs)
}

pub fn uses_canvas(app: &App) -> bool {
let package_json = app.read_file("package.json").unwrap_or_default();
let lock_json = app.read_file("package-lock.json").unwrap_or_default();
let yarn_lock = app.read_file("yarn.lock").unwrap_or_default();
let pnpm_yaml = app.read_file("pnpm-lock.yaml").unwrap_or_default();
package_json.contains("\"canvas\"")
|| lock_json.contains("/canvas/")
|| yarn_lock.contains("/canvas/")
|| pnpm_yaml.contains("/canvas/")
pub fn uses_node_dependency(app: &App, dependency: &str) -> bool {
NodeProvider::get_all_deps(app)
.unwrap_or_default()
.contains(dependency)
}

pub fn find_next_packages(app: &App) -> Result<Vec<String>> {
7 changes: 7 additions & 0 deletions tests/docker_run_tests.rs
Original file line number Diff line number Diff line change
@@ -372,6 +372,13 @@ fn test_pnpm_custom_version() {
assert!(output.contains("Hello from PNPM"));
}

#[test]
fn test_puppeteer() {
let name = simple_build("./examples/node-puppeteer");
let output = run_image(name, None);
assert!(output.contains("Hello from puppeteer"));
}

#[test]
fn test_csharp() {
let name = simple_build("./examples/csharp-cli");
64 changes: 64 additions & 0 deletions tests/snapshots/generate_plan_tests__node_puppeteer.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
source: tests/generate_plan_tests.rs
expression: plan
---
{
"nixpacksVersion": "[version]",
"buildImage": "[build_image]",
"variables": {
"CI": "true",
"NODE_ENV": "production",
"NPM_CONFIG_PRODUCTION": "false"
},
"phases": [
{
"name": "install",
"dependsOn": [
"setup"
],
"commands": [
"npm i"
],
"cacheDirectories": [
"/root/.npm"
]
},
{
"name": "setup",
"nixPackages": [
{
"name": "nodejs"
},
{
"name": "npm-8_x",
"overlay": "https://github.com/railwayapp/nix-npm-overlay/archive/main.tar.gz"
}
],
"aptPackages": [
"libnss3",
"libatk1.0-0",
"libatk-bridge2.0-0",
"libcups2",
"libgbm1",
"libasound2",
"libpangocairo-1.0-0",
"libxss1",
"libgtk-3-0",
"libxshmfence1",
"libglu1"
]
},
{
"name": "build",
"dependsOn": [
"install"
],
"cacheDirectories": [
"node_modules/.cache"
]
}
],
"startPhase": {
"cmd": "npm run start"
}
}