提交 4b3d111b 编写于 作者: fxy060608's avatar fxy060608

feat(cli): support sass-loader v8 #776

上级 e8bfd051
const semver = require('semver')
let sassLoaderVersion
try {
sassLoaderVersion = semver.major(require('sass-loader/package.json').version)
} catch (e) {}
const SCSS =
`
$uni-color-primary: #007aff;
......@@ -100,7 +107,9 @@ $uni-font-size-subtitle: 36rpx
$uni-color-paragraph: #3F536E // 文章段落颜色
$uni-font-size-paragraph: 30rpx
`
module.exports = {
SCSS,
SASS
SASS,
sassLoaderVersion
}
const fs = require('fs')
const path = require('path')
const {
// jsPreprocessOptions,
getPlatformScss,
getPlatformSass,
nvueCssPreprocessOptions
// htmlPreprocessOptions
} = require('@dcloudio/uni-cli-shared')
const {
sassLoaderVersion
} = require('@dcloudio/uni-cli-shared/lib/scss')
const nvueStyleLoader = {
loader: '@dcloudio/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/style'
}
......@@ -25,11 +32,41 @@ const postcssLoader = {
}
}
// sass 全局变量
const isSass = fs.existsSync(path.resolve(process.env.UNI_INPUT_DIR, 'uni.sass'))
const isScss = fs.existsSync(path.resolve(process.env.UNI_INPUT_DIR, 'uni.scss'))
let sassData = isSass ? getPlatformSass() : getPlatformScss()
if (isSass) {
sassData = `@import "@/uni.sass"`
} else if (isScss) {
sassData = `${sassData}
@import "@/uni.scss";`
}
const scssLoader = {
loader: 'sass-loader',
options: {
sourceMap: false
}
}
const sassLoader = {
loader: 'sass-loader',
options: {
sourceMap: false,
data: ''
sourceMap: false
}
}
if (sassLoaderVersion < 8) {
scssLoader.options.data = sassData
sassLoader.options.data = sassData
sassLoader.options.indentedSyntax = true
} else {
scssLoader.options.prependData = sassData
sassLoader.options.prependData = sassData
sassLoader.options.sassOptions = {
indentedSyntax: true
}
}
......@@ -74,7 +111,7 @@ module.exports = [{
oneOf: createOneOf()
}, {
test: /\.scss$/,
oneOf: createOneOf(sassLoader)
oneOf: createOneOf(scssLoader)
}, {
test: /\.sass$/,
oneOf: createOneOf(sassLoader)
......
function resolve (module) {
try {
return require.resolve(module)
} catch (e) {}
return module
}
exports.cssLoaders = function (options) {
options = options || {}
const cssLoader = {
loader: resolve('css-loader'),
options: {
sourceMap: options.sourceMap
}
}
const postcssLoader = {
loader: resolve('postcss-loader'),
options: {
sourceMap: options.sourceMap
}
}
// generate loader string to be used with extract text plugin
const generateLoaders = (loader, loaderOptions) => {
let loaders = options.useVue ? [cssLoader] : []
if (options.usePostCSS) {
loaders.push(postcssLoader)
}
if (loader) {
loaders.push({
loader: resolve(loader + '-loader'),
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
})
}
if (options.useVue) {
return [resolve('vue-style-loader')].concat(loaders)
} else {
return loaders
}
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
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
}
const path = require('path')
const {
sassLoaderVersion
} = require('@dcloudio/uni-cli-shared/lib/scss')
function resolve (dir) {
return path.resolve(__dirname, '..', dir)
}
......@@ -59,6 +63,22 @@ module.exports = function chainWebpack (platformOptions) {
})
})
if (sassLoaderVersion >= 8) { // check indentedSyntax
// vue cli 3 and sass-loader 8
cssTypes.forEach(type => {
webpackConfig.module.rule('sass').oneOf(type).use('sass-loader').tap(options => {
if (options.indentedSyntax) {
if (!options.sassOptions) {
options.sassOptions = {}
}
options.sassOptions.indentedSyntax = true
delete options.indentedSyntax
}
return options
})
})
}
platformOptions.chainWebpack(webpackConfig)
// define
webpackConfig
......
const fs = require('fs')
const path = require('path')
const {
sassLoaderVersion
} = require('@dcloudio/uni-cli-shared/lib/scss')
module.exports = function initOptions (options) {
const {
getPlatformScss,
......@@ -55,14 +59,17 @@ module.exports = function initOptions (options) {
let sassData = isSass ? getPlatformSass() : getPlatformScss()
if (isSass) {
sassData = `${sassData}
@import "@/uni.sass"`
sassData = `@import "@/uni.sass"`
} else if (isScss) {
sassData = `${sassData}
@import "@/uni.scss";`
}
if (sassLoaderVersion < 8) {
options.css.loaderOptions.sass.data = sassData
} else {
options.css.loaderOptions.sass.prependData = sassData
}
let userPostcssConfigPath = path.resolve(process.env.UNI_INPUT_DIR, 'postcss.config.js')
if (fs.existsSync(userPostcssConfigPath)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册