1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , '__esModule' , {
4+ value : true
5+ } ) ;
6+
7+ var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( 'value' in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
8+
9+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { 'default' : obj } ; }
10+
11+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( 'Cannot call a class as a function' ) ; } }
12+
13+ var _indexJs = require ( './index.js' ) ;
14+
15+ var _indexJs2 = _interopRequireDefault ( _indexJs ) ;
16+
17+ var _fs = require ( 'fs' ) ;
18+
19+ var _fs2 = _interopRequireDefault ( _fs ) ;
20+
21+ var _path = require ( 'path' ) ;
22+
23+ var _path2 = _interopRequireDefault ( _path ) ;
24+
25+ var _pathIsAbsolute = require ( 'path-is-absolute' ) ;
26+
27+ var _pathIsAbsolute2 = _interopRequireDefault ( _pathIsAbsolute ) ;
28+
29+ // Sorts dependencies in the following way:
30+ // AAA comes before AA and A
31+ // AB comes after AA and before A
32+ // All Bs come after all As
33+ // This ensures that the files are always returned in the following order:
34+ // - In the order they were required, except
35+ // - After all their dependencies
36+ var traceKeySorter = function traceKeySorter ( a , b ) {
37+ if ( a . length < b . length ) {
38+ return a < b . substring ( 0 , a . length ) ? - 1 : 1 ;
39+ } else if ( a . length > b . length ) {
40+ return a . substring ( 0 , b . length ) <= b ? - 1 : 1 ;
41+ } else {
42+ return a < b ? - 1 : 1 ;
43+ }
44+ } ;
45+
46+ var FileSystemLoader = ( function ( ) {
47+ function FileSystemLoader ( root , plugins ) {
48+ _classCallCheck ( this , FileSystemLoader ) ;
49+
50+ this . root = root ;
51+ this . sources = { } ;
52+ this . traces = { } ;
53+ this . importNr = 0 ;
54+ this . core = new _indexJs2 [ 'default' ] ( plugins ) ;
55+ this . tokensByFile = { } ;
56+
57+ _indexJs2 [ 'default' ] . scope . generateScopedName = function ( exportedName , unsanitizedPath ) {
58+ var sanitizedPath = _path2 [ 'default' ] . relative ( root , unsanitizedPath ) . replace ( / \. [ ^ \. \/ \\ ] + $ / , '' ) . replace ( / [ \W _ ] + / g, '_' ) . replace ( / ^ _ | _ $ / g, '' ) ;
59+ return '_' + sanitizedPath + '__' + exportedName ;
60+ } ;
61+ }
62+
63+ _createClass ( FileSystemLoader , [ {
64+ key : 'fetch' ,
65+ value : function fetch ( _newPath , relativeTo , _trace ) {
66+ var _this = this ;
67+
68+ var newPath = _newPath . replace ( / ^ [ " ' ] | [ " ' ] $ / g, "" ) ,
69+ trace = _trace || String . fromCharCode ( this . importNr ++ ) ;
70+ return new Promise ( function ( resolve , reject ) {
71+ var relativeDir = _path2 [ 'default' ] . dirname ( relativeTo ) ,
72+ fileRelativePath = _path2 [ 'default' ] . resolve ( ( 0 , _pathIsAbsolute2 [ 'default' ] ) ( relativeDir ) ? relativeDir : _path2 [ 'default' ] . join ( _this . root , relativeDir ) , newPath ) ;
73+
74+ // if the path is not relative or absolute, try to resolve it in node_modules
75+ if ( newPath [ 0 ] !== '.' && newPath [ 0 ] !== '/' ) {
76+ try {
77+ fileRelativePath = require . resolve ( newPath ) ;
78+ } catch ( e ) { }
79+ }
80+
81+ var tokens = _this . tokensByFile [ fileRelativePath ] ;
82+ if ( tokens ) {
83+ return resolve ( tokens ) ;
84+ }
85+
86+ _fs2 [ 'default' ] . readFile ( fileRelativePath , "utf-8" , function ( err , source ) {
87+ if ( err ) reject ( err ) ;
88+ _this . core . load ( source , fileRelativePath , trace , _this . fetch . bind ( _this ) ) . then ( function ( _ref ) {
89+ var injectableSource = _ref . injectableSource ;
90+ var exportTokens = _ref . exportTokens ;
91+
92+ _this . sources [ fileRelativePath ] = injectableSource ;
93+ _this . traces [ trace ] = fileRelativePath ;
94+ _this . tokensByFile [ fileRelativePath ] = exportTokens ;
95+ resolve ( exportTokens ) ;
96+ } , reject ) ;
97+ } ) ;
98+ } ) ;
99+ }
100+ } , {
101+ key : 'finalSource' ,
102+ get : function get ( ) {
103+ var traces = this . traces ;
104+ var sources = this . sources ;
105+ var written = new Set ( ) ;
106+
107+ return Object . keys ( traces ) . sort ( traceKeySorter ) . map ( function ( key ) {
108+ var filename = traces [ key ] ;
109+ if ( written . has ( filename ) ) {
110+ return null ;
111+ }
112+ written . add ( filename ) ;
113+
114+ return sources [ filename ] ;
115+ } ) . join ( "" ) ;
116+ }
117+ } ] ) ;
118+
119+ return FileSystemLoader ;
120+ } ) ( ) ;
121+
122+ exports [ 'default' ] = FileSystemLoader ;
123+ module . exports = exports [ 'default' ] ;
0 commit comments