|
| 1 | +/** |
| 2 | + * @file utils |
| 3 | + * @author panyuqi ([email protected]) |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const path = require('path'); |
| 9 | +const ExtractTextPlugin = require('extract-text-webpack-plugin'); |
| 10 | + |
| 11 | +exports.assetsPath = function (newPath) { |
| 12 | + return path.posix.join('static', newPath); |
| 13 | +}; |
| 14 | + |
| 15 | +exports.cssLoaders = function (options) { |
| 16 | + options = options || {}; |
| 17 | + |
| 18 | + let cssLoader = { |
| 19 | + loader: 'css-loader', |
| 20 | + options: { |
| 21 | + minimize: process.env.NODE_ENV === 'production', |
| 22 | + sourceMap: options.sourceMap |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + // generate loader string to be used with extract text plugin |
| 27 | + function generateLoaders(loader, loaderOptions) { |
| 28 | + let loaders = [cssLoader]; |
| 29 | + if (loader) { |
| 30 | + loaders.push({ |
| 31 | + loader: loader + '-loader', |
| 32 | + options: Object.assign({}, loaderOptions, { |
| 33 | + sourceMap: options.sourceMap |
| 34 | + }) |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // Extract CSS when that option is specified |
| 39 | + // (which is the case during production build) |
| 40 | + if (options.extract) { |
| 41 | + return ExtractTextPlugin.extract({ |
| 42 | + use: loaders, |
| 43 | + fallback: 'vue-style-loader' |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + return ['vue-style-loader'].concat(loaders); |
| 48 | + } |
| 49 | + |
| 50 | + // https://vue-loader.vuejs.org/en/configurations/extract-css.html |
| 51 | + return { |
| 52 | + css: generateLoaders(), |
| 53 | + postcss: generateLoaders(), |
| 54 | + less: generateLoaders('less'), |
| 55 | + sass: generateLoaders('sass', {indentedSyntax: true}), |
| 56 | + scss: generateLoaders('sass'), |
| 57 | + stylus: generateLoaders('stylus'), |
| 58 | + styl: generateLoaders('stylus') |
| 59 | + }; |
| 60 | +}; |
| 61 | + |
| 62 | +// Generate loaders for standalone style files (outside of .vue) |
| 63 | +exports.styleLoaders = function (options) { |
| 64 | + let output = []; |
| 65 | + let loaders = exports.cssLoaders(options); |
| 66 | + |
| 67 | + Object.keys(loaders).forEach(function (extension) { |
| 68 | + output.push({ |
| 69 | + test: new RegExp('\\.' + extension + '$'), |
| 70 | + use: loaders[extension] |
| 71 | + }); |
| 72 | + }); |
| 73 | + return output; |
| 74 | +}; |
0 commit comments