1 | const path = require('path')
|
2 | const fs = require('fs-extra')
|
3 |
|
4 | const defaultEntrypoints = require('../defaults/entrypoints')
|
5 |
|
6 | const getChunkmap = require('../utils/get-chunkmap')
|
7 | const readClientFile = require('../utils/read-client-file')
|
8 |
|
9 | module.exports = (settings = {}) => {
|
10 | const {
|
11 | WEBPACK_BUILD_ENV: ENV,
|
12 | } = process.env
|
13 | const {
|
14 | localeId,
|
15 | chunkmap = getChunkmap(localeId) || {},
|
16 | compilation,
|
17 | inject = {},
|
18 | } = settings
|
19 |
|
20 | const {
|
21 | '.entrypoints': entrypoints = {},
|
22 | '.files': filemap = {},
|
23 | } = chunkmap
|
24 |
|
25 | return Object.assign({}, {
|
26 |
|
27 | htmlLang: localeId ? ` lang="${localeId}"` : '',
|
28 | metas: `<!--SUPER_METAS_START--><!--SUPER_METAS_END-->`,
|
29 | styles: (() => {
|
30 | let r = ''
|
31 | if (typeof filemap['critical.css'] === 'string') {
|
32 | if (ENV === 'prod')
|
33 | r += `<style id="__super-critical-styles" type="text/css">${readClientFile('critical.css', localeId, compilation)}</style>`
|
34 | if (ENV === 'dev')
|
35 | r += `<link id="__super-critical-styles" media="all" rel="stylesheet" href="/${filemap['critical.css']}" />`
|
36 | }
|
37 | return r
|
38 | })(),
|
39 |
|
40 | scripts: (() => {
|
41 | let r = `<script type="text/javascript">window.__REDUX_STATE__ = {};</script>`
|
42 |
|
43 | if (typeof entrypoints === 'object') {
|
44 |
|
45 | if (Array.isArray(entrypoints.critical)) {
|
46 | entrypoints.critical
|
47 | .filter(filename => path.extname(filename) === '.js')
|
48 | .forEach(filename => {
|
49 | r += ENV === 'prod'
|
50 | ? `<script type="text/javascript">${fs.readFileSync(
|
51 | path.resolve(
|
52 | process.env.SUPER_DIST_DIR,
|
53 | filename.replace(/^\//, '')
|
54 | ),
|
55 | 'utf-8'
|
56 | )}</script>`
|
57 | : `<script type="text/javascript" src="/${filename}"></script>`
|
58 | })
|
59 | }
|
60 |
|
61 |
|
62 | if (typeof entrypoints === 'object') {
|
63 |
|
64 |
|
65 |
|
66 | defaultEntrypoints.forEach(key => {
|
67 | if (Array.isArray(entrypoints[key])) {
|
68 | entrypoints[key].forEach(file => {
|
69 | r += ENV === 'prod'
|
70 | ? `<script type="text/javascript" src="${file.replace(/(^\.\/|^)public\//, '')}" defer></script>`
|
71 | : `<script type="text/javascript" src="/${file}" defer></script>`
|
72 | })
|
73 | }
|
74 | })
|
75 | }
|
76 | }
|
77 |
|
78 | return r
|
79 | })()
|
80 | }, inject)
|
81 | }
|