Skip to content

Commit

Permalink
fix(taro-plugin-sass): 修复 plugins.sass 只配置了 resource 时编译出错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 committed Oct 23, 2019
1 parent 5839608 commit 92b53d7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
24 changes: 24 additions & 0 deletions packages/taro-plugin-sass/bundler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Bundler = require('scss-bundle').Bundler

/**
* Return bundled sass content.
*
* @param {string} url Absolute file path.
* @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located. Used for resolving tilde imports.
* @returns Bundle result.
*/
async function getBundleContent(url, projectDirectory = undefined) {
let bundler = undefined
if (projectDirectory) {
bundler = new Bundler(undefined, projectDirectory)
} else {
bundler = new Bundler()
}
if (!bundler) {
throw new ReferenceError(`'bundler' is not defined.`)
}
const res = await bundler.Bundle(url)
return res
}

module.exports = getBundleContent
43 changes: 32 additions & 11 deletions packages/taro-plugin-sass/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
const sass = require('node-sass')
const Bundler = require('scss-bundle').Bundler
const getBundleContent = require('./bundler')

module.exports = function compileSass (content, file, config) {
return new Promise(async (resolve, reject) => {
let bundledContent = ''

// when plugins.sass only configured resource property
if (config.resource && !config.projectDirectory) {
const { resource } = config
try {
if (typeof resource === 'string') {
const res = await getBundleContent(resource)
bundledContent += res.bundledContent
}
if (Array.isArray(resource)) {
for (const url of resource) {
const res = await getBundleContent(url)
bundledContent += res.bundledContent
}
}
} catch (e) {
reject(e)
}
}

// check resource & projectDirectory property
// projectDirectory used for resolving tilde imports
if (config.resource && config.projectDirectory) {
const { resource, projectDirectory } = config
const getBundleContent = async (url) => {
const bundler = new Bundler(undefined, projectDirectory)
const res = await bundler.Bundle(url)
bundledContent += res.bundledContent
}
try {
if (typeof resource === 'string') {
await getBundleContent(resource)
} else if (Array.isArray(resource)) {
for (let url of resource) {
await getBundleContent(url)
const res = await getBundleContent(resource, projectDirectory)
bundledContent += res.bundledContent
}
if (Array.isArray(resource)) {
for (const url of resource) {
const res = await getBundleContent(url, projectDirectory)
bundledContent += res.bundledContent
}
}
} catch (e) {
reject(e)
}
}

if (config.data) {
bundledContent += config.data
}

const opts = Object.assign({}, config, {
file,
data: bundledContent ? bundledContent + content : content
})
var result

let result
try {
result = sass.renderSync(opts)
} catch (e) {
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-plugin-sass/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"version": "1.3.21",
"description": "Taro编译sass文件",
"main": "index.js",
"files": [
"bundler.js",
"index.js"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down

0 comments on commit 92b53d7

Please sign in to comment.