Webpack S3 Plugin

1.2.0-rc.0 · active · verified Tue Apr 21

The `webpack-s3-plugin` is a specialized Webpack plugin designed to streamline the process of uploading compiled assets from a Webpack build directly to an Amazon S3 bucket. It is currently in a release candidate phase, indicated by version `1.2.0-rc.0`, suggesting ongoing development towards a stable 1.2.0 release. The plugin integrates deeply into the Webpack compilation lifecycle, allowing developers to define precise `include` or `exclude` rules, which can be regular expressions, functions, or arrays of rules, to control which assets are uploaded. It provides extensive configuration options for AWS S3, including credential management, region specification, and S3 object upload parameters like `ACL`. A key differentiator is its focus on handling assets directly from the build output, rather than reading from a local directory post-compilation, for efficiency. It requires `webpack` version 5 or higher as a peer dependency.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates configuring `webpack-s3-plugin` to upload bundled JavaScript and CSS files to a specified S3 bucket. It includes essential S3 authentication and region settings, shows how to define include/exclude rules, and overrides the default ACL to 'private' for enhanced security, leveraging environment variables for sensitive credentials.

import path from 'path';
import S3Plugin from 'webpack-s3-plugin';
import type { Configuration } from 'webpack';

const config: Configuration = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: 'https://my-bucket.s3.amazonaws.com/'
  },
  plugins: [
    new S3Plugin({
      // Only upload JavaScript and CSS files for the production build
      include: /\.(js|css)$/,
      // Exclude source maps to keep bucket cleaner (optional)
      exclude: /\.map$/,
      // s3Options are mandatory for AWS credentials and region
      s3Options: {
        accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? '',
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? '',
        region: process.env.AWS_REGION ?? 'us-east-1'
      },
      // s3UploadOptions define properties for uploaded objects
      s3UploadOptions: {
        Bucket: process.env.AWS_S3_BUCKET_NAME ?? 'my-webpack-assets-bucket',
        // Override default 'public-read' ACL if needed (e.g., for private assets)
        ACL: 'private'
      },
      // Optionally, configure cdnizer for asset URL rewriting
      cdnizerOptions: {
        defaultCDNBase: 'https://cdn.example.com'
      }
    })
  ]
};

export default config;

view raw JSON →