Skip to content

Instantly share code, notes, and snippets.

@avosalmon
Last active July 9, 2022 18:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save avosalmon/ebb98f7303cd842050b2583265ff1aea to your computer and use it in GitHub Desktop.
Save avosalmon/ebb98f7303cd842050b2583265ff1aea to your computer and use it in GitHub Desktop.
Upload assets to S3 using Laravel Mix
const mix = require('laravel-mix');
const s3Plugin = require('webpack-s3-plugin');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
let webpackPlugins = [];
if (mix.inProduction() && process.env.UPLOAD_S3) {
webpackPlugins = [
new s3Plugin({
include: /.*\.(css|js)$/,
s3Options: {
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_SECRET,
region: 'ap-northeast-1',
},
s3UploadOptions: {
Bucket: process.env.ASSETS_S3_BUCKET,
CacheControl: 'public, max-age=31536000'
},
basePath: 'app',
directory: 'public'
})
]
}
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
mix.webpackConfig({
plugins: webpackPlugins
});
if (mix.inProduction()) {
mix.version();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment