Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
4f8a5c02
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
4f8a5c02
编写于
2月 01, 2020
作者:
A
Andy Finch
提交者:
GitHub
2月 01, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: support crate imports in deno_typescript (#3814)
Co-authored-by:
N
Ryan Dahl
<
ry@tinyclouds.org
>
上级
2cd39949
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
85 addition
and
26 deletion
+85
-26
cli/build.rs
cli/build.rs
+15
-6
core/lib.rs
core/lib.rs
+2
-0
core/modules.rs
core/modules.rs
+20
-0
deno_typescript/compiler_main.js
deno_typescript/compiler_main.js
+2
-2
deno_typescript/lib.rs
deno_typescript/lib.rs
+15
-4
deno_typescript/ops.rs
deno_typescript/ops.rs
+31
-14
未找到文件。
cli/build.rs
浏览文件 @
4f8a5c02
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use
deno_core
::
include_crate_modules
;
use
deno_core
::
CoreOp
;
use
deno_core
::
CoreOp
;
use
deno_core
::
Isolate
;
use
deno_core
::
Isolate
;
use
deno_core
::
Op
;
use
deno_core
::
Op
;
...
@@ -47,6 +48,8 @@ fn main() {
...
@@ -47,6 +48,8 @@ fn main() {
deno_typescript
::
ts_version
()
deno_typescript
::
ts_version
()
);
);
let
extern_crate_modules
=
include_crate_modules!
[
deno_core
];
// The generation of snapshots is slow and often unnecessary. Until we figure
// The generation of snapshots is slow and often unnecessary. Until we figure
// out how to speed it up, or avoid it when unnecessary, this env var provides
// out how to speed it up, or avoid it when unnecessary, this env var provides
// an escape hatch for the impatient hacker in need of faster incremental
// an escape hatch for the impatient hacker in need of faster incremental
...
@@ -65,8 +68,11 @@ fn main() {
...
@@ -65,8 +68,11 @@ fn main() {
let
bundle_path
=
o
.join
(
"CLI_SNAPSHOT.js"
);
let
bundle_path
=
o
.join
(
"CLI_SNAPSHOT.js"
);
let
snapshot_path
=
o
.join
(
"CLI_SNAPSHOT.bin"
);
let
snapshot_path
=
o
.join
(
"CLI_SNAPSHOT.bin"
);
let
main_module_name
=
let
main_module_name
=
deno_typescript
::
compile_bundle
(
deno_typescript
::
compile_bundle
(
&
bundle_path
,
root_names
)
&
bundle_path
,
root_names
,
Some
(
extern_crate_modules
.clone
()),
)
.expect
(
"Bundle compilation failed"
);
.expect
(
"Bundle compilation failed"
);
assert
!
(
bundle_path
.exists
());
assert
!
(
bundle_path
.exists
());
...
@@ -102,8 +108,11 @@ fn main() {
...
@@ -102,8 +108,11 @@ fn main() {
c
.join
(
"js/lib.deno.ns.d.ts"
),
c
.join
(
"js/lib.deno.ns.d.ts"
),
);
);
let
main_module_name
=
let
main_module_name
=
deno_typescript
::
compile_bundle
(
deno_typescript
::
compile_bundle
(
&
bundle_path
,
root_names
)
&
bundle_path
,
root_names
,
Some
(
extern_crate_modules
),
)
.expect
(
"Bundle compilation failed"
);
.expect
(
"Bundle compilation failed"
);
assert
!
(
bundle_path
.exists
());
assert
!
(
bundle_path
.exists
());
...
...
core/lib.rs
浏览文件 @
4f8a5c02
...
@@ -45,3 +45,5 @@ pub fn v8_version() -> &'static str {
...
@@ -45,3 +45,5 @@ pub fn v8_version() -> &'static str {
fn
test_v8_version
()
{
fn
test_v8_version
()
{
assert
!
(
v8_version
()
.len
()
>
3
);
assert
!
(
v8_version
()
.len
()
>
3
);
}
}
crate_modules!
();
core/modules.rs
浏览文件 @
4f8a5c02
...
@@ -449,6 +449,26 @@ impl fmt::Display for Deps {
...
@@ -449,6 +449,26 @@ impl fmt::Display for Deps {
}
}
}
}
#[macro_export]
macro_rules!
crate_modules
{
()
=>
{
pub
const
DENO_CRATE_PATH
:
&
'static
str
=
env!
(
"CARGO_MANIFEST_DIR"
);
};
}
#[macro_export]
macro_rules!
include_crate_modules
{
(
$
(
$x:ident
),
*
)
=>
{
{
let
mut
temp
:
HashMap
<
String
,
String
>
=
HashMap
::
new
();
$
(
temp
.insert
(
stringify!
(
$x
)
.to_string
(),
$x
::
DENO_CRATE_PATH
.to_string
());
)
*
temp
}
};
}
#[cfg(test)]
#[cfg(test)]
mod
tests
{
mod
tests
{
use
super
::
*
;
use
super
::
*
;
...
...
deno_typescript/compiler_main.js
浏览文件 @
4f8a5c02
...
@@ -184,8 +184,8 @@ class Host {
...
@@ -184,8 +184,8 @@ class Host {
fileName
=
moduleMap
.
get
(
fileName
);
fileName
=
moduleMap
.
get
(
fileName
);
}
}
const
{
sourceCode
,
moduleName
}
=
dispatch
(
"
readFi
le
"
,
{
const
{
sourceCode
,
moduleName
}
=
dispatch
(
"
loadModu
le
"
,
{
fileName
,
moduleUrl
:
fileName
,
languageVersion
,
languageVersion
,
shouldCreateNewSourceFile
shouldCreateNewSourceFile
});
});
...
...
deno_typescript/lib.rs
浏览文件 @
4f8a5c02
...
@@ -16,6 +16,7 @@ use deno_core::StartupData;
...
@@ -16,6 +16,7 @@ use deno_core::StartupData;
use
deno_core
::
ZeroCopyBuf
;
use
deno_core
::
ZeroCopyBuf
;
pub
use
ops
::
EmitResult
;
pub
use
ops
::
EmitResult
;
use
ops
::
WrittenFile
;
use
ops
::
WrittenFile
;
use
std
::
collections
::
HashMap
;
use
std
::
fs
;
use
std
::
fs
;
use
std
::
path
::
Path
;
use
std
::
path
::
Path
;
use
std
::
path
::
PathBuf
;
use
std
::
path
::
PathBuf
;
...
@@ -32,6 +33,8 @@ pub fn ts_version() -> String {
...
@@ -32,6 +33,8 @@ pub fn ts_version() -> String {
pkg
[
"version"
]
.as_str
()
.unwrap
()
.to_string
()
pkg
[
"version"
]
.as_str
()
.unwrap
()
.to_string
()
}
}
type
ExternCrateModules
=
HashMap
<
String
,
String
>
;
#[derive(Debug)]
#[derive(Debug)]
pub
struct
TSState
{
pub
struct
TSState
{
bundle
:
bool
,
bundle
:
bool
,
...
@@ -40,6 +43,7 @@ pub struct TSState {
...
@@ -40,6 +43,7 @@ pub struct TSState {
/// A list of files emitted by typescript. WrittenFile is tuple of the form
/// A list of files emitted by typescript. WrittenFile is tuple of the form
/// (url, corresponding_module, source_code)
/// (url, corresponding_module, source_code)
written_files
:
Vec
<
WrittenFile
>
,
written_files
:
Vec
<
WrittenFile
>
,
extern_crate_modules
:
ExternCrateModules
,
}
}
fn
compiler_op
<
D
>
(
fn
compiler_op
<
D
>
(
...
@@ -62,21 +66,27 @@ pub struct TSIsolate {
...
@@ -62,21 +66,27 @@ pub struct TSIsolate {
}
}
impl
TSIsolate
{
impl
TSIsolate
{
fn
new
(
bundle
:
bool
)
->
TSIsolate
{
fn
new
(
bundle
:
bool
,
maybe_extern_crate_modules
:
Option
<
ExternCrateModules
>
,
)
->
TSIsolate
{
let
mut
isolate
=
Isolate
::
new
(
StartupData
::
None
,
false
);
let
mut
isolate
=
Isolate
::
new
(
StartupData
::
None
,
false
);
js_check
(
isolate
.execute
(
"assets/typescript.js"
,
TYPESCRIPT_CODE
));
js_check
(
isolate
.execute
(
"assets/typescript.js"
,
TYPESCRIPT_CODE
));
js_check
(
isolate
.execute
(
"compiler_main.js"
,
COMPILER_CODE
));
js_check
(
isolate
.execute
(
"compiler_main.js"
,
COMPILER_CODE
));
let
extern_crate_modules
=
maybe_extern_crate_modules
.unwrap_or_default
();
let
state
=
Arc
::
new
(
Mutex
::
new
(
TSState
{
let
state
=
Arc
::
new
(
Mutex
::
new
(
TSState
{
bundle
,
bundle
,
exit_code
:
0
,
exit_code
:
0
,
emit_result
:
None
,
emit_result
:
None
,
written_files
:
Vec
::
new
(),
written_files
:
Vec
::
new
(),
extern_crate_modules
,
}));
}));
isolate
.register_op
(
isolate
.register_op
(
"
readFi
le"
,
"
loadModu
le"
,
compiler_op
(
state
.clone
(),
ops
::
json_op
(
ops
::
read_fi
le
)),
compiler_op
(
state
.clone
(),
ops
::
json_op
(
ops
::
load_modu
le
)),
);
);
isolate
isolate
.register_op
(
"exit"
,
compiler_op
(
state
.clone
(),
ops
::
json_op
(
ops
::
exit
)));
.register_op
(
"exit"
,
compiler_op
(
state
.clone
(),
ops
::
json_op
(
ops
::
exit
)));
...
@@ -125,8 +135,9 @@ impl TSIsolate {
...
@@ -125,8 +135,9 @@ impl TSIsolate {
pub
fn
compile_bundle
(
pub
fn
compile_bundle
(
bundle_filename
:
&
Path
,
bundle_filename
:
&
Path
,
root_names
:
Vec
<
PathBuf
>
,
root_names
:
Vec
<
PathBuf
>
,
extern_crate_modules
:
Option
<
ExternCrateModules
>
,
)
->
Result
<
String
,
ErrBox
>
{
)
->
Result
<
String
,
ErrBox
>
{
let
ts_isolate
=
TSIsolate
::
new
(
true
);
let
ts_isolate
=
TSIsolate
::
new
(
true
,
extern_crate_modules
);
let
config_json
=
serde_json
::
json!
({
let
config_json
=
serde_json
::
json!
({
"compilerOptions"
:
{
"compilerOptions"
:
{
...
...
deno_typescript/ops.rs
浏览文件 @
4f8a5c02
...
@@ -35,16 +35,16 @@ pub fn json_op(d: Dispatcher) -> impl Fn(&mut TSState, &[u8]) -> CoreOp {
...
@@ -35,16 +35,16 @@ pub fn json_op(d: Dispatcher) -> impl Fn(&mut TSState, &[u8]) -> CoreOp {
#[derive(Debug,
Deserialize)]
#[derive(Debug,
Deserialize)]
#[serde(rename_all
=
"camelCase"
)]
#[serde(rename_all
=
"camelCase"
)]
struct
ReadFi
le
{
struct
LoadModu
le
{
file_name
:
String
,
module_url
:
String
,
language_version
:
Option
<
i32
>
,
language_version
:
Option
<
i32
>
,
should_create_new_source_file
:
bool
,
should_create_new_source_file
:
bool
,
}
}
pub
fn
read_file
(
_
s
:
&
mut
TSState
,
v
:
Value
)
->
Result
<
Value
,
ErrBox
>
{
pub
fn
load_module
(
s
:
&
mut
TSState
,
v
:
Value
)
->
Result
<
Value
,
ErrBox
>
{
let
v
:
ReadFi
le
=
serde_json
::
from_value
(
v
)
?
;
let
v
:
LoadModu
le
=
serde_json
::
from_value
(
v
)
?
;
let
(
module_name
,
source_code
)
=
if
v
.
file_name
.starts_with
(
"$asset$/"
)
{
let
(
module_name
,
source_code
)
=
if
v
.
module_url
.starts_with
(
"$asset$/"
)
{
let
asset
=
v
.
file_name
.replace
(
"$asset$/"
,
""
);
let
asset
=
v
.
module_url
.replace
(
"$asset$/"
,
""
);
let
source_code
=
match
crate
::
get_asset
(
&
asset
)
{
let
source_code
=
match
crate
::
get_asset
(
&
asset
)
{
Some
(
code
)
=>
code
.to_string
(),
Some
(
code
)
=>
code
.to_string
(),
...
@@ -58,14 +58,31 @@ pub fn read_file(_s: &mut TSState, v: Value) -> Result<Value, ErrBox> {
...
@@ -58,14 +58,31 @@ pub fn read_file(_s: &mut TSState, v: Value) -> Result<Value, ErrBox> {
(
asset
,
source_code
)
(
asset
,
source_code
)
}
else
{
}
else
{
assert
!
(
!
v
.file_name
.starts_with
(
"$assets$"
),
"you meant $asset$"
);
assert
!
(
!
v
.module_url
.starts_with
(
"$assets$"
),
"you meant $asset$"
);
let
module_specifier
=
ModuleSpecifier
::
resolve_url_or_path
(
&
v
.file_name
)
?
;
let
module_specifier
=
ModuleSpecifier
::
resolve_url_or_path
(
&
v
.module_url
)
?
;
let
path
=
module_specifier
.as_url
()
.to_file_path
()
.unwrap
();
let
module_url
=
module_specifier
.as_url
();
match
module_url
.scheme
()
{
"file"
=>
{
let
path
=
module_url
.to_file_path
()
.unwrap
();
println!
(
"cargo:rerun-if-changed={}"
,
path
.display
());
println!
(
"cargo:rerun-if-changed={}"
,
path
.display
());
(
(
module_specifier
.as_str
()
.to_string
(),
module_specifier
.as_str
()
.to_string
(),
std
::
fs
::
read_to_string
(
&
path
)
?
,
std
::
fs
::
read_to_string
(
&
path
)
?
,
)
)
}
"crate"
=>
{
let
crate_name
=
module_url
.host_str
()
.unwrap
();
// TODO(afinch7) turn failures here into real error messages.
let
path_prefix
=
s
.extern_crate_modules
.get
(
crate_name
)
.unwrap
();
let
path
=
std
::
path
::
Path
::
new
(
path_prefix
)
.join
(
&
module_url
.path
()[
1
..
]);
(
module_specifier
.as_str
()
.to_string
(),
std
::
fs
::
read_to_string
(
&
path
)
?
,
)
}
_
=>
unimplemented!
(),
}
};
};
Ok
(
json!
({
Ok
(
json!
({
"moduleName"
:
module_name
,
"moduleName"
:
module_name
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录