Skip to content

Instantly share code, notes, and snippets.

@smeijer
Last active November 7, 2019 14:46
Show Gist options
  • Save smeijer/d205b9a86537fbad6186781423165d5c to your computer and use it in GitHub Desktop.
Save smeijer/d205b9a86537fbad6186781423165d5c to your computer and use it in GitHub Desktop.

Revisions

  1. smeijer revised this gist Nov 7, 2019. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion post-install.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,10 @@ const getModules = projectDir =>

    return `${projectDir}/node_modules/${ns}/package.json`;
    })
    .flat()
    .reduce(
    (acc, path) => (Array.isArray(path) ? [...acc, ...path] : [...acc, path]),
    [],
    )
    .filter(path => fs.existsSync(path))
    .map(path => ({
    pkg: JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })),
  2. smeijer created this gist Nov 7, 2019.
    40 changes: 40 additions & 0 deletions post-install.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const fs = require('fs');

    const getModules = projectDir =>
    fs
    .readdirSync(`${projectDir}/node_modules`)
    .map(ns => {
    if (ns[0] === '@') {
    return fs
    .readdirSync(`${projectDir}/node_modules/${ns}`)
    .map(name => `${projectDir}/node_modules/${ns}/${name}/package.json`);
    }

    return `${projectDir}/node_modules/${ns}/package.json`;
    })
    .flat()
    .filter(path => fs.existsSync(path))
    .map(path => ({
    pkg: JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })),
    path,
    }));

    const fixPackages = projectDir => {
    const modules = getModules(projectDir);
    let count = 0;

    modules.forEach(({ path, pkg }) => {
    if (pkg.module && pkg.main && !pkg.browser) {
    pkg.browser = pkg.main;
    fs.writeFileSync(path, JSON.stringify(pkg, '', ' '), {
    encoding: 'utf8',
    });
    count++;
    }
    });

    console.log(`modified ${count} packages to prefer 'main' over 'module'`);
    };

    // the directory that holds your 'package.json'
    fixPackages('./');