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

Commit

Permalink
feat(sassoptions): expose importers
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jul 20, 2018
1 parent 84d897e commit 4fd16e9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/interop/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SassDataContext } from './data/sassDataContext';
import { SassFileContext, SassSourceContext } from './file/sassFileContext';
import { wrapSassImporter } from './importer/wrapSassImporter';
import { buildInteropUtility } from './interopUtility';
import { SassOptions, SassOptionsInterface } from './options/sassOptions';
import { wrapSassOptions } from './options/wrapSassOptions';
Expand All @@ -14,13 +15,15 @@ import { wrapSassContext } from './wrapSassContext';
const buildContext = (
cwrapContext: ReturnType<typeof wrapSassContext>,
cwrapOptions: ReturnType<typeof wrapSassOptions>,
cwrapImporter: ReturnType<typeof wrapSassImporter>,
interop: ReturnType<typeof buildInteropUtility>
) => {
const { str, mount, unmount } = interop;

return {
options: {
create: () => new SassOptions(cwrapContext, cwrapOptions, mount, unmount, str) as SassOptionsInterface
create: () =>
new SassOptions(cwrapContext, cwrapOptions, cwrapImporter, mount, unmount, str) as SassOptionsInterface
},
file: {
create: (inputPath: string) => new SassFileContext(inputPath, cwrapContext, str) as SassSourceContext
Expand Down
2 changes: 1 addition & 1 deletion src/interop/importer/wrapSassImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { cwrapSignature } from 'emscripten-wasm-loader';
*/
//TODO: verify return type / param type of cwrapped signature for Sass_Import_Entry
const wrapSassImporter = (cwrap: cwrapSignature) => ({
//Sass_C_Import_Callback sass_make_importer (Sass_C_Import_Fn, void* cookie);
//Sass_Importer_Entry sass_make_importer (Sass_Importer_Fn importer, double priority, void* cookie);
make_importer: cwrap<(importFnPtr: number, cookie: number) => number>(`sass_make_importer`, 'number', [
'number',
'number'
Expand Down
25 changes: 25 additions & 0 deletions src/interop/options/sassOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { mountDirectory, unmount } from 'emscripten-wasm-loader';
import { log } from '../../util/logger';
import { SassImportEntryInterface } from '../importer/sassImportEntry';
import { SassImportEntryList } from '../importer/sassImportEntryList';
import { wrapSassImporter } from '../importer/wrapSassImporter';
import { StringMethodInterface } from '../interopUtility';
import { wrapSassContext } from '../wrapSassContext';
import { wrapSassOptions } from './wrapSassOptions';
Expand Down Expand Up @@ -53,6 +56,11 @@ interface SassOptionsInterface {
*/
inputPath: string;

/**
* Property accessor to `sass_option_(get|set)_c_importers`
*/
importers: Array<SassImportEntryInterface>;

/**
* Push include path for compilation.
* Accessor to sass_option_push_include_path
Expand Down Expand Up @@ -100,6 +108,11 @@ class SassOptions implements SassOptionsInterface {
* List of virtual mounted path.
*/
private readonly mountedPath: Array<string> = [];

/**
* List of importers.
*/
private importersList: SassImportEntryList | null;
/**
* Construct new instance of SassOptions.
*
Expand All @@ -112,6 +125,7 @@ class SassOptions implements SassOptionsInterface {
constructor(
private readonly cwrapCtx: ReturnType<typeof wrapSassContext>,
private readonly cwrapOptions: ReturnType<typeof wrapSassOptions>,
private readonly cwrapImporter: ReturnType<typeof wrapSassImporter>,
private readonly mount: ReturnType<typeof mountDirectory>,
private readonly unmountPath: ReturnType<typeof unmount>,
private readonly strMethod: StringMethodInterface
Expand Down Expand Up @@ -193,6 +207,17 @@ class SassOptions implements SassOptionsInterface {
this.cwrapOptions.option_set_input_path(this.sassOptionsPtr, this.strMethod.alloc(outPath));
}

public get importers(): Array<SassImportEntryInterface> {
return !!this.importersList ? this.importersList.entry : [];
}

public set importers(values: Array<SassImportEntryInterface>) {
this.importersList = new SassImportEntryList(this.cwrapImporter, values.length);
this.importersList.entry = values;

this.cwrapOptions.option_set_c_importers(this.sassOptionsPtr, this.importersList.sassImportEntryListPtr);
}

public addIncludePath(includePath: string): void {
const mounted = this.mount(includePath);
this.mountedPath.push(mounted);
Expand Down
12 changes: 9 additions & 3 deletions src/interop/options/wrapSassOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
//const char* sass_option_get_source_map_root (struct Sass_Options* options);
option_get_source_map_root: null,
//Sass_Importer_List sass_option_get_c_headers (struct Sass_Options* options);
sass_option_get_c_headers: null,
option_get_c_headers: null,
//Sass_Importer_List sass_option_get_c_importers (struct Sass_Options* options);
sass_option_get_c_importers: null,
option_get_c_importers: cwrap<(sassOptionsPtr: number) => number>(`sass_option_get_c_importers`, 'number', [
'number'
]),
//Sass_Function_List sass_option_get_c_functions (struct Sass_Options* options);
option_get_c_functions: null,

Expand Down Expand Up @@ -144,7 +146,11 @@ const wrapSassOptions = (cwrap: cwrapSignature) => ({
//void sass_option_set_c_headers (struct Sass_Options* options, Sass_Importer_List c_headers);
option_set_c_headers: null,
//void sass_option_set_c_importers (struct Sass_Options* options, Sass_Importer_List c_importers);
option_set_c_importers: null,
option_set_c_importers: cwrap<(sassOptionsPtr: number, importersList: number) => void>(
`sass_option_set_c_importers`,
null,
['number', 'number']
),
//void sass_option_set_c_functions (struct Sass_Options* options, Sass_Function_List c_functions);
option_set_c_functions: null,

Expand Down
2 changes: 1 addition & 1 deletion src/sassLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const sassLoader = (asmModule: SassAsmModule): SassFactory => {

return {
getVersion: getVersion(asmModule),
context: buildContext(cwrapCtx, cwrapOptions, interop),
context: buildContext(cwrapCtx, cwrapOptions, cwrapImporter, interop),
importer: buildImporter(cwrapImporter, interop),
interop,
raw: {
Expand Down

0 comments on commit 4fd16e9

Please sign in to comment.