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

chore: use the sensible build... compatibly #419

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = {
},
extends: [
/* disbale rules temporary, before the codebase refactored with module system */
// "eslint:recommended",
"eslint:recommended",
"prettier",
"prettier/babel"
]
}
}
22 changes: 16 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
all: ide cmdline
ide:
cd tools; node make_ide.js;
cmdline:
cd tools; node make_cmdline.js;
ALLSRC = $(wildcard lib/*.js) $(wildcard src/*.js)
ALLTOOL = $(wildcard tools/*.js)

all: ide site
.PHONY: ide site clean

ide: site/ide.html
site: site/index.html

site/ide.html: ${ALLSRC} ${ALLTOOL}
node tools/make_ide.js;

site/index.html: ${ALLSRC} ${ALLTOOL}
node tools/make_site.js > site/index.html;

clean:
rm -f site/ide.html; rm -f build/*
rm -f site/ide.html; rm -f build/*
6 changes: 5 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const { render, unrender } = require("./render");
const path = require("path");
const commander = require("commander");

var Logo = ` ,_ ,_\n \\/ ==\n /\\ []\n`;
var Logo = `\
,_ ,_
|/ ==
/| []
`.replace(/\|/g, "\\");

const program = new commander.Command();
program
Expand Down
4 changes: 1 addition & 3 deletions src/compiler/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ class BaseCompiler {
}
}
var Base = BaseCompiler;
try {
module.exports = Base;
} catch (e) {}
module.exports = BaseCompiler;
13 changes: 5 additions & 8 deletions src/compiler/compilers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
try {
var JS = require("./js");
var PY = require("./py");
var RB = require("./rb");
} catch (e) {}
var JS = require("./js");
var PY = require("./py");
var RB = require("./rb");
const compilers = {
js: JS,
py: PY,
rb: RB
};

try {
module.exports = compilers;
} catch (e) {}
module.exports = compilers;

10 changes: 4 additions & 6 deletions src/compiler/js.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
try {
var Base = require("./base");
} catch (e) {}
var Base = require("./base");

class JSCompiler extends Base {
compile(options = {}) {
var imports = options.imports || [];
Expand Down Expand Up @@ -303,6 +302,5 @@ class JSCompiler extends Base {
}
}
const JS = JSCompiler;
try {
module.exports = JS;
} catch (e) {}
module.exports = JS;

10 changes: 4 additions & 6 deletions src/compiler/py.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
try {
var Base = require("./base");
} catch (e) {}
var Base = require("./base");

class PYCompiler extends Base {
compile(options = {}) {
var imports = options.imports || [];
Expand Down Expand Up @@ -331,6 +330,5 @@ class Ctnr():
`;

const PY = PYCompiler;
try {
module.exports = PY;
} catch (e) {}
module.exports = PY;

10 changes: 4 additions & 6 deletions src/compiler/rb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
try {
var Base = require("./base");
} catch (e) {}
var Base = require("./base");

class RBCompiler extends Base {
rename(name) {
return name && `${name.toLowerCase()}`;
Expand Down Expand Up @@ -381,6 +380,5 @@ require 'forwardable'
#####
`;
const RB = RBCompiler;
try {
module.exports = RB;
} catch (error) {}
module.exports = RB;

4 changes: 1 addition & 3 deletions src/hanzi2num.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,4 @@ function num2hanzi(n, nfrac = 6) {
}
}

try {
module.exports = { hanzi2num, num2hanzi };
} catch (e) {}
module.exports = { hanzi2num, num2hanzi };
4 changes: 1 addition & 3 deletions src/hanzi2pinyin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32806,6 +32806,4 @@ function hanzi2pinyin(a, system = "pinyin") {
}
return s;
}
try {
module.exports = hanzi2pinyin;
} catch (e) {}
module.exports = hanzi2pinyin;
6 changes: 2 additions & 4 deletions src/highlight.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
try {
var { num2hanzi } = require("./hanzi2num");
var { NUMBER_KEYWORDS, KEYWORDS } = require("./keywords");
} catch (e) {}
var { num2hanzi } = require("./hanzi2num");
var { NUMBER_KEYWORDS, KEYWORDS } = require("./keywords");

var DEFAULT_COLORS = {
ctrl: "#F92672",
Expand Down
4 changes: 1 addition & 3 deletions src/keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,4 @@ if (!Object.fromEntries) {
}
var KEYWORDS = Object.fromEntries(ke);

try {
module.exports = { NUMBER_KEYWORDS, KEYWORDS };
} catch (e) {}
module.exports = { NUMBER_KEYWORDS, KEYWORDS };
22 changes: 11 additions & 11 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
try {
var { hanzi2num, num2hanzi } = require("./hanzi2num");
var hanzi2pinyin = require("./hanzi2pinyin");
var STDLIB = require("./stdlib");
var { NUMBER_KEYWORDS, KEYWORDS } = require("./keywords");
var version = require("./version");
var compilers = require("./compiler/compilers");
} catch (e) {}
var { hanzi2num, num2hanzi } = require("./hanzi2num");
var hanzi2pinyin = require("./hanzi2pinyin");
var STDLIB = require("./stdlib");
var { NUMBER_KEYWORDS, KEYWORDS } = require("./keywords");
var version = require("./version");
var compilers = require("./compiler/compilers");

function wy2tokens(txt) {
var tokens = [];
Expand Down Expand Up @@ -714,6 +712,10 @@ function compile(
return targ;
}

if (typeof version === 'undefined') {
let version = 'web-unknown';
}

var parser = {
compile,
version,
Expand All @@ -726,6 +728,4 @@ var parser = {
NUMBER_KEYWORDS,
STDLIB
};
try {
module.exports = parser;
} catch (e) {}
module.exports = parser;
10 changes: 4 additions & 6 deletions src/render.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
try {
var fs = require("fs");
var { semantic } = require("./highlight");
var { num2hanzi } = require("./hanzi2num");
var parser = require("./parser");
} catch (e) {}
var fs = require("fs");
var { semantic } = require("./highlight");
var { num2hanzi } = require("./hanzi2num");
var parser = require("./parser");

const FONT = "'I.Ming', 'Source Han Serif KR', 'Noto Serif CJK KR', serif"; //"Source Han Serif TC"
const RED = "#E53";
Expand Down
32 changes: 16 additions & 16 deletions src/stdlib.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
try {
function loadStdlib() {
const STDLIB = {};
function loadStdlib() {
const STDLIB = {};

const raw = require.context("../lib", true, /.*\.wy$/);
const raw = require.context("../lib", true, /.*\.wy$/);

raw.keys().forEach(key => {
const parts = key.slice(2, -3).split("/");
const data = raw(key).default;
let lib = STDLIB;
for (const part of parts.slice(0, -1)) {
if (!lib[part]) lib[part] = {};
lib = lib[part];
}
lib[parts[parts.length - 1]] = data;
});
raw.keys().forEach(key => {
const parts = key.slice(2, -3).split("/");
const data = raw(key).default;
let lib = STDLIB;
for (const part of parts.slice(0, -1)) {
if (!lib[part]) lib[part] = {};
lib = lib[part];
}
lib[parts[parts.length - 1]] = data;
});

return STDLIB;
}
return STDLIB;
}

try {
module.exports = loadStdlib();
} catch (e) {}
9 changes: 6 additions & 3 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
try {
const package = require("../package.json");
var package = require("../package.json");
} catch (e) {
var package = { version: 'Unknown' };
}

module.exports = package.version;

module.exports = package.version;
} catch (e) {}
7 changes: 7 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"mocha": true,
"node": true,
"es6": true
}
}
4 changes: 1 addition & 3 deletions tools/make_ide.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
try {
process.chdir("./tools");
} catch (e) {} //make sure we're in tools directory
process.chdir(__dirname); //make sure we're in tools directory

var fs = require("fs");
var execSync = require("child_process").execSync;
Expand Down
6 changes: 2 additions & 4 deletions tools/make_site.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
try {
process.chdir("./tools");
} catch (e) {} //make sure we're in tools directory
process.chdir(__dirname); //make sure we're in tools directory

const fs = require("fs");
const utils = require("./utils");
Expand Down Expand Up @@ -148,7 +146,7 @@ pre{
#bg{
width: 100%;
height: 400px;
// overflow: scroll;
/*overflow: scroll;*/
overflow: hidden;
position: absolute;
left: 0px;
Expand Down
4 changes: 1 addition & 3 deletions tools/test_parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
try {
process.chdir("./tools");
} catch (e) {}
process.chdir(__dirname); //make sure we're in tools directory

var fs = require("fs");
var parser = require("../src/parser");
Expand Down
11 changes: 9 additions & 2 deletions tools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ function remotelib(urls) {
}

function catsrc() {
var s = "";
var s = `
;
if (typeof require !== 'function') { require = function() { return undefined; } }
if (typeof module !== 'object') { module = {} }
;`;
var rootPath = "../src/";
var compilerPath = "../src/compiler/";
var compilerList = ["base", "js", "py", "rb", "compilers"];
Expand All @@ -20,13 +24,16 @@ function catsrc() {
var srcs = fs.readdirSync(rootPath).map(filename => rootPath + filename);
srcs = srcs.concat(compilerList);

// "Pacify" the source: don't require anything and don't exclusively declare anything
for (var i = 0; i < srcs.length; i++) {
if (srcs[i].endsWith(".js") && !srcs[i].includes("cli")) {
s +=
fs
.readFileSync(srcs[i])
.toString()
.replace(/const\s/g, "var ") + ";\n";
.replace(/(const|let)\s/g, "var ")
.replace(/^\s*var\s([{},\s\w]+)\s=\srequire(\("[^"]+"\));?$/gm, "/* $1 = REQ $2 */;");
s += ";\n";
}
}
return s;
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Default = () => {
return {
devtool: 'source-map',
output: {
globalObject: '(typeof self !== "undefined" ? self : this)', // make it works for both node and browser
globalObject: 'globalThis', // make it works for both node and browser
libraryTarget: 'umd2',
library: ["Wenyan", "[name]"],
path: path.resolve(__dirname, 'dist'),
Expand Down Expand Up @@ -59,4 +59,4 @@ const Utils = {
}
}

module.exports = [Cli, Core, Utils]
module.exports = [Cli, Core, Utils]