diff --git a/dev-apps/js-cloud-server/cloudflare-worker/webpack.config.js b/dev-apps/js-cloud-server/cloudflare-worker/webpack.config.js index d0a4edd31..8551bd932 100644 --- a/dev-apps/js-cloud-server/cloudflare-worker/webpack.config.js +++ b/dev-apps/js-cloud-server/cloudflare-worker/webpack.config.js @@ -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 }) diff --git a/dev-apps/nodejs-cloud/webpack.config.js b/dev-apps/nodejs-cloud/webpack.config.js index 2db5044a7..25f74ca61 100644 --- a/dev-apps/nodejs-cloud/webpack.config.js +++ b/dev-apps/nodejs-cloud/webpack.config.js @@ -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 }) diff --git a/dev-apps/nodejs-local/webpack.config.js b/dev-apps/nodejs-local/webpack.config.js index b3674e7c0..5307cde2a 100644 --- a/dev-apps/nodejs-local/webpack.config.js +++ b/dev-apps/nodejs-local/webpack.config.js @@ -1,5 +1,9 @@ const { composePlugins, withNx } = require('@nx/webpack') const path = require('path') +const { + configureExternals, + configureNodeResolve, +} = require('../../webpack-utils') module.exports = composePlugins(withNx(), (config, { options }) => { // turn on __dirname replacement so that the WASM file can be referenced by the assemblyscript lib @@ -24,15 +28,7 @@ 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'] + configureNodeResolve(config) // Don't use browser field for resolution if (!config.resolve.aliasFields) { config.resolve.aliasFields = [] @@ -41,25 +37,7 @@ module.exports = composePlugins(withNx(), (config, { options }) => { if (!config.resolve.modules) { config.resolve.modules = ['node_modules'] } - // Configure externals - use array format which is simpler and more reliable - if (options.external && Array.isArray(options.external)) { - // Convert to object format for webpack externals - const externalsObj = {} - options.external.forEach((pkg) => { - externalsObj[pkg] = `commonjs ${pkg}` - }) - const originalExternals = config.externals - if (originalExternals) { - if (Array.isArray(originalExternals)) { - config.externals = [...originalExternals, externalsObj] - } else if (typeof originalExternals === 'object') { - config.externals = { ...originalExternals, ...externalsObj } - } else { - config.externals = [originalExternals, externalsObj] - } - } else { - config.externals = externalsObj - } - } + + configureExternals(config, options) return config }) diff --git a/dev-apps/openfeature-nodejs/webpack.config.js b/dev-apps/openfeature-nodejs/webpack.config.js index 00b19d453..a87da7bf1 100644 --- a/dev-apps/openfeature-nodejs/webpack.config.js +++ b/dev-apps/openfeature-nodejs/webpack.config.js @@ -1,6 +1,10 @@ const { composePlugins, withNx } = require('@nx/webpack') const path = require('path') const webpack = require('webpack') +const { + configureExternals, + configureNodeResolve, +} = require('../../webpack-utils') module.exports = composePlugins(withNx(), (config, { options }) => { // turn on __dirname replacement so that the WASM file can be referenced by the assemblyscript lib @@ -24,15 +28,7 @@ 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'] + configureNodeResolve(config) // Don't use browser field for resolution if (!config.resolve.aliasFields) { config.resolve.aliasFields = [] @@ -41,18 +37,7 @@ module.exports = composePlugins(withNx(), (config, { options }) => { if (!config.resolve.modules) { config.resolve.modules = ['node_modules'] } - // 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}`, - })), - ] - } + + configureExternals(config, options) return config }) diff --git a/webpack-utils.js b/webpack-utils.js new file mode 100644 index 000000000..e5ae5b99d --- /dev/null +++ b/webpack-utils.js @@ -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. + * + * @param {Object} config - Webpack configuration object + * @param {Object} options - Nx build options + */ +function configureExternals(config, options) { + if (!options.external || !Array.isArray(options.external)) { + return + } + + 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] + } +} + +/** + * Configures resolve settings for Node.js targets. + * Sets up extension aliases, main fields, and condition names. + * + * @param {Object} config - Webpack configuration object + */ +function configureNodeResolve(config) { + if (!config.resolve) { + config.resolve = {} + } + config.resolve.extensionAlias = { + '.js': ['.ts', '.js'], + } + config.resolve.mainFields = ['main', 'module'] + config.resolve.conditionNames = ['node', 'require', 'import'] +} + +module.exports = { + configureExternals, + configureNodeResolve, +}