Skip to content

Commit

Permalink
Install dart-sass dev snapshot as npm module
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Nov 2, 2024
1 parent 6ca7947 commit 5843423
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 41 deletions.
43 changes: 17 additions & 26 deletions lib/src/compiler-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import * as fs from 'fs';
import * as p from 'path';
import {getElfInterpreter} from './elf';
import {isErrnoException} from './utils';

Check warning on line 7 in lib/src/compiler-path.ts

View workflow job for this annotation

GitHub Actions / Static analysis

'isErrnoException' is defined but never used
Expand All @@ -23,51 +22,43 @@ function isLinuxMusl(path: string): boolean {
}
}

/** The full command for the embedded compiler executable. */
export const compilerCommand = (() => {
/** The module name for the embedded compiler executable. */
export const compilerModule = (() => {
const platform =
process.platform === 'linux' && isLinuxMusl(process.execPath)
? 'linux-musl'
: (process.platform as string);

const arch = process.arch;

// find for development
for (const path of ['vendor', '../../../lib/src/vendor']) {
const executable = p.resolve(
__dirname,
path,
`dart-sass/sass${platform === 'win32' ? '.bat' : ''}`
);

if (fs.existsSync(executable)) return [executable];
}
return `sass-embedded-${platform}-${arch}`;
})();

/** The full command for the embedded compiler executable. */
export const compilerCommand = (() => {
try {
return [
require.resolve(
`sass-embedded-${platform}-${arch}/dart-sass/src/dart` +
(platform === 'win32' ? '.exe' : '')
),
require.resolve(
`sass-embedded-${platform}-${arch}/dart-sass/src/sass.snapshot`
`${compilerModule}/dart-sass/src/dart` +
(process.platform === 'win32' ? '.exe' : '')
),
require.resolve(`${compilerModule}/dart-sass/src/sass.snapshot`),
];
} catch (e) {
if (!(isErrnoException(e) && e.code === 'MODULE_NOT_FOUND')) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

try {
return [
require.resolve(
`sass-embedded-${platform}-${arch}/dart-sass/sass` +
(platform === 'win32' ? '.bat' : '')
`${compilerModule}/dart-sass/sass` +
(process.platform === 'win32' ? '.bat' : '')
),
];
} catch (e: unknown) {
if (!(isErrnoException(e) && e.code === 'MODULE_NOT_FOUND')) {
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
Expand All @@ -77,16 +68,16 @@ export const compilerCommand = (() => {
process.execPath,
p.join(p.dirname(require.resolve('sass')), 'sass.js'),
];
} catch (e: unknown) {
if (!(isErrnoException(e) && e.code === 'MODULE_NOT_FOUND')) {
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

throw new Error(
"Embedded Dart Sass couldn't find the embedded compiler executable. " +
'Please make sure the optional dependency ' +
`sass-embedded-${platform}-${arch} or sass is installed in ` +
`${compilerModule} or sass is installed in ` +
'node_modules.'
);
})();
27 changes: 12 additions & 15 deletions tool/get-embedded-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {promises as fs, readdirSync} from 'fs';
import * as p from 'path';
import * as shell from 'shelljs';

import {compilerModule} from '../lib/src/compiler-path';
import * as utils from './utils';

/**
Expand Down Expand Up @@ -44,24 +45,20 @@ export async function getEmbeddedCompiler(
}

buildDartSassEmbedded(source, js ?? false);

const jsModulePath = p.resolve('node_modules/sass');
const dartModulePath = p.resolve(p.join('node_modules', compilerModule));
if (js) {
// Remove sass-embedded-* packages
const modules = ['node_modules', p.join(source, 'node_modules')].flatMap(
node_modules =>
readdirSync(node_modules)
.filter(dir => dir.startsWith('sass-embedded-'))
.map(dir => p.join(node_modules, dir))
);
if (modules.length > 0) {
console.log(`Removing ${modules.join(', ')}.`);
await Promise.all(
modules.map(module => fs.rm(module, {force: true, recursive: true}))
);
}
// The next line is only needed temporarily because sass local dev
// package.json currently installs sass-embedded for sync-message-port.js
// https://github.com/sass/dart-sass/pull/2413
await fs.rm(p.join(source, 'node_modules', compilerModule));

await utils.link(p.join(source, 'build/npm'), 'node_modules/sass');
await fs.rm(dartModulePath, {force: true, recursive: true});
await utils.link(p.join(source, 'build/npm'), jsModulePath);
} else {
await utils.link(p.join(source, 'build'), p.join(outPath, repo));
await fs.rm(jsModulePath, {force: true, recursive: true});
await utils.link(p.join(source, 'build'), p.join(dartModulePath, repo));
}
}

Expand Down

0 comments on commit 5843423

Please sign in to comment.