11'use strict' ;
2- var path = require ( 'path' ) ;
2+ var path = require ( 'path' ) ,
3+ detectNewline = require ( 'detect-newline' ) ;
34
45function unixStylePath ( filePath ) {
56 return filePath . split ( path . sep ) . join ( '/' ) ;
@@ -9,8 +10,65 @@ var PLUGIN_NAME = require('../package.json').name;
910
1011var urlRegex = / ^ ( h t t p s ? | w e b p a c k ( - [ ^ : ] + ) ? ) : \/ \/ / ;
1112
13+ var debug = require ( 'debug-fabulous' ) ( ) ( PLUGIN_NAME + ':utils' ) ;
14+
15+ /*
16+ So reusing the same ref for a regex (with global (g)) is from a poor decision in js.
17+ See http://stackoverflow.com/questions/10229144/bug-with-regexp-in-javascript-when-do-global-search
18+
19+ So we either need to use a new instance of a regex everywhere.
20+ */
21+ var sourceMapUrlRegEx = function ( ) { return / \/ \/ \# s o u r c e M a p p i n g U R L \= .* / g; }
22+
23+
24+ var getCommentFormatter = function ( file ) {
25+ var extension = file . relative . split ( '.' ) . pop ( ) ,
26+ fileContents = file . contents . toString ( ) ,
27+ newline = detectNewline . graceful ( fileContents || '' ) ,
28+ commentFormatter = function ( url ) {
29+ return '' ;
30+ } ;
31+
32+ if ( file . sourceMap . preExisting ) {
33+ debug ( 'preExisting commentFormatter' ) ;
34+ commentFormatter = function ( url ) {
35+ return file . sourceMap . preExisting ;
36+ } ;
37+ return commentFormatter
38+ }
39+
40+ switch ( extension ) {
41+ case 'css' :
42+ debug ( 'css commentFormatter' ) ;
43+ commentFormatter = function ( url ) {
44+ return newline + "/*# sourceMappingURL=" + url + " */" + newline ;
45+ } ;
46+ break ;
47+ case 'js' :
48+ debug ( 'js commentFormatter' ) ;
49+ commentFormatter = function ( url ) {
50+ return newline + "//# sourceMappingURL=" + url + newline ;
51+ } ;
52+ break ;
53+ default :
54+ debug ( 'unknown commentFormatter' )
55+ }
56+
57+ return commentFormatter ;
58+ }
59+
60+ var getPreExisting = function ( fileContent ) {
61+ if ( sourceMapUrlRegEx ( ) . test ( fileContent ) ) {
62+ debug ( 'has preExisting' ) ;
63+ return fileContent . match ( sourceMapUrlRegEx ( ) ) [ 0 ] ;
64+ }
65+ }
66+
1267module . exports = {
1368 unixStylePath : unixStylePath ,
1469 PLUGIN_NAME : PLUGIN_NAME ,
15- urlRegex : urlRegex
70+ urlRegex : urlRegex ,
71+ sourceMapUrlRegEx : sourceMapUrlRegEx ,
72+ getCommentFormatter : getCommentFormatter ,
73+ getPreExisting : getPreExisting
1674} ;
0 commit comments