UNPKG

6.68 kBMarkdownView Raw
1
2S3 Plugin
3===
4[![Travis Badge](https://travis-ci.org/MikaAK/s3-plugin-webpack.svg?branch=master)](https://travis-ci.org/MikaAK/s3-plugin-webpack)
5[![Code Climate](https://codeclimate.com/github/MikaAK/s3-plugin-webpack/badges/gpa.svg)](https://codeclimate.com/github/MikaAK/s3-plugin-webpack)
6
7This plugin will upload all built assets to s3
8
9
10### Install Instructions
11
12```bash
13$ npm i webpack-s3-plugin
14```
15Note: This plugin needs NodeJS > 0.12.0
16
17### Usage Instructions
18> I notice a lot of people are setting the directory option when the files are part of their build. Please don't set directory if your uploading your build. Using the directory option reads the files after compilation to upload instead of from the build process.
19
20> You can also use a [credentials file](https://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs) from AWS.
21
22##### Require `webpack-s3-plugin`
23```javascript
24var S3Plugin = require('webpack-s3-plugin')
25```
26
27##### With exclude
28```javascript
29var config = {
30 plugins: [
31 new S3Plugin({
32 // Exclude uploading of html
33 exclude: /.*\.html$/,
34 // s3Options are required
35 s3Options: {
36 accessKeyId: process.env.AWS_ACCESS_KEY_ID,
37 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
38 region: 'us-west-1'
39 },
40 s3UploadOptions: {
41 Bucket: 'MyBucket'
42 },
43 cdnizerOptions: {
44 defaultCDNBase: 'http://asdf.ca'
45 }
46 })
47 ]
48}
49```
50
51##### With include
52```javascript
53var config = {
54 plugins: [
55 new S3Plugin({
56 // Only upload css and js
57 include: /.*\.(css|js)/,
58 // s3Options are required
59 s3Options: {
60 accessKeyId: process.env.AWS_ACCESS_KEY_ID,
61 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
62 },
63 s3UploadOptions: {
64 Bucket: 'MyBucket'
65 }
66 })
67 ]
68}
69```
70
71##### Advanced `include` and `exclude rules`
72
73`include` and `exclude` rules behave similarly to Webpack's loader options. In addition to a RegExp you can pass a function which will be called with the path as its first argument. Returning a truthy value will match the rule. You can also pass an Array of rules, all of which must pass for the file to be included or excluded.
74
75```javascript
76import isGitIgnored from 'is-gitignored'
77
78// Up to you how to handle this
79var isPathOkToUpload = function(path) {
80 return require('my-projects-publishing-rules').checkFile(path)
81}
82
83var config = {
84 plugins: [
85 new S3Plugin({
86 // Only upload css and js and only the paths that our rules database allows
87 include: [
88 /.*\.(css|js)/,
89 function(path) { isPathOkToUpload(path) }
90 ],
91
92 // function to check if the path is gitignored
93 exclude: isGitIgnored,
94
95 // s3Options are required
96 s3Options: {
97 accessKeyId: process.env.AWS_ACCESS_KEY_ID,
98 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
99 },
100 s3UploadOptions: {
101 Bucket: 'MyBucket'
102 }
103 })
104 ]
105}
106```
107
108##### With basePathTransform
109```javascript
110import gitsha from 'gitsha'
111
112var addSha = function() {
113 return new Promise(function(resolve, reject) {
114 gitsha(__dirname, function(error, output) {
115 if(error)
116 reject(error)
117 else
118 // resolve to first 5 characters of sha
119 resolve(output.slice(0, 5))
120 })
121 })
122}
123
124var config = {
125 plugins: [
126 new S3Plugin({
127 s3Options: {
128 accessKeyId: process.env.AWS_ACCESS_KEY_ID,
129 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
130 },
131 s3UploadOptions: {
132 Bucket: 'MyBucket'
133 },
134 basePathTransform: addSha
135 })
136 ]
137}
138
139
140// Will output to /${mySha}/${fileName}
141```
142
143##### With CloudFront invalidation
144```javascript
145var config = {
146 plugins: [
147 new S3Plugin({
148 s3Options: {
149 accessKeyId: process.env.AWS_ACCESS_KEY_ID,
150 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
151 },
152 s3UploadOptions: {
153 Bucket: 'MyBucket'
154 },
155 cloudfrontInvalidateOptions: {
156 DistributionId: process.env.CLOUDFRONT_DISTRIBUTION_ID,
157 Items: ["/*"]
158 }
159 })
160 ]
161}
162```
163
164##### With Dynamic Upload Options
165```javascript
166var config = {
167 plugins: [
168 new S3Plugin({
169 s3Options: {
170 accessKeyId: process.env.AWS_ACCESS_KEY_ID,
171 secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
172 },
173 s3UploadOptions: {
174 Bucket: 'MyBucket',
175 ContentEncoding(fileName) {
176 if (/\.gz/.test(fileName))
177 return 'gzip'
178 },
179
180 ContentType(fileName) {
181 if (/\.js/.test(fileName))
182 return 'application/javascript'
183 else
184 return 'text/plain'
185 }
186 }
187 })
188 ]
189}
190```
191
192### Options
193
194- `exclude`: A Pattern to match for excluded content. Behaves similarly to webpack's loader configuration.
195- `include`: A Pattern to match for included content. Behaves the same as the `exclude`.
196- `s3Options`: Provide keys for upload extention of [s3Config](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property)
197- `s3UploadOptions`: Provide upload options [putObject](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property )
198- `basePath`: Provide the namespace where upload files on S3
199- `directory`: Provide a directory to upload (if not supplied will upload js/css from compilation)
200- `htmlFiles`: Html files to cdnize (defaults to all in output directory)
201- `cdnizerCss`: Config for css cdnizer check below
202- `noCdnizer`: Disable cdnizer (defaults true if no cdnizerOptions passed)
203- `cdnizerOptions`: options to pass to [cdnizer](https://www.npmjs.com/package/cdnizer)
204- `basePathTransform`: transform the base path to add a folder name. Can return a promise or a string
205- `progress`: Enable progress bar (defaults true)
206
207### Contributing
208All contributions are welcome. Please make a pull request and make sure things still pass after running `npm run test`
209For tests you will need to either have the environment variables set or setup a .env file. There's a .env.sample so you can `cp .env.sample .env` and fill it in. Make sure to add any new environment variables.
210
211#### Commands to be aware of
212###### *WARNING*: The test suit generates random files for certain checks. Ensure you delete files leftover on your Bucket.
213- `npm run test` - Run test suit (You must have the .env file setup)
214- `npm run build` - Run build
215
216### Thanks
217
218- Thanks to [@Omer](https://github.com/Omer) for fixing credentials from `~/.aws/credentials`
219- Thanks to [@lostjimmy](https://github.com/lostjimmy) for pointing out `path.sep` for Windows compatibility