Skip to content

Commit

Permalink
simplify API and file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jul 19, 2019
1 parent 3417a73 commit c7f2afc
Show file tree
Hide file tree
Showing 23 changed files with 377 additions and 314 deletions.
75 changes: 42 additions & 33 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const rollup = require("rollup");
const babel = require("rollup-plugin-babel");
const eslint = require("rollup-plugin-eslint").eslint;
const includePaths = require("rollup-plugin-includepaths");
const uglify = require("uglify-js");
const gzipSize = require("gzip-size");
const fs = require("fs");
Expand All @@ -10,6 +9,12 @@ const pkg = require("../package.json");
const SPACES_RE = / /g;
const ENV_RE = /process\.env\.MOON_ENV/g;

const resolver = {
resolveId(id, origin) {
return path.extname(id) === "" ? path.join("./packages", id) + ".js" : id;
}
};

async function build(package) {
const options = require(`../packages/${package}/config.js`);
const name = options.name;
Expand All @@ -23,46 +28,50 @@ async function build(package) {
* https://kbrsh.github.io/moon
*/\r\n`;

const bundle = await rollup.rollup({
input: `./packages/${package}/src/index.js`,
plugins: [
includePaths({ paths: ["./packages"] }),
eslint(),
babel()
],
onwarn: (warning) => {
if (warning.code !== "CIRCULAR_DEPENDENCY") {
console.warn(`Rollup [Warn]: ${warning}`);
try {
const bundle = await rollup.rollup({
input: `./packages/${package}/src/index.js`,
plugins: [
resolver,
eslint(),
babel()
],
onwarn: (warning) => {
if (warning.code !== "CIRCULAR_DEPENDENCY") {
console.warn(`Rollup [Warn]: ${warning}`);
}
}
}
});
});

let { output } = await bundle.generate({
name: exportName,
format: "iife"
});
let { output } = await bundle.generate({
name: exportName,
format: "iife"
});

output = output[0].code;
output = output.replace(SPACES_RE, "\t");
output = output[0].code;
output = output.replace(SPACES_RE, "\t");

if (type === "module") {
output = fs.readFileSync("./build/wrapper.js").toString().replace("MODULE_NAME", exportName).replace("MODULE_CONTENT", output.split("\n").slice(1, -3).join("\n"));
}
if (type === "module") {
output = fs.readFileSync("./build/wrapper.js").toString().replace("MODULE_NAME", exportName).replace("MODULE_CONTENT", output.split("\n").slice(1, -3).join("\n"));
}

output = output.replace("'use strict'", "\"use strict\"");
output = output.replace("'use strict'", "\"use strict\"");

const developmentCode = comment + output.replace(ENV_RE, '"development"');
const productionCode = comment + uglify.minify(output.replace(ENV_RE, '"production"'), {
output: {
ascii_only: true
}
}).code;
const developmentCode = comment + output.replace(ENV_RE, '"development"');
const productionCode = comment + uglify.minify(output.replace(ENV_RE, '"production"'), {
output: {
ascii_only: true
}
}).code;

fs.writeFileSync(`./packages/${package}/dist/${package}.js`, developmentCode);
fs.writeFileSync(`./packages/${package}/dist/${package}.min.js`, productionCode);
fs.writeFileSync(`./packages/${package}/dist/${package}.js`, developmentCode);
fs.writeFileSync(`./packages/${package}/dist/${package}.min.js`, productionCode);

console.log(`${name} development -> ${developmentCode.length / 1000}kb / ${gzipSize.sync(developmentCode) / 1000}kb (gzip)`);
console.log(`${name} production -> ${productionCode.length / 1000}kb / ${gzipSize.sync(productionCode) / 1000}kb (gzip)\n`);
console.log(`${name} development -> ${developmentCode.length / 1000}kb / ${gzipSize.sync(developmentCode) / 1000}kb (gzip)`);
console.log(`${name} production -> ${productionCode.length / 1000}kb / ${gzipSize.sync(productionCode) / 1000}kb (gzip)\n`);
} catch (error) {
console.error(error);
}
}

const packages = fs.readdirSync("./packages");
Expand Down
4 changes: 2 additions & 2 deletions build/test/transform.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const babel = require("@babel/core");
const compile = require("../../packages/moon-compiler/dist/moon-compiler.min.js");
const compiler = require("../../packages/moon-compiler/dist/moon-compiler.min.js");

module.exports = {
process(src, filename) {
return babel.transformSync(compile(src), { filename, sourceMaps: true });
return babel.transformSync(compiler.compile(src), { filename, sourceMaps: true });
}
};
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
"@babel/preset-env": "^7.4.3",
"gzip-size": "^5.0.0",
"jest": "^24.6.0",
"rollup": "^1.8.0",
"rollup": "^1.17.0",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-eslint": "^5.1.0",
"rollup-plugin-includepaths": "^0.2.3",
"uglify-js": "^3.5.3"
"uglify-js": "^3.6.0"
}
}
11 changes: 9 additions & 2 deletions packages/moon-browser/dist/moon-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,13 @@
return prelude + output;
}

var compiler = {
compile: compile,
generate: generate,
lex: lex,
parse: parse
};

/**
* Head element
*/
Expand All @@ -1244,7 +1251,7 @@

if (src.length === 0) {
var scriptNew = document.createElement("script");
scriptNew.text = compile(script.text);
scriptNew.text = compiler.compile(script.text);
head.appendChild(scriptNew);
script.parentNode.removeChild(script);
load();
Expand All @@ -1255,7 +1262,7 @@
if (xhr.status === 0 || xhr.status === 200) {
var _scriptNew = document.createElement("script");

_scriptNew.text = compile(this.responseText);
_scriptNew.text = compiler.compile(this.responseText);
head.appendChild(_scriptNew);
} else {
error("Failed to load script with source \"" + src + "\" and status " + xhr.status + ".");
Expand Down
2 changes: 1 addition & 1 deletion packages/moon-browser/dist/moon-browser.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/moon-browser/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import compile from "moon-compiler/src/index.js";
import compiler from "moon-compiler/src/index";
import { error } from "util/util";

/**
Expand All @@ -21,7 +21,7 @@ function load() {

if (src.length === 0) {
const scriptNew = document.createElement("script");
scriptNew.text = compile(script.text);
scriptNew.text = compiler.compile(script.text);
head.appendChild(scriptNew);
script.parentNode.removeChild(script);
load();
Expand All @@ -32,7 +32,7 @@ function load() {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 0 || xhr.status === 200) {
const scriptNew = document.createElement("script");
scriptNew.text = compile(this.responseText);
scriptNew.text = compiler.compile(this.responseText);
head.appendChild(scriptNew);
} else {
error(`Failed to load script with source "${src}" and status ${xhr.status}.`);
Expand Down
2 changes: 1 addition & 1 deletion packages/moon-compiler/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
name: "Moon Compiler",
exportName: "MoonCompile",
exportName: "MoonCompiler",
type: "module"
};
11 changes: 9 additions & 2 deletions packages/moon-compiler/dist/moon-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
(function(root, factory) {
if (typeof module === "undefined") {
root.MoonCompile = factory();
root.MoonCompiler = factory();
} else {
module.exports = factory();
}
Expand Down Expand Up @@ -1229,5 +1229,12 @@
return prelude + output;
}

return compile;
var index = {
compile: compile,
generate: generate,
lex: lex,
parse: parse
};

return index;
}));
Loading

0 comments on commit c7f2afc

Please sign in to comment.