From b2dfcb29eacfe5c91a75762b617b7bd573eb25d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Mon, 28 Oct 2024 12:46:58 -0700 Subject: [PATCH] Add init.ts option for pure js compiler --- tool/get-embedded-compiler.ts | 49 +++++++++++++++++++++++++++++------ tool/init.ts | 10 ++++--- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/tool/get-embedded-compiler.ts b/tool/get-embedded-compiler.ts index cb8b7245..d836e487 100644 --- a/tool/get-embedded-compiler.ts +++ b/tool/get-embedded-compiler.ts @@ -2,6 +2,7 @@ // 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 * as p from 'path'; import * as shell from 'shelljs'; @@ -15,6 +16,7 @@ import * as utils from './utils'; */ export async function getEmbeddedCompiler( outPath: string, + js?: boolean, options?: {ref: string} | {path: string} ): Promise { const repo = 'dart-sass'; @@ -41,21 +43,52 @@ export async function getEmbeddedCompiler( await utils.link(languageInHost, languageInCompiler); } - buildDartSassEmbedded(source); - await utils.link(p.join(source, 'build'), p.join(outPath, repo)); + buildDartSassEmbedded(source, js ?? false); + if (js) { + // Remove any dart sass binary 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})) + ); + } + + await utils.link(p.join(source, 'build/npm'), 'node_modules/sass'); + } else { + await utils.link(p.join(source, 'build'), p.join(outPath, repo)); + } } // Builds the Embedded Dart Sass executable from the source at `repoPath`. -function buildDartSassEmbedded(repoPath: string): void { +function buildDartSassEmbedded(repoPath: string, js: boolean): void { console.log("Downloading Dart Sass's dependencies."); shell.exec('dart pub upgrade', { cwd: repoPath, silent: true, }); - console.log('Building the Dart Sass executable.'); - shell.exec('dart run grinder protobuf pkg-standalone-dev', { - cwd: repoPath, - env: {...process.env, UPDATE_SASS_PROTOCOL: 'false'}, - }); + if (js) { + shell.exec('npm install', { + cwd: repoPath, + silent: true, + }); + + console.log('Building the Dart Sass npm package.'); + shell.exec('dart run grinder protobuf pkg-npm-dev', { + cwd: repoPath, + env: {...process.env, UPDATE_SASS_PROTOCOL: 'false'}, + }); + } else { + console.log('Building the Dart Sass executable.'); + shell.exec('dart run grinder protobuf pkg-standalone-dev', { + cwd: repoPath, + env: {...process.env, UPDATE_SASS_PROTOCOL: 'false'}, + }); + } } diff --git a/tool/init.ts b/tool/init.ts index 230cd09d..c14e15d1 100644 --- a/tool/init.ts +++ b/tool/init.ts @@ -18,6 +18,10 @@ const argv = yargs(process.argv.slice(2)) type: 'string', description: 'Build the Embedded Dart Sass binary from this Git ref.', }) + .option('compiler-js', { + type: 'boolean', + description: 'Build the Embedded Dart Sass with dart2js.', + }) .option('skip-compiler', { type: 'boolean', description: "Don't Embedded Dart Sass at all.", @@ -55,15 +59,15 @@ void (async () => { if (!argv['skip-compiler']) { if (argv['compiler-ref']) { - await getEmbeddedCompiler(outPath, { + await getEmbeddedCompiler(outPath, argv['compiler-js'], { ref: argv['compiler-ref'], }); } else if (argv['compiler-path']) { - await getEmbeddedCompiler(outPath, { + await getEmbeddedCompiler(outPath, argv['compiler-js'], { path: argv['compiler-path'], }); } else { - await getEmbeddedCompiler(outPath); + await getEmbeddedCompiler(outPath, argv['compiler-js']); } }