Skip to content

Commit

Permalink
Upgrade dependencies and reduce bundle size (#83)
Browse files Browse the repository at this point in the history
* Upgrade packages

* Replace makeTrampoline with fromArray in takeLast

* Remove makeTrampoline helper

* Replace Curry helpers with transpilation step

* Update yarn.lock

* Fix bind naming in Babel transform

* Fix curry arguments being pushed to parent scope instead of current

* Upgrade to upcoming bs-platform version

* Prevent transformer to create local variable if that's unnecessary
  • Loading branch information
kitten authored May 1, 2020
1 parent 1a51315 commit 1102b36
Show file tree
Hide file tree
Showing 6 changed files with 5,453 additions and 4,417 deletions.
64 changes: 31 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,53 +65,51 @@
"license": "MIT",
"dependencies": {},
"devDependencies": {
"@ampproject/rollup-plugin-closure-compiler": "^0.18.1",
"@babel/core": "^7.7.7",
"@babel/plugin-syntax-typescript": "^7.7.4",
"@babel/plugin-transform-modules-commonjs": "^7.7.5",
"@babel/preset-env": "^7.7.7",
"@glennsl/bs-jest": "^0.4.9",
"@rollup/plugin-buble": "^0.21.0",
"@rollup/plugin-commonjs": "^11.0.1",
"@rollup/plugin-node-resolve": "^6.1.0",
"@types/jest": "^24.0.25",
"@ampproject/rollup-plugin-closure-compiler": "^0.25.2",
"@babel/core": "^7.9.6",
"@babel/plugin-syntax-typescript": "^7.8.3",
"@babel/plugin-transform-modules-commonjs": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@glennsl/bs-jest": "^0.5.1",
"@rollup/plugin-buble": "^0.21.3",
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
"@types/jest": "^25.2.1",
"@types/zen-observable": "^0.8.0",
"babel-plugin-closure-elimination": "^1.3.0",
"bs-platform": "7.0.2-dev.1",
"babel-plugin-closure-elimination": "^1.3.1",
"bs-platform": "^7.3.1",
"callbag-from-iter": "^1.2.0",
"callbag-iterate": "^1.0.0",
"callbag-take": "^1.4.0",
"coveralls": "^3.0.9",
"flow-bin": "^0.115.0",
"callbag-take": "^1.5.0",
"coveralls": "^3.1.0",
"flow-bin": "^0.123.0",
"flowgen": "^1.10.0",
"gatsby": "^2.18.17",
"gatsby-plugin-netlify": "^2.1.30",
"gatsby-theme-docs-system": "^0.2.2",
"gentype": "^3.9.1",
"globby": "^10.0.1",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"gentype": "^3.21.0",
"globby": "^11.0.0",
"husky": "^4.2.5",
"lint-staged": "^10.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"rimraf": "^3.0.0",
"rollup": "^1.28.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-terser": "^5.1.3",
"rollup-plugin-typescript2": "^0.25.3",
"ts-jest": "^24.2.0",
"typescript": "^3.7.4",
"prettier": "^2.0.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"rimraf": "^3.0.2",
"rollup": "^2.7.6",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.27.0",
"ts-jest": "^25.4.0",
"typescript": "^3.8.3",
"zen-observable": "^0.8.15"
},
"lint-staged": {
"*.{d.ts,js}": [
"prettier --write",
"git add"
"prettier --write"
],
"*.{re,rei}": [
"bsrefmt --in-place",
"git add"
"bsrefmt --in-place"
]
},
"husky": {
Expand Down
122 changes: 82 additions & 40 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ const terserPretty = terser({
sequences: false,
loops: false,
conditionals: false,
join_vars: false
join_vars: false,
},
mangle: false,
output: {
beautify: true,
braces: true,
indent_level: 2
}
indent_level: 2,
},
});

