Skip to content

Commit

Permalink
Add cache (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 authored May 11, 2020
1 parent 04312b6 commit 3da9bf9
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ builderz(options);
-p, --sourcemap <boolean> Enable sourcemap in output
-c, --camel-case <boolean> Add camel-cased output file (default: true)
-l, --clean-build <boolean> Clean previous build folder (default: false)
-t, --strict <boolean> Enable Strict Mode (default: false)
-t, --strict <boolean> Enable Strict Mode (default: false)
-r, --sort-pkg <boolean> Enable sorting packages for monorepo (default: true)
-d, --es-module <boolean> Define Property exports- es_model (default: false)
-d, --es-module <boolean> Define Property exports- es_model (default: false)
--formats <list> Specific build format (default: [])
--build-name <string> Specific folder build name (default: "dist")
--output <string> Custom output name
Expand Down
7 changes: 6 additions & 1 deletion src/builderz.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getJsonByName, getJsonByPath } from "get-info";

import { getInput, getOutput } from "./config/index";

import { isValidArr, bindFunc } from "./utils";
import { isValidArr, bindFunc, cache } from "./utils";

import { SORT_PACKAGES, PKG_PATHS, PKG_NAMES } from "./constants";

Expand Down Expand Up @@ -53,6 +53,9 @@ async function build(inputFunc, outputFunc, { isProd, buildFormat, order }) {
* @param {number} index - index to call path if invalid package.json
*/
async function bundlePkg(state, pkgInfo, json, index) {
cache({ type: "babel", isDestroy: true });
cache({ type: "input", isDestroy: true });

let pkgPath;

state.setNewPkg();
Expand Down Expand Up @@ -144,6 +147,8 @@ async function builderz(opts) {
}
} catch (err) {
console.error(err);
} finally {
cache({ isDestroy: true });
}
}

Expand Down
26 changes: 23 additions & 3 deletions src/config/babel/babelTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as babel from "@babel/core";
import path from "path";
import { getPlugins, getPresets } from "./babelPresets";
import { isValidArr } from "../../utils";
import { isValidArr, cache } from "../../utils";

const PLUGIN = "plugin";
const PRESET = "preset";
Expand Down Expand Up @@ -121,11 +121,31 @@ async function babelTransformer(inputCode, babelOptions) {
const { options } = babel.loadPartialConfig(rest);

if (enablePreset) {
options.presets = presetsHandler(PRESET, options.presets, isESM);
const cacheObj = { type: "babel", key: "presets" };

let result = cache(cacheObj);

if (!result) {
result = presetsHandler(PRESET, options.presets, isESM);

cache(cacheObj, result);
}

options.presets = result;
}

if (enablePlugins) {
options.plugins = presetsHandler(PLUGIN, options.plugins, isESM);
const cacheObj = { type: "babel", key: "plugins" };

let result = cache(cacheObj);

if (!result) {
result = presetsHandler(PLUGIN, options.plugins, isESM);

cache(cacheObj, result);
}

options.plugins = result;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/config/babel/rollupBabelPlugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFilter } from "@rollup/pluginutils";
import babelTransformer from "./babelTransformer";
import extRegExp from "./utils";
import { cache } from "../../utils";

const unpackOptions = ({
// rollup uses sourcemap, babel uses sourceMaps
Expand Down Expand Up @@ -48,12 +49,24 @@ function babelPlugin(options) {
return null;
}

const cacheObj = { type: "babel", key: filename };

const result = cache(cacheObj);

if (result) {
return result;
}

const babelOptions = {
...rest,
filename,
};

return babelTransformer(code, babelOptions);
const res = babelTransformer(code, babelOptions);

cache(cacheObj, res);

return res;
},
};
}
Expand Down
10 changes: 9 additions & 1 deletion src/config/input/getInput.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getPlugins from "./getInputPlugins";
import getExternal from "./getInputExternal";
import { cache } from "../../utils";

/**
* Generates output build
Expand All @@ -12,7 +13,14 @@ function genInput(
{ plugins, output, opts, pkg },
{ idx, isProd, buildFormat }
) {
const externalFunc = getExternal({ opts, pkg }, buildFormat);
const cacheObj = { type: "input", key: buildFormat };
let externalFunc = cache(cacheObj);

if (!externalFunc) {
externalFunc = getExternal({ opts, pkg }, buildFormat);

cache(cacheObj, externalFunc);
}

const extractedPlugins = getPlugins(
{ plugins, output, pkg, opts },
Expand Down
41 changes: 21 additions & 20 deletions src/store/StateHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolve, basename } from "path";
import { validateAccess } from "validate-access";
import { CAMEL_CASE, OUTPUT, ENTRIES, ALIAS, BUILD_NAME } from "../constants";

import { camelizeOutputBuild, bindFunc } from "../utils";
import { camelizeOutputBuild, bindFunc, isValidArr } from "../utils";
import State from "./State";

class StateHandler extends State {
Expand Down Expand Up @@ -109,7 +109,7 @@ class StateHandler extends State {
* @memberof StateHandler
*/
extractAlias() {
let alias = this.opts[ALIAS];
const alias = this.opts[ALIAS];

// const alias = [
// { find: "utils", replacement: "localPkgPath/../../../utils" },
Expand All @@ -119,25 +119,26 @@ class StateHandler extends State {
/**
* If there's local alias passed in package, let's resolve the pass.
*/
alias = alias.map((str) => {
let find;
let replacement;

if (typeof str === "string") {
// eslint-disable-next-line prefer-const
[find, replacement] = str.split("=");
} else {
({ find, replacement } = str);
}

if (this.shouldPathResolved) {
replacement = this.resolvePathSrc(replacement);
}

return { find, replacement };
});

this.plugins.alias = alias;
this.plugins.alias = isValidArr(alias)
? alias.map((str) => {
let find;
let replacement;

if (typeof str === "string") {
// eslint-disable-next-line prefer-const
[find, replacement] = str.split("=");
} else {
({ find, replacement } = str);
}

if (this.shouldPathResolved) {
replacement = this.resolvePathSrc(replacement);
}

return { find, replacement };
})
: [];
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,54 @@

import { UMD, CJS, ES } from "./constants";

const cached = new Map();

/**
* Silly cache
*
* @param {string} key
* @param {any} result
* @param {boolean} isDestroy
* @returns
*/
function cache({ type, key, isDestroy }, result) {
let bank;

if (type) {
if (cached.has(type)) {
bank = cached.get(type);
} else {
const newBank = new Map();
bank = cached.set(type, newBank);
}
}

if (isDestroy) {
if (type) {
bank.clear();
} else {
/**
* clear all
*/
cached.clear();
}

return -1;
}

if (result) {
bank.set(key, result);

return null;
}

if (bank.has(key)) {
return bank.get(key);
}

return null;
}

/**
* True, when length is above zero.
*
Expand Down Expand Up @@ -96,4 +144,5 @@ export {
camelizeOutputBuild,
getBundleOpt,
bindFunc,
cache,
};

0 comments on commit 3da9bf9

Please sign in to comment.