Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hasbang & other options #17

Merged
merged 17 commits into from
Apr 9, 2020
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ In your `packages.json` to compile to a CommonJS module (cjs) and minify the
bundle just pass the required args.

```json
"build": "builderz --formats=cjs --minify"
"build": "builderz --formats=cjs --minify=false"
```

## Options
Expand All @@ -62,6 +62,7 @@ bundle just pass the required args.
-n, --pkg-names <list> Building specific package[s], in workspace (default: [])
-a, --alias <list> Package Alias (default: [])
-e, --entries <list> Add multi entries instead of default src/index. (default: [])
-r, --banner <string> Add banner to output
-h, --help display help for command
```

Expand Down
11 changes: 6 additions & 5 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ module.exports = api => {
"@babel/preset-env",
{
targets: {
node: "current"
}
}
]
]
node: "current",
},
},
],
],
plugins: ["@babel/plugin-transform-classes"],
};
};
5 changes: 3 additions & 2 deletions build.self.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import builderz from "./src";

[
{ entries: ["src/cli.js"], output: "cli" },
{ entries: ["src/cli.js"], output: "cli", banner: `#!/usr/bin/env node` },
{ entries: "src/builderz.js", output: "builderz" },
].forEach(({ entries, output }) => {
].forEach(({ entries, output, banner }) => {
builderz({
isSilent: true,
formats: ["cjs"],
entries,
output,
banner,
minify: false,
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"validate-access": "^0.1.1"
},
"devDependencies": {
"@babel/plugin-transform-classes": "^7.9.5",
"babel-eslint": "^10.0.3",
"babel-jest": "^25.1.0",
"eslint": "^6.8.0",
Expand Down
38 changes: 14 additions & 24 deletions src/builderz.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ import { getInput, getOutput } from "./config/index";

import { NotEmptyArr } from "./utils";

import {
setOpt,
initOpts,
getBooleanOpt,
extractBundleOpt,
extractAlias,
extractEntries,
extractName,
} from "./optionsHandler";
import State from "./store";

import resolveArgs from "./resolveArgs";

Expand All @@ -46,9 +38,9 @@ async function build(inputOptions, outputOptions) {
}

async function builderz(opts, { isInitOpts = true } = {}) {
const generalOpts = isInitOpts ? initOpts(opts) : opts;
const state = new State(opts, isInitOpts);

const { buildName, pkgPaths, pkgNames } = generalOpts;
const { buildName, pkgPaths, pkgNames } = state.generalOpts;

const { json: allPkgJson, pkgInfo: allPkgInfo } = NotEmptyArr(pkgNames)
? getJsonByName(...pkgNames)
Expand Down Expand Up @@ -97,32 +89,29 @@ async function builderz(opts, { isInitOpts = true } = {}) {
/**
* Setting options allowing extracts functions to work properly.
*/
setOpt(localOpts, generalOpts);
state.setLocal(localOpts);

const { isSilent } = generalOpts;

/**
* Give localOpts the priority first.
*/
const bundleOpt = extractBundleOpt();
const { isSilent } = state.generalOpts;

const pkgInfo = allPkgInfo[name];

const { path: pkgPath } = pkgInfo;

const buildPath = resolve(pkgPath, buildName);

if (getBooleanOpt("cleanBuild")) {
if (state.get("boolean", "cleanBuild")) {
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);

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

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

Expand Down Expand Up @@ -150,6 +139,7 @@ async function builderz(opts, { isInitOpts = true } = {}) {
},
buildPath,
BUILD_FORMAT,
banner,
});

await build(input, output);
Expand All @@ -158,7 +148,7 @@ async function builderz(opts, { isInitOpts = true } = {}) {
);
}, Promise.resolve());
} catch (err) {
error(err);
console.error(err);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

import error from "@mytools/print";
import builderz from "./builderz";
import resolveArgs from "./resolveArgs";
Expand Down
6 changes: 6 additions & 0 deletions src/config/output/getOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join } from "path";
import getGlobal from "./getGlobalOutput";

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

/**
* Gets full bundle name camelized with extension
Expand Down Expand Up @@ -52,6 +53,7 @@ function getOutput({
json: { peerDependencies },
buildPath,
BUILD_FORMAT,
banner,
}) {
const { IS_PROD } = flags;

Expand All @@ -76,6 +78,10 @@ function getOutput({
output.sourcemap = true;
}

if (banner && NotEmptyArr(banner)) {
output.banner = banner;
}

return output;
}

Expand Down
Loading