diff --git a/.eslintrc.js b/.eslintrc.js index 319d9880..9ddb1039 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,8 +9,8 @@ module.exports = { }, extends: [ /* disbale rules temporary, before the codebase refactored with module system */ - // "eslint:recommended", + "eslint:recommended", "prettier", "prettier/babel" ] -} \ No newline at end of file +} diff --git a/Makefile b/Makefile index 7a56ff2a..e5fa4bea 100644 --- a/Makefile +++ b/Makefile @@ -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/* \ No newline at end of file + rm -f site/ide.html; rm -f build/* diff --git a/src/cli.js b/src/cli.js index 9ab9f138..27bea5f4 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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 diff --git a/src/compiler/base.js b/src/compiler/base.js index 9f63e41b..08cf7867 100644 --- a/src/compiler/base.js +++ b/src/compiler/base.js @@ -19,6 +19,4 @@ class BaseCompiler { } } var Base = BaseCompiler; -try { - module.exports = Base; -} catch (e) {} +module.exports = BaseCompiler; diff --git a/src/compiler/compilers.js b/src/compiler/compilers.js index 1a2a6624..68fbab13 100644 --- a/src/compiler/compilers.js +++ b/src/compiler/compilers.js @@ -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; + diff --git a/src/compiler/js.js b/src/compiler/js.js index 8516403a..0c821839 100644 --- a/src/compiler/js.js +++ b/src/compiler/js.js @@ -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 || []; @@ -303,6 +302,5 @@ class JSCompiler extends Base { } } const JS = JSCompiler; -try { - module.exports = JS; -} catch (e) {} +module.exports = JS; + diff --git a/src/compiler/py.js b/src/compiler/py.js index f0f00a65..70024392 100644 --- a/src/compiler/py.js +++ b/src/compiler/py.js @@ -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 || []; @@ -331,6 +330,5 @@ class Ctnr(): `; const PY = PYCompiler; -try { - module.exports = PY; -} catch (e) {} +module.exports = PY; + diff --git a/src/compiler/rb.js b/src/compiler/rb.js index 7ac361a1..a5cb2658 100644 --- a/src/compiler/rb.js +++ b/src/compiler/rb.js @@ -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()}`; @@ -381,6 +380,5 @@ require 'forwardable' ##### `; const RB = RBCompiler; -try { - module.exports = RB; -} catch (error) {} +module.exports = RB; + diff --git a/src/hanzi2num.js b/src/hanzi2num.js index d7622ebe..be13300e 100644 --- a/src/hanzi2num.js +++ b/src/hanzi2num.js @@ -258,6 +258,4 @@ function num2hanzi(n, nfrac = 6) { } } -try { - module.exports = { hanzi2num, num2hanzi }; -} catch (e) {} +module.exports = { hanzi2num, num2hanzi }; diff --git a/src/hanzi2pinyin.js b/src/hanzi2pinyin.js index 2b159fe5..c6166ef9 100644 --- a/src/hanzi2pinyin.js +++ b/src/hanzi2pinyin.js @@ -32806,6 +32806,4 @@ function hanzi2pinyin(a, system = "pinyin") { } return s; } -try { - module.exports = hanzi2pinyin; -} catch (e) {} +module.exports = hanzi2pinyin; diff --git a/src/highlight.js b/src/highlight.js index 7eadca56..8ca9c949 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -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", diff --git a/src/keywords.js b/src/keywords.js index 9e20a53d..d7e1756f 100644 --- a/src/keywords.js +++ b/src/keywords.js @@ -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 }; diff --git a/src/parser.js b/src/parser.js index 0ca2399a..ed7ad1ae 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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 = []; @@ -714,6 +712,10 @@ function compile( return targ; } +if (typeof version === 'undefined') { + let version = 'web-unknown'; +} + var parser = { compile, version, @@ -726,6 +728,4 @@ var parser = { NUMBER_KEYWORDS, STDLIB }; -try { - module.exports = parser; -} catch (e) {} +module.exports = parser; diff --git a/src/render.js b/src/render.js index 3afc4355..41ce3e4b 100644 --- a/src/render.js +++ b/src/render.js @@ -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"; diff --git a/src/stdlib.js b/src/stdlib.js index 48c71e4b..58282f33 100644 --- a/src/stdlib.js +++ b/src/stdlib.js @@ -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) {} diff --git a/src/version.js b/src/version.js index f7edb383..da68ce2c 100644 --- a/src/version.js +++ b/src/version.js @@ -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) {} diff --git a/test/.eslintrc.json b/test/.eslintrc.json new file mode 100644 index 00000000..6bb730d9 --- /dev/null +++ b/test/.eslintrc.json @@ -0,0 +1,7 @@ +{ + "env": { + "mocha": true, + "node": true, + "es6": true + } +} diff --git a/tools/make_ide.js b/tools/make_ide.js index cebd710c..4d4f4433 100644 --- a/tools/make_ide.js +++ b/tools/make_ide.js @@ -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; diff --git a/tools/make_site.js b/tools/make_site.js index c5f13257..03cad705 100644 --- a/tools/make_site.js +++ b/tools/make_site.js @@ -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"); @@ -148,7 +146,7 @@ pre{ #bg{ width: 100%; height: 400px; - // overflow: scroll; + /*overflow: scroll;*/ overflow: hidden; position: absolute; left: 0px; diff --git a/tools/test_parser.js b/tools/test_parser.js index d023934c..164639d9 100644 --- a/tools/test_parser.js +++ b/tools/test_parser.js @@ -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"); diff --git a/tools/utils.js b/tools/utils.js index 2e37f066..f4a7fd7f 100644 --- a/tools/utils.js +++ b/tools/utils.js @@ -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"]; @@ -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; diff --git a/webpack.config.js b/webpack.config.js index 964abed1..2b6fb4c6 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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'), @@ -59,4 +59,4 @@ const Utils = { } } -module.exports = [Cli, Core, Utils] \ No newline at end of file +module.exports = [Cli, Core, Utils]