Skip to content

Commit

Permalink
Improve launch performance by skipping wrapper script (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Feb 28, 2023
1 parent 12e8733 commit 3088620
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/src/async-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import {spawn} from 'child_process';
import {Observable} from 'rxjs';
import {takeUntil} from 'rxjs/operators';

import {compilerPath} from './compiler-path';
import {compilerCommand} from './compiler-path';

/**
* An asynchronous wrapper for the embedded Sass compiler that exposes its stdio
* streams as Observables.
*/
export class AsyncEmbeddedCompiler {
/** The underlying process that's being wrapped. */
private readonly process = spawn(compilerPath, {windowsHide: true});
private readonly process = spawn(
compilerCommand[0],
compilerCommand.slice(1),
{windowsHide: true}
);

/** The child process's exit event. */
readonly exit$ = new Promise<number | null>(resolve => {
Expand Down
34 changes: 26 additions & 8 deletions lib/src/compiler-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as fs from 'fs';
import * as p from 'path';
import {isErrnoException} from './utils';

/** The path to the embedded compiler executable. */
export const compilerPath = (() => {
/** The full command for the embedded compiler executable. */
export const compilerCommand = (() => {
// find for development
for (const path of ['vendor', '../../../lib/src/vendor']) {
const executable = p.resolve(
Expand All @@ -18,15 +18,33 @@ export const compilerPath = (() => {
}`
);

if (fs.existsSync(executable)) return executable;
if (fs.existsSync(executable)) return [executable];
}

try {
return require.resolve(
`sass-embedded-${process.platform}-${process.arch}/` +
'dart-sass-embedded/dart-sass-embedded' +
(process.platform === 'win32' ? '.bat' : '')
);
return [
require.resolve(
`sass-embedded-${process.platform}-${process.arch}/` +
'dart-sass-embedded/src/dart' +
(process.platform === 'win32' ? '.exe' : '')
),
require.resolve(
`sass-embedded-${process.platform}-${process.arch}/` +
'dart-sass-embedded/src/dart-sass-embedded.snapshot'
),
];
} catch (ignored) {
// ignored
}

try {
return [
require.resolve(
`sass-embedded-${process.platform}-${process.arch}/` +
'dart-sass-embedded/dart-sass-embedded' +
(process.platform === 'win32' ? '.bat' : '')
),
];
} catch (e: unknown) {
if (!(isErrnoException(e) && e.code === 'MODULE_NOT_FOUND')) {
throw e;
Expand Down
8 changes: 6 additions & 2 deletions lib/src/sync-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import {Subject} from 'rxjs';

import {SyncProcess} from './sync-process';
import {compilerPath} from './compiler-path';
import {compilerCommand} from './compiler-path';

/**
* A synchronous wrapper for the embedded Sass compiler that exposes its stdio
* streams as Observables.
*/
export class SyncEmbeddedCompiler {
/** The underlying process that's being wrapped. */
private readonly process = new SyncProcess(compilerPath, {windowsHide: true});
private readonly process = new SyncProcess(
compilerCommand[0],
compilerCommand.slice(1),
{windowsHide: true}
);

/** The buffers emitted by the child process's stdout. */
readonly stdout$ = new Subject<Buffer>();
Expand Down

0 comments on commit 3088620

Please sign in to comment.