Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复h5环境下plugins-sass配置不生效的问题 #4474

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/taro-webpack-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"webpack": "4.28.4",
"webpack-chain": "4.9.0",
"webpack-dev-server": "3.1.14",
"webpack-format-messages": "2.0.3"
"webpack-format-messages": "2.0.3",
"scss-bundle": "^2.5.1"
},
"devDependencies": {
"@types/autoprefixer": "6.7.3",
Expand Down
9 changes: 5 additions & 4 deletions packages/taro-webpack-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import * as path from 'path'
import { format as formatUrl } from 'url'
import * as webpack from 'webpack'
import * as WebpackDevServer from 'webpack-dev-server'

import buildConf from './config/build.conf'
import devConf from './config/dev.conf'
import baseDevServerOption from './config/devServer.conf'
import prodConf from './config/prod.conf'
import { addLeadingSlash, addTrailingSlash, recursiveMerge } from './util'
import { bindDevLogger, bindProdLogger, printBuildError } from './util/logHelper'
import { BuildConfig } from './util/types'
import { makeConfig } from './util/chain';

const stripTrailingSlash = (path: string): string =>
path.charAt(path.length - 1) === '/' ? path.slice(0, -1) : path
Expand Down Expand Up @@ -128,15 +128,16 @@ const buildDev = async (appPath: string, config: BuildConfig): Promise<any> => {
}

export default async (appPath: string, config: BuildConfig): Promise<void> => {
if (config.isWatch) {
const newConfig: BuildConfig = await makeConfig(config);
if (newConfig.isWatch) {
try {
await buildDev(appPath, config)
await buildDev(appPath, newConfig)
} catch (e) {
console.error(e)
}
} else {
try {
await buildProd(appPath, config)
await buildProd(appPath, newConfig)
} catch (e) {
console.error(e)
process.exit(1);
Expand Down
47 changes: 44 additions & 3 deletions packages/taro-webpack-runner/src/util/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,52 @@ 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 { 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 || {}

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
}

try {
if (typeof resource === 'string') {
await getBundleContent(resource)
} else if (Array.isArray(resource)) {
for (const url of resource) {
await getBundleContent(url)
}
}
} catch (e) {
console.log(e)
}
}
if (sass.data) {
bundledContent += sass.data
}
return {
...config,
plugins,
sassLoaderOption: {
...sassLoaderOption,
data: sassLoaderOption.data ? `${sassLoaderOption.data}${bundledContent}` : bundledContent
}
}
}

const defaultUglifyJsOption = {
keep_fnames: true,
Expand Down Expand Up @@ -261,7 +301,7 @@ const getModule = (appPath: string, {
const extractCssLoader = getExtractCssLoader()

const lastStyleLoader = enableExtract ? extractCssLoader : styleLoader

/**
* css-loader 1.0.0版本移除了minimize选项...升级需谨慎
*
Expand Down Expand Up @@ -421,7 +461,8 @@ const getDevtool = enableSourceMap => {
export {
isNodeModule,
isTaroModule,
getEsnextModuleRules
getEsnextModuleRules,
makeConfig
}

export { getOutput, getMiniCssExtractPlugin, getHtmlWebpackPlugin, getDefinePlugin, processEnvOption, getHotModuleReplacementPlugin, getModule, getUglifyPlugin, getDevtool, getCssoWebpackPlugin, getCopyWebpackPlugin }
1 change: 1 addition & 0 deletions packages/taro-webpack-runner/src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface TaroPlugins {
babel: Option;
csso?: TogglableOptions;
uglify?: TogglableOptions;
sass?: Option;
}

export interface CopyOptions {
Expand Down