-
Notifications
You must be signed in to change notification settings - Fork 10
refactor: extract common webpack utilities into shared module #1202
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
base: feat-nx-migration
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,14 @@ | ||
| const { composePlugins, withNx } = require('@nx/webpack') | ||
| const { | ||
| configureExternals, | ||
| configureNodeResolve, | ||
| } = require('../../../webpack-utils') | ||
|
|
||
| module.exports = composePlugins(withNx(), (config, { options }) => { | ||
| config.node = { | ||
| __dirname: true, | ||
| } | ||
| if (!config.resolve) { | ||
| config.resolve = {} | ||
| } | ||
| // Disable browser field resolution for node targets | ||
| config.resolve.mainFields = ['main', 'module'] | ||
| config.resolve.conditionNames = ['node', 'require', 'import'] | ||
| // Configure externals - webpack externals function | ||
| if (options.external && Array.isArray(options.external)) { | ||
| config.externals = config.externals || [] | ||
| const externalArray = Array.isArray(config.externals) | ||
| ? config.externals | ||
| : [config.externals] | ||
| config.externals = [ | ||
| ...externalArray, | ||
| ...options.external.map((pkg) => ({ | ||
| [pkg]: `commonjs ${pkg}`, | ||
| })), | ||
| ] | ||
| } | ||
| configureNodeResolve(config) | ||
| configureExternals(config, options) | ||
| return config | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,11 @@ | ||
| const { composePlugins, withNx } = require('@nx/webpack') | ||
| const { | ||
| configureExternals, | ||
| configureNodeResolve, | ||
| } = require('../../webpack-utils') | ||
|
|
||
| module.exports = composePlugins(withNx(), (config, { options }) => { | ||
| if (!config.resolve) { | ||
| config.resolve = {} | ||
| } | ||
| config.resolve.extensionAlias = { | ||
| '.js': ['.ts', '.js'], | ||
| } | ||
| // Disable browser field resolution for node targets | ||
| config.resolve.mainFields = ['main', 'module'] | ||
| config.resolve.conditionNames = ['node', 'require', 'import'] | ||
| // Configure externals - webpack externals function | ||
| if (options.external && Array.isArray(options.external)) { | ||
| config.externals = config.externals || [] | ||
| const externalArray = Array.isArray(config.externals) | ||
| ? config.externals | ||
| : [config.externals] | ||
| config.externals = [ | ||
| ...externalArray, | ||
| ...options.external.map((pkg) => ({ | ||
| [pkg]: `commonjs ${pkg}`, | ||
| })), | ||
| ] | ||
| } | ||
| configureNodeResolve(config) | ||
| configureExternals(config, options) | ||
| return config | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||
| * Shared webpack configuration utilities | ||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||
| * Merges external packages from Nx options into webpack config.externals. | ||||||||||||||||||||||||||||||||||||||||||||||||
| * Handles array, object, and function formats for existing externals. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
| * Handles array, object, and function formats for existing externals. | |
| * Supports existing externals configured as arrays or other Webpack-supported types. |
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The configureExternals function now creates an array of separate objects for external packages (e.g., [{pkg1: 'commonjs pkg1'}, {pkg2: 'commonjs pkg2'}]), which differs from the nodejs-local original implementation that merged all packages into a single object (e.g., {pkg1: 'commonjs pkg1', pkg2: 'commonjs pkg2'}). While both formats are valid for webpack externals, this changes the behavior for nodejs-local. Consider consolidating the external packages into a single object to maintain backward compatibility with the original nodejs-local implementation, or verify that all builds work correctly with the new format.
| const externalPackages = options.external.map((pkg) => ({ | |
| [pkg]: `commonjs ${pkg}`, | |
| })) | |
| const originalExternals = config.externals | |
| if (!originalExternals) { | |
| config.externals = externalPackages | |
| } else if (Array.isArray(originalExternals)) { | |
| config.externals = [...originalExternals, ...externalPackages] | |
| } else { | |
| config.externals = [originalExternals, ...externalPackages] | |
| const externalPackages = options.external.reduce((acc, pkg) => { | |
| acc[pkg] = `commonjs ${pkg}` | |
| return acc | |
| }, {}) | |
| const originalExternals = config.externals | |
| if (!originalExternals) { | |
| config.externals = externalPackages | |
| } else if (Array.isArray(originalExternals)) { | |
| config.externals = [...originalExternals, externalPackages] | |
| } else { | |
| config.externals = [originalExternals, externalPackages] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cloudflare-worker config now includes extensionAlias configuration (
.js': ['.ts', '.js']) through configureNodeResolve, but the original implementation did not have this setting. This adds new module resolution behavior that wasn't previously present. While the PR description states the cloudflare-worker builds successfully, adding extension aliasing could potentially affect runtime behavior in the Cloudflare Worker environment. Verify that this addition is intentional and doesn't cause issues with how the worker resolves TypeScript imports at runtime.