@travetto/email-compiler 7.0.0-rc.2 → 7.0.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/email-compiler",
3
- "version": "7.0.0-rc.2",
3
+ "version": "7.0.0-rc.3",
4
4
  "description": "Email compiling module",
5
5
  "keywords": [
6
6
  "email",
@@ -26,12 +26,12 @@
26
26
  "directory": "module/email-compiler"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/config": "^7.0.0-rc.2",
30
- "@travetto/di": "^7.0.0-rc.2",
31
- "@travetto/email": "^7.0.0-rc.2",
32
- "@travetto/image": "^7.0.0-rc.2",
33
- "@travetto/runtime": "^7.0.0-rc.2",
34
- "@travetto/worker": "^7.0.0-rc.2",
29
+ "@travetto/config": "^7.0.0-rc.3",
30
+ "@travetto/di": "^7.0.0-rc.3",
31
+ "@travetto/email": "^7.0.0-rc.3",
32
+ "@travetto/image": "^7.0.0-rc.3",
33
+ "@travetto/runtime": "^7.0.0-rc.3",
34
+ "@travetto/worker": "^7.0.0-rc.3",
35
35
  "@types/inline-css": "^3.0.4",
36
36
  "html-entities": "^2.6.0",
37
37
  "inline-css": "^4.0.3",
@@ -39,7 +39,7 @@
39
39
  "sass": "^1.94.2"
40
40
  },
41
41
  "peerDependencies": {
42
- "@travetto/cli": "^7.0.0-rc.2"
42
+ "@travetto/cli": "^7.0.0-rc.3"
43
43
  },
44
44
  "peerDependenciesMeta": {
45
45
  "@travetto/cli": {
package/src/compiler.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
 
4
- import { TypedObject, RuntimeIndex, watchCompiler, Runtime, BinaryUtil } from '@travetto/runtime';
4
+ import { TypedObject, RuntimeIndex, Runtime, BinaryUtil, ExecUtil } from '@travetto/runtime';
5
5
  import { EmailCompiled, MailUtil, EmailTemplateImport, EmailTemplateModule } from '@travetto/email';
6
6
 
7
7
  import { EmailCompileUtil } from './util.ts';
@@ -84,22 +84,26 @@ export class EmailCompiler {
84
84
  }
85
85
 
86
86
  /**
87
- * Watch compilation
87
+ * Spawn the compiler for a given file
88
88
  */
89
- static async * watchCompile(signal?: AbortSignal): AsyncIterable<string> {
90
- // Watch template files
91
- for await (const { file, action } of watchCompiler({ signal })) {
92
- const entry = RuntimeIndex.getEntry(file);
93
- if (!entry || !EmailCompileUtil.isTemplateFile(entry.sourceFile) || action === 'delete') {
94
- continue;
95
- }
96
- try {
97
- await this.compile(file);
98
- console.log('Successfully compiled template', { changed: [file] });
99
- yield file;
100
- } catch (error) {
101
- console.error(`Error in compiling ${file}`, error && error instanceof Error ? error.message : `${error}`);
102
- }
89
+ static async spawnCompile(file: string): Promise<boolean> {
90
+ if (!EmailCompileUtil.isTemplateFile(file)) {
91
+ return false;
103
92
  }
93
+
94
+ const child = ExecUtil.spawnTrv('email:compile', [file], {
95
+ cwd: Runtime.mainSourcePath,
96
+ env: { ...process.env },
97
+ });
98
+
99
+ const result = await ExecUtil.getResult(child, { catch: true });
100
+
101
+ if (!result.valid) {
102
+ console.error('Error compiling template', { file, stderr: result.stderr });
103
+ } else {
104
+ console.log('Successfully compiled template', { changed: [file] });
105
+ }
106
+
107
+ return result.valid;
104
108
  }
105
109
  }
package/src/util.ts CHANGED
@@ -1,9 +1,10 @@
1
- import util from 'node:util';
2
1
  import { buffer as toBuffer } from 'node:stream/consumers';
3
2
  import path from 'node:path';
3
+ import type { CompileResult, Options } from 'sass';
4
4
 
5
5
  import { EmailCompiled, EmailTemplateModule, EmailTemplateResource } from '@travetto/email';
6
6
  import { ImageUtil } from '@travetto/image';
7
+ import { RuntimeIndex } from '@travetto/runtime';
7
8
 
8
9
  type Tokenized = {
9
10
  text: string;
@@ -29,7 +30,7 @@ export class EmailCompileUtil {
29
30
  * Is file a template?
30
31
  */
31
32
  static isTemplateFile(file: string): boolean {
32
- return EXT.test(file);
33
+ return EXT.test(file) && RuntimeIndex.findModuleForArbitraryFile(file) !== undefined;
33
34
  }
34
35
 
35
36
  /**
@@ -80,12 +81,20 @@ export class EmailCompileUtil {
80
81
  * Compile SCSS content with roots as search paths for additional assets
81
82
  */
82
83
  static async compileSass(input: { data: string } | { file: string }, options: EmailTemplateResource): Promise<string> {
83
- const sass = await import('sass');
84
- const result = await util.promisify(sass.render)({
85
- ...input,
84
+ const { initAsyncCompiler } = await import('sass');
85
+ const compiler = await initAsyncCompiler();
86
+ const compilerOptions: Options<'async'> = {
86
87
  sourceMap: false,
87
- includePaths: options.loader.searchPaths.slice(0)
88
- });
88
+ quietDeps: true,
89
+ loadPaths: options.loader.searchPaths.slice(0),
90
+ };
91
+
92
+ let result: CompileResult;
93
+ if ('data' in input) {
94
+ result = await compiler.compileStringAsync(input.data, compilerOptions);
95
+ } else {
96
+ result = await compiler.compileAsync(input.file, compilerOptions);
97
+ }
89
98
  return result!.css.toString();
90
99
  }
91
100
 
@@ -1,6 +1,6 @@
1
1
  import { Inject, Injectable } from '@travetto/di';
2
2
  import { MailUtil, EmailCompiled, MailInterpolator } from '@travetto/email';
3
- import { AppError, TypedObject } from '@travetto/runtime';
3
+ import { AppError, TypedObject, watchCompiler } from '@travetto/runtime';
4
4
 
5
5
  import { EditorSendService } from './send.ts';
6
6
  import { EditorConfig } from './config.ts';
@@ -87,11 +87,14 @@ export class EditorService {
87
87
 
88
88
  process.send({ type: 'init' });
89
89
 
90
- for await (const file of EmailCompiler.watchCompile()) {
91
- await this.#response(this.#renderFile(file),
92
- result => ({ type: 'compiled', ...result }),
93
- error => ({ type: 'compiled-failed', message: error.message, stack: error.stack, file })
94
- );
90
+ // Watch template files
91
+ for await (const { file } of watchCompiler({ restartOnCompilerExit: true })) {
92
+ if (await EmailCompiler.spawnCompile(file)) {
93
+ await this.#response(this.#renderFile(file),
94
+ result => ({ type: 'compiled', ...result }),
95
+ error => ({ type: 'compiled-failed', message: error.message, stack: error.stack, file })
96
+ );
97
+ }
95
98
  }
96
99
  }
97
100
  }
@@ -33,7 +33,7 @@ export class EditorSendService {
33
33
  }
34
34
  }
35
35
  });
