Skip to content

Instantly share code, notes, and snippets.

@sudo-suhas
Last active February 20, 2017 13:26
Show Gist options
  • Save sudo-suhas/a06197a38ab105696b670fc9aed9d246 to your computer and use it in GitHub Desktop.
Save sudo-suhas/a06197a38ab105696b670fc9aed9d246 to your computer and use it in GitHub Desktop.
Sample code for Vue build utils
function cssLoaders(options = {}) {
const defaultLoaderOptions = !_.has(options, 'sourceMap') ? false : { sourceMap: options.sourceMap };
const cssLoader = {
loader: 'css-loader',
options: {
sourceMap: options.sourceMap,
minimize: nodeEnv === 'production'
}
};
/**
* Generate css loaders for given loader
*
* @param {string|Object} loader
* @return {Array|Object} Array of loaders to be used or the Extract text loader
*/
function generateLoaders(loader) {
const styleLoaders = [cssLoader];
if (loader) {
let styleLoader;
if (_.isString(loader)) {
styleLoader = defaultLoaderOptions === false ? `${loader}-loader` : {
loader: `${loader}-loader`,
options: defaultLoaderOptions
};
} else { // assume it is an object
if (defaultLoaderOptions !== false) {
_.assign(loader.options, defaultLoaderOptions);
}
styleLoader = loader;
}
styleLoaders.push(styleLoader); // loadeObj can be string/object
}
// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ExtractTextPlugin.extract({
use: styleLoaders,
fallback: 'vue-style-loader'
});
}
return ['vue-style-loader', ...styleLoaders];
}
// http://vuejs.github.io/vue-loader/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders({ loader: 'sass-loader', options: { indentedSyntax: true } }),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment