-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfile-system-loader.js
77 lines (68 loc) · 2.41 KB
/
file-system-loader.js
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
import Core from './index.js'
import fs from 'fs'
import path from 'path'
// Sorts dependencies in the following way:
// AAA comes before AA and A
// AB comes after AA and before A
// All Bs come after all As
// This ensures that the files are always returned in the following order:
// - In the order they were required, except
// - After all their dependencies
const traceKeySorter = ( a, b ) => {
if ( a.length < b.length ) {
return a < b.substring( 0, a.length ) ? -1 : 1
} else if ( a.length > b.length ) {
return a.substring( 0, b.length ) <= b ? -1 : 1
} else {
return a < b ? -1 : 1
}
};
export default class FileSystemLoader {
constructor( root, plugins ) {
this.root = root
this.sources = {}
this.traces = {}
this.importNr = 0
this.core = new Core(plugins)
this.tokensByFile = {};
}
fetch( _newPath, relativeTo, _trace ) {
let newPath = _newPath.replace( /^["']|["']$/g, "" ),
trace = _trace || String.fromCharCode( this.importNr++ )
return new Promise( ( resolve, reject ) => {
let relativeDir = path.dirname( relativeTo ),
rootRelativePath = path.resolve( relativeDir, newPath ),
fileRelativePath = path.resolve( path.join( this.root, relativeDir ), newPath )
// if the path is not relative or absolute, try to resolve it in node_modules
if (newPath[0] !== '.' && newPath[0] !== '/') {
try {
fileRelativePath = require.resolve(newPath);
}
catch (e) {}
}
const tokens = this.tokensByFile[fileRelativePath]
if (tokens) { return resolve(tokens) }
fs.readFile( fileRelativePath, "utf-8", ( err, source ) => {
if ( err ) reject( err )
this.core.load( source, rootRelativePath, trace, this.fetch.bind( this ) )
.then( ( { injectableSource, exportTokens } ) => {
this.sources[fileRelativePath] = injectableSource
this.traces[trace] = fileRelativePath
this.tokensByFile[fileRelativePath] = exportTokens
resolve( exportTokens )
}, reject )
} )
} )
}
get finalSource() {
const traces = this.traces
const sources = this.sources
let written = new Set()
return Object.keys( traces ).sort( traceKeySorter ).map(key => {
const filename = traces[key]
if (written.has(filename)) { return null }
written.add(filename)
return sources[filename];
}).join( "" )
}
}