UNPKG

1.17 kBJavaScriptView Raw
1function isPath(path) {
2 return typeof path === 'string';
3}
4
5function isContents(contents) {
6 return typeof contents === 'string' || Buffer.isBuffer(contents);
7}
8
9export default function memory(config = {}) {
10 let path = isPath(config.path) ? config.path : null;
11 let contents = isContents(config.contents) ? String(config.contents) : null;
12
13 return {
14 options(options) {
15 const { entry } = options;
16 if (entry && typeof entry === 'object') {
17 if (isPath(entry.path)) {
18 path = entry.path;
19 }
20 if (isContents(entry.contents)) {
21 contents = String(entry.contents);
22 }
23 }
24 options.entry = path;
25 },
26
27 resolveId(id) {
28 if (path === null || contents === null) {
29 throw Error('\'path\' should be a string and \'contents\' should be a string of Buffer');
30 }
31 if (id === path) {
32 return path;
33 }
34 },
35
36 load(id) {
37 if (id === path) {
38 return contents;
39 }
40 }
41 };
42}