typescript 角度编译器“编译”什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46670430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:57:42  来源:igfitidea点击:

What does the angular compiler "compile"?

javascriptangulartypescriptangular-cliangular-compiler-cli

提问by codepleb

I was asked that today and was not able to give a proper answer.

我今天被问到这个问题,无法给出正确的答案。

Typescript transpiles to JS. Then there is tree shaking, "less" (optional) and what else in the process of making a deployment. But nothing like that (afaik) has anything to do with "compiling". Everything gets bundled and heavily optimized, but it's not actually compiled, right?

打字稿转译为 JS。然后是摇树,“少”(可选)以及部署过程中的其他内容。但是这样的(afaik)与“编译”没有任何关系。一切都被捆绑并进行了大量优化,但实际上并没有编译,对吧?

There is even an "ahead of time"-compiler, which really does a noticeable job. What do I miss?

甚至还有一个“提前”编译器,它确实做了一个引人注目的工作。我想念什么?

Javascript itself is still interpreted, right?

Javascript 本身仍然是解释性的,对吗?

回答by Liam

You're presuming compiling means taking the source code and producing machine code, low-level codes, etc. But compiling actually just means taking one source code and turning it into another. So it seems reasonable to say that taking Typescript and producing JavaScript is a formof compiling. It's not dissimilar to what (for example) c# does when its compiled into IL language.

您假设编译意味着获取源代码并生成机器代码、低级代码等。但编译实际上只是意味着获取一个源代码并将其转换为另一个。所以说采用 Typescript 并生成 JavaScript 是一种编译形式似乎是合理的。它与(例如)c# 在编译成 IL 语言时所做的没有什么不同。

That said, I'd say a better word for this is Transpiling. I'd suggest that the Typescript compiler is betterdescribed as a Transpiler.

也就是说,我会说一个更好的词是Transpiling。我建议将 Typescript 编译器更好地描述为 Transpiler。

The difference is subtle and a transpiler can be thought of as a type of compiler; but a (pure)compiled language is (usually) turning a high-level language to a low(er) level language (nearer to machine code), like the C# example. A transpiler turns a high-level language into a similar level (of abstraction) language (also high level).*

区别很细微,可以将转译器视为一种编译器;但是(纯)编译语言(通常)正在将高级语言转换为低级(更)级语言(更接近机器代码),就像 C# 示例一样。转译器将高级语言转换为类似级别的(抽象)语言(也是高级)。*

The result of the compiled code is typically not a language that you would write yourself. The result of a transpiler is another high-level language. In theory, you could write IL (as an example) but it's really designed to be produced by a compiler and there are no tools or support for doing this, you produce IL by compiling C#/vb.net, only. Whereas Javascript is a usable (and used) programming language in its own right.

编译代码的结果通常不是您自己编写的语言。转译器的结果是另一种高级语言。理论上,您可以编写 IL(作为示例),但它实际上是设计为由编译器生成的,并且没有工具或支持来执行此操作,您只能通过编译 C#/vb.net 来生成 IL。而 Javascript 本身就是一种可用(和使用)的编程语言。

*Lots of caveats as the definitions of these words and their usage are pretty vague

*很多警告,因为这些词的定义及其用法非常模糊

回答by Max Koretskyi

You seem to be asking three questions in one:

你似乎在问三个问题:

  • What is the difference between a compiler and a transpiler?
  • Do Angular and TypeScript implement compilers or transpilers?
  • Is there a separate Angular compiler? What does it compile?
  • 编译器和转译器有什么区别?
  • Angular 和 TypeScript 是否实现了编译器或转译器?
  • 是否有单独的 Angular 编译器?它编译什么?

What is the difference between a compiler and transpiler?

编译器和转译器有什么区别?

@J?rgWMittag provided a very good answerto this question.

@J?rgWMittag为这个问题提供了一个很好的答案

Do Angular and TypeScript implement compilers or transpilers?

