Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
110 lines (100 loc) · 3.12 KB
/
Copy pathindex.js
File metadata and controls
110 lines (100 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
const path = require('path');
const fs = require('fs-extra');
const _ = require('lodash');
const tryRequire = require('try-require');
const CACHE_FILES = {};
const CACHE_LOADED_FILES = {};
function hash(contents) {
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update(contents);
return hash.digest('hex');
}
function planA(_cache) {
const _filepath = _cache.filepath;
const _dirname = _cache.dirname;
const _filename = _cache.filename;
// 改变 _contents 中 require 的目录结构
const _contents = _cache.contents.replace(/require(\.resolve)?[ ]*\(['|"](\.[\w\\\/\.]+)['|"]\)/gm, function(word, $1) {
return `require('${path.resolve(_dirname, $1)}')`;
});
const _module = {
id: _filepath,
exports: {},
parent: exports.parent,
filename: _filename,
loaded: false,
children: [],
};
// eslint-disable-next-line no-new-func
const func = new Function('exports', 'require', 'module', '__filename', '__dirname', _contents);
func(_module.exports, function(p) {
const _p = tryRequire.resolve(p);
return require(_p || path.resolve(_dirname, p));
}, _module, _filename, _dirname);
module.loaded = true;
return _module.exports;
}
function planB(_cache) {
const _extname = _cache.extname;
const _basename = _cache.basename;
const _dirname = _cache.dirname;
// const _filename = _cache.filename;
const _contents = _cache.contents;
const _hash = hash(_contents);
const newLink = path.resolve(_dirname, `${_basename}.vfile.${_hash}${_extname}`);
fs.writeFileSync(newLink, _contents, 'utf8');
const _fileCache = require(newLink);
fs.unlinkSync(newLink);
return _fileCache;
}
module.exports.register = function(filepath, contents, opts = {}) {
let filename = filepath;
if (!fs.existsSync(filepath)) {
filename = tryRequire.resolve(filepath) || filename;
}
if (_.isFunction(contents)) {
contents = contents(fs.readFileSync(filename, 'utf8'), filename, opts);
}
const extname = path.extname(filename) || '';
CACHE_FILES[filepath] = {
...opts,
filepath,
filename,
dirname: path.dirname(filename),
basename: path.basename(filename, extname),
extname,
contents,
};
};
module.exports.require = function(filepath) {
const _file = CACHE_LOADED_FILES[filepath];
if (!_.isUndefined(_file)) {
return _file;
}
const _cache = CACHE_FILES[filepath];
if (_cache) {
let _fileCache;
try {
_fileCache = planA(_cache);
} catch (error) {
_fileCache = planB(_cache);
}
if (!_.isUndefined(_fileCache)) {
CACHE_LOADED_FILES[filepath] = _fileCache;
}
return _fileCache;
}
return null;
};
Object.defineProperty(module.exports.require, 'cache', {
get() {
return Object.keys(CACHE_LOADED_FILES);
},
});
module.exports.reset = function() {
Object.keys(CACHE_LOADED_FILES).forEach(key => {
delete CACHE_LOADED_FILES[key];
});
};