1 | const fs = require('fs-extra')
|
2 | const path = require('path')
|
3 | const isUrl = require('is-url')
|
4 |
|
5 | const getFilePath = require('./get-client-file-path')
|
6 | const generateFilemap = require('./generate-filemap-from-compilation')
|
7 | const getDistPath = require('./get-dist-path')
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const readClientFile = (filename, localeId, compilation, isPathname = false) => {
|
18 |
|
19 | if (filename === true) return readClientFile(localeId, compilation || undefined, isPathname || undefined, true)
|
20 |
|
21 |
|
22 | if (typeof compilation === 'object') {
|
23 | const filemap = generateFilemap(compilation)
|
24 | if (typeof filemap === 'object') {
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | if (typeof filemap[filename] === 'string' &&
|
30 | typeof compilation.assets[filemap[filename]] !== 'undefined'
|
31 | ) {
|
32 | const obj = compilation.assets[filemap[filename]]
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | if (typeof obj._value !== 'undefined') return obj._value
|
38 | if (typeof obj._cachedSource !== 'undefined') return obj._cachedSource
|
39 |
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 | const pathname = getFilePath(filename, localeId, isPathname)
|
45 | if (isUrl(pathname)) {
|
46 | if (__DEV__) {
|
47 | const syncRequest = require('sync-request')
|
48 |
|
49 | return syncRequest('GET', pathname, {}).getBody()
|
50 | } else {
|
51 | return `<!-- The pathname for file '${filename}' is a URL. Rendering file content from URL can only be done in DEV mode. -->`
|
52 | }
|
53 | }
|
54 |
|
55 | return fs.readFileSync(
|
56 | path.resolve(
|
57 | getDistPath(),
|
58 | 'public/',
|
59 | getFilePath(filename, localeId, isPathname).replace(/^\//, '')
|
60 | ),
|
61 | 'utf-8'
|
62 | )
|
63 | }
|
64 |
|
65 | module.exports = readClientFile
|