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

Commit

Permalink
feat(sassoptions): export sourcemap property
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 12, 2018
1 parent 8327d6e commit 8d3badc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
17 changes: 10 additions & 7 deletions src/interop/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import { wrapSassOptions } from './wrapSassOptions';
* @param asmModule
*/
const buildContext = (asmModule: SassAsmModule) => {
const { cwrap, FS, stackAlloc, stringToUTF8 } = asmModule;
const { cwrap, FS, stackAlloc, stringToUTF8, Pointer_stringify } = asmModule;
const cwrapCtx = wrapSassContext(cwrap);
const cwrapOptions = wrapSassOptions(cwrap);

const allocString = (value: string) => {
const len = (value.length << 2) + 1;
const ret = stackAlloc(len);
stringToUTF8(value, ret, len);
return ret;
const str = {
alloc: (value: string) => {
const len = (value.length << 2) + 1;
const ret = stackAlloc(len);
stringToUTF8(value, ret, len);
return ret;
},
ptrToString: Pointer_stringify
};

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

return {
options: {
create: () => new SassOptions(cwrapCtx, cwrapOptions, mountPath, unmountPath, allocString) as SassOptionsInterface
create: () => new SassOptions(cwrapCtx, cwrapOptions, mountPath, unmountPath, str) as SassOptionsInterface
}
};
};
Expand Down
36 changes: 33 additions & 3 deletions src/interop/sassOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ interface SassOptionsInterface {
*/
omitMapComment: boolean;

/**
* Property accessor to `sass_option_(get|set)_omit_source_map_embed`
*/
sourceMapEmbed: boolean;

/**
* Property accessor to `sass_option_(get|set)_source_map_file`
*/
sourceMapFile: string;

/**
* Push include path for compilation.
* @param {string} includePath path to be inlcluded
Expand Down Expand Up @@ -86,7 +96,10 @@ class SassOptions implements SassOptionsInterface {
private readonly cwrapOptions: ReturnType<typeof wrapSassOptions>,
private readonly mount: ReturnType<typeof mountDirectory>,
private readonly unmountPath: ReturnType<typeof unmount>,
private readonly allocString: (value: string) => number
private readonly strMethod: {
alloc: (value: string) => number;
ptrToString: (value: number) => string;
}
) {
this.sassOptionsPtr = cwrapCtx.make_options();
log(`SassOptions: created new instance`, { sassOptionsPtr: this.sassOptionsPtr });
Expand Down Expand Up @@ -130,18 +143,35 @@ class SassOptions implements SassOptionsInterface {
this.cwrapOptions.option_set_omit_source_map_url(this.sassOptionsPtr, isOmitted);
}

public get sourceMapEmbed(): boolean {
return !!this.cwrapOptions.option_get_source_map_embed(this.sassOptionsPtr);
}

public set sourceMapEmbed(embed: boolean) {
this.cwrapOptions.option_set_source_map_embed(this.sassOptionsPtr, embed);
}

public get sourceMapFile(): string {
const mapFilePtr = this.cwrapOptions.option_get_source_map_file(this.sassOptionsPtr);
return this.strMethod.ptrToString(mapFilePtr);
}

public set sourceMapFile(mapFile: string) {
this.cwrapOptions.option_set_source_map_file(this.sassOptionsPtr, this.strMethod.alloc(mapFile));
}

public addIncludePath(includePath: string): void {
const mounted = this.mount(includePath);
this.mountedPath.push(mounted);

this.cwrapOptions.option_push_include_path(this.sassOptionsPtr, this.allocString(mounted));
this.cwrapOptions.option_push_include_path(this.sassOptionsPtr, this.strMethod.alloc(mounted));
}

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

this.cwrapOptions.option_push_plugin_path(this.sassOptionsPtr, this.allocString(mounted));
this.cwrapOptions.option_push_plugin_path(this.sassOptionsPtr, this.strMethod.alloc(mounted));
}

public dispose(): void {
Expand Down
22 changes: 18 additions & 4 deletions src/interop/wrapSassOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
'number'
]),
//bool sass_option_get_source_map_embed (struct Sass_Options* options);
option_get_source_map_embed: null,
option_get_source_map_embed: cwrap<(sassOptionsPtr: number) => boolean>(
`sass_option_get_source_map_embed`,
'boolean',
['number']
),
//bool sass_option_get_source_map_contents (struct Sass_Options* options);
option_get_source_map_contents: null,
//bool sass_option_get_source_map_file_urls (struct Sass_Options* options);
Expand All @@ -41,7 +45,9 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
//const char* sass_option_get_output_path (struct Sass_Options* options);
option_get_output_path: null,
//const char* sass_option_get_source_map_file (struct Sass_Options* options);
option_get_source_map_file: null,
option_get_source_map_file: cwrap<(sassOptionsPtr: number) => number>(`sass_option_get_source_map_file`, 'number', [
'number'
]),
//const char* sass_option_get_source_map_root (struct Sass_Options* options);
option_get_source_map_root: null,
//Sass_C_Function_List sass_option_get_c_functions (struct Sass_Options* options);
Expand Down Expand Up @@ -77,7 +83,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
['number', 'boolean']
),
//void sass_option_set_source_map_embed (struct Sass_Options* options, bool source_map_embed);
option_set_source_map_embed: null,
option_set_source_map_embed: cwrap<(sassOptionsPtr: number, embed: boolean) => void>(
`sass_option_set_source_map_embed`,
null,
['number', 'boolean']
),
//void sass_option_set_source_map_contents (struct Sass_Options* options, bool source_map_contents);
option_set_source_map_contents: null,
//void sass_option_set_source_map_file_urls (struct Sass_Options* options, bool source_map_file_urls);
Expand Down Expand Up @@ -107,7 +117,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
//void sass_option_set_include_path (struct Sass_Options* options, const char* include_path);
option_set_include_path: null,
//void sass_option_set_source_map_file (struct Sass_Options* options, const char* source_map_file);
option_set_source_map_file: null,
option_set_source_map_file: cwrap<(sassOptionsPtr: number, mapFile: number) => void>(
`sass_option_set_source_map_file`,
null,
['number', 'number']
),
//void sass_option_set_source_map_root (struct Sass_Options* options, const char* source_map_root);
option_set_source_map_root: null,
//void sass_option_set_c_functions (struct Sass_Options* options, Sass_C_Function_List c_functions);
Expand Down

0 comments on commit 8d3badc

Please sign in to comment.