Skip to content

Commit

Permalink
Change to import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
jgranstrom committed Jan 27, 2017
1 parent 906f8a8 commit 55e51d2
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 55 deletions.
17 changes: 7 additions & 10 deletions src/extract.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Promise = require('bluebird');
const sass = require('node-sass');
const { loadCompiledFiles, loadCompiledFilesSync } = require('./load');
const { processFiles } = require('./process');
const { makeImporter, makeSyncImporter } = require('./importer');
import Promise from 'bluebird';
import sass from 'node-sass';
import { loadCompiledFiles, loadCompiledFilesSync } from './load';
import { processFiles } from './process';
import { makeImporter, makeSyncImporter } from './importer';

Promise.promisifyAll(sass);

Expand Down Expand Up @@ -68,7 +68,7 @@ function compileExtractionResult(extractions) {
* Extract the variables from already rendered sass file(s)
* Returns the extracted variables
*/
function extract(rendered, { compileOptions = {} } = {}) {
export function extract(rendered, { compileOptions = {} } = {}) {
const { entryFilename, includedFiles } = getRenderedStats(rendered);

return loadCompiledFiles(includedFiles, entryFilename, compileOptions.data)
Expand All @@ -88,7 +88,7 @@ function extract(rendered, { compileOptions = {} } = {}) {
* Synchronously extract the variables from already rendered sass file(s)
* Returns the extracted variables
*/
function extractSync(rendered, { compileOptions = {} } = {}) {
export function extractSync(rendered, { compileOptions = {} } = {}) {
const { entryFilename, includedFiles } = getRenderedStats(rendered);

const compiledFiles = loadCompiledFilesSync(includedFiles, entryFilename, compileOptions.data);
Expand All @@ -100,6 +100,3 @@ function extractSync(rendered, { compileOptions = {} } = {}) {

return compileExtractionResult(extractions);
}

exports.extract = extract;
exports.extractSync = extractSync;
9 changes: 3 additions & 6 deletions src/importer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path');
import path from 'path';

/**
* Search for the likely absolute path from a relative path using known paths from compilation
Expand Down Expand Up @@ -49,7 +49,7 @@ function getImportResult(extractions, url, prev) {
* Create an importer that will resolve @import directives with the injected
* data found in provided extractions object
*/
function makeImporter(extractions) {
export function makeImporter(extractions) {
return function(url, prev, done) {
try {
const result = getImportResult(extractions, url, prev);
Expand All @@ -64,7 +64,7 @@ function makeImporter(extractions) {
* Create a synchronous importer that will resolve @import directives with the injected
* data found in provided extractions object
*/
function makeSyncImporter(extractions) {
export function makeSyncImporter(extractions) {
return function(url, prev) {
try {
const result = getImportResult(extractions, url, prev);
Expand All @@ -75,6 +75,3 @@ function makeSyncImporter(extractions) {
}
}
}

exports.makeImporter = makeImporter;
exports.makeSyncImporter = makeSyncImporter;
9 changes: 2 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
const { render, renderSync } = require('./render');
const { extract, extractSync } = require('./extract');

exports.render = render;
exports.renderSync = renderSync;
exports.extract = extract;
exports.extractSync = extractSync;
export { render, renderSync } from './render';
export { extract, extractSync } from'./extract';
6 changes: 2 additions & 4 deletions src/inject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createStructuredValue } = require('./struct');
import { createStructuredValue } from './struct';

const FN_PREFIX = '___SV_INJECT';
const FN_PREFIX_IMPLICIT_GLOBAL = 'IG';
Expand Down Expand Up @@ -28,7 +28,7 @@ function createInjection(fileId, categoryPrefix, declaration, declarationResultH
* Declaration result handlers will be called with the extracted value of each declaration
* Provided file id will be used to ensure unique function names per file
*/
function injectExtractionFunctions(fileId, declarations, { globalDeclarationResultHandler }) {
export function injectExtractionFunctions(fileId, declarations, { globalDeclarationResultHandler }) {
let injectedData = ``;
const injectedFunctions = {};

Expand All @@ -48,5 +48,3 @@ function injectExtractionFunctions(fileId, declarations, { globalDeclarationResu

return { injectedData, injectedFunctions };
}

exports.injectExtractionFunctions = injectExtractionFunctions;
12 changes: 5 additions & 7 deletions src/load.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Promise = require('bluebird');
const fs = require('fs');
import Promise from 'bluebird';
import fs from 'fs';

Promise.promisifyAll(fs);

const RAW_DATA_FILE = 'data';
Expand Down Expand Up @@ -32,7 +33,7 @@ function loadSync(filename, encoding = 'utf8') {
/**
* Load all files included in a sass compilations including potential raw data input
*/
function loadCompiledFiles(includedFiles, entryFilename, data) {
export function loadCompiledFiles(includedFiles, entryFilename, data) {
const files = {};

return Promise.all(includedFiles.map(filename => {
Expand All @@ -49,7 +50,7 @@ function loadCompiledFiles(includedFiles, entryFilename, data) {
/**
* Synchronously load all files included in a sass compilations including potential raw data input
*/
function loadCompiledFilesSync(includedFiles, entryFilename, data) {
export function loadCompiledFilesSync(includedFiles, entryFilename, data) {
const files = {};

includedFiles.forEach(filename => {
Expand All @@ -58,6 +59,3 @@ function loadCompiledFilesSync(includedFiles, entryFilename, data) {

return includeRawDataFile(files, entryFilename, data);
}

module.exports.loadCompiledFiles = loadCompiledFiles;
module.exports.loadCompiledFilesSync = loadCompiledFilesSync;
6 changes: 2 additions & 4 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sass = require('node-sass');
import sass from 'node-sass';

const REGEX_VARIABLE_GLOBAL_IMPLICIT = /(\$[\w-_]+)\s*:\s*((.*?\n?)+?);/g;
const REGEX_VARIABLE_GLOBAL_EXPLICIT = /(\$[\w-_]+)\s*:\s*(.*?)\s+!global\s*;/g;
Expand Down Expand Up @@ -40,7 +40,7 @@ function extractVariables(data, regex) {
/**
* Parse variables declarations from a chunk of sass source
*/
function parseDeclarations(data) {
export function parseDeclarations(data) {
const decommentedData = stripByRegex(data, REGEX_COMMENTS);
const decontextifiedData = stripByRegex(decommentedData, REGEX_DEEP_CONTEXT);

Expand All @@ -49,5 +49,3 @@ function parseDeclarations(data) {

return { explicitGlobals, implicitGlobals };
}

exports.parseDeclarations = parseDeclarations;
8 changes: 3 additions & 5 deletions src/process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { injectExtractionFunctions } = require('./inject');
const { parseDeclarations } = require('./parse');
import { injectExtractionFunctions } from './inject';
import { parseDeclarations } from './parse';

/**
* Get a string id for a filename
Expand Down Expand Up @@ -37,7 +37,7 @@ function processFile(filename, data) {
* Process a set of sass files to get declarations, injected source and functions
* Files are provided in a map of filename -> key entries
*/
function processFiles(files) {
export function processFiles(files) {
const extractions = {};

Object.keys(files).map(filename => {
Expand All @@ -46,5 +46,3 @@ function processFiles(files) {

return extractions;
}

exports.processFiles = processFiles;
13 changes: 5 additions & 8 deletions src/render.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Promise = require('bluebird');
const sass = require('node-sass');
const { extract, extractSync } = require('./extract');
import Promise from 'bluebird';
import sass from 'node-sass';
import { extract, extractSync } from './extract';

Promise.promisifyAll(sass);

/**
* Render with node-sass using provided compile options and augment variable extraction
*/
function render(compileOptions = {}) {
export function render(compileOptions = {}) {
return sass.renderAsync(compileOptions)
.then(rendered => {
return extract(rendered, { compileOptions })
Expand All @@ -21,11 +21,8 @@ function render(compileOptions = {}) {
/**
* Render synchronously with node-sass using provided compile options and augment variable extraction
*/
function renderSync(compileOptions = {}) {
export function renderSync(compileOptions = {}) {
const rendered = sass.renderSync(compileOptions);
rendered.vars = extractSync(rendered, { compileOptions })
return rendered;
}

exports.render = render;
exports.renderSync = renderSync;
6 changes: 2 additions & 4 deletions src/struct.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sass = require('node-sass');
import sass from 'node-sass';

/**
* Convert a color value 0-255 to hex 00-FF
Expand Down Expand Up @@ -63,12 +63,10 @@ function makeValue(sassValue) {
/**
* Create a structured value definition from a sassValue object
*/
function createStructuredValue(sassValue) {
export function createStructuredValue(sassValue) {
const value = Object.assign({
type: sassValue.constructor.name,
}, makeValue(sassValue));

return value;
};

exports.createStructuredValue = createStructuredValue;

0 comments on commit 55e51d2

Please sign in to comment.