Skip to content

Commit 9adc707

Browse files
committed
feat: cli and runtime
1 parent 07953d8 commit 9adc707

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/browser_runtime.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@
99
script.attributes.outputHanzi &&
1010
script.attributes.outputHanzi.value === "false"
1111
);
12+
const allowHttp = !!script.attributes.allowHttp;
1213
let code = script.innerText;
14+
let importPaths = [window.location.origin];
1315
if (script.src) {
1416
const response = await fetch(script.src);
1517
code = (await response.text()).toString();
18+
importPaths.push(
19+
script.src
20+
.split("/")
21+
.slice(0, -1)
22+
.join("/")
23+
);
1624
}
1725
execute(code, {
1826
scoped,
1927
outputHanzi,
2028
logCallback: isDev ? console.log : () => {},
21-
resetVarCnt: false
29+
resetVarCnt: false,
30+
importPaths,
31+
allowHttp
2232
});
2333
}
2434

src/cli.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ program
3838
"--roman [method]",
3939
'Romanize identifiers. The method can be "pinyin", "baxter" or "unicode"'
4040
)
41+
.option("--strict", "Enable static typechecking")
42+
.option("--allowHttp", "Allow to import from http")
43+
.option("--dir <path>", "Directory to importing from, seprates with comma(,)")
4144
.option("--outputHanzi", "Convert output to hanzi", true)
4245
.option("--log <file>", "Save log to file")
4346
.option("--title <title>", "Override title in rendering")
@@ -107,25 +110,35 @@ function preprocess() {
107110

108111
function getCompiled() {
109112
const source = getSource();
110-
const importPaths = getImportPaths();
111-
// console.log(importPaths)
112113

113-
return compile(program.lang, source, {
114+
return compile(source, {
115+
...getCompileOptions()
116+
});
117+
}
118+
119+
function getImportPaths() {
120+
const pathes = [];
121+
if (program.dir) {
122+
pathes.push(...program.dir.split(","));
123+
}
124+
pathes.push(...program.files.map(file => path.resolve(path.dirname(file))));
125+
pathes.push(path.resolve("."));
126+
return Array.from(new Set(pathes));
127+
}
128+
129+
function getCompileOptions() {
130+
return {
131+
lang: program.lang,
114132
romanizeIdentifiers: program.roman,
133+
strict: !!program.strict,
134+
allowHttp: !!program.allowHttp,
135+
importPaths: getImportPaths(),
115136
logCallback: logHandler(program.log, "a"),
116137
errorCallback: function(x) {
117138
console.error(x);
118139
process.exit();
119-
},
120-
importPaths
121-
});
122-
}
123-
124-
function getImportPaths() {
125-
const dir = new Set(
126-
program.files.map(file => path.resolve(path.dirname(file)))
127-
);
128-
return [...dir, path.resolve(".")];
140+
}
141+
};
129142
}
130143

131144
function resolvePath(x) {

src/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ function compile(arg1, arg2, arg3) {
754754
} else if (imports[i] in lib) {
755755
isrc = lib[imports[i]];
756756
} else {
757-
isrc = reader(imports[i], importPaths);
757+
isrc = reader(imports[i], importPaths, requestOptions);
758758
}
759759
targ =
760760
mwrapper(

0 commit comments

Comments
 (0)