1 | const getChunkmap = require('../utils/get-chunkmap')
|
2 |
|
3 | const readClientFile = require('../utils/read-client-file')
|
4 |
|
5 | module.exports = (settings = {}) => {
|
6 | const {
|
7 | WEBPACK_BUILD_ENV: ENV,
|
8 | } = process.env
|
9 | const {
|
10 | localeId,
|
11 | chunkmap = ENV === 'prod' ? getChunkmap(localeId) : undefined,
|
12 | compilation,
|
13 | } = settings
|
14 |
|
15 | return {
|
16 | stylesInHead: (() => {
|
17 | if (ENV === 'prod') {
|
18 | return (Array.isArray(chunkmap.critical) ? `<style type="text/css">${readClientFile('critical.css')}</style>` : '')
|
19 | }
|
20 | if (ENV === 'dev') {
|
21 | if (typeof compilation === 'object') {
|
22 | return '<!-- TODO: -->'
|
23 | }
|
24 | return '<!-- 112233 TODO: -->'
|
25 | }
|
26 | })(),
|
27 | scriptsInBody: (() => {
|
28 | let r = `<script type="text/javascript">var __REDUX_STATE__ = {};</script>`
|
29 | if (ENV === 'prod') {
|
30 | let r = `<script type="text/javascript">`
|
31 | + (Array.isArray(chunkmap.critical) ? readClientFile('critical.js') : '')
|
32 | + `</script>`
|
33 | if (typeof chunkmap['.entrypoints'] === 'object') {
|
34 | const entries = chunkmap['.entrypoints']
|
35 | Object.keys(entries).filter(key => (
|
36 | key !== 'critical'
|
37 | )).forEach(key => {
|
38 | if (Array.isArray(entries[key])) {
|
39 | entries[key].forEach(file => {
|
40 | r += `<script type="text/javascript" src="${
|
41 | file.replace(/(^\.\/|^)public\//, '')}" defer></script>`
|
42 | })
|
43 | }
|
44 | })
|
45 | }
|
46 | return r
|
47 | }
|
48 | if (ENV === 'dev') {
|
49 | if (typeof chunkmap === 'object' && typeof chunkmap['.entrypoints'] === 'object') {
|
50 | const entries = chunkmap['.entrypoints']
|
51 | if (Array.isArray(entries.critical)) {
|
52 | entries.critical.forEach(file => {
|
53 | r += `<script type="text/javascript" src="/${file}"></script>`
|
54 | })
|
55 | }
|
56 | Object.keys(entries).filter(key => (
|
57 | key !== 'critical'
|
58 | )).forEach(key => {
|
59 | if (Array.isArray(entries[key])) {
|
60 | entries[key].forEach(file => {
|
61 | r += `<script type="text/javascript" src="/${file}" defer></script>`
|
62 | })
|
63 | }
|
64 | })
|
65 | return r
|
66 | }
|
67 | if (typeof compilation === 'object') {
|
68 | return '<!-- TODO: -->'
|
69 | }
|
70 | return '<!-- TODO: -->'
|
71 | }
|
72 | })()
|
73 | }
|
74 | }
|