UNPKG

4.13 kBJavaScriptView Raw
1const fs = require('fs-extra');
2const path = require('path');
3const isUrl = require('is-url');
4
5const getFilePath = require('./get-client-file-path');
6const generateFilemap = require('./generate-filemap-from-compilation');
7const getDistPath = require('./get-dist-path');
8const getPort = require('./get-port');
9const getDirDistPublic = require('../libs/get-dir-dist-public');
10
11// ============================================================================
12
13/**
14 * 读取目标客户端打包结果文件的内容
15 * @param {String} filename 要查找的文件的文件名。根据打包文件对应表 (chunkmap) 查询文件名和实际打包结果文件的对应关系
16 * @param {String} [localeId] 当前语言
17 * @param {Object} [compilation] Webpack compilation 对象
18 * @param {Boolean} [isPathname=false] 如果标记为 true,表示提供的 filename 为确切的访问地址,无需查询对照表,直接返回结果
19 * @returns {String} 文件内容
20 */
21const readClientFile = (
22 filename,
23 localeId,
24 compilation,
25 isPathname = false
26) => {
27 // 如果第一个参数为 true,表示标记为 pathname
28 if (filename === true)
29 return readClientFile(
30 localeId,
31 compilation || undefined,
32 isPathname || undefined,
33 true
34 );
35
36 // 如果提供了 webpack compilation 数据,尝试从其中查询对应文件的最终内容并返回
37 if (typeof compilation === 'object') {
38 const filemap = generateFilemap(compilation);
39 if (typeof filemap === 'object') {
40 // console.log('\n' + filename)
41 // console.log(`typeof filemap["${filename}"]`, typeof filemap[filename])
42 // console.log(`typeof compilation.assets["${filemap[filename]}"]`, typeof compilation.assets[filemap[filename]])
43 // for (let key in compilation) {
44 // console.log(key)
45 // }
46
47 if (
48 typeof filemap[filename] === 'string' &&
49 typeof compilation.assets[filemap[filename]] !== 'undefined'
50 ) {
51 const asset = compilation.assets[filemap[filename]];
52 // console.log(filename, filemap[filename])
53 // if (!asset._value) {
54 // console.log(asset)
55 // }
56 // console.log('typeof asset.source', typeof asset.source)
57 if (typeof asset.source === 'function') return asset.source();
58 if (typeof asset._value !== 'undefined') return asset._value;
59 if (typeof asset._cachedSource !== 'undefined')
60 return asset._cachedSource;
61 // return '123'
62 }
63 }
64 }
65
66 // 在打包结果中寻找指定文件
67 let pathnames = getFilePath(filename, localeId, isPathname);
68 if (!Array.isArray(pathnames)) pathnames = [pathnames];
69
70 const results = pathnames.map(pathname => {
71 if (isUrl(pathname)) {
72 if (__DEV__) {
73 const syncRequest = require('sync-request');
74 // console.log(`${pathname} is URL`)
75 return syncRequest('GET', pathname, {}).getBody();
76 } else {
77 return `<!-- The pathname for file '${filename}' is a URL. Rendering file content from URL can only be done in DEV mode. -->`;
78 }
79 }
80
81 if (
82 process.env.WEBPACK_BUILD_TYPE === 'spa' &&
83 process.env.WEBPACK_BUILD_ENV === 'dev'
84 ) {
85 return `<!-- http://localhost:${getPort()}${pathname} -->`;
86 // const syncRequest = require('sync-request')
87 // return syncRequest('GET', `http://localhost:${getPort()}${pathname}`, {}).getBody()
88 }
89
90 return fs.readFileSync(
91 path.resolve(
92 getDirDistPublic(getDistPath()),
93 pathname.replace(/^\//, '')
94 ),
95 'utf-8'
96 );
97 });
98
99 return results.join('\n\n');
100};
101
102module.exports = readClientFile;