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

Separate modules #53

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,23 @@ function getItemPriceById (id) {

## Modules

Trine has been designed to be modular and decoupled from the ground up. Each exposed function is in its own module, so for example if you're using Webpack, instead of a huge library, you will only be transferring the needed functions to the client.
Trine has been designed to be modular and decoupled from the ground up. Each exposed function is in its own module, so for example if you're using Webpack, instead of a huge library, you will only be transferring the needed functions to the client. For example:

```javascript
import { add } from "trine/number/add";
```

Trine is also published as separate packages on npm to make the fingerprint smaller also when using node. The naming convention is using a similar scheme, except that slashes (`/`) are replaced with dots (`.`):

```
npm install --save trine.number.add
```

```javascript
import { add } from "trine.number.add";
```

This is the recommended way of using Trine.

## Installation

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"scripts": {
"prepublish": "./scripts/build.bash",
"publish-packages": "./scripts/publish-packages.bash",
"generate-packages": "./scripts/generate-packages.js",
"testsuite": "./scripts/testsuite.bash",
"test": "./scripts/test.bash",
Expand Down
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");
});
16 changes: 16 additions & 0 deletions scripts/publish-packages.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

root_dir=`pwd`

for package in dist/split/*; do
cd $package
npm publish
cd $root_dir
done


cd dist/main
npm publish
cd $root_dir
8 changes: 8 additions & 0 deletions tools/DocumentationBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,17 @@ ${ glob.sync(path.join("*", "*.js"), { cwd: "src" }).map((path) => {
return `tree.set("trine/${path.replace(/\.js$/, "")}", require("../src/${path}"));`;
}).join("\n") }

function splitModule (modulePath) {
const split = modulePath.split(/\\./g);
if ( split.length !== 3 ) { return modulePath; }
return split.join("/");
}

export function trineRequire (modulePath) {
if ( tree.has(modulePath) ) {
return tree.get(modulePath);
} else if ( tree.has(splitModule(modulePath)) ) {
return tree.get(splitModule(modulePath));
} else {
throw new Error("Module not found:" + modulePath);
}
Expand Down