1 | /**
|
2 | * This class is the base interface for compilers that are used by
|
3 | * electron-compile. If your compiler library only supports a
|
4 | * synchronous API, use SimpleCompilerBase instead.
|
5 | *
|
6 | * @interface
|
7 | */
|
8 | export class CompilerBase {
|
9 | constructor() {
|
10 | this.compilerOptions = {};
|
11 | }
|
12 |
|
13 | /**
|
14 | * This method describes the MIME types that your compiler supports as input.
|
15 | * Many precompiled file types don't have a specific MIME type, so if it's not
|
16 | * recognized by the mime-types package, you need to patch rig-mime-types in
|
17 | * electron-compile.
|
18 | *
|
19 | * @return {string[]} An array of MIME types that this compiler can compile.
|
20 | *
|
21 | * @abstract
|
22 | */
|
23 | static getInputMimeTypes() {
|
24 | throw new Error("Implement me!");
|
25 | }
|
26 |
|
27 |
|
28 | /**
|
29 | * Determines whether a file should be compiled
|
30 | *
|
31 | * @param {string} fileName The full path of a file to compile.
|
32 | * @param {object} compilerContext An object that compilers can add extra
|
33 | information to as part of a job - the caller
|
34 | won't do anything with this.
|
35 | * @return {Promise<bool>} True if you are able to compile this file.
|
36 | *
|
37 | * @abstract
|
38 | */
|
39 | async shouldCompileFile(fileName, compilerContext) {
|
40 | throw new Error("Implement me!");
|
41 | }
|
42 |
|
43 |
|
44 | /**
|
45 | * Returns the dependent files of this file. This is used for languages such
|
46 | * as LESS which allow you to import / reference other related files. In future
|
47 | * versions of electron-compile, we will use this information to invalidate
|
48 | * all of the parent files if a child file changes.
|
49 | *
|
50 | * @param {string} sourceCode The contents of filePath
|
51 | * @param {string} fileName The full path of a file to compile.
|
52 | * @param {object} compilerContext An object that compilers can add extra
|
53 | information to as part of a job - the caller
|
54 | won't do anything with this.
|
55 | * @return {Promise<string[]>} An array of dependent file paths, or an empty
|
56 | * array if there are no dependent files.
|
57 | *
|
58 | * @abstract
|
59 | */
|
60 | async determineDependentFiles(sourceCode, fileName, compilerContext) {
|
61 | throw new Error("Implement me!");
|
62 | }
|
63 |
|
64 |
|
65 | /**
|
66 | * Compiles the file
|
67 | *
|
68 | * @param {string} sourceCode The contents of filePath
|
69 | * @param {string} fileName The full path of a file to compile.
|
70 | * @param {object} compilerContext An object that compilers can add extra
|
71 | information to as part of a job - the caller
|
72 | won't do anything with this.
|
73 | * @return {Promise<object>} An object representing the compiled result
|
74 | * @property {string} code The compiled code
|
75 | * @property {string} mimeType The MIME type of the compiled result, which
|
76 | * should exist in the mime-types database.
|
77 | *
|
78 | * @abstract
|
79 | */
|
80 | async compile(sourceCode, fileName, compilerContext) {
|
81 | throw new Error("Implement me!");
|
82 | }
|
83 |
|
84 | shouldCompileFileSync(fileName, compilerContext) {
|
85 | throw new Error("Implement me!");
|
86 | }
|
87 |
|
88 | determineDependentFilesSync(sourceCode, fileName, compilerContext) {
|
89 | throw new Error("Implement me!");
|
90 | }
|
91 |
|
92 | compileSync(sourceCode, fileName, compilerContext) {
|
93 | throw new Error("Implement me!");
|
94 | }
|
95 |
|
96 | /**
|
97 | * Returns a version number representing the version of the underlying
|
98 | * compiler library. When this number changes, electron-compile knows
|
99 | * to throw all away its generated code.
|
100 | *
|
101 | * @return {string} A version number. Note that this string isn't
|
102 | * parsed in any way, just compared to the previous
|
103 | * one for equality.
|
104 | *
|
105 | * @abstract
|
106 | */
|
107 | getCompilerVersion() {
|
108 | throw new Error("Implement me!");
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 | /**
|
114 | * This class implements all of the async methods of CompilerBase by just
|
115 | * calling the sync version. Use it to save some time when implementing
|
116 | * simple compilers.
|
117 | *
|
118 | * To use it, implement the compile method, the getCompilerVersion method,
|
119 | * and the getInputMimeTypes static method.
|
120 | *
|
121 | * @abstract
|
122 | */
|
123 | export class SimpleCompilerBase extends CompilerBase {
|
124 | constructor() {
|
125 | super();
|
126 | }
|
127 |
|
128 | async shouldCompileFile(fileName, compilerContext) {
|
129 | return true;
|
130 | }
|
131 |
|
132 | async determineDependentFiles(sourceCode, filePath, compilerContext) {
|
133 | return [];
|
134 | }
|
135 |
|
136 | async compile(sourceCode, filePath, compilerContext) {
|
137 | return this.compileSync(sourceCode, filePath, compilerContext);
|
138 | }
|
139 |
|
140 | shouldCompileFileSync(fileName, compilerContext) {
|
141 | return true;
|
142 | }
|
143 |
|
144 | determineDependentFilesSync(sourceCode, filePath, compilerContext) {
|
145 | return [];
|
146 | }
|
147 | }
|