Angular 和 TypeScript 是否实现了编译器或转译器?

Both TS and Angular implement realcompilers. They follow the same stages of lexical analysis, parsing, semantic analysis, and code generation as C/C++ compilers that produce assembly code (except probably for optimization). You can see that the class/folder are named "compiler" in both Angularand TS.

TS 和 Angular 都实现了真正的编译器。它们遵循与生成汇编代码的 C/C++ 编译器相同的词法分析、解析、语义分析和代码生成阶段(可能优化除外)。您可以看到类/文件夹在AngularTS中都被命名为“编译器” 。

The angular compiler is not really related to TypeScript compiler. These are very different compilers.

angular 编译器与 TypeScript 编译器并没有真正的关系。这些是非常不同的编译器。

Is there a separate Angular compiler? What does it compile?

是否有单独的 Angular 编译器?它编译什么?

Angular has two compilers:

Angular 有两个编译器:

  • View Compiler
  • Module Compiler
  • 查看编译器
  • 模块编译器

The job of the view compiler is to transform the template you specify for the component template into the internal representation of a component which is a view factorythat is then used to instantiate a view instance.

视图编译器的工作是将您为组件模板指定的模板转换为组件的内部表示,该组件是一个视图工厂,然后用于实例化视图实例

Besides transforming the template, the view compiler also compiles various metadata information in the form of decorators like @HostBinding, @ViewChildetc.

除了转换模板之外,视图编译器还以装饰器的形式编译各种元数据信息,例如@HostBinding@ViewChild等。

Suppose you define a component and its template like this:

假设您像这样定义一个组件及其模板:

@Component({
  selector: 'a-comp',
  template: '<span>A Component</span>'
})
class AComponent {}

Using this data the compiler generates the following slightly simplified component factory:

使用此数据,编译器生成以下稍微简化的组件工厂:

