-
-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there an example for nextjs? #84
Comments
Hello, I've tried to configure esbuild with nextjs and got some success. Here's the config. const {
ESBuildPlugin,
// ESBuildMinifyPlugin
} = require('esbuild-loader');
const tsconfig = require('./tsconfig.json');
module.exports = {
webpack: (config, { webpack, dev }) => {
if (!dev) {
config.plugins.push(
new webpack.ProvidePlugin({
/**
* Not sure why, This increases bundle size
* when you would think the oposite would be true!
*/
// __jsx: ['react', 'createElement'],
// __fragment: ['react', 'Fragment'],
React: 'react',
}),
);
config.plugins.push(new ESBuildPlugin());
const convertToESBuild = (obj) => {
if (obj.loader === 'next-babel-loader') {
return {
loader: 'esbuild-loader',
options: {
loader: 'tsx',
target: 'es2017',
tsconfigRaw: tsconfig,
// jsxFactory: '__jsx',
// jsxFragment: '__fragment',
},
};
}
return obj;
};
const rule = config.module.rules[0];
if (rule) {
if (Array.isArray(rule.use)) {
rule.use = rule.use.map((e) => {
if (typeof e === 'object') {
return convertToESBuild(e);
}
return e;
});
} else {
rule.use = convertToESBuild(rule.use);
}
}
/**
* Not sure why, but ESBuildMinifyPlugin makes the bundle larger.
*
* With Default Minimization:
* Main Bundle: 239 KB
* Largest Page: 122 KB
*
* With ESBuild Minification:
* Main Bundle: 664 KB
* Largest Page: 132 KB
*
* Probably a configuration error.
*/
// // Remove Default TerserPlugin
// config.optimization.minimizer.shift();
// // Add ESBuild Minify
// config.optimization.minimizer.unshift(
// new ESBuildMinifyPlugin({
// target: 'es2017',
// minify: true,
// }),
// );
}
return config;
},
};
|
I would also love to see an example with Next! |
Tried out a config based on your attempt @kuasha420 - noticed some CSS issues, namely styles coming from styled components. We use a mix of styled components as well as SCSS modules. Have you noticed any styling issues when comparing the build output from esbuild-loader vs the standard config? |
I've also tested a similar config, but when built, |
I also experienced same behavior. My next step is to try swc and see if that's any different there. |
I don't have any experience with Next.js, but I can try taking a look if one of you wants to provide a minimal reproduction repo. Pure speculation but I also worry that replacing |
I've modified the Next.js starter slightly here to demo the issue. In dev, you'll see that |
Thanks @tdjsnelling I added a Next.js example here: https://github.com/privatenumber/esbuild-loader-examples/tree/master/examples/next Hope that helps. Closing this as I think it's addressed but will re-open if there's anything more to do. |
Thank you for sharing that! Do you have a confidence level for the stability of it? As in, do you think it’s safe to ship in production Next apps? |
Every Next.js app is configured and used differently so I can't speak to that. I don't know anything about your code, so even if I said I was confident, that won't mean much in the context of your app. You'll have to do your own due diligence of thorough testing to develop confidence. If you run into any issues with the recommended Next.js configuration, let me know and I can try to help. |
The example seems to not work anymore in Next.js 11. |
@hyoretsu You should skip the loader patch for |
To make it work with webpack 5, u can use replace next lines
I highly discourage anyone to use example above in real projects. You should find a better and more reliable way to maintain your webpack config. |
I can't make this work with Next 13
/* eslint-disable @typescript-eslint/no-var-requires */
const { EsbuildPlugin } = require("esbuild-loader");
function esbuildMinify(config, options) {
const { minimizer } = config.optimization;
const terserIndex = minimizer.findIndex((minifier) => minifier.constructor.name === "TerserPlugin");
minimizer.splice(terserIndex, 1, new EsbuildPlugin(options));
}
function esbuildLoader(config, options) {
const { rules } = config.module;
const rule = rules.find((rule) => rule.test?.test(".js"));
if (rule) {
rule.use = {
...rule.use,
loader: "esbuild-loader",
options,
};
}
}
const path = require("path");
const withPurgeCSSModules = require("next-purge-css-modules");
/**
* @type {import('next').NextConfig}
**/
const nextConfig = {
reactStrictMode: true,
compress: true,
images: {
unoptimized: true,
},
exportPathMap: function () {
return {
};
},
purgeCSSModules: {
content: path.join(__dirname, "src/**/*.{js,jsx,ts,tsx}"),
},
experimental: {
optimizeCss: true,
},
modularizeImports: {
"@mui/icons-material": {
transform: "@mui/icons-material/{{member}}",
},
},
webpack: (config, { webpack }) => {
config.plugins.push(
new webpack.ProvidePlugin({
React: "react",
})
);
esbuildMinify(config);
esbuildLoader(config, {
loader: "tsx",
target: "esnext",
});
Object.assign(config.resolve.alias, {
react: "preact/compat",
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
});
return config;
},
};
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
});
module.exports = () => {
return withBundleAnalyzer(withPurgeCSSModules(nextConfig));
}; |
This issue was closed 3 years ago. If you need help, please ask a question in Discussions or Next.js's repo. |
Hi!
I would love to try esbuild-loader with nextjs. Do you know any examples for a working configuration of nextjs with
esbuild-loader
?The text was updated successfully, but these errors were encountered: