Skip to content

Commit

Permalink
Add all handlers to state where all logic should be
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed Apr 9, 2020
1 parent 27b2537 commit 5863eaa
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 212 deletions.
21 changes: 5 additions & 16 deletions src/builderz.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { getInput, getOutput } from "./config/index";

import { NotEmptyArr } from "./utils";

import {
State,
extractBundleOpt,
extractAlias,
extractEntries,
extractName,
} from "./store";
import State from "./store";

import resolveArgs from "./resolveArgs";

Expand Down Expand Up @@ -99,11 +93,6 @@ async function builderz(opts, { isInitOpts = true } = {}) {

const { isSilent } = state.generalOpts;

/**
* Give localOpts the priority first.
*/
const bundleOpt = extractBundleOpt();

const pkgInfo = allPkgInfo[name];

const { path: pkgPath } = pkgInfo;
Expand All @@ -114,15 +103,15 @@ async function builderz(opts, { isInitOpts = true } = {}) {
await del(buildPath);
}

const entries = extractEntries(entriesJson, pkgPath);
const entries = state.extractEntries(entriesJson, pkgPath);

const alias = extractAlias(pkgPath);
const alias = state.extractAlias(pkgPath);

const outputName = extractName(name);
const outputName = state.extractName(name);

const banner = state.get("string", "banner");

await bundleOpt.reduce(
await state.bundleOpt.reduce(
async (bundleOptPromise, { IS_PROD, BUILD_FORMAT }) => {
await bundleOptPromise;

Expand Down
19 changes: 1 addition & 18 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
import State from "./store";

import {
extractBundleOpt,
initOpts,
extractAlias,
extractEntries,
extractName,
} from "./stateHandlers";

export {
State,
extractBundleOpt,
initOpts,
extractAlias,
extractEntries,
extractName,
};
export { default } from "./store";
275 changes: 102 additions & 173 deletions src/store/stateHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,176 +4,105 @@ import { validateAccess } from "validate-access";
import { msg } from "@mytools/print";
import { NotEmptyArr, isValidArr, camelizeOutputBuild } from "../utils";
import { UMD, CJS, ES } from "../constants";
import state from "./store";

/**
* Gen bundle format and minify options
*
* @param {Array} customFormats
* @param {boolean} isMinify
* @returns {Object[]} bundle output options
*/
function getBundleOpt(customFormats, isMinify) {
const DEFAULT_FORMATS = [UMD, CJS, ES];

const gen = [];

const buildFormat = NotEmptyArr(customFormats)
? customFormats
: DEFAULT_FORMATS;

const minifyingProcess =
NotEmptyArr(customFormats) && isBoolean(isMinify)
? [isMinify]
: [true, false];

buildFormat.forEach((format) => {
minifyingProcess.forEach((bool) => {
gen.push({ BUILD_FORMAT: format, IS_PROD: bool });
});
});

return gen;
}

/**
* Extracts bundle options depending on localOpts and globalOpts that should be
* already set.
*
* @returns {Array}
*/
function extractBundleOpt() {
const format = state.get("array", "formats");
const isMinify = state.get("boolean", "minify");

return getBundleOpt(format, isMinify);
}

/**
* Inits options
*
* @param {Object} options - input options
* @returns {Object} - initialize options
*/
function initOpts(options) {
const defaultOptions = {
isSilent: true,
formats: [],
isMinify: undefined,
cleanBuild: false,
camelCase: true,
buildName: "dist",
output: undefined,
pkgPaths: [],
pkgNames: [],
alias: [],
entries: [],
banner: undefined,
};

Object.keys(options).forEach((option) => {
// eslint-disable-next-line no-underscore-dangle
const _default = options[option];

const value = state[option] || _default;

defaultOptions[option] = value;
});

return defaultOptions;
}

/**
* Extract name based on output option if found, else, it looks if isCamelCase
* then camelize. otherwise, return jsonPkgName.
*
* @param {string} jsonPkgName
* @returns {string} - output name
*/
function extractName(jsonPkgName) {
let output = jsonPkgName;

const givenName = state.get("string", "output");

if (givenName) {
output = givenName;
} else if (state.get("boolean", "camelCase")) {
output = camelizeOutputBuild(jsonPkgName);
}

msg(`bundle output as ${output}`);

return output;
}

/**
* Extracts alias. If there's local alias in package, then revolve path:
* by adding localPkgPath:
alias({
entries: [
{ find: 'utils', replacement: 'localPkgPath/../../../utils' },
{ find: 'batman-1.0.0', replacement: 'localPkgPath/joker-1.5.0' }
]
*
* @param {string} localPkgPath
* @returns {Array}
*/
function extractAlias(pkgPath) {
const { alias: localAlias } = state.localOpts;

if (!isValidArr(localAlias)) return state.globalOpts.alias;

/**
* If there's local alias passed in package, let's resolve the pass.
*/
localAlias.forEach(({ replacement }, i) => {
/**
* Assuming we're working in `src` by default.
*/
localAlias[i].replacement = resolve(pkgPath, "src", replacement);
});

return localAlias;
}

/**
* Extracts entries by checking global entries, entries passed in Json as
* property. If no valid entries, it returns default path: src/index.extension
*
* @param {Array} entriesJson
* @param {string} pkgPath
* @returns {Array|string}
*/
function extractEntries(entriesJson, pkgPath) {
const entries = state.get("array", "entries");

if (isValidArr(entries)) {
return entries.map((entry) => resolve(pkgPath, entry));
}

if (isValidArr(entriesJson)) {
return entriesJson.map((entryJson) => resolve(pkgPath, entryJson));
}

const { isValid, isSrc, ext } = validateAccess({
dir: pkgPath,
isValidateEntry: true,
entry: "index",
srcName: "src",
});

// eslint-disable-next-line no-nested-ternary
return !isValid
? null
: isSrc
? resolve(pkgPath, "src", `index.${ext}`)
: resolve(pkgPath, `index.${ext}`);
}

export {
extractBundleOpt,
initOpts,
extractAlias,
extractEntries,
extractName,
};

// /**
// * Extracts bundle options depending on localOpts and globalOpts that should be
// * already set.
// *
// * @returns {Array}
// */
// function extractBundleOpt() {
// const format = state.get("array", "formats");
// const isMinify = state.get("boolean", "minify");

// return getBundleOpt(format, isMinify);
// }

// /**
// * Extract name based on output option if found, else, it looks if isCamelCase
// * then camelize. otherwise, return jsonPkgName.
// *
// * @param {string} jsonPkgName
// * @returns {string} - output name
// */
// function extractName(jsonPkgName) {
// let output = jsonPkgName;

// const givenName = state.get("string", "output");

// if (givenName) {
// output = givenName;
// } else if (state.get("boolean", "camelCase")) {
// output = camelizeOutputBuild(jsonPkgName);
// }

// msg(`bundle output as ${output}`);

// return output;
// }

// /**
// * Extracts alias. If there's local alias in package, then revolve path:
// * by adding localPkgPath:
// alias({
// entries: [
// { find: 'utils', replacement: 'localPkgPath/../../../utils' },
// { find: 'batman-1.0.0', replacement: 'localPkgPath/joker-1.5.0' }
// ]
// *
// * @param {string} localPkgPath
// * @returns {Array}
// */
// function extractAlias(pkgPath) {
// const { alias: localAlias } = state.localOpts;

// if (!isValidArr(localAlias)) return state.globalOpts.alias;

// /**
// * If there's local alias passed in package, let's resolve the pass.
// */
// localAlias.forEach(({ replacement }, i) => {
// /**
// * Assuming we're working in `src` by default.
// */
// localAlias[i].replacement = resolve(pkgPath, "src", replacement);
// });

// return localAlias;
// }

// /**
// * Extracts entries by checking global entries, entries passed in Json as
// * property. If no valid entries, it returns default path: src/index.extension
// *
// * @param {Array} entriesJson
// * @param {string} pkgPath
// * @returns {Array|string}
// */
// function extractEntries(entriesJson, pkgPath) {
// const entries = state.get("array", "entries");

// if (isValidArr(entries)) {
// return entries.map((entry) => resolve(pkgPath, entry));
// }

// if (isValidArr(entriesJson)) {
// return entriesJson.map((entryJson) => resolve(pkgPath, entryJson));
// }

// const { isValid, isSrc, ext } = validateAccess({
// dir: pkgPath,
// isValidateEntry: true,
// entry: "index",
// srcName: "src",
// });

// // eslint-disable-next-line no-nested-ternary
// return !isValid
// ? null
// : isSrc
// ? resolve(pkgPath, "src", `index.${ext}`)
// : resolve(pkgPath, `index.${ext}`);
// }

// export { extractAlias, extractEntries, extractName };
Loading

0 comments on commit 5863eaa

Please sign in to comment.