🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 淘宝适配方案 问题: > px2remLoader, 无法和less共用。 起因: > 我开始是用了淘宝的适配方案,用了px2rem-loader进行自动转码,在这个项目当中,还使用了less来进行编写css,可惜的是less的全局变量无法,引入,每个子组件,都需要重新引入。 > 经过奋斗的百度后,总算解决了,全局引入的问题。可惜,px2rem-loader不生效了。经过查看。(还好我懂点webpack配置,之后一定要好好看看配置了。。。。) 我的配置如下: ~~~ '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 } } const postcssLoader = { loader: 'postcss-loader', options: { sourceMap: options.sourceMap } } var px2remLoader = { loader: 'px2rem-loader', options: { remUnit: 75 } } function resolveResource(name) { return path.resolve(__dirname, '../src/assets/css/' + name); } function generateSassResourceLoader() { var loaders = [ cssLoader, // 'postcss-loader', px2remLoader, 'less-loader', { loader: 'sass-resources-loader', options: { // it need a absolute path resources: [resolveResource('basic.less')] } } ]; if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, fallback: 'vue-style-loader', publicPath: "../../" }) } else { return ['vue-style-loader'].concat(loaders) } } // generate loader string to be used with extract text plugin function generateLoaders (loader, loaderOptions) { const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader,px2remLoader] 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', publicPath: "../../" }) } else { return ['vue-style-loader'].concat(loaders) } } // https://vue-loader.vuejs.org/en/configurations/extract-css.html return { css: generateLoaders(), postcss: generateLoaders(), less: generateSassResourceLoader(), 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') }) } } ~~~ * vue升级过后,loader都是放在build文件夹下面的utils.js。提供options的是webpack.dev.conf.js和webpack.prod.conf.js 资料链接: [解决第三方ui库的问题](https://segmentfault.com/q/1010000014793903/a-1020000014804930) [vue 引入less全局变量的解决方案](https://blog.csdn.net/u014292161/article/details/79193381)