2
2
// MIT-style license that can be found in the LICENSE file or at
3
3
// https://opensource.org/licenses/MIT.
4
4
5
+ import { promises as fs } from 'fs' ;
5
6
import * as p from 'path' ;
6
7
import * as shell from 'shelljs' ;
7
8
9
+ import { compilerModule } from '../lib/src/compiler-module' ;
8
10
import * as utils from './utils' ;
9
11
10
12
/**
11
13
* Downloads and builds the Embedded Dart Sass compiler.
12
14
*
13
15
* Can check out and build the source from a Git `ref` or build from the source
14
16
* at `path`. By default, checks out the latest revision from GitHub.
17
+ *
18
+ * The embedded compiler will be built as dart snapshot by default, or pure node
19
+ * js if the `js` option is `true`.
15
20
*/
16
21
export async function getEmbeddedCompiler (
17
- outPath : string ,
18
- options ?: { ref : string } | { path : string } ,
22
+ options ?:
23
+ | {
24
+ ref ?: string ;
25
+ js ?: boolean ;
26
+ }
27
+ | {
28
+ path : string ;
29
+ js ?: boolean ;
30
+ } ,
19
31
) : Promise < void > {
20
32
const repo = 'dart-sass' ;
21
33
22
34
let source : string ;
23
- if ( ! options || 'ref' in options ) {
35
+ if ( options !== undefined && 'path' in options ) {
36
+ source = options . path ;
37
+ } else {
24
38
utils . fetchRepo ( {
25
39
repo,
26
40
outPath : 'build' ,
27
41
ref : options ?. ref ?? 'main' ,
28
42
} ) ;
29
43
source = p . join ( 'build' , repo ) ;
30
- } else {
31
- source = options . path ;
32
44
}
33
45
34
46
// Make sure the compiler sees the same version of the language repo that the
@@ -41,21 +53,44 @@ export async function getEmbeddedCompiler(
41
53
await utils . link ( languageInHost , languageInCompiler ) ;
42
54
}
43
55
44
- buildDartSassEmbedded ( source ) ;
45
- await utils . link ( p . join ( source , 'build' ) , p . join ( outPath , repo ) ) ;
56
+ const js = options ?. js ?? false ;
57
+ buildDartSassEmbedded ( source , js ) ;
58
+
59
+ const jsModulePath = p . resolve ( 'node_modules/sass' ) ;
60
+ const dartModulePath = p . resolve ( p . join ( 'node_modules' , compilerModule ) ) ;
61
+ if ( js ) {
62
+ await fs . rm ( dartModulePath , { force : true , recursive : true } ) ;
63
+ await utils . link ( p . join ( source , 'build/npm' ) , jsModulePath ) ;
64
+ } else {
65
+ await fs . rm ( jsModulePath , { force : true , recursive : true } ) ;
66
+ await utils . link ( p . join ( source , 'build' ) , p . join ( dartModulePath , repo ) ) ;
67
+ }
46
68
}
47
69
48
70
// Builds the Embedded Dart Sass executable from the source at `repoPath`.
49
- function buildDartSassEmbedded ( repoPath : string ) : void {
71
+ function buildDartSassEmbedded ( repoPath : string , js : boolean ) : void {
50
72
console . log ( "Downloading Dart Sass's dependencies." ) ;
51
73
shell . exec ( 'dart pub upgrade' , {
52
74
cwd : repoPath ,
53
75
silent : true ,
54
76
} ) ;
55
77
56
- console . log ( 'Building the Dart Sass executable.' ) ;
57
- shell . exec ( 'dart run grinder protobuf pkg-standalone-dev' , {
58
- cwd : repoPath ,
59
- env : { ...process . env , UPDATE_SASS_PROTOCOL : 'false' } ,
60
- } ) ;
78
+ if ( js ) {
79
+ shell . exec ( 'npm install' , {
80
+ cwd : repoPath ,
81
+ silent : true ,
82
+ } ) ;
83
+
84
+ console . log ( 'Building the Dart Sass npm package.' ) ;
85
+ shell . exec ( 'dart run grinder protobuf pkg-npm-dev' , {
86
+ cwd : repoPath ,
87
+ env : { ...process . env , UPDATE_SASS_PROTOCOL : 'false' } ,
88
+ } ) ;
89
+ } else {
90
+ console . log ( 'Building the Dart Sass executable.' ) ;
91
+ shell . exec ( 'dart run grinder protobuf pkg-standalone-dev' , {
92
+ cwd : repoPath ,
93
+ env : { ...process . env , UPDATE_SASS_PROTOCOL : 'false' } ,
94
+ } ) ;
95
+ }
61
96
}
0 commit comments