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

feat(mini-runner/mini-split-chunks): (主包大小最优方案)添加小程序提取公共模块插件,主包没有引用的,且分包内引用的mod… #8135

Merged
merged 35 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8315494
feat(mini-runner/mini-split-chunks): 添加小程序提取公共模块插件,主包没有引用的,且分包内引用的mod…
huangcj99 Nov 24, 2020
4e078e4
feat(mini-runner/mini-split-chunks): optimizeMainPackage配置改成对象,添加excl…
huangcj99 Dec 3, 2020
d2e0c3f
Merge branch 'next' into mini-split-chunk
luckyadam Dec 22, 2020
2edf51f
Merge branch 'mini-split-chunk' of github.com:huangcj99/taro into min…
huangcj99 Dec 24, 2020
63350d0
refactor(mini-split-chunks): update snapshot of mini-split-chunks
huangcj99 Dec 24, 2020
a06dd25
Merge branch 'next' into mini-split-chunk
huangcj99 Jan 9, 2021
b5da699
refactor(mini-runner/mini-split-chunks): update snapshot of mini-spli…
huangcj99 Jan 9, 2021
8176a61
fix(mini-runner): resolve conflict of yarn.lock
huangcj99 Jan 29, 2021
9bc0462
fix(mini-runner): update snapshot of mini-split-chunk
huangcj99 Jan 29, 2021
7937bb3
Merge branch 'mini-split-chunk' of github.com:huangcj99/taro into min…
huangcj99 Jan 29, 2021
2b5181e
Merge branch 'next' into mini-split-chunk
huangcj99 Jan 29, 2021
fad42e8
Merge branch 'next' of github.com:NervJS/taro into mini-split-chunk
huangcj99 Jan 29, 2021
e4a7e95
Merge branch 'mini-split-chunk' of github.com:huangcj99/taro into min…
huangcj99 Jan 29, 2021
1b22c39
Merge branch 'next' into mini-split-chunk
huangcj99 Jan 30, 2021
b95612e
feat(mini-runner): vendor改成sub-vendors,避免common中@import导致报错,该提取方案支持cs…
huangcj99 Feb 16, 2021
a7b0ab0
Merge branch 'mini-split-chunk' of github.com:huangcj99/taro into min…
huangcj99 Feb 16, 2021
37e57c0
Merge branch 'next' into mini-split-chunk
huangcj99 Feb 16, 2021
9d6f9b4
style(mini-runner): fix lint error
huangcj99 Feb 16, 2021
7a89e78
test(mini-runner): update snapshot of mini-split-chunks.spec.ts
huangcj99 Feb 16, 2021
94862e8
feat(mini-runner): ext改用枚举
huangcj99 Feb 17, 2021
0c58830
feat(mini-runner/mini-split-chunk): 避免dynamic或者css类型的module取resource为…
huangcj99 Mar 11, 2021
5e377f3
refactor(mini-runner): resolve conflicts
huangcj99 Mar 26, 2021
08ce473
test(mini-runner): update snapshot of mini-split-chunk
huangcj99 Mar 26, 2021
5bf83ef
test(mini-runner): 修改测试用例,改用判断输出chunk是否符合预期
huangcj99 Mar 27, 2021
9abaa17
Merge branch 'next' into mini-split-chunk
huangcj99 Mar 28, 2021
ce88ab8
Merge branch 'next' into mini-split-chunk
huangcj99 Mar 31, 2021
2372687
chore(mini-split-chunk): 解决yarn.lock冲突
huangcj99 Apr 2, 2021
3ab1483
Merge branch 'mini-split-chunk' of github.com:huangcj99/taro into min…
huangcj99 Apr 2, 2021
f7fc2da
chore(mini-split-chunk): 解决yarn.lock冲突
huangcj99 Apr 9, 2021
e7e27e7
Merge branch 'next' into mini-split-chunk
huangcj99 Apr 17, 2021
7169b88
Merge branch 'next' into mini-split-chunk
huangcj99 Apr 24, 2021
885631b
refactor(mini-runner): resolve conflicts
huangcj99 Apr 29, 2021
3e9673e
Merge branch 'next' into mini-split-chunk
luckyadam May 21, 2021
798b595
chore(taro): delete taro/lib
huangcj99 May 21, 2021
0300f87
Merge branch 'next' into mini-split-chunk
luckyadam May 21, 2021
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
2 changes: 2 additions & 0 deletions packages/taro-mini-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
"less-loader": "7.3.0",
"loader-utils": "^1.2.3",
"lodash": "^4.17.11",
"md5": "^2.3.0",
"micromatch": "^4.0.2",
"mini-css-extract-plugin": "0.8.0",
"miniprogram-simulate": "^1.1.5",
"mkdirp": "^1.0.4",
"ora": "^3.4.0",
"postcss-import": "12.0.1",
"postcss-loader": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mini-split-chunks should process mini-split-chunks 1`] = `40`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// babel-preset-taro 更多选项和默认值:
// https://github.com/NervJS/taro/blob/next/packages/babel-preset-taro/README.md
module.exports = {
presets: [
['taro', {
framework: 'react',
ts: false
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
pages: [
'pages/index/index'
],
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black'
},
subpackages: [
{
root: 'packageA',
pages: [
'detail/index',
'my/index'
]
},
{
root: 'packageB',
pages: [
'list/index'
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
page {
width: '100%';
height: '100%';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import React, { Component } from 'react'
import './app.css'

class App extends Component {
componentDidMount () {}

componentDidShow () {}

componentDidHide () {}

componentDidCatchError () {}

// this.props.children 是将要会渲染的页面
render () {
return this.props.children
}
}

export default App
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sub-common {
content: 'sub-common';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sub-common-module {
content: 'sub-common-module';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sub-vendors {
content: 'sub-vendors';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sub-vendors-module {
content: 'sub-vendors-module';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta content="width=device-width,initial-scale=1,user-scalable=no" name="viewport">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="format-detection" content="telephone=no,address=no">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
<title></title>
<script>
!function(x){function w(){var v,u,t,tes,s=x.document,r=s.documentElement,a=r.getBoundingClientRect().width;if(!v&&!u){var n=!!x.navigator.appVersion.match(/AppleWebKit.*Mobile.*/);v=x.devicePixelRatio;tes=x.devicePixelRatio;v=n?v:1,u=1/v}if(a>=640){r.style.fontSize="40px"}else{if(a<=320){r.style.fontSize="20px"}else{r.style.fontSize=a/320*20+"px"}}}x.addEventListener("resize",function(){w()});w()}(window);
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
navigationBarTitleText: '详情页'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.detail {
width: 100%;
height: 100%;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { Component } from 'react'
import { View, Text } from '@tarojs/components'
import consoleLogMain from '../../utils/consoleLogMain'
import consoleLogSubVendors from '../../utils/consoleLogSubVendors'
import testExcludeString from '../../utils/testExcludeString'
import './index.css'
import '../../css/sub-vendors.css'
import '../../css/sub-common.css'
import subCommonStyles from '../../css/sub-common.module.css'
import vendorsStyles from '../../css/sub-vendors.module.css'

export default class Detail extends Component {
componentDidMount() {
consoleLogMain()
consoleLogSubVendors()
testExcludeString()
}

render() {
return (
<View className='detail'>
<Text className={`
sub-vendors
sub-common
${subCommonStyles['sub-common-module']}
${vendorsStyles['sub-vendors-module']}
`}>I m detail</Text>
</View>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
navigationBarTitleText: '个人中心页'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { Component } from 'react'
import { View, Text } from '@tarojs/components'
import consoleLogMain from '../../utils/consoleLogMain'
import consoleLogSubVendors from '../../utils/consoleLogSubVendors'
import consoleLogSubCommon from '../../utils/consoleLogSubCommon'
import testExcludeString from '../../utils/testExcludeString'
import testExcludeFunction from '../../utils/testExcludeFunction'
import '../../css/sub-vendors.css'
import vendorsStyles from '../../css/sub-vendors.module.css'

export default class My extends Component {
componentDidMount () {
consoleLogMain()
consoleLogSubVendors()
consoleLogSubCommon('packageA')
testExcludeString()
testExcludeFunction()
}

render () {
return (
<View
className={`
sub-vendors
${vendorsStyles['sub-vendors-module']}
`}
>
<Text>I m my</Text>
</View>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
navigationBarTitleText: '列表页'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { Component } from 'react'
import { View, Text } from '@tarojs/components'
import consoleLogMain from '../../utils/consoleLogMain'
import consoleLogSubCommon from '../../utils/consoleLogSubCommon'
import testExcludeFunction from '../../utils/testExcludeFunction'
import '../../css/sub-common.css'
import subCommonStyles from '../../css/sub-common.module.css'

export default class My extends Component {
componentDidMount () {
consoleLogMain()
consoleLogSubCommon('packageB')
testExcludeFunction()
}

render () {
return (
<View
className={`
sub-common
${subCommonStyles['sub-common-module']}
`}
>
<Text>list page</Text>
</View>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
navigationBarTitleText: '首页'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { Component } from 'react'
import { View } from '@tarojs/components'
import Taro from '@tarojs/taro'
import consoleLogMain from '../../utils/consoleLogMain'

export default class Index extends Component {
componentDidMount() {
consoleLogMain()
}

render () {
return (
<View className='index'>
<View onClick={() => Taro.navigateTo({ url: '/packageA/detail/index' })}>
Go to detail
</View>
<View onClick={() => Taro.navigateTo({ url: '/packageA/my/index' })}>
Go to my
</View>
<View onClick={() => Taro.navigateTo({ url: '/packageB/list/index' })}>
Go to list
</View>
</View>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
console.log('Main package')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (subpackage) => {
console.log(`Subpackage: ${subpackage}`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
console.log('This is packageA vendors')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
console.log('testExcludeFunction')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
console.log('testExcludeString')
}
95 changes: 95 additions & 0 deletions packages/taro-mini-runner/src/__tests__/mini-split-chunks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import * as fs from 'fs-extra'
import * as path from 'path'

let compile
let readDir

describe('mini-split-chunks', () => {
beforeAll(() => {
jest.unmock('webpack')
})

beforeEach(() => {
jest.isolateModules(() => {
const compiler = require('./utils/compiler')
compile = compiler.compile
readDir = compiler.readDir
})
})

test('should process mini-split-chunks', async () => {
const appName = 'mini-split-chunks'
const { stats, config } = await compile(appName, {
optimizeMainPackage: {
enable: true,
exclude: [
path.resolve(__dirname, './fixtures/mini-split-chunks/src/utils/testExcludeString.js'),
(module) => module.resource?.indexOf('testExcludeFunction') >= 0
]
},
postcss: {
cssModules: {
enable: true,
config: {
namingPattern: 'module',
generateScopedName: '[name]__[local]___[hash:base64:5]'
}
}
}
})

const assets = stats.toJson().assets || []
expect(assets.length).toMatchSnapshot()

const files = readDir(fs, config.outputRoot)

/**
* 因为该plugin输出的文件名跟用户路径有关,snapshot输出的文件内容不稳定,这里改用判断输出文件是否存在来测试
*/
files.forEach(file => {
const fileContent = fs.readFileSync(file).toString()
const jsExt = '.js'
const wxssExt = '.wxss'
const needTestPages = [
'packageA/detail/index',
'packageA/my/index',
'packageB/list/index'
]
const checkChunksExist = ({ ext, regexp }) => {
const isNeedTestFile = needTestPages.find(needTestPage => new RegExp(`${needTestPage}${ext}$`).test(file))

if (!isNeedTestFile) return

const matchChunks = (fileContent || '').match(new RegExp(regexp, 'g'))
const chunkPaths = (matchChunks || []).map(chunkPath => {
return chunkPath.replace(new RegExp(regexp), '$1')
})

chunkPaths.forEach(chunkPath => {
const chunkExt = ext === jsExt ? jsExt : ''
const chunkAbsolutePath = path.resolve(file, '..', chunkPath + chunkExt)
const isExists = fs.pathExistsSync(chunkAbsolutePath)

// 根据match到的module path查看输出的chunk是否存在
expect(isExists).toBe(true)
})
}

// js
if (path.extname(file) === jsExt) {
checkChunksExist({
ext: jsExt,
regexp: 'require\\("(.*)"\\);'
})
}

// wxss
if (path.extname(file) === wxssExt) {
checkChunksExist({
ext: wxssExt,
regexp: new RegExp('@import "(.*)";')
})
}
})
})
})
2 changes: 1 addition & 1 deletion packages/taro-mini-runner/src/__tests__/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import build from '../../index'
// join: () => string
// }

function readDir (fs: IFs, dir: string) {
export function readDir (fs: IFs, dir: string) {
let files: string[] = []
const list = fs.readdirSync(dir)
list.forEach(item => {
Expand Down
Loading