Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit 8d3badc

Browse files
committed
feat(sassoptions): export sourcemap property
1 parent 8327d6e commit 8d3badc

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

src/interop/context.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ import { wrapSassOptions } from './wrapSassOptions';
1313
* @param asmModule
1414
*/
1515
const buildContext = (asmModule: SassAsmModule) => {
16-
const { cwrap, FS, stackAlloc, stringToUTF8 } = asmModule;
16+
const { cwrap, FS, stackAlloc, stringToUTF8, Pointer_stringify } = asmModule;
1717
const cwrapCtx = wrapSassContext(cwrap);
1818
const cwrapOptions = wrapSassOptions(cwrap);
1919

20-
const allocString = (value: string) => {
21-
const len = (value.length << 2) + 1;
22-
const ret = stackAlloc(len);
23-
stringToUTF8(value, ret, len);
24-
return ret;
20+
const str = {
21+
alloc: (value: string) => {
22+
const len = (value.length << 2) + 1;
23+
const ret = stackAlloc(len);
24+
stringToUTF8(value, ret, len);
25+
return ret;
26+
},
27+
ptrToString: Pointer_stringify
2528
};
2629

2730
const nodePathId = `/${nanoid(45)}`;
@@ -33,7 +36,7 @@ const buildContext = (asmModule: SassAsmModule) => {
3336

3437
return {
3538
options: {
36-
create: () => new SassOptions(cwrapCtx, cwrapOptions, mountPath, unmountPath, allocString) as SassOptionsInterface
39+
create: () => new SassOptions(cwrapCtx, cwrapOptions, mountPath, unmountPath, str) as SassOptionsInterface
3740
}
3841
};
3942
};

src/interop/sassOptions.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ interface SassOptionsInterface {
2929
*/
3030
omitMapComment: boolean;
3131

32+
/**
33+
* Property accessor to `sass_option_(get|set)_omit_source_map_embed`
34+
*/
35+
sourceMapEmbed: boolean;
36+
37+
/**
38+
* Property accessor to `sass_option_(get|set)_source_map_file`
39+
*/
40+
sourceMapFile: string;
41+
3242
/**
3343
* Push include path for compilation.
3444
* @param {string} includePath path to be inlcluded
@@ -86,7 +96,10 @@ class SassOptions implements SassOptionsInterface {
8696
private readonly cwrapOptions: ReturnType<typeof wrapSassOptions>,
8797
private readonly mount: ReturnType<typeof mountDirectory>,
8898
private readonly unmountPath: ReturnType<typeof unmount>,
89-
private readonly allocString: (value: string) => number
99+
private readonly strMethod: {
100+
alloc: (value: string) => number;
101+
ptrToString: (value: number) => string;
102+
}
90103
) {
91104
this.sassOptionsPtr = cwrapCtx.make_options();
92105
log(`SassOptions: created new instance`, { sassOptionsPtr: this.sassOptionsPtr });
@@ -130,18 +143,35 @@ class SassOptions implements SassOptionsInterface {
130143
this.cwrapOptions.option_set_omit_source_map_url(this.sassOptionsPtr, isOmitted);
131144
}
132145

146+
public get sourceMapEmbed(): boolean {
147+
return !!this.cwrapOptions.option_get_source_map_embed(this.sassOptionsPtr);
148+
}
149+
150+
public set sourceMapEmbed(embed: boolean) {
151+
this.cwrapOptions.option_set_source_map_embed(this.sassOptionsPtr, embed);
152+
}
153+
154+
public get sourceMapFile(): string {
155+
const mapFilePtr = this.cwrapOptions.option_get_source_map_file(this.sassOptionsPtr);
156+
return this.strMethod.ptrToString(mapFilePtr);
157+
}
158+
159+
public set sourceMapFile(mapFile: string) {
160+
this.cwrapOptions.option_set_source_map_file(this.sassOptionsPtr, this.strMethod.alloc(mapFile));
161+
}
162+
133163
public addIncludePath(includePath: string): void {
134164
const mounted = this.mount(includePath);
135165
this.mountedPath.push(mounted);
136166

137-
this.cwrapOptions.option_push_include_path(this.sassOptionsPtr, this.allocString(mounted));
167+
this.cwrapOptions.option_push_include_path(this.sassOptionsPtr, this.strMethod.alloc(mounted));
138168
}
139169

140170
public addPluginPath(pluginPath: string): void {
141171
const mounted = this.mount(pluginPath);
142172
this.mountedPath.push(mounted);
143173

144-
this.cwrapOptions.option_push_plugin_path(this.sassOptionsPtr, this.allocString(mounted));
174+
this.cwrapOptions.option_push_plugin_path(this.sassOptionsPtr, this.strMethod.alloc(mounted));
145175
}
146176

147177
public dispose(): void {

src/interop/wrapSassOptions.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
1515
'number'
1616
]),
1717
//bool sass_option_get_source_map_embed (struct Sass_Options* options);
18-
option_get_source_map_embed: null,
18+
option_get_source_map_embed: cwrap<(sassOptionsPtr: number) => boolean>(
19+
`sass_option_get_source_map_embed`,
20+
'boolean',
21+
['number']
22+
),
1923
//bool sass_option_get_source_map_contents (struct Sass_Options* options);
2024
option_get_source_map_contents: null,
2125
//bool sass_option_get_source_map_file_urls (struct Sass_Options* options);
@@ -41,7 +45,9 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
4145
//const char* sass_option_get_output_path (struct Sass_Options* options);
4246
option_get_output_path: null,
4347
//const char* sass_option_get_source_map_file (struct Sass_Options* options);
44-
option_get_source_map_file: null,
48+
option_get_source_map_file: cwrap<(sassOptionsPtr: number) => number>(`sass_option_get_source_map_file`, 'number', [
49+
'number'
50+
]),
4551
//const char* sass_option_get_source_map_root (struct Sass_Options* options);
4652
option_get_source_map_root: null,
4753
//Sass_C_Function_List sass_option_get_c_functions (struct Sass_Options* options);
@@ -77,7 +83,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
7783
['number', 'boolean']
7884
),
7985
//void sass_option_set_source_map_embed (struct Sass_Options* options, bool source_map_embed);
80-
option_set_source_map_embed: null,
86+
option_set_source_map_embed: cwrap<(sassOptionsPtr: number, embed: boolean) => void>(
87+
`sass_option_set_source_map_embed`,
88+
null,
89+
['number', 'boolean']
90+
),
8191
//void sass_option_set_source_map_contents (struct Sass_Options* options, bool source_map_contents);
8292
option_set_source_map_contents: null,
8393
//void sass_option_set_source_map_file_urls (struct Sass_Options* options, bool source_map_file_urls);
@@ -107,7 +117,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
107117
//void sass_option_set_include_path (struct Sass_Options* options, const char* include_path);
108118
option_set_include_path: null,
109119
//void sass_option_set_source_map_file (struct Sass_Options* options, const char* source_map_file);
110-
option_set_source_map_file: null,
120+
option_set_source_map_file: cwrap<(sassOptionsPtr: number, mapFile: number) => void>(
121+
`sass_option_set_source_map_file`,
122+
null,
123+
['number', 'number']
124+
),
111125
//void sass_option_set_source_map_root (struct Sass_Options* options, const char* source_map_root);
112126
option_set_source_map_root: null,
113127
//void sass_option_set_c_functions (struct Sass_Options* options, Sass_C_Function_List c_functions);

0 commit comments

Comments
 (0)