From 58434233d1393b7f0eff3d570f5fe1e33aa9cc7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Fri, 1 Nov 2024 17:52:13 -0700 Subject: [PATCH] Install dart-sass dev snapshot as npm module --- lib/src/compiler-path.ts | 43 ++++++++++++++--------------------- tool/get-embedded-compiler.ts | 27 ++++++++++------------ 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/lib/src/compiler-path.ts b/lib/src/compiler-path.ts index 4b9b2e8a..9df76441 100644 --- a/lib/src/compiler-path.ts +++ b/lib/src/compiler-path.ts @@ -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'; @@ -23,8 +22,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 +31,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 +53,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,8 +68,8 @@ 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; } } @@ -86,7 +77,7 @@ export const compilerCommand = (() => { 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.' ); })(); diff --git a/tool/get-embedded-compiler.ts b/tool/get-embedded-compiler.ts index 69cc3b06..aa921f90 100644 --- a/tool/get-embedded-compiler.ts +++ b/tool/get-embedded-compiler.ts @@ -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'; /** @@ -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)); } }