docusaurus/lib/server/utils.js
Sashank Thupukari d3417b3bf2 Add autoprefixing to CSS pipeline (#867)
Add [autoprefixer](https://github.com/postcss/autoprefixer) as a final build step in the CSS pipeline.
2018-07-22 00:05:13 -07:00

73 lines
1.7 KiB
JavaScript

/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const cssnano = require('cssnano');
const autoprefixer = require('autoprefixer');
const postcss = require('postcss');
const path = require('path');
const escapeStringRegexp = require('escape-string-regexp');
function getSubDir(file, refDir) {
const subDir = path.dirname(path.relative(refDir, file)).replace('\\', '/');
return subDir !== '.' && !subDir.includes('..') ? subDir : null;
}
function getLanguage(file, refDir) {
const regexSubFolder = new RegExp(
`${escapeStringRegexp(path.basename(refDir))}/(.*?)/.*`
);
const match = regexSubFolder.exec(file);
// Avoid misinterpreting subdirectory as language
const env = require('./env.js');
if (match && env.translation.enabled) {
const enabledLanguages = env.translation
.enabledLanguages()
.map(language => language.tag);
if (enabledLanguages.indexOf(match[1]) !== -1) {
return match[1];
}
}
return null;
}
function isSeparateCss(file, separateDirs) {
if (!separateDirs) {
return false;
}
for (let i = 0; i < separateDirs.length; i++) {
if (file.includes(separateDirs[i])) {
return true;
}
}
return false;
}
function minifyCss(cssContent) {
return cssnano
.process(cssContent, {
preset: 'default',
zindex: false,
})
.then(result => result.css);
}
function autoPrefixCss(cssContent) {
return postcss([autoprefixer])
.process(cssContent, {
from: undefined,
})
.then(result => result.css);
}
module.exports = {
getSubDir,
getLanguage,
isSeparateCss,
minifyCss,
autoPrefixCss,
};