Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const setup = inputSettings => {
let settings = Object.assign(utils.clone(defaultSettings), inputSettings);
// In memory
if (settings.content) {
checkMandatoriesMemoryContent(inputSettings);
return settings;
}
checkMandatories(inputSettings);
settings = Object.assign(settings, wildcards(settings.input, settings.publicFolder));
settings = Object.assign(
settings,
checkOutput(settings.input, settings.output, settings.publicFolder, settings.replaceInPlace)
);
settings = Object.assign(settings, setPublicFolder(settings.input, settings.publicFolder));
const checkOutput = (input, output, publicFolder, replaceInPlace) => {
let reg = new RegExp('\\$1');
if (reg.test(output)) {
if (Array.isArray(input)) {
const outputMin = input.map(file =>
utils.setFileNameMin(file, output, replaceInPlace ? null : publicFolder, replaceInPlace)
);
return { output: outputMin };
} else {
return { output: utils.setFileNameMin(input, output, replaceInPlace ? null : publicFolder, replaceInPlace) };
}
}
};
if (settings.options.babelrc) {
babelOptions = JSON.parse(utils.readFile(settings.options.babelrc));
}
if (settings.options.presets) {
const babelrcPresets = babelOptions.presets || [];
babelOptions.presets = babelrcPresets.concat(settings.options.presets);
}
if (babelOptions.presets.indexOf('minify') === -1) {
babelOptions.presets = babelOptions.presets.concat([minify]);
}
const contentMinified = transform(content, babelOptions);
if (!settings.content) {
utils.writeFile({ file: settings.output, content: contentMinified.code, index });
}
if (callback) {
return callback(null, contentMinified.code);
}
return contentMinified.code;
};
const minifyUglifyES = ({ settings, content, callback, index }) => {
if (settings.options.sourceMap) {
content = { [settings.options.sourceMap.filename]: content };
}
const contentMinified = uglifyES.minify(content, settings.options);
if (contentMinified.error) {
if (callback) {
return callback(contentMinified.error);
}
}
if (contentMinified.map && settings.options.sourceMap) {
utils.writeFile({ file: `${settings.output}.map`, content: contentMinified.map, index });
}
if (!settings.content) {
utils.writeFile({ file: settings.output, content: contentMinified.code, index });
}
if (callback) {
return callback(null, contentMinified.code);
}
return contentMinified.code;
};
const noCompress = ({ settings, content, callback, index }) => {
if (!settings.content) {
utils.writeFile({ file: settings.output, content, index });
}
if (callback) {
return callback(null, content);
}
return content;
};
const minifyCssnano = async ({ settings, content, callback, index }) => {
let contentMinified;
try {
contentMinified = await postcss([cssnano]).process(content, { from: undefined });
} catch (e) {
if (callback) {
return callback(e);
}
}
if (!settings.content) {
utils.writeFile({ file: settings.output, content: contentMinified.css, index });
}
if (callback) {
return callback(null, contentMinified.css);
}
return contentMinified.css;
};
const minifyCrass = ({ settings, content, callback, index }) => {
const contentMinified = crass
.parse(content)
.optimize()
.toString();
if (!settings.content) {
utils.writeFile({ file: settings.output, content: contentMinified, index });
}
if (callback) {
return callback(null, contentMinified);
}
return contentMinified;
};
const minifyCSSO = ({ settings, content, callback, index }) => {
const contentMinified = csso.minify(content, settings.options.restructureOff);
if (!settings.content) {
utils.writeFile({ file: settings.output, content: contentMinified.css, index });
}
if (callback) {
return callback(null, contentMinified.css);
}
return contentMinified.css;
};
const minifyUglifyJS = ({ settings, content, callback, index }) => {
const contentMinified = uglifyJS.minify(content, settings.options);
if (contentMinified.error) {
if (callback) {
return callback(contentMinified.error);
}
}
if (contentMinified.map && settings.options.sourceMap) {
utils.writeFile({ file: settings.options.sourceMap.filename, content: contentMinified.map, index });
}
if (!settings.content) {
utils.writeFile({ file: settings.output, content: contentMinified.code, index });
}
if (callback) {
return callback(null, contentMinified.code);
}
return contentMinified.code;
};
const minifySqwish = ({ settings, content, callback, index }) => {
const contentMinified = sqwish.minify(content, settings.options.strict);
if (!settings.content) {
utils.writeFile({ file: settings.output, content: contentMinified, index });
}
if (callback) {
return callback(null, contentMinified);
}
return contentMinified;
};