Skip to content

Commit

Permalink
feat(taro-webpack-runner): 使 H5 支持修改后的 plugin.sass 配置
Browse files Browse the repository at this point in the history
增加 taro-plugin-sass 包并复用代码
  • Loading branch information
Garfield550 committed Oct 24, 2019
1 parent bd20fd4 commit f76467f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/taro-webpack-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"homepage": "https://github.com/NervJS/taro#readme",
"dependencies": {
"@tarojs/plugin-sass": "1.3.21",
"@tarojs/taro-h5": "1.3.21",
"autoprefixer": "8.6.4",
"babel-core": "6.26.0",
Expand Down
42 changes: 30 additions & 12 deletions packages/taro-webpack-runner/src/util/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as apis from '@tarojs/taro-h5/dist/taroApis'
import * as Bundler from '@tarojs/plugin-sass/bundler'
import * as CopyWebpackPlugin from 'copy-webpack-plugin'
import CssoWebpackPlugin from 'csso-webpack-plugin'
import * as sass from 'dart-sass'
Expand All @@ -7,34 +8,51 @@ import { partial } from 'lodash'
import { mapKeys, pipe } from 'lodash/fp'
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin'
import { join, resolve } from 'path'
import { Bundler } from 'scss-bundle'
import * as UglifyJsPlugin from 'uglifyjs-webpack-plugin'
import * as webpack from 'webpack'

import { recursiveMerge } from '.'
import { getPostcssPlugins } from '../config/postcss.conf'
import {BuildConfig, CopyOptions, Option, PostcssOption} from './types'
import { BuildConfig, CopyOptions, Option, PostcssOption } from './types'

const makeConfig = async (config: BuildConfig) => {
const plugins = config.plugins || {}
const sassLoaderOption = config.sassLoaderOption || {}
const sass= plugins.sass || {}
const sass = plugins.sass || {}

let bundledContent = ''
if (sass.resource && sass.projectDirectory) {
const { resource, projectDirectory } = sass
const getBundleContent = async (url) => {
const bundler = new Bundler(undefined, projectDirectory)
const res = await bundler.Bundle(url)
bundledContent += res.bundledContent
// when plugins.sass only configured resource property
if (sass.resource && !sass.projectDirectory) {
const { resource } = sass
try {
if (typeof resource === 'string') {
const res = await Bundler(resource)
bundledContent += res.bundledContent
}
if (Array.isArray(resource)) {
for (const url of resource) {
const res = await Bundler(url)
bundledContent += res.bundledContent
}
}
} catch (e) {
console.log(e)
}
}

// check resource & projectDirectory property
// projectDirectory used for resolving tilde imports
if (sass.resource && sass.projectDirectory) {
const { resource, projectDirectory } = sass
try {
if (typeof resource === 'string') {
await getBundleContent(resource)
} else if (Array.isArray(resource)) {
const res = await Bundler(resource, projectDirectory)
bundledContent += res.bundledContent
}
if (Array.isArray(resource)) {
for (const url of resource) {
await getBundleContent(url)
const res = await Bundler(url, projectDirectory)
bundledContent += res.bundledContent
}
}
} catch (e) {
Expand Down

0 comments on commit f76467f

Please sign in to comment.