-
Notifications
You must be signed in to change notification settings - Fork 921
/
index.ts
157 lines (140 loc) · 4.27 KB
/
index.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {existsSync, readFileSync} from 'fs';
import {resolve, pathToFileURL} from 'url';
import {ServerRuntime, ServerRuntimeConfig, LoadResult} from '../types';
import {sourcemap_stacktrace} from './sourcemaps';
import {transform} from './transform';
import {REQUIRE_OR_IMPORT} from '../util';
// This function makes it possible to load modules from the snowpack server, for the sake of SSR.
export function createLoader({config, load}: ServerRuntimeConfig): ServerRuntime {
const cache = new Map();
const graph = new Map();
async function getModule(importer: string, imported: string, urlStack: string[]) {
if (imported[0] === '/' || imported[0] === '.') {
const pathname = resolve(importer, imported);
if (!graph.has(pathname)) graph.set(pathname, new Set());
graph.get(pathname).add(importer);
return _load(pathname, urlStack);
}
const mod = await REQUIRE_OR_IMPORT(imported, {
from: config.root || config.workspaceRoot || process.cwd(),
});
return {
exports: mod,
css: [],
};
}
function invalidateModule(path) {
// If the cache doesn't have this path, check if it's a proxy file.
if (!cache.has(path) && cache.has(path + '.proxy.js')) {
path = path + '.proxy.js';
}
cache.delete(path);
const dependents = graph.get(path);
graph.delete(path);
if (dependents) dependents.forEach(invalidateModule);
}
async function _load(url, urlStack) {
if (urlStack.includes(url)) {
console.warn(`Circular dependency: ${urlStack.join(' -> ')} -> ${url}`);
return {};
}
if (cache.has(url)) {
return cache.get(url);
}
const promise = load(url)
.then((loaded) => initializeModule(url, loaded, urlStack.concat(url)))
.catch((e) => {
cache.delete(url);
throw e;
});
cache.set(url, promise);
return promise;
}
async function initializeModule(url: string, loaded: LoadResult<string>, urlStack: string[]) {
const {code, deps, css, names} = transform(loaded.contents);
const exports = {};
const allCss = new Set(css.map((relative) => resolve(url, relative)));
const fileURL = loaded.originalFileLoc ? pathToFileURL(loaded.originalFileLoc) : null;
const args = [
{
name: 'global',
value: global,
},
{
name: 'require',
value: (id) => {
// TODO can/should this restriction be relaxed?
throw new Error(
`Use import instead of require (attempted to load '${id}' from '${url}')`,
);
},
},
{
name: names.exports,
value: exports,
},
{
name: names.__export,
value: (name, get) => {
Object.defineProperty(exports, name, {get});
},
},
{
name: names.__export_all,
value: (mod) => {
// Copy over all of the descriptors.
const descriptors = Object.getOwnPropertyDescriptors(mod);
Object.defineProperties(exports, descriptors);
},
},
{
name: names.__import,
value: (source) => getModule(url, source, urlStack).then((mod) => mod.exports),
},
{
name: names.__import_meta,
value: {url: fileURL},
},
...(await Promise.all(
deps.map(async (dep) => {
const module = await getModule(url, dep.source, urlStack);
module.css.forEach((dep) => allCss.add(dep));
return {
name: dep.name,
value: module.exports,
};
}),
)),
];
const fn = new Function(...args.map((d) => d.name), `${code}\n//# sourceURL=${url}`);
try {
fn(...args.map((d) => d.value));
} catch (e) {
e.stack = await sourcemap_stacktrace(e.stack, async (address) => {
if (existsSync(address)) {
// it's a filepath
return readFileSync(address, 'utf-8');
}
try {
const {contents} = await load(address);
return contents;
} catch {
// fail gracefully
}
});
throw e;
}
return {
exports,
css: Array.from(allCss),
};
}
return {
importModule: async (url) => {
return _load(url, []);
},
invalidateModule: (url) => {
invalidateModule(url);
},
};
}