
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
babel-plugin-optimize-helpers
Advanced tools
A Babel plugin that replaces known Babel modular runtime helpers with `require()` calls to a single runtime library.
A Babel plugin that replaces known Babel modular runtime helpers with require()
calls to a single runtime library.
Many packages in NPM are vended as already transpiled code that either already contain these helper functions or require()
them from different runtime libraries (e.g. @babel/{runtime|runtime-corejs2}/helpers/[esm/]
). This plugin will transform all of these references to require()
from @babel/runtime-corejs2/helpers/esm/
(the one that the default Next.js configuration for @babel/plugin-transform-runtime
uses). That way, there should only be a one, canonical copy of these helper functions which can be included in your "common" bundle.
In your webpack config, add a module rule that loads your script with a babel-loader with this plugin.
module.exports = {
// ...
module: {
rules: [
// ...
{
test: /\.(js|mjs|jsx)$/,
exclude: /[\\/]node_modules[\\/]@babel[\\/]runtime-corejs2[\\/]/,
use: {
loader: 'babel-loader',
options: {
babelrc: false,
plugins: [
require.resolve('@babel/plugin-syntax-dynamic-import'),
require.resolve('@babel/plugin-syntax-class-properties'),
require.resolve('@babel/plugin-syntax-jsx'),
// Add other syntax plugins here.
require('./babel-plugin-optimize-helpers')
]
}
}
}
]
}
};
Important Exclude @babel/runtime-corejs2/
from this rule - this would rewrite references to helpers in this library to require()
itself and result in circular references.
If you're using Next.js, you can use the provided next-plugin:
const withOptimizeHelpers = require('babel-plugin-optimize-helpers/next-plugin');
module.exports = withOptimizeHelpers({
/* ... */
});
Alternatively, modify the webpack config in next.config.json (e.g. if you need other Babel syntax plugins):
module.exports = {
webpack(config, { dev, isServer }) {
const optimize = !dev && !isServer;
// Only optimize browser code.
if (optimize && config.optimization.splitChunks) {
// Include `@babel/runtime-corejs2/` in the commons bundle.
config.optimization.splitChunks.cacheGroups['corejs'] = {
name: 'commons',
chunks: 'all',
test: /[\\/]node_modules[\\/]@babel[\\/]runtime-corejs2[\\/]/
};
// Load script with the plugin.
config.module.rules.push({
test: /\.(js|mjs|jsx)$/,
exclude: /[\\/]node_modules[\\/]@babel[\\/]runtime-corejs2[\\/]/,
use: {
loader: 'babel-loader',
options: {
babelrc: false,
plugins: [
require.resolve('@babel/plugin-syntax-dynamic-import'),
require.resolve('@babel/plugin-syntax-class-properties'),
require.resolve('@babel/plugin-syntax-jsx'),
// Add other syntax plugins here.
require('./babel-plugin-optimize-helpers')
]
}
}
});
}
return config;
}
};
In an ideal world, libraries will publish a copy of un-transpiled source code and leave transpiling to the app developer (who can decide what the transpile target should be and what helpers to bundle). Unfortunately this is not the case today. This plugin should be considered as short-term gap solution.
FAQs
A Babel plugin that replaces known Babel modular runtime helpers with `require()` calls to a single runtime library.
The npm package babel-plugin-optimize-helpers receives a total of 0 weekly downloads. As such, babel-plugin-optimize-helpers popularity was classified as not popular.
We found that babel-plugin-optimize-helpers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.