@mpxjs/webpack-plugin 2.10.7-beta.4 → 2.10.7-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,68 +0,0 @@
1
- const RuntimeGlobals = require('webpack/lib/RuntimeGlobals')
2
- const Template = require('webpack/lib/Template')
3
- const HelperRuntimeModule = require('webpack/lib/runtime/HelperRuntimeModule')
4
-
5
- class LoadAsyncChunkRuntimeModule extends HelperRuntimeModule {
6
- constructor (timeout) {
7
- super('load async chunk')
8
- this.timeout = timeout || 5000
9
- }
10
-
11
- generate () {
12
- const { compilation } = this
13
- const { runtimeTemplate } = compilation
14
- const loadScriptFn = RuntimeGlobals.loadScript
15
- return Template.asString([
16
- 'var inProgress = {};',
17
- `${loadScriptFn} = ${runtimeTemplate.basicFunction(
18
- 'url, done, key, chunkId',
19
- [
20
- `var packageName = ${RuntimeGlobals.getChunkScriptFilename}(chunkId) || ''`,
21
- 'packageName = packageName.split("/")[0]',
22
- 'var config = {',
23
- Template.indent([
24
- 'url: url,',
25
- 'package: packageName'
26
- ]),
27
- '}',
28
- 'if(inProgress[url]) { inProgress[url].push(done); return; }',
29
- 'inProgress[url] = [done];',
30
- 'var callback = function (type, result) {',
31
- Template.indent([
32
- 'var event = {',
33
- Template.indent([
34
- 'type: type,',
35
- 'target: {',
36
- Template.indent(['src: url']),
37
- '}'
38
- ]),
39
- '}'
40
- ]),
41
- Template.indent([
42
- 'var doneFns = inProgress[url]',
43
- 'clearTimeout(timeoutCallback)',
44
- 'delete inProgress[url]',
45
- `doneFns && doneFns.forEach(${runtimeTemplate.returningFunction(
46
- 'fn(event)',
47
- 'fn'
48
- )})`
49
- ]),
50
- '}',
51
- `var timeoutCallback = setTimeout(callback.bind(null, 'timeout'), ${this.timeout})`,
52
- "var successCallback = callback.bind(null, 'load');",
53
- "var failedCallback = callback.bind(null, 'fail')",
54
- 'var loadChunkAsyncFn = global.__mpx.config.rnConfig && global.__mpx.config.rnConfig.loadChunkAsync',
55
- 'if (typeof loadChunkAsyncFn !== \'function\') {',
56
- Template.indent([
57
- 'console.error("[Mpx runtime error]: please provide correct loadChunkAsync function")',
58
- 'return'
59
- ]),
60
- '}',
61
- 'loadChunkAsyncFn(config).then(successCallback).catch(failedCallback)'
62
- ]
63
- )}`
64
- ])
65
- }
66
- }
67
-
68
- module.exports = LoadAsyncChunkRuntimeModule
@@ -1,81 +0,0 @@
1
- import { useState, ComponentType, useEffect, useCallback, useRef } from 'react'
2
- import { DefaultFallback, DefaultLoading, PageWrapper } from './AsyncContainer'
3
- import type { DefaultFallbackProps } from './AsyncContainer'
4
-
5
- const asyncChunkMap = new Map()
6
-
7
- interface props {
8
- type: 'component' | 'page'
9
- chunkName: string
10
- request: string
11
- props: any,
12
- loading: ComponentType<unknown>
13
- fallback: ComponentType<unknown>
14
- getChildren: () => Promise<unknown>
15
- }
16
-
17
- const AsyncSuspense: React.FC<props> = ({ type, props, chunkName, request, loading, fallback, getChildren }) => {
18
- const [status, setStatus] = useState('pending')
19
- const loaded = asyncChunkMap.has(request)
20
- const [, setKey] = useState(0)
21
- const chunkPromise = useRef<null | Promise<unknown>>(null)
22
-
23
- const reloadPage = useCallback(() => {
24
- setKey((preV) => preV + 1)
25
- console.log('[mpxAsyncSuspense]: reload page')
26
- setStatus('pending')
27
- }, [])
28
-
29
- useEffect(() => {
30
- if (!loaded && status === 'pending') {
31
- // todo 清楚副作用?
32
- console.log('the current :', chunkPromise.current)
33
- chunkPromise.current!
34
- .then((m: any) => {
35
- console.log('[mpxAsyncSuspense]: load sucess')
36
- asyncChunkMap.set(request, m.__esModule ? m.default : m)
37
- setStatus('loaded')
38
- })
39
- .catch((e) => {
40
- if (type === 'component') {
41
- console.log(11111, e)
42
- global.onLazyLoadError({
43
- type: 'subpackage',
44
- subpackage: [chunkName],
45
- errMsg: `loadSubpackage: ${e.type}`
46
- })
47
- }
48
- console.log('[mpxAsyncSuspense]: load eror', e)
49
- chunkPromise.current = null
50
- setStatus('error')
51
- })
52
- }
53
- })
54
-
55
- if (loaded) {
56
- const Comp = asyncChunkMap.get(request)
57
- return <Comp {...props}></Comp>
58
- } else if (status === 'error') {
59
- console.log('the status is:', status)
60
- if (type === 'page') {
61
- const Fallback = fallback as ComponentType<DefaultFallbackProps> || DefaultFallback
62
- return <><PageWrapper><Fallback onReload={reloadPage}></Fallback></PageWrapper></>
63
- } else {
64
- const Fallback = loading
65
- return <Fallback {...props}></Fallback>
66
- }
67
- } else {
68
- if (!chunkPromise.current) {
69
- chunkPromise.current = getChildren()
70
- }
71
- if (type === 'page') {
72
- const Fallback = loading || DefaultLoading
73
- return <PageWrapper><Fallback /></PageWrapper>
74
- } else {
75
- const Fallback = loading
76
- return <Fallback {...props}></Fallback>
77
- }
78
- }
79
- }
80
-
81
- export default AsyncSuspense
@@ -1,68 +0,0 @@
1
- import { useState, useEffect, useCallback, useRef } from 'react';
2
- import { DefaultFallback, DefaultLoading, PageWrapper } from './AsyncContainer';
3
- const asyncChunkMap = new Map();
4
- const AsyncSuspense = ({ type, props, chunkName, request, loading, fallback, getChildren }) => {
5
- const [status, setStatus] = useState('pending');
6
- const loaded = asyncChunkMap.has(request);
7
- const [, setKey] = useState(0);
8
- const chunkPromise = useRef(null);
9
- const reloadPage = useCallback(() => {
10
- setKey((preV) => preV + 1);
11
- console.log('[mpxAsyncSuspense]: reload page');
12
- setStatus('pending');
13
- }, []);
14
- useEffect(() => {
15
- if (!loaded && status === 'pending') {
16
- // todo 清楚副作用?
17
- console.log('the current :', chunkPromise.current);
18
- chunkPromise.current
19
- .then((m) => {
20
- console.log('[mpxAsyncSuspense]: load sucess');
21
- asyncChunkMap.set(request, m.__esModule ? m.default : m);
22
- setStatus('loaded');
23
- })
24
- .catch((e) => {
25
- if (type === 'component') {
26
- console.log(11111, e);
27
- global.onLazyLoadError({
28
- type: 'subpackage',
29
- subpackage: [chunkName],
30
- errMsg: `loadSubpackage: ${e.type}`
31
- });
32
- }
33
- console.log('[mpxAsyncSuspense]: load eror', e);
34
- chunkPromise.current = null;
35
- setStatus('error');
36
- });
37
- }
38
- });
39
- if (loaded) {
40
- const Comp = asyncChunkMap.get(request);
41
- return <Comp {...props}></Comp>;
42
- }
43
- else if (status === 'error') {
44
- console.log('the status is:', status);
45
- if (type === 'page') {
46
- const Fallback = fallback || DefaultFallback;
47
- return <><PageWrapper><Fallback onReload={reloadPage}></Fallback></PageWrapper></>;
48
- }
49
- else {
50
- const Fallback = loading;
51
- return <Fallback {...props}></Fallback>;
52
- }
53
- }
54
- else {
55
- if (!chunkPromise.current) {
56
- chunkPromise.current = getChildren();
57
- }
58
- if (type === 'page') {
59
- const Fallback = loading || DefaultLoading;
60
- return <PageWrapper><Fallback /></PageWrapper>;
61
- }
62
- else {
63
- const Fallback = loading;
64
- return <Fallback {...props}></Fallback>;
65
- }
66
- }
67
- };
68
- export default AsyncSuspense;