Skip to content

Commit 219f74a

Browse files
committed
wip - moving to dual output for x64 and arm64
1 parent 7e50784 commit 219f74a

File tree

1 file changed

+60
-32
lines changed

1 file changed

+60
-32
lines changed

webpack.config.js

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,105 @@ const path = require('path');
22
const CopyPlugin = require('copy-webpack-plugin');
33
const ZipPlugin = require('zip-webpack-plugin');
44

5+
// Read the target architecture from the environment variable set in Dockerfile
56
const targetArch = process.env.TARGET_ARCH; // Should be 'x64' or 'arm64'
67
if (!targetArch) {
78
throw new Error("TARGET_ARCH environment variable not set!");
89
}
910

10-
const targetPlatform = 'linux';
11+
// Determine the correct platform (should always be linux in this build environment)
12+
const targetPlatform = 'linux'; // We are building in a linux container
1113

12-
// --- START: Adjust filename based on arch ---
1314
// Determine the specific suffix used in the .node filename
1415
let nodeArchSuffix = targetArch; // Default to 'x64' or 'arm64'
1516
if (targetArch === 'arm64') {
1617
nodeArchSuffix = 'arm64v8'; // Use the suffix found in the logs for arm64
1718
}
1819
// Construct the final .node filename
1920
const nodeFileSuffix = `sharp-${targetPlatform}-${nodeArchSuffix}.node`;
20-
// --- END: Adjust filename based on arch ---
21-
2221

22+
// Define the output path structure for Lambda layers
2323
const layerBasePath = 'nodejs/node_modules/sharp';
24-
// This path used internally by Sharp's JS code must match the actual .node file
24+
25+
// Define the relative path that sharp's JS code uses to require the native module.
26+
// This is typically '../build/Release/...' relative to files in 'lib/'
2527
const sharpInternalRequirePath = `../build/Release/${nodeFileSuffix}`;
2628

2729
console.log(`Webpack config: Building for ${targetPlatform}-${targetArch}`);
28-
// This log will now show 'sharp-linux-arm64v8.node' for arm builds
2930
console.log(`Webpack config: Expecting native module: ${nodeFileSuffix}`);
3031
console.log(`Webpack config: Marking require path as external: ${sharpInternalRequirePath}`);
3132

3233
module.exports = {
33-
name: `layer-${targetArch}`,
34+
name: `layer-${targetArch}`, // Give the config a name incorporating the arch
3435
mode: 'production',
3536
stats: 'minimal',
36-
target: 'node',
37+
target: 'node', // Important: ensures Webpack uses Node.js style externals/requires
3738
watch: false,
3839
entry: {
40+
// Entry point for Sharp's main lib
3941
[`${layerBasePath}/index`]: './node_modules/sharp/lib/index.js',
4042
},
4143
plugins: [
42-
new CopyPlugin({
43-
patterns: [
44-
{
45-
// This 'from' pattern now uses the correct filename for arm64v8
46-
from: `node_modules/sharp/build/Release/${nodeFileSuffix}`,
47-
to: `${layerBasePath}/build/Release/${nodeFileSuffix}`, // Copy to the same relative path
48-
},
49-
// Other patterns remain unchanged
50-
{ from: 'node_modules/sharp/LICENSE', to: layerBasePath },
51-
{ from: `node_modules/sharp/vendor`, to: `${layerBasePath}/vendor` },
52-
{ from: `node_modules/sharp/package.json`, to: layerBasePath },
53-
],
54-
}),
55-
new ZipPlugin({
56-
// Keep the output ZIP name user-friendly (arm64, not arm64v8)
57-
filename: `sharp-layer-${targetArch}.zip`,
58-
pathPrefix: '',
59-
})
44+
new CopyPlugin({
45+
patterns: [
46+
{
47+
// Copy the compiled native module build ('Release' directory)
48+
// to the location expected by the runtime require
49+
from: `node_modules/sharp/build/Release/${nodeFileSuffix}`,
50+
to: `${layerBasePath}/build/Release/${nodeFileSuffix}`, // Keep the same structure relative to 'lib'
51+
},
52+
{
53+
from: 'node_modules/sharp/LICENSE',
54+
to: layerBasePath,
55+
},
56+
{
57+
// Copy vendor libraries (like libvips) installed for the target arch
58+
from: `node_modules/sharp/vendor`,
59+
to: `${layerBasePath}/vendor`,
60+
},
61+
{
62+
// Copy Sharp's package.json - sometimes needed for runtime checks
63+
from: `node_modules/sharp/package.json`,
64+
to: layerBasePath,
65+
},
66+
// Add any other necessary files from sharp's node_modules directory
67+
],
68+
}),
69+
new ZipPlugin({
70+
// Create an architecture-specific filename
71+
filename: `sharp-layer-${targetArch}.zip`,
72+
// Specify the base directory for files inside the zip
73+
// This ensures the 'nodejs/...' structure is at the root of the zip
74+
pathPrefix: '',
75+
})
6076
],
6177
optimization: {
62-
minimize: false,
78+
minimize: false, // Lambda layers generally shouldn't minimize node_modules internals
6379
},
6480
output: {
65-
filename: '[name].js',
66-
path: path.resolve(__dirname, 'dist'),
81+
filename: '[name].js', // Output filename for the entry point JS
82+
path: path.resolve(__dirname, 'dist'), // Output directory for webpack build artifacts (including the zip)
6783
libraryTarget: 'commonjs2',
6884
},
6985
externals: {
70-
// The key here now correctly matches the require path for arm64v8
86+
// *** Tell Webpack NOT to bundle the .node file ***
87+
// Map the internal require path used by Sharp to a commonjs external type.
88+
// This leaves the require statement intact for the Node.js runtime.
7189
[sharpInternalRequirePath]: `commonjs ${sharpInternalRequirePath}`
7290
},
91+
// Ensure node module resolution works
7392
resolve: {
7493
modules: [path.resolve(__dirname, 'node_modules'), 'node_modules'],
7594
},
76-
// Keep node-loader removed for now, since x64 worked without it.
77-
// module: { rules: [ /* node-loader rule */ ] },
95+
module: {
96+
rules: [
97+
{
98+
test: /\.node$/,
99+
loader: 'node-loader',
100+
options: {
101+
name: '[path][name].[ext]', // Keep original path/name
102+
},
103+
},
104+
],
105+
},
78106
};

0 commit comments

Comments
 (0)