Skip to content

Commit

Permalink
feat: remove unused library data from build
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo authored and andreimarinescu committed Sep 6, 2022
1 parent fea11cb commit b73e398
Show file tree
Hide file tree
Showing 17 changed files with 2,630 additions and 4,638 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn build-library
2 changes: 2 additions & 0 deletions LIBRARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,5 @@
| `XMLHttpRequest.send` | Send data via a web request | [Docs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send) |
| `XMLHttpRequest.setRequestHeader` | Set a request header | [Docs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader) |
| `XMLHttpRequest.XMLHttpRequest` | Communicate with a web server | [Docs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest) |
## `bind` calls
For each method listed above, Sandworm also intercepts `bind` calls. To allow `bind` calls with more than one argument, the `bind.args` permission is required. [Read more](README.md#bind-calls).
2,090 changes: 32 additions & 2,058 deletions cli/frontend/build/static/js/bundle.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions cli/frontend/src/libraries.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import webLibraryGenerator from './library/web';
import nodeLibraryGenerator from './library/node';
import webLibraryData from './library/web.json';
import nodeLibraryData from './library/node.json';
import { buildNodeLibraryFrom, buildWebLibraryFrom } from './library/builder';

const processLibrary = (library) =>
library
Expand All @@ -16,5 +17,5 @@ const processLibrary = (library) =>
}, [])
.sort((a, b) => a.name.localeCompare(b.name));

export const webLibrary = processLibrary(webLibraryGenerator());
export const nodeLibrary = processLibrary(nodeLibraryGenerator());
export const webLibrary = processLibrary(buildWebLibraryFrom(webLibraryData));
export const nodeLibrary = processLibrary(buildNodeLibraryFrom(nodeLibraryData));
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"test-web": "npx playwright test --config=tests/configs/playwright.config.js",
"test-node-ce": "yarn test-node-enforce && yarn test-node-capture",
"test-node-all": "yarn test-node-ce && yarn test-node-unit",
"test-all": "yarn test-node-all && yarn test-web"
"test-all": "yarn test-node-all && yarn test-web",
"build-library-md": "node scripts/build-library-md.js",
"build-library-min": "node scripts/build-library-min.js",
"build-library": "yarn build-library-md && yarn build-library-min",
"prepare": "husky install"
},
"bin": "cli/index.js",
"author": "Sandworm <dev@sandworm.dev>",
Expand All @@ -40,6 +44,7 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.3.0",
"husky": "^8.0.0",
"jest": "^27.5.1",
"jest-junit": "^14.0.0",
"playwright-watch": "^1.3.23",
Expand Down
14 changes: 10 additions & 4 deletions scripts/generate-library-md.js → scripts/build-library-md.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const {createWriteStream} = require('fs');
const path = require('path');

const webLibrary = require('../src/library/web');
const nodeLibrary = require('../src/library/node');
const webLibrary = require('../src/library/web.json');
const nodeLibrary = require('../src/library/node.json');
const {buildNodeLibraryFrom, buildWebLibraryFrom} = require('../src/library/builder');

const filePath = path.join(__dirname, '..', 'LIBRARY.md');
const stream = createWriteStream(filePath, {flags: 'w'});
Expand All @@ -20,9 +21,14 @@ const writeLib = (library) => {
stream.write('# Sandworm.JS Library\n');

stream.write('## Node\n');
writeLib(nodeLibrary());
writeLib(buildNodeLibraryFrom(nodeLibrary));

stream.write('## Web\n');
writeLib(webLibrary());
writeLib(buildWebLibraryFrom(webLibrary));

stream.write('## `bind` calls\n');
stream.write(
'For each method listed above, Sandworm also intercepts `bind` calls. To allow `bind` calls with more than one argument, the `bind.args` permission is required. [Read more](README.md#bind-calls).',
);

stream.end();
33 changes: 33 additions & 0 deletions scripts/build-library-min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const {writeFileSync} = require('fs');
const path = require('path');

const webLibrary = require('../src/library/web.json');
const nodeLibrary = require('../src/library/node.json');

const webMinLibraryPath = path.join(__dirname, '..', 'src', 'library', 'web.min.json');
const nodeMinLibraryPath = path.join(__dirname, '..', 'src', 'library', 'node.min.json');

const cleanMethod = (method) => ({
...method,
description: undefined,
url: undefined,
});

[
{lib: webLibrary, output: webMinLibraryPath},
{lib: nodeLibrary, output: nodeMinLibraryPath},
].forEach(({lib, output}) => {
const minimized = lib.map((family) => ({
...family,
constructorDescription: undefined,
constructorUrl: undefined,
methods: family.methods?.map(cleanMethod),
globalMethods: family.globalMethods?.map(cleanMethod),
globalPropertyMethods: family.globalPropertyMethods?.map(cleanMethod),
navigatorMethods: family.navigatorMethods?.map(cleanMethod),
documentMethods: family.documentMethods?.map(cleanMethod),
swrMethods: family.swrMethods?.map(cleanMethod),
globalConstructors: family.globalConstructors?.map(cleanMethod),
}));
writeFileSync(output, JSON.stringify(minimized));
});
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {currentStack} from './stack';
import {getSourceMap, getSourceMapFromSource} from './source';
import platform, {PLATFORMS} from './platform';
import webLibrary from './library/web';
import nodeLibrary from './library/node';
import webLibrary from './library/web.min.json';
import nodeLibrary from './library/node.min.json';
import logger from './logger';
import track, {setSkipTracking, setTrackingServer} from './track';
import {
Expand All @@ -13,6 +13,7 @@ import {
setPermissions,
} from './module';
import patch, {SandwormError, setIgnoreExtensions} from './patch';
import {buildNodeLibraryFrom, buildWebLibraryFrom} from './library/builder';

let initialized = false;
let ready = false;
Expand Down Expand Up @@ -92,9 +93,9 @@ const init = ({
let library = [];

if (currentPlatform === PLATFORMS.WEB) {
library = webLibrary();
library = buildWebLibraryFrom(webLibrary);
} else if (currentPlatform === PLATFORMS.NODE) {
library = nodeLibrary();
library = buildNodeLibraryFrom(nodeLibrary);
} else {
logger.debug('current platform is not supported');
}
Expand Down
5 changes: 4 additions & 1 deletion src/library/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@ const node = ({name, methods, globalMethod}) => {
};
};

module.exports = {web, node};
module.exports = {
buildNodeLibraryFrom: (lib) => lib.map((family) => node(family)),
buildWebLibraryFrom: (lib) => lib.reduce((acc, cur) => [...acc, ...web(cur)], []),
};
Loading

0 comments on commit b73e398

Please sign in to comment.