UNPKG

2.51 kBJavaScriptView Raw
1"use strict";
2/*tslint:disable:no-default-export*/
3/*tslint:disable:no-any*/
4/*tslint:disable:promise-function-async*/
5Object.defineProperty(exports, "__esModule", { value: true });
6const di_compiler_1 = require("@wessberg/di-compiler");
7const mark_files_for_recompilation_1 = require("./mark-files-for-recompilation/mark-files-for-recompilation");
8/**
9 * The base transformer for DI.
10 * @author Frederik Wessberg
11 */
12function DI() {
13 /**
14 * The compiler to use.
15 */
16 const compiler = new di_compiler_1.DICompiler();
17 /**
18 * Returns true if the first build is currently in progress
19 * @type {boolean}
20 */
21 let isFirstBuild = true;
22 /**
23 * The options that Rollup has been configured with
24 * @type {InputOptions}
25 */
26 let rollupOptions;
27 /**
28 * Returns true if rollup is in watch mode
29 * @type {string}
30 */
31 const IS_IN_WATCH_MODE = process.env.ROLLUP_WATCH != null;
32 return {
33 name: "DI",
34 /**
35 * Called when Rollup has received options.
36 * @param {InputOptions} rawRollupOptions
37 */
38 options(rawRollupOptions) {
39 rollupOptions = rawRollupOptions;
40 return rollupOptions;
41 },
42 /**
43 * Called when a bundle has been generated.
44 */
45 onwrite() {
46 isFirstBuild = false;
47 },
48 /**
49 * Here the source code is parsed and upgraded. A sourcemap will be returned if need be.
50 * @param {string} code
51 * @param {string} file
52 * @returns {Promise<SourceDescription|void|string>}
53 */
54 transform(code, file) {
55 // Compile the source code
56 const result = compiler.compile({ file, code });
57 // Force-compile files that has been marked for recompilation (if this is not the first build and rollup is in watch mode)
58 if (IS_IN_WATCH_MODE && !isFirstBuild) {
59 mark_files_for_recompilation_1.markFilesForRecompilation(new Set(result.filesNeedingRecompilation));
60 }
61 // If nothing changed, return null. Force-fake the typings since the Rollup typings aren't that perfect
62 if (!result.hasChanged)
63 return null;
64 // Otherwise, return the changed code and sourcemap. Force-fake the typings since the Rollup typings aren't that perfect
65 return result;
66 }
67 };
68}
69exports.default = DI;
70/*tslint:enable:no-default-export*/
71//# sourceMappingURL=index.js.map
\No newline at end of file