您好,登錄后才能下訂單哦!
讓我們考慮下場景:當使用rem/vw開發移動端的時候,你定義了一個px轉rem的函數,或者是網站配色的全局變量等,然后到工程里為每個vue文件或者組件@import ‘publicfilename.scss',那得重復做這樣的工作很多很多...次,萬一這些公用文件目錄路徑變怎么辦呢,哭都來不及,想想都覺得恐怖呀!
接下來拯救我們的神器就要登場了---sass-resources-loader,它可以省去重復性的引入,還支持less,postcss等,具體用法如下:
npm install -d sass-resource-loader
首先我們找到項目里build目錄,在該目錄下找到util.js
更新前util.js代碼如下:
'use strict' const path = require('path') const config = require('../config') const ExtractTextPlugin = require('extract-text-webpack-plugin') const packageConfig = require('../package.json') exports.assetsPath = function (_path) { const assetsSubDirectory = process.env.NODE_ENV === 'production' ? config.build.assetsSubDirectory : config.dev.assetsSubDirectory return path.posix.join(assetsSubDirectory, _path) } exports.cssLoaders = function (options) { options = options || {} const cssLoader = { loader: 'css-loader', options: { sourceMap: options.sourceMap } } // generate loader string to be used with extract text plugin function generateLoaders (loader, loaderOptions) { const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader] if (loader) { loaders.push({ loader: loader + '-loader', options: Object.assign({}, loaderOptions, { sourceMap: options.sourceMap }) }) } // Extract CSS when that option is specified // (which is the case during production build) if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, fallback: 'vue-style-loader' }) } else { return ['vue-style-loader'].concat(loaders) } } // https://vue-loader.vuejs.org/en/configurations/extract-css.html return { css: generateLoaders(), postcss: generateLoaders(), less: generateLoaders('less'), sass: generateLoaders('sass', { indentedSyntax: true }), scss: generateLoaders('sass'), stylus: generateLoaders('stylus'), styl: generateLoaders('stylus') } } // Generate loaders for standalone style files (outside of .vue) exports.styleLoaders = function (options) { const output = [] const loaders = exports.cssLoaders(options) for (const extension in loaders) { const loader = loaders[extension] output.push({ test: new RegExp('\\.' + extension + '$'), use: loader }) } return output } exports.createNotifierCallback = () => { const notifier = require('node-notifier') return (severity, errors) => { if (severity !== 'error') return const error = errors[0] const filename = error.file && error.file.split('!').pop() notifier.notify({ title: packageConfig.name, message: severity + ': ' + error.name, subtitle: filename || '', icon: path.join(__dirname, 'logo.png') }) } } /** * 增加 hljs 的 classname */ exports.wrapCustomClass = function (render) { return function (...args) { return render(...args) .replace('<code class="', '<code class="hljs ') .replace('<code>', '<code class="hljs">') } } /** * Format HTML string */ exports.convertHtml = function (str) { return str.replace(/(&#x)(\w{4});/gi, $0 => String.fromCharCode(parseInt(encodeURIComponent($0).replace(/(%26%23x)(\w{4})(%3B)/g, '$2'), 16))) }
看util.js文件,我們首先找到我需要修改目標在哪:
// https://vue-loader.vuejs.org/en/configurations/extract-css.html return { css: generateLoaders(), postcss: generateLoaders(), less: generateLoaders('less'), sass: generateLoaders('sass', { indentedSyntax: true }), scss: generateLoaders('sass'), stylus: generateLoaders('stylus'), styl: generateLoaders('stylus') }
我們那sass為例,用sass-resources-loader對其改造:
/** * sass Less 源文件 * @param name classFile * @returns {string} */ function resolveResouce(name) { return path.resolve(__dirname, '../static/' + name); } //導入全局sass mixin function等 function generateSassResourceLoader(){ var loaders = [ cssLoader, //'postcss-loader', 'sass-loader', { loader:'sass-resources-loader', options: { //需要一個全局路徑 resources: [resolveResouce('index.scss')] } } ] if(options.extract){ return ExtractTextPlugin.extract({ use:loaders, fallback: 'vue-style-loader' }) }else{ return ['vue-style-loader'].concat(loaders) } }
工具寫好好了,接下來直接把上門return對象做些調整,代碼如下
// https://vue-loader.vuejs.org/en/configurations/extract-css.html return { css: generateLoaders(), postcss: generateLoaders(), less: generateLoaders('less'), sass:generateSassResourceLoader(), scss:generateSassResourceLoader(), stylus: generateLoaders('stylus'), styl: generateLoaders('stylus') }
從代碼中可以看出,我們把sass和scss的loader換成我們自己根據sass-resources-loader寫的方法了,這樣就實現了我們的目標。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。