Skip to content

Commit

Permalink
feat: reduce bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
rayan1810 committed Feb 20, 2024
1 parent cc95099 commit 1c7644c
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# XDE
.expo/
.yarn

# VSCode
.vscode/
Expand Down
4 changes: 4 additions & 0 deletions babel-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.yarn
node_modules
transform.js
index.js
145 changes: 72 additions & 73 deletions babel-plugin/out.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
'use strict';
"use strict";

Check failure on line 1 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"use·strict"` with `'use·strict'`

// index.js
var CONFIG = {
p: 'padding',
m: 'margin',
t: 'top',
b: 'bottom',
l: 'left',
r: 'right',
h: 'height',
w: 'width',
bg: 'backgroundColor',
c: 'color',
};
module.exports = function (babel) {
var fs = require("fs");

Check failure on line 4 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"fs"` with `'fs'`
var path = require("path");

Check failure on line 5 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"path"` with `'path'`
var CONFIG = {};
var { parse } = require("@babel/parser");

Check failure on line 7 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"@babel/parser"` with `'@babel/parser'`
var { default: traverse } = require("@babel/traverse");

Check failure on line 8 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"@babel/traverse"` with `'@babel/traverse'`
function ObjectExpressionASTtoJSObject(AstNode) {
let obj = {};
AstNode.properties.forEach((prop) => {
const propName = prop.key.type === "StringLiteral" ? prop.key.value : prop.key.name;

Check failure on line 12 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `·prop.key.type·===·"StringLiteral"` with `⏎······prop.key.type·===·'StringLiteral'`
if (prop.value.value !== void 0) {

Check warning on line 13 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Expected 'undefined' and instead saw 'void'
obj[propName] = prop.value.value;
}
});
return obj;
}
var filePath = path.join(process.cwd(), "rnu.config.ts");

Check failure on line 19 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"rnu.config.ts"` with `'rnu.config.ts'`
var fileContent = fs.readFileSync(filePath, "utf8");

Check failure on line 20 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"utf8"` with `'utf8'`
var configAST = parse(fileContent, {
sourceType: "module",

Check failure on line 22 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"module"` with `'module'`
plugins: ["typescript"]

