Skip to content

Commit 2935989

Browse files
committed
wip - moving to dual output for x64 and arm64
1 parent a3c4a2a commit 2935989

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

webpack.config.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path');
22
const CopyPlugin = require('copy-webpack-plugin');
3-
const ZipPlugin = require('zip-webpack-plugin');
3+
const ZipPlugin = 'zip-webpack-plugin';
44

55
// Read the target architecture from the environment variable set in Dockerfile
66
const targetArch = process.env.TARGET_ARCH; // Should be 'x64' or 'arm64'
@@ -17,26 +17,32 @@ const nodeFileSuffix = `sharp-${targetPlatform}-${targetArch}.node`; // e.g., sh
1717
// Define the output path structure for Lambda layers
1818
const layerBasePath = 'nodejs/node_modules/sharp';
1919

20+
// Define the relative path that sharp's JS code uses to require the native module.
21+
// This is typically '../build/Release/...' relative to files in 'lib/'
22+
const sharpInternalRequirePath = `../build/Release/${nodeFileSuffix}`;
23+
2024
console.log(`Webpack config: Building for ${targetPlatform}-${targetArch}`);
2125
console.log(`Webpack config: Expecting native module: ${nodeFileSuffix}`);
26+
console.log(`Webpack config: Marking require path as external: ${sharpInternalRequirePath}`);
2227

2328
module.exports = {
2429
name: `layer-${targetArch}`, // Give the config a name incorporating the arch
2530
mode: 'production',
2631
stats: 'minimal',
27-
target: 'node',
32+
target: 'node', // Important: ensures Webpack uses Node.js style externals/requires
2833
watch: false,
2934
entry: {
3035
// Entry point for Sharp's main lib
31-
[`${layerBasePath}/index`]: './node_modules/sharp/lib/index.js', // Ensure .js is specified if needed
36+
[`${layerBasePath}/index`]: './node_modules/sharp/lib/index.js',
3237
},
3338
plugins: [
3439
new CopyPlugin({
3540
patterns: [
3641
{
3742
// Copy the compiled native module build ('Release' directory)
43+
// to the location expected by the runtime require
3844
from: `node_modules/sharp/build/Release/${nodeFileSuffix}`,
39-
to: `${layerBasePath}/build/Release/${nodeFileSuffix}`, // Keep the same structure
45+
to: `${layerBasePath}/build/Release/${nodeFileSuffix}`, // Keep the same structure relative to 'lib'
4046
},
4147
{
4248
from: 'node_modules/sharp/LICENSE',
@@ -60,7 +66,7 @@ module.exports = {
6066
filename: `sharp-layer-${targetArch}.zip`,
6167
// Specify the base directory for files inside the zip
6268
// This ensures the 'nodejs/...' structure is at the root of the zip
63-
pathPrefix: '', // Default might be fine, but explicitly empty can help
69+
pathPrefix: '',
6470
})
6571
],
6672
optimization: {
@@ -72,22 +78,25 @@ module.exports = {
7278
libraryTarget: 'commonjs2',
7379
},
7480
externals: {
75-
// *** CRITICAL: Define the native module as external relative to its expected location ***
76-
// When sharp requires './build/Release/sharp-linux-x64.node' at runtime,
77-
// webpack will resolve it to `require('./sharp-linux-x64.node')` within the bundle context.
78-
// We are providing this file via the CopyPlugin into the correct relative path.
79-
// --- This section might not be strictly needed if CopyPlugin handles the .node file correctly ---
80-
// Let's comment it out initially, as CopyPlugin places the file correctly relative to index.js
81-
// './build/Release/sharp-linux-x64.node': 'commonjs ./build/Release/sharp-linux-x64.node',
82-
// './build/Release/sharp-linux-arm64.node': 'commonjs ./build/Release/sharp-linux-arm64.node'
83-
84-
// Update: Simpler approach - let CopyPlugin place the .node file.
85-
// Sharp's internal require path is usually relative like `require('../build/Release/sharp-....node')`
86-
// from within `lib/index.js`. Ensure the CopyPlugin maintains this structure.
87-
// The current CopyPlugin path seems correct for this.
81+
// *** Tell Webpack NOT to bundle the .node file ***
82+
// Map the internal require path used by Sharp to a commonjs external type.
83+
// This leaves the require statement intact for the Node.js runtime.
84+
[sharpInternalRequirePath]: `commonjs ${sharpInternalRequirePath}`
8885
},
8986
// Ensure node module resolution works
9087
resolve: {
9188
modules: [path.resolve(__dirname, 'node_modules'), 'node_modules'],
9289
},
90+
// Add node-loader as an alternative way to handle .node files if externals doesn't work alone
91+
// module: {
92+
// rules: [
93+
// {
94+
// test: /\.node$/,
95+
// loader: 'node-loader',
96+
// options: {
97+
// name: '[path][name].[ext]', // Keep original path/name
98+
// },
99+
// },
100+
// ],
101+
// },
93102
};

0 commit comments

Comments
 (0)