Skip to content

Commit

Permalink
compiler updates; added maintainRoutesWhenDeveloping config; tools up…
Browse files Browse the repository at this point in the history
…dated; index.d.ts created and now being used by package.json
  • Loading branch information
wpdas committed Mar 7, 2024
1 parent 669dfb3 commit 296f4e5
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- https://cdn.jsdelivr.net/gh/wpdas/alem/docs/assets/near-script-logo.png -->
<div align="center"><img src='https://cdn.jsdelivr.net/gh/wpdas/alem/docs/assets/near-script-logo.png' height='57' alt='Alem logo' /></div>

# <div align="center">Alem</div>
# <div align="center">Além</div>

Alem is a web3 JavaScript / TypeScript library for building user interfaces for NEAR Bos dApps.

Expand Down
15 changes: 15 additions & 0 deletions bos.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,18 @@ export declare const Tooltip: (params: {
export declare const VM: {
require: (source: string) => any;
};

/**
* BOS State - handle the global component's state
*
* Know more: https://docs.near.org/bos/api/state
*/
export declare const State: {
init: (defaultValue: {}) => void;
update: (updatedValues: {}) => void;
};

/**
* BOS State - access all state items
*/
export declare const state: Record<string, any>;
4 changes: 3 additions & 1 deletion docs/config.file.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Create an `bos.config.json` file at the root of the project with the following c
// Alem options
"options": {
// The storage need to be loaded before rendering any content. If this is "true", a Spinner is going to be shown till the storage is ready
"showFallbackSpinner": false
"showFallbackSpinner": false,
// During development, if the route is of type ContentBased, it will return to the first registered route every time a file is changed. This property enables or disables this behavior.
"maintainRouteWhenDeveloping": true
}
}
```
3 changes: 2 additions & 1 deletion gateway/src/useRedirectMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function useRedirectMap() {
fetch(flags.bosLoaderUrl)
.then((r) => r.json())
.then((d) => {
console.log("File change detected via HTTP", d);
// console.log("File change detected via HTTP", d);
console.log("File change detected via HTTP");
setDevJson(d);
})
.catch((error) => {
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./bos";
export * from "./router";
export * from "./store";
export * from "./theme";
export * from "./utils";
10 changes: 10 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const {
removeComments,
removeBlankLines,
mimify,
applyEnvironment,
parseOptions,
} = require("./parse.js");
const { read_bos_config } = require("./config");
const pkg = require("../package.json");
Expand Down Expand Up @@ -117,6 +119,14 @@ function process_dist() {
// Remove blank lines
fileBundleBody = removeBlankLines(fileBundleBody);

// Apply ports - works for development only
// this won't affect production because the state of enviroment is going to be
// production
fileBundleBody = applyEnvironment(fileBundleBody);

// Apply changes depending of the config.options
fileBundleBody = parseOptions(fileBundleBody);

// Mimify
fileBundleBody = mimify(fileBundleBody);

Expand Down
1 change: 1 addition & 0 deletions lib/organize.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function checkImportFeatures(file, fileBundleBody) {
if (foundItems) {
foundItems.forEach((item) => {
// remove spaces and braces
// TODO: refactor using regexp
const filteredItem = item
.replaceAll(" ", "")
.replaceAll("{", "")
Expand Down
25 changes: 25 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
// const path = require("path");
const sucrase = require("sucrase");
const { read_bos_config } = require("./config");

const sucraseOptions = {
transforms: ["typescript", "jsx"],
Expand Down Expand Up @@ -92,6 +93,28 @@ const removeComments = (c) =>

const removeBlankLines = (c) => c.replace(/^\s*\n/gm, "");

const ENVIRONMENT_MARKER = ":::ENV:::";
const applyEnvironment = (c) =>
c.replaceAll(ENVIRONMENT_MARKER, process.env.NODE_ENV || "production");

const MAINTAIN_ROUTE_MARKER = '":::MAINTAIN_ROUTE:::"';
const parseOptions = (c) => {
const config = read_bos_config();

// Maintain Route When Developing
if (config?.options?.maintainRouteWhenDeveloping) {
c = c.replace(
MAINTAIN_ROUTE_MARKER,
config.options.maintainRouteWhenDeveloping,
);
}

// Default values if config is not found
c = c.replace(MAINTAIN_ROUTE_MARKER, "false");

return c;
};

const mimify = (c) =>
c
.replace(/\r?\n|\r/gm, "")
Expand Down Expand Up @@ -131,4 +154,6 @@ module.exports = {
removeImports,
removeBlankLines,
mimify,
applyEnvironment,
parseOptions,
};
18 changes: 13 additions & 5 deletions lib/tools/routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const Routes = ({ routes, type }) => {
// BOS.props
const bosProps = props;

// ContentBased config only: should maintain the current route over refreshes?
const maintainRoutesWhenDeveloping =
isDevelopment && state.alemConfig_maintainRouteWhenDeveloping;

if (routes) {
update({
// list routes
Expand All @@ -23,9 +27,12 @@ const Routes = ({ routes, type }) => {
...(bosProps.path && routeType === "URLBased" && state.alemRouteBlocked
? { activeRoute: bosProps.path }
: {
activeRoute: state.alemRouteSystemInitialized
? activeRoute
: routes[0].path,
activeRoute:
// maintainRoutesWhenDeveloping: If in development and ContentBased type,
// maintain the route even when alemRouteSystemInitialized is not initialized?
state.alemRouteSystemInitialized || maintainRoutesWhenDeveloping
? activeRoute
: routes[0].path,
}),
});

Expand All @@ -35,7 +42,7 @@ const Routes = ({ routes, type }) => {
alemRouteBlocked: true,
});
}
}, []);
}, [props.path, activeRoute]);

// Default route
if (activeRoute === "") {
Expand Down Expand Up @@ -68,12 +75,13 @@ export const navigate = (routePath) => {
}
};

export const RouteLink = ({ to, children }) => {
export const RouteLink = ({ to, children, className }) => {
const { type } = useAlemLibRoutesStore();

if (type === "URLBased") {
return (
<a
className={className}
style={{ cursor: "pointer", textDecoration: "none" }}
href={`?path=${to}`}
>
Expand Down
12 changes: 12 additions & 0 deletions lib/tools/stateManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ State.init({
alemStoreReady: false,
alemRouteSystemInitialized: false,
alemRouteBlocked: true,
// Some resources can change its behaviour depending of the environment
// This is just to help out during the development process. Final result
// is going to be the same.
// #PORT# is a marker that is going to be changed during the build process
alemEnvironment: ":::ENV:::", // production | development
// During development, if the route is of type ContentBased, it will return to the
// first registered route every time a file is changed. This property enables or
// disables this behavior.
alemConfig_maintainRouteWhenDeveloping: ":::MAINTAIN_ROUTE:::", // boolean
// Fonts
alemFontsLoaded: false,
alemFontsBody: "", // store fonts in css format
Expand Down Expand Up @@ -50,6 +59,9 @@ const removeAlemPropsFromState = (stateObj) => {
delete stateObj.alemRouteSystemInitialized;
delete stateObj.alemFontsLoaded;
delete stateObj.alemFontsBody;
delete stateObj.alemRouteBlocked;
delete stateObj.alemEnvironment;
delete stateObj.alemConfig_maintainRouteWhenDeveloping;
return stateObj;
};

Expand Down
6 changes: 6 additions & 0 deletions lib/tools/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Internal usage only
* is Development?
*/
const isDevelopment = state.alemEnvironment === "development";

/**
* Call resolve or reject for a given caller
* E.g:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "alem",
"description": "Create web3 applications for NEAR BOS with a focus on performance while using concepts that are based on ReactJS.",
"version": "0.0.1-alpha.8",
"version": "0.0.1-alpha.9",
"main": "main.js",
"types": "index.d.ts",
"author": "Wenderson Pires - wendersonpires.near",
"license": "MIT",
"repository": {
Expand Down
6 changes: 2 additions & 4 deletions router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ export declare const Routes: ({
type RouteLinkProps = {
to: string;
children: JSX.Element;
className?: string;
};

/**
* Route Link to access routes.
*/
export declare const RouteLink: ({
to,
children,
}: RouteLinkProps) => JSX.Element;
export declare const RouteLink: (props: RouteLinkProps) => JSX.Element;

/**
* Go programmatically to the route ("route Path").
Expand Down

0 comments on commit 296f4e5

Please sign in to comment.