From f76467f5a3cefeded25b7967a0314e773252b341 Mon Sep 17 00:00:00 2001 From: Garfield Lee Date: Thu, 24 Oct 2019 15:12:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(taro-webpack-runner):=20=E4=BD=BF=20H5=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BF=AE=E6=94=B9=E5=90=8E=E7=9A=84=20plugin?= =?UTF-8?q?.sass=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 taro-plugin-sass 包并复用代码 --- packages/taro-webpack-runner/package.json | 1 + .../taro-webpack-runner/src/util/chain.ts | 42 +++++++++++++------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/packages/taro-webpack-runner/package.json b/packages/taro-webpack-runner/package.json index 32f40ed97fc1..6b7e7bdbaf2b 100644 --- a/packages/taro-webpack-runner/package.json +++ b/packages/taro-webpack-runner/package.json @@ -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", diff --git a/packages/taro-webpack-runner/src/util/chain.ts b/packages/taro-webpack-runner/src/util/chain.ts index 0baba5bddf42..c9427d074b13 100644 --- a/packages/taro-webpack-runner/src/util/chain.ts +++ b/packages/taro-webpack-runner/src/util/chain.ts @@ -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' @@ -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) {