Skip to content

add tokens to processCss function #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ hook({
/**
* @param {string} css
* @param {string} filepath Absolute path to the file
* @param {object} tokens
*/
processCss: function (css, filepath) { /* */ }
processCss: function (css, filepath, tokens) { /* */ }
});
```

Expand Down
3 changes: 2 additions & 1 deletion lib/attachHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = function attachHook(compile, extension, isException) {
existingHook(m, filename);
} else {
const tokens = compile(filename);
return m._compile(`module.exports = ${JSON.stringify(tokens)}`, filename);

m.exports = tokens;
}
};
};
47 changes: 31 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function setupHook({
debugSetup(arguments[0]);
validate(arguments[0]);

const tokensByFile = {};
const resultsByFile = {};

// debug option is preferred NODE_ENV === 'development'
const debugMode = typeof devMode !== 'undefined'
Expand Down Expand Up @@ -76,58 +76,73 @@ module.exports = function setupHook({
const runner = postcss(plugins);

/**
* @todo think about replacing sequential fetch function calls with requires calls
* @param {string} _to
* @param {string} from
* @return {object}
*/
function fetch(_to, from) {
return runProcess(_to, from).tokens;
}

/**
* @param {string} _to
* @param {string} from
* @return {object}
*/
function runProcess(_to, from) {
// getting absolute path to the processing file
const filename = /[^\\/?%*:|"<>\.]/i.test(_to[0])
? require.resolve(_to)
: resolve(dirname(from), _to);

// checking cache
let tokens = tokensByFile[filename];
if (tokens) {
let results = resultsByFile[filename];
if (results) {
debugFetch(`${filename} → cache`);
debugFetch(tokens);
return tokens;
debugFetch(results.tokens);
return results;
}

const source = preprocessCss(readFileSync(filename, 'utf8'), filename);
// https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
const lazyResult = runner.process(source, assign({}, processorOpts, {from: filename}));
const css = lazyResult.css;

// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
lazyResult.warnings().forEach(message => console.warn(message.text));

tokens = lazyResult.root.tokens;
results = {
tokens: lazyResult.root.tokens,
css: lazyResult.css,
filename
};

if (!debugMode) {
// updating cache
tokensByFile[filename] = tokens;
resultsByFile[filename] = results;
} else {
// clearing cache in development mode
delete require.cache[filename];
}

if (processCss) {
processCss(lazyResult.css, filename);
}

debugFetch(`${filename} → fs`);
debugFetch(tokens);
debugFetch(results.tokens);

return tokens;
return results;
};

const exts = toArray(extensions);
const isException = buildExceptionChecker(ignore);

const hook = filename => {
const tokens = fetch(filename, filename);
return camelCase ? transformTokens(tokens, camelCase) : tokens;
const lazyResult = runProcess(filename, filename);
let tokens = camelCase ? transformTokens(lazyResult.tokens, camelCase) : lazyResult.tokens;

if (processCss) {
processCss(lazyResult.css, lazyResult.filename, tokens);
}

return tokens;
};

// @todo add possibility to specify particular config for each extension
Expand Down