Check failure on line 23 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"typescript"]` with `'typescript'],`
});
traverse(configAST, {
CallExpression(path2) {
if (path2.node.callee.name === "createConfig") {
if (ObjectExpressionASTtoJSObject(path2.node.arguments[0].expression)) {
CONFIG = ObjectExpressionASTtoJSObject(
path2.node.arguments[0].expression
);
}
}
}
});
module.exports = function(babel) {
const { types: t } = babel;
let importName = 'react-native-ustyle';
let importName = "react-native-ustyle";
let importedComponents = [];
let styleId = 0;
let Styles = [];
Expand All @@ -28,37 +48,32 @@ module.exports = function (babel) {
}
function checkIfStylesheetImportedAndImport(programPath) {
let importDeclaration = programPath.node.body.find(
(node) =>
node.type === 'ImportDeclaration' &&
node.source.value === 'react-native'
(node) => node.type === "ImportDeclaration" && node.source.value === "react-native"
);
if (importDeclaration) {
}
}
function attributesToObject(attributes) {
if (!Array.isArray(attributes)) {
throw new TypeError('Expected attributes to be an array');
throw new TypeError("Expected attributes to be an array");
}
const obj = {};
attributes.forEach((attribute) => {
const key = resolver(attribute.name.name);
let value;
if (attribute.value.type === 'JSXExpressionContainer') {
if (attribute.value.expression.type === 'ObjectExpression') {
if (attribute.value.type === "JSXExpressionContainer") {
if (attribute.value.expression.type === "ObjectExpression") {
value = {};
attribute.value.expression.properties.forEach((prop) => {
const propName =
prop.key.type === 'StringLiteral'
? prop.key.value
: prop.key.name;
const propName = prop.key.type === "StringLiteral" ? prop.key.value : prop.key.name;
if (prop.value.value !== void 0) {

Check warning on line 69 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Expected 'undefined' and instead saw 'void'
value[propName] = prop.value.value;
}
});
} else {
value = attribute.value.expression.value;
}
} else if (attribute.value.type === 'JSXElement') {
} else if (attribute.value.type === "JSXElement") {
value = attributesToObject(attribute.value.openingElement.attributes);
} else {
value = attribute.value.value;
Expand All @@ -70,96 +85,80 @@ module.exports = function (babel) {
return obj;
}
function addRnuStyleIdInStyleArrayOfCOmponent(jsxAttrArray, styleId2) {
let styleAttr = jsxAttrArray.find((attr) => attr.name.name === 'style');
let styleAttr = jsxAttrArray.find((attr) => attr.name.name === "style");
if (styleAttr) {
if (styleAttr.value.expression.type !== 'ArrayExpression') {
if (styleAttr.value.expression.type !== "ArrayExpression") {
styleAttr.value.expression = t.arrayExpression([
styleAttr.value.expression,
styleAttr.value.expression
]);
}
let styleArray = styleAttr.value.expression.elements;
styleArray.push(t.identifier('rnuStyles.styles' + styleId2));
styleArray.push(t.identifier("rnuStyles.styles" + styleId2));
} else {
jsxAttrArray.push(
t.jSXAttribute(
t.jSXIdentifier('style'),
t.jSXIdentifier("style"),
t.jSXExpressionContainer(
t.arrayExpression([t.identifier('rnuStyles.styles' + styleId2)])
t.arrayExpression([t.identifier("rnuStyles.styles" + styleId2)])
)
)
);
}
}
return {
name: 'ast-transform',
name: "ast-transform",
// not required
visitor: {
ImportDeclaration(path, opts) {
if (
opts.filename.includes('node_modules') ||
opts.filename.includes('.expo') ||
opts.filename.includes('.next')
)
ImportDeclaration(path2, opts) {
if (opts.filename.includes("node_modules") || opts.filename.includes(".expo") || opts.filename.includes(".next"))
return;
if (path.node.source.value === importName) {
path.traverse({
ImportSpecifier(path2) {
importedComponents.push(path2.node.local.name);
},
if (path2.node.source.value === importName) {
path2.traverse({
ImportSpecifier(path3) {
importedComponents.push(path3.node.local.name);
}
});
path.node.source.value = 'react-native';
path2.node.source.value = "react-native";
}
},
JSXOpeningElement(path, f, o) {
if (
f.filename.includes('node_modules') ||
f.filename.includes('.expo') ||
f.filename.includes('.next')
)
JSXOpeningElement(path2, f, o) {
if (f.filename.includes("node_modules") || f.filename.includes(".expo") || f.filename.includes(".next"))
return;
if (importedComponents.includes(path.node.name.name)) {
addRnuStyleIdInStyleArrayOfCOmponent(path.node.attributes, styleId);
if (importedComponents.includes(path2.node.name.name)) {
addRnuStyleIdInStyleArrayOfCOmponent(path2.node.attributes, styleId);
styleExpression.push(
t.objectProperty(
t.identifier('styles' + styleId++),
t.valueToNode(attributesToObject(path.node.attributes))
t.identifier("styles" + styleId++),
t.valueToNode(attributesToObject(path2.node.attributes))
)
);
let declaration = f.file.ast.program.body.find(
(node) =>
node.type === 'VariableDeclaration' &&
node.declarations[0].id.name === 'rnuStyles'
(node) => node.type === "VariableDeclaration" && node.declarations[0].id.name === "rnuStyles"
);
if (declaration) {
f.file.ast.program.body = f.file.ast.program.body.filter(
(node) =>
node.type !== 'VariableDeclaration' ||
node.declarations[0].id.name !== 'rnuStyles'
(node) => node.type !== "VariableDeclaration" || node.declarations[0].id.name !== "rnuStyles"
);
} else {
declaration = t.variableDeclaration('const', [
declaration = t.variableDeclaration("const", [
t.variableDeclarator(
t.identifier('rnuStyles'),
t.identifier("rnuStyles"),
// Not using StyleSheet.create since it is not working in the latest metro version
// t.callExpression(t.identifier("StyleSheet.create"), [
t.objectExpression(styleExpression)
// ])
),
)
]);
}
Styles.push(declaration);
f.file.ast.program.body.push(declaration);
}
},
Program(path, opts) {
if (
opts.filename.includes('node_modules') ||
opts.filename.includes('.expo') ||
opts.filename.includes('.next')
)
Program(path2, opts) {
if (opts.filename.includes("node_modules") || opts.filename.includes(".expo") || opts.filename.includes(".next"))
return;
checkIfStylesheetImportedAndImport(path);
},
},
checkIfStylesheetImportedAndImport(path2);
}
}
};
};
5 changes: 4 additions & 1 deletion babel-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-react-native-ustyle",
"version": "0.6.0",
"version": "0.7.2",
"description": "Official support babel plugin for react-native-ustyle",
"main": "out.js",
"module": "out.js",
Expand All @@ -14,5 +14,8 @@
"dependencies": {
"@babel/parser": "^7.23.9",
"@babel/traverse": "^7.23.9"
},
"devDependencies": {
"esbuild": "0.20.1"
}
}
Loading

0 comments on commit 1c7644c

Please sign in to comment.