Skip to content

Commit

Permalink
Create packages for split modules.
Browse files Browse the repository at this point in the history
Fixes #47.
  • Loading branch information
jussi-kalliokoski committed Jul 28, 2015
1 parent 86f2a49 commit 487fb8d
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions scripts/generate-packages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env babel-node

import { readFileSync as read, writeFileSync as write } from "fs";
import { sync as glob } from "glob";
import { sync as mkdir } from "mkdirp";
import { join, resolve } from "path";
import { Plugin, types as t, transformFileSync as transform } from "babel";

const source = JSON.parse(read("package.json", "utf8"));

Expand All @@ -12,3 +16,52 @@ const main = {
};

write("dist/main/package.json", JSON.stringify(main), "utf8");

function getModuleSpecifierByName (filename) {
return filename
.replace(/\.js$/, "")
.split("/");
}

glob("*/*.js", { cwd: "src" }).map((mod) => {
const [category, name] = getModuleSpecifierByName(mod);
const dependencies = {};

const transformed = transform(join("src", mod), {
plugins: [new Plugin("fix-imports", {
visitor: {
ImportDeclaration (node, parent) {
const importSource = node.source.value;

if ( importSource[0] !== "." ) { return; }

if ( importSource[1] === "." ) {
const [category, name] = getModuleSpecifierByName(importSource.substr(3));
dependencies[`${source.name}.${category}.${name}`] = source.version;
node.source = t.Literal(`${source.name}.${category}.${name}`);
} else {
const name = importSource.substr(2);
dependencies[`${source.name}.${category}.${name}`] = source.version;
node.source = t.Literal(`${source.name}.${category}.${name}`);
}
},
},
})],
});

const pkg = {
...main,
name: `${source.name}.${category}.${name}`,
index: "index.js",
files: ["index.js", "index.js.map"],
dependencies: {
...source.dependencies,
...dependencies,
},
};

const dir = join("dist", "split", `${category}.${name}`);
mkdir(dir);
write(join(dir, "package.json"), JSON.stringify(pkg), "utf8");
write(join(dir, "index.js"), transformed.code, "utf8");
});

0 comments on commit 487fb8d

Please sign in to comment.