const terserMinified = terser({
Expand All @@ -48,11 +48,11 @@ const terserMinified = terser({
compress: {
keep_infinity: true,
pure_getters: true,
passes: 10
passes: 10,
},
output: {
comments: false
}
comments: false,
},
});

const importAllPlugin = ({ types: t }) => ({
Expand All @@ -72,14 +72,14 @@ const importAllPlugin = ({ types: t }) => ({
)
);
}
}
}
},
},
});

const unwrapStatePlugin = ({ types: t }) => ({
pre() {
this.props = new Map();
this.test = node =>
this.test = (node) =>
/state$/i.test(node.id.name) ||
(node.init.properties.length === 1 && node.init.properties[0].key.name === 'contents');
},
Expand All @@ -89,14 +89,14 @@ const unwrapStatePlugin = ({ types: t }) => ({
t.isIdentifier(path.node.id) &&
t.isObjectExpression(path.node.init) &&
path.node.init.properties.every(
prop => t.isObjectProperty(prop) && t.isIdentifier(prop.key)
(prop) => t.isObjectProperty(prop) && t.isIdentifier(prop.key)
) &&
this.test(path.node)
) {
const id = path.node.id.name;
const properties = path.node.init.properties;
const propNames = new Set(properties.map(x => x.key.name));
const decl = properties.map(prop => {
const propNames = new Set(properties.map((x) => x.key.name));
const decl = properties.map((prop) => {
const key = `${id}$${prop.key.name}`;
return t.variableDeclarator(t.identifier(key), prop.value);
});
Expand All @@ -116,23 +116,66 @@ const unwrapStatePlugin = ({ types: t }) => ({
const propName = path.node.property.name;
path.replaceWith(t.identifier(`${id}$${propName}`));
}
}
}
},
},
});

const makePlugins = isProduction =>
const curryGuaranteePlugin = ({ types: t }) => {
const curryFnName = /^_(\d)$/;
const lengthId = t.identifier('length');
const bindId = t.identifier('bind');

return {
visitor: {
CallExpression(path) {
if (
!t.isMemberExpression(path.node.callee) ||
!t.isIdentifier(path.node.callee.object) ||
!t.isIdentifier(path.node.callee.property) ||
!path.node.callee.object.name === 'Curry' ||
!curryFnName.test(path.node.callee.property.name)
)
return;

const callFn = path.node.arguments[0];
const callArgs = path.node.arguments.slice(1);
if (t.isExpressionStatement(path.parent)) {
path.replaceWith(t.callExpression(callFn, callArgs));
return;
}

const arityLiteral = t.numericLiteral(callArgs.length);
const argIds = callArgs.map((init) => {
if (t.isIdentifier(init)) return init;
const id = path.scope.generateUidIdentifierBasedOnNode(path.node.id);
path.scope.push({ id, init });
return id;
});

path.replaceWith(
t.conditionalExpression(
t.binaryExpression('===', t.memberExpression(callFn, lengthId), arityLiteral),
t.callExpression(callFn, argIds),
t.callExpression(t.memberExpression(callFn, bindId), [t.nullLiteral()].concat(argIds))
)
);
},
},
};
};

const makePlugins = (isProduction) =>
[
babel({
babelrc: false,
extensions: ['ts', 'tsx', 'js'],
exclude: 'node_modules/**',
presets: [],
plugins: ['@babel/plugin-syntax-typescript', importAllPlugin]
plugins: ['@babel/plugin-syntax-typescript', importAllPlugin],
}),
typescript({
typescript: require('typescript'),
cacheRoot: './node_modules/.cache/.rts2_cache',
objectHashIgnoreUnknownHack: true,
useTsconfigDeclarationDir: true,
tsconfigOverride: {
compilerOptions: {
Expand All @@ -142,49 +185,50 @@ const makePlugins = isProduction =>
declarationDir: resolve(cwd, './dist/types/'),
target: 'esnext',
module: 'es2015',
rootDir: cwd
}
}
rootDir: cwd,
},
},
}),
commonjs({
ignoreGlobal: true,
include: ['*', '**'],
extensions: ['.js', '.ts', '.tsx']
extensions: ['.js', '.ts', '.tsx'],
}),
nodeResolve({
mainFields: ['module', 'jsnext', 'main'],
extensions: ['.js', '.ts', '.tsx'],
browser: true
browser: true,
}),
buble({
transforms: {
unicodeRegExp: false,
dangerousForOf: true,
dangerousTaggedTemplateString: true
dangerousTaggedTemplateString: true,
},
objectAssign: 'Object.assign',
exclude: 'node_modules/**'
exclude: 'node_modules/**',
}),
babel({
babelrc: false,
extensions: ['ts', 'tsx', 'js'],
exclude: 'node_modules/**',
presets: [],
plugins: ['babel-plugin-closure-elimination', unwrapStatePlugin]
plugins: ['babel-plugin-closure-elimination', unwrapStatePlugin, curryGuaranteePlugin],
}),
compiler({
compilation_level: 'SIMPLE_OPTIMIZATIONS'
formatting: 'PRETTY_PRINT',
compilation_level: 'SIMPLE_OPTIMIZATIONS',
}),
isProduction ? terserMinified : terserPretty
isProduction ? terserMinified : terserPretty,
].filter(Boolean);

