Skip to content

Commit

Permalink
fix: fix packaging issue involving graceful-fs
Browse files Browse the repository at this point in the history
- changes in `graceful-fs` generated errors in our side; by forcing usage
  of it on our side, all works fine
  • Loading branch information
nolde committed Mar 23, 2020
1 parent b19ec43 commit 2099e52
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 20 deletions.
65 changes: 53 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Serverless Browserifier Plugin

> A [Serverless](https://serverless.com) v1 plugin that uses [`browserify`][browserify-url] to bundle your Node.js Lambda functions.
1. [Motivation](#motivation)
1. [Installation](#installation)
1. [Basic Setup](#basic-setup)
1. [Advanced Configuration](#advanced-configuration)
1. [FAQ](#faq)
1. [Useful Information](#useful-information)
1. [License](#license)


Motivation
----------

Expand Down Expand Up @@ -41,6 +50,9 @@ From your target serverless project, run:
npm install serverless-plugin-browserifier --save-dev
```

Basic Setup
-----------

Add the plugin to your `serverless.yml`:

```yaml
Expand All @@ -52,11 +64,13 @@ package:
The `package.individually` setting must be set -- either on global or function level -- to allow minimal bundle size based on each lambda's entrypoint.

You're all set! Use your normal serverless commands to package and deploy.

Configuration
-------------

For most use cases you should **NOT** need to do any configuration. You can, however, introduce custom configuration.
Advanced Configuration
----------------------

For most use cases you should **NOT** need to do any extra configuration. That said, the ability is present if you need it.

The base config for browserify is read from the `custom.browserify` section of `serverless.yml`. All [browserify options][browserify-options] are supported (most are auto configured by this plugin). This plugin adds one special option `disable` which if `true` will bypass this plugin.

Expand Down Expand Up @@ -99,8 +113,7 @@ functions:
```


Usage
-----
### Debugging

When this plugin is enabled, and `package.individually` is `true`, running `serverless deploy` and `serverless deploy -f <funcName>` will automatically browserify your Node.js lambda code.

Expand All @@ -114,8 +127,7 @@ $ sls deploy function -v -f usersGet
You may also verify your bundles by simply using `sls package`, which bundles everything up but does not deploy.


Using browserify plugins/transforms
-----------------------------------
### Using browserify plugins/transforms

If you want to use browserify plugins, you can easily do that by using the global browserify options. As the plugin merely passes that up to browserify, as if it is calling the main [`browserify`][browserify-options] function, you can use it to add any transformations you want.

Expand Down Expand Up @@ -147,10 +159,9 @@ custom:
For an in-depth example, please check [this issue](https://github.com/digitalmaas/serverless-plugin-browserifier/issues/8).


Best practices
--------------
### Best practices

__If using it with AWS, use discrete SDK clients!__
#### If using it with AWS, use discrete SDK clients!

The official [aws-sdk-js][aws-sdk] officially [supports browserify][aws-sdk-support]. That allows us to further reduce the size of our bundles (and Lambda memory usage and speed) by loading only what is strictly needed.

Expand All @@ -163,7 +174,7 @@ const S3 = require('aws-sdk/clients/s3')
const s3 = new S3()
```

__Ignore AWS SDK!__
#### Ignore AWS SDK!

Although you can use discrete clients (see item above), AWS Lambda service always bundles up the latest SDK version in its Lambda container image. That means that, even if you don't add AWS SDK to your bundle, it will still be available in runtime.

Expand All @@ -177,6 +188,36 @@ custom:
- aws-sdk/clients/s3
```

To help you out, here's a script you can use to hide `aws-sdk` and all its clients from browserify. You can use it in your custom config for the plugin in _serverless.yml_:

```yml
# serverless.yml
custom:
browserify: browserify: ${file(./custom.browserify.js)}
```

```js
// custom.browserify.js
//
const fs = require('fs')
const path = require('path')
module.exports = function browserifyOptions () {
return {
// any other valid browserify configuration...
noParse: ['/**/*.json'],
exclude: ['aws-sdk', ...getAllAwsSdkClients()]
}
}
function getAllAwsSdkClients () {
return fs
.readdirSync('./node_modules/aws-sdk/clients', { withFileTypes: true })
.filter(file => file.isFile() && path.extname(file.name) === '.js')
.map(file => `aws-sdk/clients/${path.basename(file.name, '.js')}`)
}
```

FAQ
---
Expand All @@ -194,7 +235,7 @@ __Avoid mixing this plugin with other plugins that modify serverless' packaging
This plugin _hijacks_ the normal serverless packaging process, so it will probably conflict with other plugins that use similar mechanisms.


Useful information
Useful Information
------------------

- [List of browserify's transforms][useful-transforms-list]
Expand Down
5 changes: 3 additions & 2 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const Promise = require('bluebird')
const archiver = require('archiver')
const browserify = require('browserify')
const filesize = require('filesize')
const fs = require('fs-extra')
const globby = require('globby')
const path = require('path')
const semver = require('semver')

const fs = require('./fs')

/// //////////////////////// exports && main functions

module.exports = {
Expand Down Expand Up @@ -116,7 +117,7 @@ function zipIt (data) {
this.S.cli.log(`Browserifier: Zipping ${data.outputFolder} to ${data.outputBundle}...`)
}
const handleStream = (resolve, reject) => {
const output = fs.createWriteStream(data.outputBundle)
const output = fs.getNewFileStream(data.outputBundle)
const zip = archiver.create('zip', { zlib: { level: 9 } })
output.on('close', () => resolve(zip.pointer()))
zip.on('error', err => reject(err))
Expand Down
15 changes: 15 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

// fixes packaging issues

const fs = require('fs')
const fsx = require('fs-extra')

const graceful = require('graceful-fs')
graceful.gracefulify(fs)

module.exports = Object.assign(fsx, {
getNewFileStream (file) {
return graceful.createWriteStream(file)
}
})
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
"filesize": "^6.0.1",
"fs-extra": "^8.1.0",
"globby": "^11.0.0",
"graceful-fs": "^4.2.3",
"lodash": "^4.17.15",
"semver": "^7.1.2"
},
"peerDependencies": {
"browserify": ">=13.3.0"
},
"devDependencies": {
"@commitlint/cli": "8.3.5",
"@commitlint/config-conventional": "8.3.4",
Expand All @@ -38,6 +36,9 @@
"lint-staged": "10.0.7",
"semantic-release": "17.0.2"
},
"peerDependencies": {
"browserify": ">=13.3.0"
},
"engines": {
"node": ">=6"
},
Expand Down

0 comments on commit 2099e52

Please sign in to comment.