GYP File Format Parser

1.0.4 · active · verified Tue Apr 21

gyp-parser is a JavaScript library dedicated to parsing files written in the GYP (Generate Your Projects) format. GYP is a build system historically used by projects like Chromium and Node.js to generate native project files (e.g., Makefiles, Visual Studio projects). This package provides a robust, pure JavaScript implementation that converts GYP source strings into standard JavaScript objects. It serves as a secure and reliable alternative to using `eval()` for GYP content, adeptly handling complex GYP features such as implicit string concatenation, variable expansion, and dictionary merging that `eval()` might misinterpret or fail to process. The current stable version is 1.0.4, indicating a mature and specific tool that likely has an infrequent release cadence. Its key differentiator is its ability to parse the GYP format comprehensively without relying on `eval()` or performing any I/O operations, making it suitable for secure and isolated parsing tasks within applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing a complex GYP file string, including nested objects, arrays, variables, and actions, and then logging the resulting JavaScript object.

import { parse } from 'gyp-parser';

// Example GYP source with implicit string concatenation and a variable
const gypFileSource = `
  {
    'target_name': 'my_app',
    'type': 'executable',
    'variables': {
      'common_flags': [ '-Wall', '-std=c++17' ]
    },
    'sources': [
      'src/main.cc',
      'src/utils.cc'
    ],
    'defines': [
      'VERSION=1.0',
      'DEBUG=1'
    ],
    'configurations': {
      'Release': {
        'defines': [ 'NDEBUG=1' ]
      }
    },
    'actions': [
      {
        'action_name': 'generate_header',
        'inputs': [ 'templates/config.h.in' ],
        'outputs': [ '$$(RULE_DIR)/config.h' ],
        'action': ['python', 'generate_config.py', '<@(_inputs)', '>', '<@(_outputs)'],
      }
    ]
  }
`;

try {
  const parsedGyp = parse(gypFileSource);
  console.log('Successfully parsed GYP data:');
  console.log(JSON.stringify(parsedGyp, null, 2));

  // Accessing a specific property
  console.log(`\nTarget Name: ${parsedGyp.target_name}`);
  console.log(`Common Flags: ${parsedGyp.variables.common_flags.join(', ')}`);
} catch (error) {
  console.error('Failed to parse GYP data:', error.message);
}

view raw JSON →