function View_AComponent {
  return jit_viewDef1(0,[
      elementDef2(0,null,null,1,'span',...),
      jit_textDef3(null,['My name is ',...])
    ]

It describes the structure of a component view and is used when instantiating the component. The first node is element definition and the second one is text definition. You can see that each node gets the information it needs when being instantiated through parameters list. It's a job of a compiler to resolve all the required dependencies and provide them at the runtime.

它描述了组件视图的结构,并在实例化组件时使用。第一个节点是元素定义,第二个节点是文本定义。可以看到每个节点在实例化时通过参数列表获取到需要的信息。解析所有必需的依赖项并在运行时提供它们是编译器的一项工作。

I strongly recommend reading these articles:

我强烈建议阅读这些文章:

Also, see the answer to What is the difference between Angular AOT and JIT compiler.

另外,请参阅什么是 Angular AOT 和 JIT 编译器之间的区别的答案

The job of the module compiler is to create a module factory which basically contains merged definitions of the providers.

模块编译器的工作是创建一个模块工厂,它基本上包含提供者的合并定义。

For more information, read:

有关更多信息,请阅读:

回答by J?rg W Mittag

Typescript transpires to JS. Then there is tree shaking, "less" (optional) and what else in the process of making a deployment. But nothing like that (afaik) has anything to do with "compiling". Everything gets bundled and heavily optimized, but it's not actually compiled, right?

打字稿转化为 JS。然后是摇树,“少”(可选)以及部署过程中的其他内容。但是这样的(afaik)与“编译”没有任何关系。一切都被捆绑并进行了大量优化,但实际上并没有编译,对吧?

Compilationmeans transforming a program written in a language Ainto a semantically equivalent program written in language Bsuch that evaluating the compiled program according to the rules of language B(for example interpreting it with an interpreter for B) yields the same result and has the same side-effects as evaluating the original program according to the rules of language A(for example interpreting it with an interpreter for A).

编译意味着将用语言A编写的程序转换成用语言B编写的语义等效程序,以便根据语言B的规则评估编译后的程序(例如用B的解释器解释它)产生相同的结果,并具有与根据语言A的规则评估原始程序相同的副作用(例如用A的解释器解释它)。

Compilation simply means translating a program from language Ato language B. That's all it means. (Also note that it is perfectly possible for Aand Bto be the same language.)

编译只是意味着将程序从A语言翻译成B语言。这就是全部意思。(另请注意,AB完全有可能是同一种语言。)

In some cases, we have more specialized names for certain kinds of compilers, depending on what Aand Bare, and what the compiler does:

在某些情况下,根据AB是什么以及编译器做什么,我们为某些类型的编译器提供了更专业的名称:

  • if Ais perceived to be assembly language and Bis perceived to be machine language, then we call it an assembler,
  • if Ais perceived to be machine language and Bis perceived to be assembly language, then we call it a disassembler,
  • if Ais perceived to be lower-level than B, then we call it a decompiler,
  • if Aand Bare the same language, and the resulting program is in some way faster or lighter, then we call it an optimizer,
  • if Aand Bare the same languages, and the resulting program is smaller, then we call it a minifier,
  • if Aand Bare the same languages, and the resulting program is less readable, then we call it an obfuscator,
  • if Aand Bare perceived to be at roughly the same level of abstraction, then we call it a transpiler, and
  • if Aand Bare perceived to be at roughly the same level of abstraction and the resulting program preserves formatting, comments, and programmer intent such that it is possible to maintain the resulting the program in the same fashion as the original program, then we call it a re-engineering tool.
  • 如果A被认为是汇编语言而B被认为是机器语言,那么我们称之为汇编程序
  • 如果A被认为是机器语言而B被认为是汇编语言,那么我们称之为反汇编器
  • 如果A被认为比B级别低,那么我们称其为反编译器
  • 如果AB是同一种语言,并且生成的程序在某种程度上更快或更轻,那么我们称之为优化器
  • 如果AB是相同的语言,并且生成的程序更小,那么我们称其为minifier
  • 如果AB是相同的语言,并且生成的程序可读性较差,那么我们将其称为混淆器
  • 如果AB被认为处于大致相同的抽象级别,那么我们称其为转译器,并且
  • 如果AB被认为处于大致相同的抽象级别,并且生成的程序保留了格式、注释和程序员意图,以便可以以与原始程序相同的方式维护生成的程序,那么我们调用它是一个重新设计的工具

Also, note that older sources may use the terms "translation" and "translator" instead of "compilation" and "compiler". For example, C talks about "translation units".

另请注意,较旧的来源可能使用术语“翻译”和“翻译器”而不是“编译”和“编译器”。例如,C 谈到“翻译单元”。

You may also stumble across the term "language processor". This can mean either a compiler, an interpreter, or both compilers and interpreters depending on the definition.

您可能还会偶然发现术语“语言处理器”。这可以表示编译器、解释器或编译器和解释器两者,具体取决于定义。

Javascript itself is still interpreted, right?

Javascript 本身仍然是解释性的,对吗?

JavaScript is a language. Languages are a set of logical rules and restrictions. Languages aren't interpreted or compiled. Languages just are.

JavaScript 是一种语言。语言是一组逻辑规则和限制。语言不会被解释或编译。语言只是

Compilation and interpretation are traits of a compiler or interpreter (duh!). Every language can be implemented with a compiler and every language can be implemented with an interpreter. Many languages have both compilers and interpreters. Many modern high-performance execution engines have both at least one compiler and at least one interpreter.

编译和解释是编译器或解释器的特征(废话!)。每种语言都可以用编译器来实现,每种语言都可以用解释器来实现。许多语言都有编译器和解释器。许多现代高性能执行引擎同时具有至少一个编译器和至少一个解释器。

These two terms belong on different layers of abstraction. If English were a typed language, "interpreted-language" would be a type error.

这两个术语属于不同的抽象层。如果英语是一种类型语言,“解释语言”将是一个类型错误。

Note also that some languages have neither an interpreter nor a compiler. There are languages which have no implementation at all. Still, they are languages, and you can write programs in them. You just can't run them.

另请注意,某些语言既没有解释器也没有编译器。有些语言根本没有实现。尽管如此,它们还是语言,您可以用它们编写程序。你就是不能运行它们。

Also, note that everything is interpreted at some point: if you want to execute something, you mustinterpret it. Compilation just translates code from one language to another. It doesn't run it. Interpretationruns it. (Sometimes, when an interpreter is implemented in hardware, we call it a "CPU", but it's still an interpreter.)

另外,请注意,一切都会在某个时候被解释:如果你想执行某事,你必须解释它。编译只是将代码从一种语言翻译成另一种语言。它不运行它。解释运行它。(有时,当解释器在硬件中实现时,我们称它为“CPU”,但它仍然是一个解释器。)

Case in point: every single currently existing mainstream JavaScript implementation has a compiler.

举个例子:每个当前存在的主流 JavaScript 实现都有一个编译器。

V8 started out as a pure compiler: it compiled JavaScript straight to moderately optimized native machine code. Later, a second compiler was added. Now, there are two compilers: a lightweight compiler that produces moderately optimized code but the compiler itself is very fast and uses little RAM. This compiler also injects profiling code into the compiled code. The second compiler is a more heavyweight, slower, more expensive compiler, which, however, produces much tighter, much faster code. It also uses the results of the profiling code injected by the first compiler to make dynamic optimization decisions. Also, the decision which code to re-compile using the second compiler is made based on that profiling information. Note that at no time there is an interpreter involved. V8 never interprets, it always compiles. It doesn't even contain an interpreter. (Actually, I believe nowadays it does, I am describing the first two iterations.)

V8 最初是一个纯编译器:它直接将 JavaScript 编译为适度优化的本地机器代码。后来又添加了第二个编译器。现在,有两种编译器:一种是轻量级编译器,可生成适度优化的代码,但编译器本身速度非常快,占用的 RAM 很少。此编译器还将分析代码注入已编译的代码中。第二个编译器是一个更重量级、更慢、更昂贵的编译器,然而,它生成更紧凑、更快的代码。它还使用第一个编译器注入的分析代码的结果来做出动态优化决策。此外,根据分析信息决定使用第二个编译器重新编译哪些代码。请注意,在任何时候都不会涉及口译员。V8 从不解释,它总是编译。它没有 甚至包含一个口译员。(实际上,我相信现在确实如此,我正在描述前两次迭代。)

SpiderMonkey compiles JavaScript to SpiderMonkey bytecode, which it then interprets. The interpreter also profiles the code, and then the code which gets executed most often is compiled by a compiler to native machine code. So, SpiderMonkey contains twocompilers: one from JavaScript to SpiderMonkey bytecode, and another from SpiderMonkey bytecode to native machine code.

SpiderMonkey 将 JavaScript 编译为 SpiderMonkey 字节码,然后对其进行解释。解释器还会分析代码,然后最常执行的代码由编译器编译为本地机器代码。因此,SpiderMonkey 包含两种编译器:一种是从 JavaScript 到 SpiderMonkey 字节码,另一种是从 SpiderMonkey 字节码到本地机器码。

Almost all JavaScript execution engines (with the exception of V8) follow this model of an AOT compiler that compiles JavaScript to bytecode, and a mixed-mode engine that switches between interpreting and compiling that bytecode.

几乎所有 JavaScript 执行引擎(V8 除外)都遵循这种 AOT 编译器模型,将 JavaScript 编译为字节码,以及在解释和编译字节码之间切换的混合模式引擎。

You wrote in a comment:

你在评论中写道:

I really was thinking that machine code is somewhere involved.

我真的在想机器代码在某个地方。

What does "machine code" even mean?

“机器代码”甚至是什么意思?

What is one man's machine language is another man's intermediate language and vice versa? For example, there are CPUs which can natively execute JVM bytecode, on such a CPU, JVM bytecode isnative machine code. And there are interpreters for x86 machine code, when you run of those x86 machine code isinterpreted bytecode.

什么是一个人的机器语言是另一个人的中间语言,反之亦然?例如,有可以本地执行JVM字节码的CPU,在这样的CPU上,JVM字节码就是本地机器码。并且有 x86 机器码的解释器,当你运行那些 x86 机器码时,就是解释过的字节码。

There is an x86 interpreter called JPC written in Java. If I run x86 machine code on JPC running on a native JVM CPU …?which is the bytecode and which is the native code? If I compile x86 machine code to JavaScript (yes, there are tools which can do that) and run it in a browser on my phone (which has an ARM CPU), which is the bytecode and which is the native machine code? What if the program I am compiling is a SPARC emulator, and I use it to run SPARC code?

有一个用 Java 编写的名为 JPC 的 x86 解释器。如果我在运行在本机 JVM CPU 上的 JPC 上运行 x86 机器代码……哪个是字节码,哪个是本机代码?如果我将 x86 机器码编译为 JavaScript(是的,有一些工具可以做到这一点)并在我的手机(有一个 ARM CPU)上的浏览器中运行它,哪个是字节码,哪个是本机机器码?如果我正在编译的程序是一个 SPARC 仿真器,并且我用它来运行 SPARC 代码怎么办?

Note that everylanguage induces an abstract machine, and is machine language for that machine. So, every language (including very high-level languages) is native machine code. Also, you can write an interpreter for every language. So, every language (including x86 machine code) is not-native.

请注意,每种语言都包含一个抽象机器,并且是该机器的机器语言。因此,每种语言(包括非常高级的语言)都是本机机器代码。此外,您可以为每种语言编写一个解释器。因此,每种语言(包括 x86 机器代码)都不是本地语言。

回答by Nathan Cooper

Getting the code you've written to run on a browser involves two things:

让您编写的代码在浏览器上运行涉及两件事:

1) Transpiling the Typescript into JavaScript. This is kind of a solved problem. I think they just use webpack.

1) 将Typescript 转译为 JavaScript。这是一个已经解决的问题。我认为他们只是使用 webpack。

2) Compiling the angular abstractions into JavaScript. I mean things like a components, pipes, directives, templates etc. This is what the angular core team work on.

2)将 angular 抽象编译为 JavaScript。我的意思是组件、管道、指令、模板等。这就是 angular 核心团队的工作。

In case you're really interested in that second bit, the angular compiler, watch compiler author Tobias Bosch explain the Angular Compiler at AngularConnect 2016.

如果您真的对第二位,即 Angular 编译器感兴趣,请观看编译器作者 Tobias Bosch 在 AngularConnect 2016 上对 Angular 编译器的解释

I think there's a bit of confusion going on here between transpiling and compilation. It sort of doesn't matter and is a matter of personal taste, they're both just transforms between representations of code. But the definitionI personally use is that transpilationis between two different languages at a similar abstraction level (eg typescript to javascript), whereas compilationrequires a step down in level of abstraction. I think from templates, components, pipes, directives etc to just javascript is a step down the abstraction ladder, and that's why it's called a compiler.

我认为在转译和编译之间存在一些混淆。这有点无关紧要,是个人品味的问题,它们都只是代码表示之间的转换。但我个人使用的定义是,转译是在相似抽象级别的两种不同语言之间(例如,打字稿到 javascript),而编译需要降低抽象级别。我认为从模板、组件、管道、指令等到 javascript 是抽象阶梯的一个台阶,这就是它被称为编译器的原因。

回答by u5675325

Angular Compiler

角度编译器

One of the most important changes from Angular 4 to 5 is that the compiler has been rewritten be faster and more thorough. In the past, Angular applications used what we call Just-in-Time (JIT) compilation, where the application was compiled at runtime in the browser before running. The compiler updates in Angular 5 advanced the move to AOT, which made the app run faster as it performes less compilation when running the app. AOT become enabled by default in any production build since the 1.5 version of the Angular CLI.

从 Angular 4 到 5,最重要的变化之一是编译器被重写得更快更彻底。过去,Angular 应用程序使用我们所说的即时 (JIT) 编译,即应用程序在运行前在浏览器中运行时编译。Angular 5 中的编译器更新推动了向 AOT 的转变,这使得应用程序运行得更快,因为它在运行应用程序时执行的编译更少。自 Angular CLI 1.5 版本以来,AOT 在任何生产版本中默认启用。

Let's say we want to build an application for deployment and run the following command:

假设我们要构建一个用于部署的应用程序并运行以下命令:

ng build --prod

A few things happen: production version, minification, bunddles assets, filename hashing, tree shaking, AOT ... (we can enable/disable this using flags, ex. aot=false). In short, the prod flag creates an optimized bundle of the application by doing AOT compilation using the ngc(the Angular compiler) to create optimized code ready for the browser (Yes, it pre-compiles templates).

会发生一些事情:生产版本、缩小、捆绑资产、文件名散列、摇树、AOT ......(我们可以使用标志启用/禁用它,例如 aot=false)。简而言之,prod 标志通过使用ngc(Angular 编译器)进行 AOT 编译来创建为浏览器准备好的优化代码(是的,它预编译模板)来创建应用程序的优化包。

TypeScript Compiler

打字稿编译器

The TypeScript compiler, tsc, is responsible for compiling TypeScript files. It is the compiler that is responsible for implementing TypeScript features, such as static types, and the result is pure JavaScript from which the TypeScript keywords and expressions have been removed.

TypeScript 编译器tsc负责编译 TypeScript 文件。编译器负责实现 TypeScript 功能,例如静态类型,结果是纯 JavaScript,其中已删除 TypeScript 关键字和表达式。

The TypeScript compiler has two main features: it is a transpiler and a type checker. The compiler transpiles TypeScript to JavaScript. It does the following transformations on your source code:

TypeScript 编译器有两个主要特性:它是一个转译器和一个类型检查器。编译器将 TypeScript 转换为 JavaScript。它对您的源代码执行以下转换:

  • Remove all type annotations.
  • Compile new JavaScript features for old versions of JavaScript.
  • Compile TypeScript features that are not standard JavaScript.
  • 删除所有类型注释。
  • 为旧版本的 JavaScript 编译新的 JavaScript 功能。
  • 编译非标准 JavaScript 的 TypeScript 功能。

Invoking it, the compiler searches for configurations loaded in tsconfig.json (A detailed list of all the compiler options, along with default values, can be found here).

调用它,编译器搜索 tsconfig.json 中加载的配置(所有编译器选项的详细列表,以及默认值,可以在这里找到)。

In most respects, the TypeScript compiler works like any compiler. But there is one difference that can catch out the unwary: by default, the compiler continues to emit JavaScript code even when it encounters an error. Fortunately, this behavior can be disabled by setting the noEmitOnErrorconfiguration setting to true in the tsconfig.json file.

在大多数方面,TypeScript 编译器的工作方式与任何编译器一样。但是有一个区别可以让粗心的人发现:默认情况下,编译器即使遇到错误也会继续发出 JavaScript 代码。幸运的是,可以通过noEmitOnError在 tsconfig.json 文件中将配置设置设置为 true来禁用此行为。

To note: tscand ngchave different purposes and it's not about selecting one over the other. This answer might be of interest.

注意tscngc有不同的目的,这不是选择一个而不是另一个。这个答案可能很有趣

This answer was crafted based on the content from the following books

这个答案是根据以下书籍的内容精心制作的

  • Cloe, M. (2018). "Angular 5 Projects: Learn to Build Single Page Web Applications Using 70+ Projects".

  • Dewey, B., Grossnicklaus, K., Japikse, P. (2017). "Building Web Applications with Visual Studio 2017: Using .NET Core and Modern JavaScript Frameworks".

  • Freeman, A. (2019). "Essential TypeScript: From Beginner to Pro".

  • Ghiya, P. (2018). "TypeScript Microservices".

  • Iskandar, A., Chivukulu, S. (2019). "Web Development with Angular and Bootstrap - Third Edition".

  • Hennessy, K., Arora, C. (2018). "Angular 6 by Example".

  • Jansen, R., Wolf, I., Vane, V. (2016). "TypeScript: Modern JavaScript Development".

  • Mohammed, Z. (2019). "Angular Projects".

  • Seshadri, S. (2018). "Angular: Up and Running".

  • Wilken, J. (2018). "Angular in Action".

  • Cloe, M. (2018)。“Angular 5 项目:学习使用 70 多个项目构建单页 Web 应用程序”。

  • Dewey, B.、Grossnicklaus, K.、Japikse, P. (2017)。“使用 Visual Studio 2017 构建 Web 应用程序:使用 .NET Core 和现代 JavaScript 框架”。

  • 弗里曼 A.(2019 年)。“基本的打字稿:从初学者到专业人士”。

  • Ghiya, P. (2018)。“打字稿微服务”。

  • Iskandar, A., Chivukulu, S. (2019)。“使用 Angular 和 Bootstrap 进行 Web 开发 - 第三版”。

  • 轩尼诗,K.,阿罗拉,C.(2018 年)。“Angular 6 示例”。

  • Jansen, R., Wolf, I., Vane, V. (2016)。“TypeScript:现代 JavaScript 开发”。

  • 穆罕默德,Z.(2019 年)。“角度项目”。

  • Seshadri, S. (2018)。“角度:启动并运行”。

  • 威尔肯,J.(2018 年)。“角度在行动”。

回答by Mark Vogt

ADVICE FOR EVERYONE: Always always always START responses (and research) with DEFINITIONS ! MERRIAM-WEBSTER DICTIONARY... Compile: verb computers : "to change (computer programming instructions) into a form the computer can understand and use"

给每个人的建议:始终始终以定义开始响应(和研究)!MERRIAM-WEBSTER DICTIONARY... 编译:动词计算机:“将(计算机编程指令)更改为计算机可以理解和使用的形式”

WIKIPEDIA... A compiler is computer software that transforms computer code written in one programming language (the source language) into another programming language (the target language). Compilers are a type of translator that support digital devices, primarily computers. The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program.[1]

维基百科... 编译器是一种计算机软件,它将用一种编程语言(源语言)编写的计算机代码转换为另一种编程语言(目标语言)。编译器是一种支持数字设备(主要是计算机)的转换器。编译器这一名称主要用于将源代码从高级编程语言转换为低级语言(例如,汇编语言、目标代码或机器代码)以创建可执行程序的程序。[1]

DISCUSSION: The Webster Dictionary should be considered the highest authority here, yet its definition is frustratingly vague.

讨论:韦伯斯特词典应该被认为是这里的最高权威,但它的定义令人沮丧地含糊不清。

Wikipedia is indeed a de facto authoritative source as well. If in this case we consider its explanation the superior authority, then it goes out of its way (per usual) to offer some genuinely useful insights, particularly the following:

维基百科确实也是一个事实上的权威来源。如果在这种情况下我们认为它的解释是最高权威,那么它会(通常)提供一些真正有用的见解,特别是以下内容:

  1. Compiling in its strictest sense was meant to mean "the translation of programming code FROM a language which was NOT directly executable by a computer into code that was directly executable by a computer."
  2. IF we accept the above as the GENUINE definition of "compile"... IF all that happens in Angular is the "translation" of Typescript into Javascript... IF Javascript code still requires "Interpretation" into bytecode... If bytecode is then interpreted by node.js into code executable by the computer... THEN what Angular is doing ISN'T "COMPILING" in the truest sense. Period.
  1. 在严格意义上编制是为了表示“从并非由计算机转换成代码,这是直接执行语言编程代码的翻译由计算机直接执行。”
  2. 如果我们接受上述作为“编译”的 GENUINE 定义...然后由 node.js 解释为计算机可执行的代码......那么 Angular 所做的并不是真正意义上的“编译”。时期。

