-
Notifications
You must be signed in to change notification settings - Fork 422
/
compiler-api.md
156 lines (108 loc) · 4.63 KB
/
compiler-api.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: Stencil Core Compiler API
sidebar_label: Compiler API
description: Stencil Core Compiler API
slug: /compiler-api
---
# Stencil Core Compiler API
The compiler API can be found at `@stencil/core/compiler/stencil.js`. This module can
work within a NodeJS environment, web worker, and browser window. The
`stencil.min.js` file is also provided and recommended when used within a browser.
```tsx
// NodeJS (commonjs)
const stencil = require('@stencil/core/compiler');
```
## transpile()
```tsx
transpile(code: string, opts?: TranspileOptions): Promise<TranspileResults>
```
The `transpile()` function inputs source code as a string, with various options
within the second argument. The function is stateless and returns a `Promise` of the
results, including diagnostics and the transpiled code. The `transpile()` function
does not handle any bundling, minifying, or precompiling any CSS preprocessing like
Sass or Less.
The `transpileSync()` equivalent is available so the same function
it can be called synchronously. However, TypeScript must be already loaded within
the global for it to work, where as the async `transpile()` function will load
TypeScript automatically.
Since TypeScript is used, the source code will transpile from TypeScript to JavaScript,
and does not require Babel presets. Additionally, the results includes an `imports`
array of all the import paths found in the source file. The transpile options can be
used to set the `module` format, such as `cjs`, and JavaScript `target` version, such
as `es2017`.
## transpileSync()
```tsx
transpileSync(code: string, opts?: TranspileOptions): TranspileResults
```
Synchronous equivalent of the `transpile()` function. When used in a browser
environment, TypeScript must already be available globally, where as the async
`transpile()` function will load TypeScript automatically.
## createCompiler()
```tsx
createCompiler(config: Config): Promise<Compiler>
```
The compiler is the utility that brings together many tools to build optimized components, such as a
transpiler, bundler and minifier. When using the CLI, the `stencil build` command uses the compiler for
the various builds, such as a production build, or watch mode during development. If only one file should
be transpiled (converting source code from TypeScript to JavaScript) then the `transpile()` function should be used instead.
Given a Stencil config, this method asynchronously returns a `Compiler` instance. The config provided
should already be created using the `loadConfig({...})` method.
Below is an example of a NodeJS environment running a full build.
```tsx
import { createNodeLogger, createNodeSys } from '@stencil/core/sys/node';
import { createCompiler, loadConfig } from '@stencil/core/compiler';
const logger = createNodeLogger(process);
const sys = createNodeSys(process);
const validated = await loadConfig({
logger,
sys,
config: {
/* user config */
},
});
const compiler = await createCompiler(validated.config);
const results = await compiler.build();
```
## createSystem()
```tsx
createSystem(): CompilerSystem
```
The compiler uses a `CompilerSystem` instance to access any file system reads and writes. When used
from the CLI, the CLI will provide its own system based on NodeJS. This method provide a compiler
system is in-memory only and independent of any platform.
## dependencies
```tsx
dependencies: CompilerDependency[]
```
The `dependencies` array is only informational and provided to state which versions of dependencies
the compiler was built and works with. For example, the version of TypeScript, Rollup and Terser used
for this version of Stencil are listed here.
## loadConfig()
```tsx
loadConfig(init?: LoadConfigInit): Promise<LoadConfigResults>
```
The `loadConfig(init)` method is used to take raw config information and transform it into a
usable config object for the compiler and dev-server. The `init` argument should be given
an already created system and logger which can also be used by the compiler.
## optimizeCss()
```tsx
optimizeCss(cssInput?: OptimizeCssInput): Promise<OptimizeCssOutput>
```
Utility function used by the compiler to optimize CSS.
## optimizeJs()
```jsx
optimizeJs(jsInput?: OptimizeJsInput): Promise<OptimizeJsOutput>
```
Utility function used by the compiler to optimize JavaScript. Knowing the JavaScript target
will further apply minification optimizations beyond usual minification.
## path
```tsx
path: PlatformPath
```
Utility of the `path` API provided by NodeJS, but capable of running in any environment.
This `path` API is only the POSIX version: https://nodejs.org/api/path.html
## version
```tsx
version: string
```
Current version of `@stencil/core`.