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.
Post install script to make node_modules IE compatible again
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`;
})
.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' })),
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('./');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment