Skip to content

Monkeypatching source code of other packages is fragile #278

@gaearon

Description

@gaearon

As a maintainer of babel-preset-react-app I am not very happy about these lines:

//
// Inject "babel-plugin-styled-components"
// -----------------------------------------------------------------------------
file = path.resolve('./node_modules/babel-preset-react-app/index.js');
text = fs.readFileSync(file, 'utf8');
if (!text.includes('babel-plugin-styled-components')) {
if (text.includes('const plugins = [')) {
text = text.replace(
'const plugins = [',
"const plugins = [\n require.resolve('babel-plugin-styled-components'),"); // prettier-ignore
fs.writeFileSync(file, text, 'utf8');
} else {
throw new Error(`Failed to inject babel-plugin-styled-components in ${file}.`); // prettier-ignore
}
}
//
// Inject "babel-plugin-transform-export-extensions"
// -----------------------------------------------------------------------------
file = path.resolve('./node_modules/babel-preset-react-app/index.js');
text = fs.readFileSync(file, 'utf8');
if (!text.includes('babel-plugin-transform-export-extensions')) {
if (text.includes('const plugins = [')) {
text = text.replace(
'const plugins = [',
"const plugins = [\n require.resolve('babel-plugin-transform-export-extensions'),"); // prettier-ignore
fs.writeFileSync(file, text, 'utf8');
} else {
throw new Error(`Failed to inject babel-plugin-transform-export-extensions in ${file}.`); // prettier-ignore
}
}

This is incredibly fragile, and is doing a disservice to all the people using this starter kit when we break this in the future.

It is quite possible that your users will be left with no good upgrade path. It is also very likely they are not experienced enough to figure out an alternative solution to this hack. In my opinion it’s irresponsible to rely on something like this without a clear disclaimer in the README that the project is built on a fragile foundation and can break with patch updates of the underlying tools.

I don’t have a great solution to this. Perhaps you could fork react-scripts or at least babel-preset-react-app? Even using Yarn resolutions to point to a fork would be less fragile than trying to monkeypatch the source depending on a variable name.

Thanks for consideration!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions