Skip to content

Commit

Permalink
Add new approach includes decoupling paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed Mar 31, 2020
1 parent a34c2c2 commit 4836d42
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"dependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.8.3",
"@mytools/print": "^0.1.3",
"@mytools/print": "^0.1.5",
"@rollup/plugin-alias": "^3.0.1",
"@rollup/plugin-auto-install": "^2.0.0",
"@rollup/plugin-beep": "^0.1.2",
Expand All @@ -58,8 +58,9 @@
"chalk": "^3.0.0",
"commander": "^5.0.0",
"del": "^5.1.0",
"get-info": "^2.2.0",
"get-info": "^2.3.0",
"glob": "^7.1.6",
"move-position": "^1.0.0",
"ms": "^2.1.2",
"package-sorter": "^1.1.1",
"rollup": "^1.29.1",
Expand Down
30 changes: 19 additions & 11 deletions src/config/initBuild.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable func-names */

import { resolve } from "fs";
import packageSorter from "package-sorter";
import { getJsonByName } from "get-info";
import { getJsonByName, getJsonByPath } from "get-info";
import del from "del";

import { msg, error } from "@mytools/print";
Expand All @@ -14,15 +14,16 @@ import { camelizeOutputBuild } from "../utils";
* clean previous build.
* sort packages.
*
* @param {string} [buildName="dist"]
* @param {Array} packagesNames - packages name to be built
* @param {string} buildName
* @param {Array} pkgNames - packages name to be built
* @param {Array} pkgPaths - packages name to be built
* @returns {Array} sortedJson
*/
async function initBuild(buildName = "dist", paths, packagesNames) {
const { json, pkgInfo } = getJsonByName(
buildName,
...paths
)(...packagesNames);
async function initBuild(buildName, pkgPaths, pkgNames) {
const { json, pkgInfo } =
pkgNames.length > 0
? getJsonByName(...pkgNames)
: getJsonByPath(...pkgPaths);

/**
* Sort packages before bump to production.
Expand All @@ -46,14 +47,21 @@ async function initBuild(buildName = "dist", paths, packagesNames) {
*/
await promise;

const { dist } = pkgInfo[pkgName];
const { path, ext } = pkgInfo[pkgName];

const srcPath = resolve(path, "src", `index.${ext}`);

await del(dist);
const buildPath = resolve(path, buildName);

await del(buildPath);

const camelizedName = camelizeOutputBuild(pkgName);

pkgInfo[pkgName].camelizedName = camelizedName;

pkgInfo[pkgName].srcPath = srcPath;
pkgInfo[pkgName].buildPath = buildPath;

msg(
camelizedName !== pkgName
? `bundle ${pkgName} as ${camelizedName}`
Expand Down
1 change: 1 addition & 0 deletions src/config/input/getInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function genInput({
IS_PROD,
BUILD_FORMAT,
plugins: extraPlugins,
sourcePath,
alias
});

Expand Down
6 changes: 1 addition & 5 deletions src/config/input/getInputPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,29 @@ function getPlugins({
runtimeHelpers: true,
babelrc: true
}),

/**
* Automatically installs dependencies that are imported by a bundle, even
* if not yet in package.json.
*/
auto(),

/**
* Convert CommonJS modules to ES6, so they can be included in a Rollup
* bundle.
*/
commonjs(),

/**
* Locates modules using the Node resolution algorithm, for using third
* party modules in node_modules.
*/
resolve({ extensions: [".mjs", ".js", ".jsx", ".json", ".node"] }),

/**
* Converts .json files to ES6 modules.
*/
json()
];

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

if (extraPlugins.length > 0) {
Expand Down
15 changes: 8 additions & 7 deletions src/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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";
Expand Down Expand Up @@ -40,17 +39,19 @@ async function build(inputOptions, outputOptions) {
async function bundlePackage({
plugins,
alias,
flags: { IS_PROD, IS_SILENT },
flags,
BUILD_FORMAT,
json,
pkgInfo: { dist, camelizedName }
pkgInfo
}) {
const { peerDependencies = {}, dependencies = {}, sourcePath } = json;
const { IS_PROD, IS_SILENT } = flags;
const { peerDependencies = {}, dependencies = {} } = json;
const { buildPath, srcPath, camelizedName } = pkgInfo;

const input = await getInput({
flags: { IS_SILENT, IS_PROD },
json: { peerDependencies, dependencies },
sourcePath,
srcPath,
BUILD_FORMAT,
plugins,
alias
Expand All @@ -60,7 +61,7 @@ async function bundlePackage({
flags: { IS_PROD },
camelizedName,
json: {},
dist,
buildPath,
BUILD_FORMAT
});

Expand Down
2 changes: 1 addition & 1 deletion test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ setIsSilent(true);

describe("config function", () => {
it("initBuild default", async () => {
const { sorted, pkgInfo } = await initBuild(undefined, [], []);
const { sorted, pkgInfo } = await initBuild("dist", [], []);

// snapshot the name cause everything else is changeable.
expect(sorted[0].name).toMatchSnapshot();
Expand Down
2 changes: 1 addition & 1 deletion test/production.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import del from "del";
import builderz from "../src";

describe("production", () => {
it.each(["pure", "alias"])("tests %s)", async pkgName => {
it.each(["alias"])("tests %s)", async pkgName => {
const pathPure = resolve(__dirname, "samples", pkgName);
const distPath = resolve(pathPure, "dist");

Expand Down

0 comments on commit 4836d42

Please sign in to comment.