Skip to content

Commit

Permalink
fix(webpack): 修复文件压缩的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 26, 2019
1 parent d348879 commit 1094923
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/taro-mini-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"url-loader": "^2.0.0",
"virtual-module-webpack-plugin": "^0.4.1",
"webpack": "^4.31.0",
"webpack-chain": "^6.0.0",
"webpack-chain": "4.9.0",
"webpack-format-messages": "^2.0.5",
"yauzl": "2.10.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export default class TaroLoadChunksPlugin {
})
})
const source = new ConcatSource()
const id = chunk.id
const name = chunk.name
commonChunks.forEach(chunkItem => {
source.add(`require(${JSON.stringify(promoteRelativePath(path.relative(id, chunkItem.name)))});\n`)
source.add(`require(${JSON.stringify(promoteRelativePath(path.relative(name, chunkItem.name)))});\n`)
})
source.add('\n')
source.add(modules)
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-mini-runner/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const REG_SCRIPTS: RegExp = /\.[tj]sx?$/i
export const REG_SASS: RegExp = /\.(s[ac]ss)$/
export const REG_LESS: RegExp = /\.less$/
export const REG_STYLUS: RegExp = /\.styl$/
export const REG_STYLE: RegExp = /\.(css|scss|sass|less|styl|wxss)(\?.*)?$/
export const REG_STYLE: RegExp = /\.(css|scss|sass|less|styl|wxss|acss)(\?.*)?$/
export const REG_MEDIA: RegExp = /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/
export const REG_IMAGE: RegExp = /\.(png|jpe?g|gif|bpm|svg|webp)(\?.*)?$/
export const REG_FONT: RegExp = /\.(woff2?|eot|ttf|otf)(\?.*)?$/
Expand Down
24 changes: 24 additions & 0 deletions packages/taro-mini-runner/src/webpack/base.conf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as path from 'path'
import * as Chain from 'webpack-chain'

export default (appPath: string) => {
const chain = new Chain()
chain.merge({
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
mainFields: ['main', 'module'],
symlinks: true,
modules: [
path.join(appPath, 'node_modules'),
'node_modules'
]
},
resolveLoader: {
modules: [
'node_modules'
]
}
})

return chain
}
31 changes: 8 additions & 23 deletions packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from 'path'
import * as Chain from 'webpack-chain'

import { IBuildConfig } from '../utils/types'
import {
Expand All @@ -15,13 +14,14 @@ import {
getMiniPlugin,
getMiniCssExtractPlugin,
} from './chain'
import getBaseConf from './base.conf'
import { BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES } from '../utils/constants'
import { Targets } from '../plugins/MiniPlugin'

const emptyObj = {}

export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
const chain = new Chain()
const chain = getBaseConf(appPath)
const {
buildAdapter = BUILD_TYPES.WEAPP,
alias = emptyObj,
Expand Down Expand Up @@ -77,7 +77,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {

plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{
filename: `[name]${MINI_APP_FILES[buildAdapter].STYLE}`,
chunkFilename: `[id]${MINI_APP_FILES[buildAdapter].STYLE}`
chunkFilename: `[name]${MINI_APP_FILES[buildAdapter].STYLE}`
}, miniCssExtractPluginOption])

const isCssoEnabled = (csso && csso.enable === false)
Expand All @@ -98,10 +98,11 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
}

if (isCssoEnabled) {
plugin.cssoWebpackPlugin = getCssoWebpackPlugin([csso ? csso.config : {}])
const cssoConfig: any = csso ? csso.config : {}
plugin.cssoWebpackPlugin = getCssoWebpackPlugin([cssoConfig])
}
}
const mainConfig = {
chain.merge({
mode,
devtool: getDevtool(enableSourceMap),
entry,
Expand All @@ -111,21 +112,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
buildAdapter,
}, output]),
target: Targets[buildAdapter],
resolve: {
alias,
extensions: ['.js', '.jsx', '.ts', '.tsx'],
mainFields: ['main', 'module'],
symlinks: true,
modules: [
path.join(appPath, 'node_modules'),
'node_modules'
]
},
resolveLoader: {
modules: [
'node_modules'
]
},
resolve: { alias },
module: getModule(appPath, {
sourceDir,

Expand Down Expand Up @@ -166,8 +153,6 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
}
}
}
}

chain.merge(mainConfig)
})
return chain
}
5 changes: 3 additions & 2 deletions packages/taro-mini-runner/src/webpack/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const mergeOption = ([...options]: IOption[]): IOption => {
const styleModuleReg = /(.*\.module).*\.(css|s[ac]ss|less|styl)\b/
const styleGlobalReg = /(.*\.global).*\.(css|s[ac]ss|less|styl)\b/

export const processEnvOption = partial(mapKeys as any, (key: string) => `process.env.${key}`)
export const processEnvOption = partial(mapKeys as any, (key: string) => `process.env.${key}`) as any

export const getCssLoader = pipe(mergeOption, partial(getLoader, 'css-loader'))
export const getPostcssLoader = pipe(mergeOption, partial(getLoader, 'postcss-loader'))
Expand Down Expand Up @@ -130,7 +130,7 @@ export const getUglifyPlugin = ([enableSourceMap, uglifyOptions]) => {
})
}
export const getCssoWebpackPlugin = ([cssoOption]) => {
return pipe(mergeOption, listify, partial(getPlugin, CssoWebpackPlugin))([defaultCSSCompressOption, cssoOption])
return pipe(listify, partial(getPlugin, CssoWebpackPlugin))([mergeOption([defaultCSSCompressOption, cssoOption]), REG_STYLE])
}
export const getCopyWebpackPlugin = ({ copy, appPath }: {
copy: ICopyOptions,
Expand Down Expand Up @@ -344,6 +344,7 @@ export function getOutput (appPath: string, [{ outputRoot, publicPath, buildAdap
path: path.join(appPath, outputRoot),
publicPath,
filename: '[name].js',
chunkFilename: '[name].js',
globalObject: globalObjectMap[buildAdapter],
...customOutput
}
Expand Down
3 changes: 1 addition & 2 deletions packages/taro/types/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,5 @@ export interface IProjectConfig extends IProjectBaseConfig {
extraWatchFiles?: any[]
},
mini?: IMiniAppConfig,
h5?: IH5Config,
quickApp?: ITaroManifestConfig
h5?: IH5Config
}

0 comments on commit 1094923

Please sign in to comment.