-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(platform-taro): split chunks (#169)
- Loading branch information
Showing
3 changed files
with
83 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,8 @@ import { PACKAGE_DIR } from '../paths'; | |
import webpack from 'webpack'; | ||
import fs from 'fs'; | ||
import BuildAssetsPlugin from './plugins/build-assets-plugin'; | ||
import LoadChunkPlugin from './plugins/load-chunk-plugin'; | ||
export function config(api: IApi) { | ||
const resolveLocal = (m: string, sub?: string) => { | ||
const pck = path.dirname(require.resolve(`${m}/package.json`)); | ||
return sub ? `${pck}/${sub}` : pck; | ||
}; | ||
const resolveUser = (m: string) => | ||
path.join(api.paths.rootDir, 'node_modules', m); | ||
const EXT_REGEXP = /\.[a-zA-Z]+$/; | ||
|
||
const getAllFiles = ( | ||
|
@@ -55,6 +50,13 @@ export function config(api: IApi) { | |
}); | ||
config.entryPoints.clear(); | ||
config.optimization.clear(); | ||
config.output.chunkFilename( | ||
`${ | ||
process.env.NODE_ENV === 'development' | ||
? '[name]' | ||
: '[name].[contenthash:8]' | ||
}.js` | ||
); | ||
config.merge({ | ||
entry, | ||
resolve: { | ||
|
@@ -65,12 +67,12 @@ export function config(api: IApi) { | |
// 小程序使用 [email protected] | ||
'regenerator-runtime': require.resolve('regenerator-runtime'), | ||
// 开发组件库时 link 到本地调试,runtime 包需要指向本地 node_modules 顶层的 runtime,保证闭包值 Current 一致 | ||
//'@tarojs/runtime': require.resolve('@tarojs/runtime') | ||
'@tarojs/runtime': require.resolve('@tarojs/runtime'), | ||
'@binance/mp-service': '@tarojs/taro', | ||
'@binance/mp-components': '@tarojs/components', | ||
'@tarojs/components': '@tarojs/components/mini', | ||
'@binance/mp-api': '@tarojs/api', | ||
// make sure only one version of react exsit | ||
'@shuvi/platform-taro': PACKAGE_DIR, | ||
'@tarojs/react': '@tarojs/react', | ||
'react-dom$': '@tarojs/react', | ||
// 'react-reconciler$': resolveModule('react-reconciler'), | ||
|
@@ -93,7 +95,39 @@ export function config(api: IApi) { | |
modules: ['node_modules'] | ||
}, | ||
optimization: { | ||
sideEffects: true | ||
sideEffects: true, | ||
runtimeChunk: { | ||
name: 'runtime' | ||
}, | ||
splitChunks: { | ||
maxInitialRequests: Infinity, | ||
minSize: 0, | ||
usedExports: true, | ||
automaticNameDelimiter: '.', | ||
cacheGroups: { | ||
common: { | ||
name: 'common', | ||
chunks: 'all', | ||
minChunks: 2, | ||
priority: 1 | ||
}, | ||
vendors: { | ||
name: 'vendors', | ||
chunks: 'all', | ||
minChunks: 2, | ||
test: (module: any) => | ||
/[\\/]node_modules[\\/]/.test(module.resource), | ||
priority: 10 | ||
}, | ||
taro: { | ||
name: 'taro', | ||
chunks: 'all', | ||
test: (module: any) => | ||
/@tarojs[\\/][a-z]+/.test(module.context), | ||
priority: 100 | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
|
@@ -109,19 +143,7 @@ export function config(api: IApi) { | |
} | ||
]); | ||
config.plugin('BuildAssetsPlugin').use(BuildAssetsPlugin); | ||
config.resolve.alias.set('@shuvi/platform-react', PACKAGE_DIR); | ||
config.resolve.alias.set( | ||
'@shuvi/router-react$', | ||
resolveLocal('@shuvi/router-react') | ||
); | ||
config.resolve.alias.set('@shuvi/router$', resolveLocal('@shuvi/router')); | ||
// @ts-ignore | ||
config.resolve.alias.set('react$', [ | ||
resolveUser('react'), | ||
resolveLocal('react') | ||
]); | ||
// @ts-ignore | ||
config.resolve.alias.set('react-dom$', require.resolve('@tarojs/react')); | ||
config.plugin('LoadChunkPlugin').use(LoadChunkPlugin); | ||
return config; | ||
} | ||
}); | ||
|
37 changes: 37 additions & 0 deletions
37
packages/platform-taro/src/lib/bundler/plugins/load-chunk-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import webpack, { sources } from 'webpack'; | ||
const PLUGIN_NAME = 'LoadChunksPlugin'; | ||
const commonChunks = ['runtime', 'vendors', 'taro', 'common']; | ||
const entryChunk = 'app.js'; | ||
|
||
export default class TaroLoadChunksPlugin { | ||
constructor() {} | ||
|
||
apply(compiler: webpack.Compiler) { | ||
compiler.hooks.thisCompilation.tap( | ||
PLUGIN_NAME, | ||
(compilation: webpack.Compilation) => { | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: PLUGIN_NAME, | ||
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE | ||
}, | ||
() => { | ||
// require other chunks at the top of app.js | ||
const file = compilation.getAsset('app.js'); | ||
if (file) { | ||
compilation.updateAsset( | ||
entryChunk, | ||
source => | ||
new sources.RawSource( | ||
commonChunks | ||
.map(name => `require('./${name}');\n`) | ||
.join('') + source.source() | ||
) | ||
); | ||
} | ||
} | ||
); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters