Skip to content

Commit

Permalink
Add enhancement related to #9
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed Mar 26, 2020
1 parent 1d81f09 commit f8c7a98
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 48 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@rollup/plugin-node-resolve": "^7.0.0",
"camelize": "^1.0.0",
"chalk": "^3.0.0",
"commander": "^4.1.1",
"commander": "^5.0.0",
"del": "^5.1.0",
"get-info": "^2.2.0",
"glob": "^7.1.6",
Expand All @@ -65,7 +65,8 @@
"rollup": "^1.29.1",
"rollup-plugin-analyzer": "^3.2.2",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-terser": "^5.2.0"
"rollup-plugin-terser": "^5.2.0",
"shell-quote": "^1.7.2"
},
"devDependencies": {
"babel-eslint": "^10.0.3",
Expand Down
2 changes: 0 additions & 2 deletions src/config/initBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ async function initBuild(buildName = "dist", paths, packagesNames) {
? `bundle ${pkgName} as ${camelizedName}`
: `bundle ${camelizedName}`
);

//
}, Promise.resolve());

return {
Expand Down
9 changes: 3 additions & 6 deletions src/config/input/getInputPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ function getPlugins({
json()
];

// if (alias.length > 0) {
// console.log("alias", alias);
// const extractedAlias = extractAlias(alias);

// essentialPlugins.push(aliasPlugin({ entries: extractedAlias }));
// }
if (alias.length > 0) {
aliasPlugin({ entries: alias });
}

if (extraPlugins.length > 0) {
extraPlugins.forEach(plg => {
Expand Down
51 changes: 28 additions & 23 deletions src/resolveArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,32 @@ function extractAlias(aliasStr) {
return alias;
}

program
.option("-s, --silent", "Silent mode, mutes build massages")
.option("--formats <list>", "Specific build format", string2Arr)
.option("-m, --minify", "Minify bundle works only if format is provided")
.option("-b, --build-name <string>", "Specific build name")
.option(
"--plugins <list>",
"Custom plugins works as additional ones",
string2Arr
)
.option(
"--paths <list>",
"Provide custom paths not in the root/src",
string2Arr
)
.option(
"--package-names <list>",
"Building specific package[s], in monorepo",
string2Arr
)
.option("-a, --alias <list>", "package alias", extractAlias)
.parse(process.argv);
function resolveArgs(argv) {
return program
.option("-s, --silent", "Silent mode, mutes build massages")
.option("--formats <list>", "Specific build format", string2Arr, [])
.option("-m, --minify", "Minify bundle works only if format is provided")
.option("-b, --build-name <string>", "Specific build name", "dist")
.option(
"--plugins <list>",
"Custom plugins works as additional ones",
string2Arr,
[]
)
.option(
"--paths <list>",
"Provide custom paths not in the root/src",
string2Arr,
[]
)
.option(
"--package-names <list>",
"Building specific package[s], in monorepo",
string2Arr,
[]
)
.option("-a, --alias <list>", "package alias", extractAlias, [])
.parse(argv || process.argv);
}

export default program;
export default resolveArgs;
57 changes: 43 additions & 14 deletions src/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { rollup } from "rollup";

import { error } from "@mytools/print";

import { parse } from "shell-quote";
import { initBuild, getInput, getOutput } from "./config/index";
import { getBundleOpt } from "./utils";
import resolveArgs from "./resolveArgs";

const globalArgs = resolveArgs();

async function build(inputOptions, outputOptions) {
try {
/**
Expand Down Expand Up @@ -64,28 +67,54 @@ async function bundlePackage({
await build(input, output);
}

async function start(params = {}) {
const args = resolveArgs;
function initOpts(opt1, opt2) {
const options = {
isSilent: undefined,
formats: undefined,
isMinify: undefined,
buildName: undefined,
plugins: undefined,
paths: undefined,
packageNames: undefined,
alias: undefined
};

Object.keys(options).forEach(option => {
const value = opt1[option] || opt2[option] || options[option];

options[option] = value;
});

const isSilent = args.isSilent || params.isSilent;
const isMinify = args.isMinify || params.isMinify;
const buildName = args.buildName || params.buildName;
return options;
}

const formats = args.format || params.format || [];
const plugins = args.plugins || params.plugins || [];
const paths = args.paths || params.paths || [];
const packageNames = args.packageNames || params.packageNames || [];
const alias = args.alias || params.alias || [];
async function start(params = {}) {
let options = initOpts(params, globalArgs);

try {
const { sorted, pkgInfo } = await initBuild(buildName, paths, packageNames);
const { buildName, paths, packageNames } = options;

const bundleOpt = getBundleOpt(formats, isMinify);
const { sorted, pkgInfo } = await initBuild(buildName, paths, packageNames);

await sorted.reduce(async (sortedPromise, json) => {
await sortedPromise;

const { name } = json;
const { name, scripts: { build: buildArgs } = {} } = json;

if (buildArgs) {
const parsedBuildArgs = parse(buildArgs);

if (parsedBuildArgs.length > 0) {
const localArgs = resolveArgs(parsedBuildArgs);

options = initOpts(localArgs, options);
}
}

const { isSilent, formats, isMinify, plugins, alias } = options;
console.log("start -> options", options);

const bundleOpt = getBundleOpt(formats, isMinify);

await bundleOpt.reduce(
async (bundleOptPromise, { IS_PROD, BUILD_FORMAT }) => {
Expand All @@ -107,7 +136,7 @@ async function start(params = {}) {
);
}, Promise.resolve());
} catch (err) {
error(err);
console.error(err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/samples/alias/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alias-mapping",
"scripts": {
"build": "microbundle --no-sourcemap --alias ./constants=./constants-debug"
"build": "microbundle --no-sourcemap --alias=./constants=./constants-debug"
}
}

1 comment on commit f8c7a98

@jalal246
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach generates issues related to overriding essential parameters. Need more restrictions.

Please sign in to comment.