···
import { terser } from 'rollup-plugin-terser';
import compiler from '@ampproject/rollup-plugin-closure-compiler';
10
+
import minifyBucklescript from './scripts/minify-bucklescript-plugin';
const cwd = process.cwd();
const pkgInfo = require('./package.json');
const name = basename(pkgInfo.main, '.js');
···
79
-
const unwrapStatePlugin = ({ types: t }) => ({
81
-
this.props = new Map();
82
-
this.test = (node) =>
83
-
/state$/i.test(node.id.name) ||
84
-
(node.init.properties.length === 1 && node.init.properties[0].key.name === 'contents');
87
-
VariableDeclarator(path) {
89
-
t.isIdentifier(path.node.id) &&
90
-
t.isObjectExpression(path.node.init) &&
91
-
path.node.init.properties.every(
92
-
(prop) => t.isObjectProperty(prop) && t.isIdentifier(prop.key)
94
-
this.test(path.node)
96
-
const id = path.node.id.name;
97
-
const properties = path.node.init.properties;
98
-
const propNames = new Set(properties.map((x) => x.key.name));
99
-
const decl = properties.map((prop) => {
100
-
const key = `${id}$${prop.key.name}`;
101
-
return t.variableDeclarator(t.identifier(key), prop.value);
104
-
this.props.set(id, propNames);
105
-
path.parentPath.replaceWithMultiple(t.variableDeclaration('let', decl));
108
-
MemberExpression(path) {
110
-
t.isIdentifier(path.node.object) &&
111
-
this.props.has(path.node.object.name) &&
112
-
t.isIdentifier(path.node.property) &&
113
-
this.props.get(path.node.object.name).has(path.node.property.name)
115
-
const id = path.node.object.name;
116
-
const propName = path.node.property.name;
117
-
path.replaceWith(t.identifier(`${id}$${propName}`));
123
-
const curryGuaranteePlugin = ({ types: t }) => {
124
-
const curryFnName = /^_(\d)$/;
125
-
const lengthId = t.identifier('length');
126
-
const bindId = t.identifier('bind');
130
-
CallExpression(path) {
132
-
!t.isMemberExpression(path.node.callee) ||
133
-
!t.isIdentifier(path.node.callee.object) ||
134
-
!t.isIdentifier(path.node.callee.property) ||
135
-
!path.node.callee.object.name === 'Curry' ||
136
-
!curryFnName.test(path.node.callee.property.name)
140
-
const callFn = path.node.arguments[0];
141
-
const callArgs = path.node.arguments.slice(1);
142
-
if (t.isExpressionStatement(path.parent)) {
143
-
path.replaceWith(t.callExpression(callFn, callArgs));
147
-
const arityLiteral = t.numericLiteral(callArgs.length);
148
-
const argIds = callArgs.map((init) => {
149
-
if (t.isIdentifier(init)) return init;
150
-
const id = path.scope.generateUidIdentifierBasedOnNode(path.node.id);
151
-
path.scope.push({ id, init });
156
-
t.conditionalExpression(
157
-
t.binaryExpression('===', t.memberExpression(callFn, lengthId), arityLiteral),
158
-
t.callExpression(callFn, argIds),
159
-
t.callExpression(t.memberExpression(callFn, bindId), [t.nullLiteral()].concat(argIds))
const makePlugins = (isProduction) =>
···
extensions: ['ts', 'tsx', 'js'],
exclude: 'node_modules/**',
216
-
plugins: ['babel-plugin-closure-elimination', unwrapStatePlugin, curryGuaranteePlugin],
130
+
plugins: ['babel-plugin-closure-elimination'],
132
+
minifyBucklescript(),
formatting: 'PRETTY_PRINT',
compilation_level: 'SIMPLE_OPTIMIZATIONS',