36
- Registry.process([{ type: 'added', current: cls }]);
36
+ Registry.process([cls]);
37
37
 
38
38
  this.ethereal = !!senderConfig.host?.includes('ethereal.email');
39
39
  } catch {
@@ -41,7 +41,9 @@ export class EditorSendService {
41
41
  throw new Error('A mail transport is currently needed to support sending emails. Please install @travetto/email-nodemailer or any other compatible transport');
42
42
  }
43
43
  }
44
- return await DependencyRegistryIndex.getInstance(MailService);
44
+ const service = await DependencyRegistryIndex.getInstance(MailService);
45
+ service.setCacheState(false); // Ensure we don't cache locally
46
+ return service;
45
47
  }
46
48
 
47
49
  /**
@@ -1,6 +1,6 @@
1
1
  import { Registry } from '@travetto/registry';
2
2
  import { CliCommandShape, CliCommand, cliTpl } from '@travetto/cli';
3
- import { Env, Runtime } from '@travetto/runtime';
3
+ import { Env, Runtime, watchCompiler } from '@travetto/runtime';
4
4
 
5
5
  import { EmailCompiler } from '../src/compiler.ts';
6
6
 
@@ -16,7 +16,6 @@ export class EmailCompileCommand implements CliCommandShape {
16
16
  preMain(): void {
17
17
  Env.DEBUG.set(false);
18
18
  Env.TRV_ROLE.set('build');
19
- Env.TRV_DYNAMIC.set(this.watch);
20
19
  }
21
20
 
22
21
  async main(): Promise<void> {
@@ -30,8 +29,8 @@ export class EmailCompileCommand implements CliCommandShape {
30
29
  }
31
30
 
32
31
  if (this.watch) {
33
- for await (const _ of EmailCompiler.watchCompile()) {
34
- // Iterate until done
32
+ for await (const { file } of watchCompiler({ restartOnCompilerExit: true })) {
33
+ await EmailCompiler.spawnCompile(file);
35
34
  }
36
35
  }
37
36
  }
@@ -1,5 +1,5 @@
1
1
  import { Env } from '@travetto/runtime';
2
- import { CliCommand, CliUtil } from '@travetto/cli';
2
+ import { CliCommand } from '@travetto/cli';
3
3
  import { Registry } from '@travetto/registry';
4
4
  import { DependencyRegistryIndex } from '@travetto/di';
5
5
 
@@ -10,15 +10,10 @@ import { EditorService } from './bin/editor.ts';
10
10
  export class EmailEditorCommand {
11
11
 
12
12
  preMain(): void {
13
- Env.TRV_DYNAMIC.set(true);
14
13
  Env.TRV_ROLE.set('build');
15
14
  }
16
15
 
17
16
  async main(): Promise<void> {
18
- if (await CliUtil.runWithRestart(this, true)) {
19
- return;
20
- }
21
-
22
17
  await Registry.init();
23
18
  const service = await DependencyRegistryIndex.getInstance(EditorService);
24
19
  await service.listen();