Skip to content

Commit

Permalink
Merge pull request #44 from ably-forks/tree-shaking
Browse files Browse the repository at this point in the history
Make library tree-shakable
  • Loading branch information
lawrence-forooghian authored Nov 27, 2023
2 parents b81c5ed + f733809 commit faaf677
Show file tree
Hide file tree
Showing 18 changed files with 1,364 additions and 981 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Check
on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Use Python 3.8
uses: actions/setup-python@v4
with:
python-version: '3.8'

- name: Use Node.js 8.x
uses: actions/setup-node@v4
with:
node-version: 8.x

- run: npm ci
- run: npm run grunt -- test:node
- run: npm run grunt -- test:browser:local
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
npm-debug.log
local.log
browserstack.err
test/build/

48 changes: 32 additions & 16 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-karma');

function cdnWebpackTarget({ minify }) {
return {
entry: './lib/vcdiff_decoder.js',
output: {
path: path.resolve(__dirname, 'dist'),
library: 'vcdiffDecoder',
filename: minify ? 'vcdiff-decoder.min.js' : 'vcdiff-decoder.js'
},
mode: minify ? 'production' : 'none', // Opts out of any default optimization options
}
}

grunt.initConfig({
bump: {
options: {
Expand All @@ -26,11 +38,6 @@ module.exports = function (grunt) {
},
webpack: {
options: {
entry: './lib/vcdiff_decoder.js',
output: {
path: path.resolve(__dirname, 'dist'),
library: 'vcdiffDecoder'
},
module: {
rules: [
{
Expand All @@ -57,17 +64,25 @@ module.exports = function (grunt) {
]
}
},
'vcdiff-decoder.js': {
mode: 'none', // Opts out of any default optimization options
'cdn': cdnWebpackTarget({ minify: false }),
'cdn-min': cdnWebpackTarget({ minify: true }),
'cjs': {
entry: './lib/vcdiff_decoder.js',
output: {
path: path.resolve(__dirname, 'build', 'cjs'),
libraryTarget: 'commonjs',
filename: 'vcdiff-decoder.js'
}
},
mode: 'none', // Opts out of any default optimization options
},
'vcdiff-decoder.min.js': {
mode: 'production',
'node-test-support': {
entry: './test/support/node/with-internal-exports.js',
output: {
filename: 'vcdiff-decoder.min.js'
}
path: path.resolve(__dirname, 'test', 'build', 'node'),
libraryTarget: 'commonjs',
filename: 'with-internal-exports.js'
},
mode: 'none',
}
},
karma: {
Expand Down Expand Up @@ -109,7 +124,7 @@ module.exports = function (grunt) {
},
local: {
reporters: ['progress', 'mocha'],
browsers: ['Firefox'],
browsers: ['FirefoxHeadless'],
},
remote: {
browserStack: {
Expand Down Expand Up @@ -148,13 +163,13 @@ module.exports = function (grunt) {
}
});

grunt.registerTask('build', 'webpack');
grunt.registerTask('build', ['webpack:cdn', 'webpack:cdn-min', 'webpack:cjs']);

grunt.registerTask('test:all', ['test', 'test:browser:remote']);

grunt.registerTask('test', ['test:node', 'test:browser:local']);

grunt.registerTask('test:node', 'mochaTest');
grunt.registerTask('test:node', ['webpack:node-test-support', 'mochaTest']);

grunt.registerTask('test:browser',
'Runs browser tests in given context. Run as "grunt test:browser:<context>", where <context> is "local" or "remote"',
Expand Down Expand Up @@ -240,7 +255,8 @@ module.exports = function (grunt) {
var generatedFiles = [
'package.json',
'dist/vcdiff-decoder.js',
'dist/vcdiff-decoder.min.js'
'dist/vcdiff-decoder.min.js',
'build/cjs/vcdiff-decoder.js',
];

var cmd = 'git add -A ' + generatedFiles.join(' ');
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ You can trigger a build using Webpack with:

npm run grunt -- build

which creates `vcdiff-decoder.js` and `vcdiff-decoder.min.js` in the `dist` folder.
which creates `vcdiff-decoder.js` and `vcdiff-decoder.min.js` in the `dist` folder, and `vcdiff-decoder.js` in the `build/cjs` folder.

### Testing

Expand All @@ -130,7 +130,7 @@ for which you will need to configure environment variables for `BROWSERSTACK_USE

On the `main` branch:

1. Increment the version, regenerate from source (a.k.a. build / bundle) and make a tagged commit which includes the built output from the `/dist` folder by running `npm run grunt -- release:patch` (or "major", "minor" or "prepatch" as appropriate - see [grunt-bump Usage Examples](https://github.com/vojtajina/grunt-bump#usage-examples))
1. Increment the version, regenerate from source (a.k.a. build / bundle) and make a tagged commit which includes the built output from the `/dist` and `/build` folders by running `npm run grunt -- release:patch` (or "major", "minor" or "prepatch" as appropriate - see [grunt-bump Usage Examples](https://github.com/vojtajina/grunt-bump#usage-examples))
2. Release the tagged commit to Github using `git push origin main --follow-tags`
3. Release to NPM using `npm publish . --access public` ([this package](https://www.npmjs.com/package/@ably/vcdiff-decoder) is configured to require that [2FA](https://docs.npmjs.com/configuring-two-factor-authentication) is used by publishers)
4. Release to Ably's CDN using `npm run grunt -- publish-cdn` (operable by Ably staff only)
Expand Down
32 changes: 16 additions & 16 deletions lib/address_caches/near.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict';

function NearCache(size) {
this.size = size;
this.near = new Array(this.size).fill(0);
this.nextSlot = 0;
}

NearCache.prototype.update = function(address) {
if (this.near.length > 0) {
this.near[this.nextSlot] = address;
this.nextSlot = (this.nextSlot + 1) % this.near.length;
export default class NearCache {
constructor(size) {
this.size = size;
this.near = new Array(this.size).fill(0);
this.nextSlot = 0;
}
};

NearCache.prototype.get = function(m, offset) {
let address = this.near[m] + offset;
return address;
};
update(address) {
if (this.near.length > 0) {
this.near[this.nextSlot] = address;
this.nextSlot = (this.nextSlot + 1) % this.near.length;
}
}

module.exports = NearCache;
get(m, offset) {
let address = this.near[m] + offset;
return address;
};
}
28 changes: 14 additions & 14 deletions lib/address_caches/same.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use strict';

function SameCache(size) {
this.size = size;
this.same = new Array(this.size * 256).fill(0);
}

SameCache.prototype.update = function(address) {
if (this.same.length > 0) {
this.same[address % (this.size * 256)] = address;
export default class SameCache {
constructor(size) {
this.size = size;
this.same = new Array(this.size * 256).fill(0);
}
};

SameCache.prototype.get = function(m, offset) {
let address = this.same[m * 256 + offset];
return address;
};
update(address) {
if (this.same.length > 0) {
this.same[address % (this.size * 256)] = address;
}
};

module.exports = SameCache;
get(m, offset) {
let address = this.same[m * 256 + offset];
return address;
};
}
11 changes: 4 additions & 7 deletions lib/deserialize/delta.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const errors = require('../errors');
const deserializeInteger = require('./integer');
const tokenizeInstructions = require('../tokenize_instructions');
import errors from '../errors';
import deserializeInteger from './integer';
import tokenizeInstructions from '../tokenize_instructions';

function delta(delta, position) {
export default function delta(delta, position) {

let targetWindowLength, dataLength, instructionsLength, addressesLength;

Expand Down Expand Up @@ -45,6 +45,3 @@ function delta(delta, position) {

return window;
}

module.exports = delta;

4 changes: 1 addition & 3 deletions lib/deserialize/integer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param position {Number}
* @returns {{position: {Number}, value: {Number}}}
*/
function integer(buffer, position) {
export default function integer(buffer, position) {
const result = { position, value: 0 };

do {
Expand All @@ -23,5 +23,3 @@ function integer(buffer, position) {

return result;
}

module.exports = integer;
2 changes: 1 addition & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ function CustomErrors(names) {
return errors;
}

module.exports = CustomErrors(['NotImplemented', 'InvalidDelta']);
export default /* @__PURE__ */ CustomErrors(['NotImplemented', 'InvalidDelta']);
Loading

0 comments on commit faaf677

Please sign in to comment.