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 dd17ebb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 60 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,7 @@ jobs:
working-directory: sass-spec

- name: Compile
run: |
npm run compile
if [[ "$RUNNER_OS" == "Windows" ]]; then
# Avoid copying the entire Dart Sass build directory on Windows,
# since it may contain symlinks that cp will choke on.
mkdir -p dist/lib/src/vendor/dart-sass/
cp {`pwd`/,dist/}lib/src/vendor/dart-sass/sass.bat
cp {`pwd`/,dist/}lib/src/vendor/dart-sass/sass.snapshot
else
ln -s {`pwd`/,dist/}lib/src/vendor/dart-sass
fi
run: npm run compile

- name: Run tests
run: npm run js-api-spec -- --sassPackage .. --sassSassRepo ../language
Expand Down
47 changes: 18 additions & 29 deletions lib/src/compiler-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// 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';

/**
* Detect if the given binary is linked with musl libc by checking if
Expand All @@ -23,51 +21,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 +67,15 @@ 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 ` +
'node_modules.'
`Please make sure the optional dependency ${compilerModule} or sass is ` +
'installed in node_modules.'
);
})();
33 changes: 16 additions & 17 deletions tool/get-embedded-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import {promises as fs, readdirSync} from 'fs';
import {promises as fs} 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 All @@ -15,7 +16,6 @@ import * as utils from './utils';
* at `path`. By default, checks out the latest revision from GitHub.
*/
export async function getEmbeddedCompiler(
outPath: string,
js?: boolean,
options?: {ref: string} | {path: string}
): Promise<void> {
Expand Down Expand Up @@ -44,24 +44,23 @@ 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), {
force: true,
recursive: true,
});

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
6 changes: 3 additions & 3 deletions tool/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ void (async () => {

if (!argv['skip-compiler']) {
if (argv['compiler-ref']) {
await getEmbeddedCompiler(outPath, argv['compiler-js'], {
await getEmbeddedCompiler(argv['compiler-js'], {
ref: argv['compiler-ref'],
});
} else if (argv['compiler-path']) {
await getEmbeddedCompiler(outPath, argv['compiler-js'], {
await getEmbeddedCompiler(argv['compiler-js'], {
path: argv['compiler-path'],
});
} else {
await getEmbeddedCompiler(outPath, argv['compiler-js']);
await getEmbeddedCompiler(argv['compiler-js']);
}
}

Expand Down

0 comments on commit dd17ebb

Please sign in to comment.