THAT SAID...

那说...

  1. IF we relax our definition of "compile" to simply mean "translating programming code from a higher-level language into a lower-level language"... IF we accept that Typescript IS a (in numerous ways) "higher-level" language than Javascript... THEN what Angular does (translate Typescript into Javascript) IS "compiling"...
  1. 如果我们将“编译”的定义放宽到仅仅意味着“将编程代码从高级语言翻译成低级语言”……如果我们接受 Typescript 是(在许多方面)“高级”语言比 Javascript ……那么 Angular 所做的(将 Typescript 翻译成 Javascript)就是“编译”……

THAT SAID...

那说...

  1. IF we introduce some new TYPES of compiling (per Wikipedia):
    • Compiling: translating code from a higher-level language to a lower-level one;
    • Transpiling: translating code between essentially "equivalent" languages; and
    • Decompiling: translating code from a lower-level language to a higher-level one; THEN whether Angular "compiles" or "transpiles" depends on whether or not you accept the assertion that Typescript is (even slightly) a "higher" level language than Javascript.
  1. 如果我们引入一些新的编译类型(根据维基百科):
    • 编译:将代码从高级语言翻译成低级语言;
    • 转译:在本质上“等效”的语言之间翻译代码;和
    • 反编译:将代码从低级语言翻译成高级语言;那么 Angular 是“编译”还是“转译”取决于您是否接受 Typescript 是(甚至略微)一种比 Javascript“更高”级别的语言的断言。

Me? a. I prefer the "working definition" of Compiling being "translating from high to low"; (it's more generalized and leaves room for evolution of the definition) b. I also do indeed consider Typescript an arguably "higher-level" language than Javascript; SO c. I'm ok with Angular asserting that it IS "compiling"

我?一个。我更喜欢编译的“工作定义”是“从高到低的转换”;(它更笼统,为定义的演变留下了空间) b.我也确实认为 Typescript 可以说是一种比 Javascript 更“高级”的语言;所以 C. 我对 Angular 断言它是“编译”没意见

THE ALTERNATIVE IS UNDESIREABLE: IF we demand "compiling" must mean "translating to 'executable' code"; IF we accept that Typescript IS "higher" than Javascript; THEN Angular ISN'T "compiling" :-| but ALSO Angular ISN'T even "transpiling" either :-( ! IN FACT we CAN'T EVEN DESCRIBE just WHAT Angular IS DOING use available vocabulary !

替代方案是不可取的:如果我们要求“编译”必须意味着“转换为‘可执行’代码”;如果我们接受 Typescript 比 Javascript“更高”;然后 Angular 不是“编译”:-| 但 Angular 甚至都不是“转译”:-(!事实上,我们甚至无法描述 Angular 正在做什么使用可用词汇!

So I "generalize" (read "relax") my definition of "Compile" to mean merely "higher-to-lower" instead of "higher-to-machine", and now my available vocabulary CAN describe what Angular is doing - it IS "compiling"

所以我“概括”(读“放松”)我对“编译”的定义仅仅意味着“从高到低”而不是“从高到机器”,现在我的可用词汇可以描述 Angular 正在做什么 - 它是“编译”

IF the goal of this discussion is ENHANCING UNDERSTANDING, then I hope my own small offering is useful to some.

如果这次讨论的目标是增强理解,那么我希望我自己的小奉献对某些人有用。

Cheers, -Mark in North Aurora IL

干杯,-马克在伊利诺伊州北奥罗拉