Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve launch performance by skipping wrapper script #207

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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