Skip to content

Commit

Permalink
feat: webpack-runner 和 mini-runner 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 authored and luckyadam committed Jan 7, 2020
1 parent 7c46d85 commit 991588d
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 132 deletions.
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"packages/taro-quickapp",
"packages/taro-qq",
"packages/taro-jd",
"packages/taro-runner-utils",
"packages/taro-webpack-runner",
"packages/taro-mini-runner",
"packages/postcss-plugin-constparse",
Expand Down
1 change: 1 addition & 0 deletions packages/taro-mini-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/runner-utils": "2.0.0-beta.6",
"@tarojs/taro": "2.0.0-beta.6",
"@tarojs/transformer-wx": "2.0.0-beta.6",
"babel-core": "^6.26.3",
Expand Down
56 changes: 3 additions & 53 deletions packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as webpack from 'webpack'
import { getSassLoaderOption } from '@tarojs/runner-utils'

import { IBuildConfig } from './utils/types'
import { IBuildConfig, IOption } from './utils/types'
import { BUILD_TYPES } from './utils/constants'
import { printBuildError, bindProdLogger, bindDevLogger } from './utils/logHelper'
import Bundler from './utils/bundler'
import buildConf from './webpack/build.conf'

const customizeChain = (chain, customizeFunc: Function) => {
Expand All @@ -12,58 +12,8 @@ const customizeChain = (chain, customizeFunc: Function) => {
}
}

const getSassLoaderOption = async ({ sass, sassLoaderOption }: IBuildConfig) => {
let bundledContent = ''
sassLoaderOption = sassLoaderOption || {}
if (!sass) {
return sassLoaderOption
}
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)
}
}

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

const makeConfig = async (buildConfig: IBuildConfig) => {
const sassLoaderOption = await getSassLoaderOption(buildConfig)
const sassLoaderOption: IOption = await getSassLoaderOption(buildConfig)
return {
...buildConfig,
sassLoaderOption
Expand Down
24 changes: 0 additions & 24 deletions packages/taro-mini-runner/src/utils/bundler.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/taro-webpack-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"homepage": "https://github.com/NervJS/taro#readme",
"dependencies": {
"@tarojs/plugin-sass": "2.0.0-beta.6",
"@tarojs/runner-utils": "2.0.0-beta.6",
"@tarojs/taro": "2.0.0-beta.6",
"@tarojs/taro-h5": "2.0.0-beta.6",
"autoprefixer": "8.6.4",
Expand Down Expand Up @@ -63,7 +64,6 @@
"resolve-url-loader": "2.3.0",
"sass": "^1.23.1",
"sass-loader": "7.1.0",
"scss-bundle": "^2.5.1",
"style-loader": "0.21.0",
"stylus": "0.54.5",
"stylus-loader": "3.0.2",
Expand Down
56 changes: 2 additions & 54 deletions packages/taro-webpack-runner/src/util/chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as apis from '@tarojs/taro-h5/dist/taroApis'
import * as Bundler from '@tarojs/plugin-sass/bundler'
import { getSassLoaderOption } from '@tarojs/runner-utils'
import * as CopyWebpackPlugin from 'copy-webpack-plugin'
import CssoWebpackPlugin from 'csso-webpack-plugin'
import * as sass from 'sass'
Expand All @@ -16,60 +16,8 @@ import { recursiveMerge } from '.'
import { getPostcssPlugins } from '../config/postcss.conf'
import { Option, BuildConfig } from './types'

const getSassLoaderOption = async ({ sass, sassLoaderOption }: BuildConfig) => {
let bundledContent = ''
sassLoaderOption = sassLoaderOption || {}
if (!sass) {
return sassLoaderOption
}
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') {
const res = await Bundler(resource, projectDirectory)
bundledContent += res.bundledContent
}
if (Array.isArray(resource)) {
for (const url of resource) {
const res = await Bundler(url, projectDirectory)
bundledContent += res.bundledContent
}
}
} catch (e) {
console.log(e)
}
}
if (sass.data) {
bundledContent += sass.data
}
return {
...sassLoaderOption,
data: sassLoaderOption.data ? `${sassLoaderOption.data}${bundledContent}` : bundledContent
}
}

const makeConfig = async (buildConfig: BuildConfig) => {
const sassLoaderOption = await getSassLoaderOption(buildConfig)
const sassLoaderOption: Option = await getSassLoaderOption(buildConfig)
return {
...buildConfig,
sassLoaderOption
Expand Down

0 comments on commit 991588d

Please sign in to comment.