const config = {
input: './src/Wonka.ts',
onwarn: () => {},
external: () => false,
treeshake: {
propertyReadSideEffects: false
}
propertyReadSideEffects: false,
},
};

export default [
Expand All @@ -197,16 +241,14 @@ export default [
freeze: false,
esModule: false,
file: `./dist/${name}.js`,
format: 'cjs'
format: 'cjs',
},
{
legacy: true,
freeze: false,
esModule: false,
compact: true,
file: `./dist/${name}.mjs`,
format: 'esm'
}
]
format: 'esm',
},
],
},
{
...config,
Expand All @@ -217,8 +259,8 @@ export default [
freeze: false,
esModule: false,
file: `./dist/${name}.min.js`,
format: 'cjs'
}
]
}
format: 'cjs',
},
],
},
];
36 changes: 23 additions & 13 deletions src/Wonka_operators.re
Original file line number Diff line number Diff line change
Expand Up @@ -967,31 +967,41 @@ let take = (max: int): operatorT('a, 'a) =>
})
);

type takeLastStateT('a) = {
mutable queue: Rebel.MutableQueue.t('a),
mutable talkback: (. talkbackT) => unit,
};

[@genType]
let takeLast = (max: int): operatorT('a, 'a) =>
curry(source =>
curry(sink => {
open Rebel;
let talkback = ref(talkbackPlaceholder);
let queue = MutableQueue.make();
let state: takeLastStateT('a) = {
queue: Rebel.MutableQueue.make(),
talkback: talkbackPlaceholder,
};

source((. signal) =>
switch (signal) {
| Start(tb) when max <= 0 =>
tb(. Close);
| Start(talkback) when max <= 0 =>
talkback(. Close);
Wonka_sources.empty(sink);
| Start(tb) =>
talkback := tb;
tb(. Pull);
| Start(talkback) =>
state.talkback = talkback;
talkback(. Pull);
| Push(x) =>
let size = MutableQueue.size(queue);
let size = Rebel.MutableQueue.size(state.queue);
if (size >= max && max > 0) {
ignore(MutableQueue.pop(queue));
ignore(Rebel.MutableQueue.pop(state.queue));
};

MutableQueue.add(queue, x);
talkback^(. Pull);
| End => makeTrampoline(sink, (.) => MutableQueue.pop(queue))
Rebel.MutableQueue.add(state.queue, x);
state.talkback(. Pull);
| End =>
Wonka_sources.fromArray(
Rebel.MutableQueue.toArray(state.queue),
sink,
)
}
);
})
Expand Down
Loading

0 comments on commit 1102b36

Please sign in to comment.