diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dd3614a3..c16c75c3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/lib/src/compiler-path.ts b/lib/src/compiler-path.ts index 4b9b2e8a..1208e14e 100644 --- a/lib/src/compiler-path.ts +++ b/lib/src/compiler-path.ts @@ -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 @@ -23,8 +21,8 @@ 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' @@ -32,29 +30,21 @@ export const compilerCommand = (() => { 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; } } @@ -62,12 +52,12 @@ export const compilerCommand = (() => { 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; } } @@ -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.' ); })(); diff --git a/tool/get-embedded-compiler.ts b/tool/get-embedded-compiler.ts index 69cc3b06..2c39c11c 100644 --- a/tool/get-embedded-compiler.ts +++ b/tool/get-embedded-compiler.ts @@ -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'; /** @@ -44,24 +45,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)); } }