-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flatten sourcemaps lazily when serving (#68)
- Loading branch information
1 parent
c98c180
commit 455b40b
Showing
10 changed files
with
151 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,32 @@ | ||
import { basename, extname } from 'path'; | ||
import { lookup } from 'mime'; | ||
import { readFile } from 'sander'; | ||
import { readFile, stat, createReadStream } from 'sander'; | ||
import { getSourcemapComment, SOURCEMAP_COMMENT } from '../../utils/sourcemap'; | ||
|
||
export default function serveFile ( filepath, request, response ) { | ||
return readFile( filepath ).then( data => { | ||
const ext = extname( filepath ); | ||
|
||
// this might be turn out to be a really bad idea. But let's try it and see | ||
if ( ext === '.js' || ext === '.css' ) { | ||
return readFile( filepath ).then( data => { | ||
// this takes the auto-generated absolute sourcemap path, and turns | ||
// it into what you'd get with `gobble build` or `gobble watch` | ||
const sourcemapComment = getSourcemapComment( basename( filepath ) + '.map', ext ); | ||
data = data.toString().replace( SOURCEMAP_COMMENT, sourcemapComment ); | ||
|
||
response.statusCode = 200; | ||
response.setHeader( 'Content-Type', lookup( filepath ) ); | ||
|
||
response.write( data ); | ||
response.end(); | ||
}); | ||
} | ||
|
||
return stat( filepath ).then( stats => { | ||
response.statusCode = 200; | ||
response.setHeader( 'Content-Type', lookup( filepath ) ); | ||
response.setHeader( 'Content-Length', data.length ); | ||
response.setHeader( 'Content-Length', stats.size ); | ||
|
||
response.write( data ); | ||
response.end(); | ||
createReadStream( filepath ).pipe( response ); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { load } from 'sorcery'; | ||
|
||
export default function serveSourcemap ( filepath, sourcemapPromises, request, response ) { | ||
const owner = filepath.slice( 0, -4 ); | ||
|
||
if ( !sourcemapPromises[ filepath ] ) { | ||
sourcemapPromises[ filepath ] = load( owner ) | ||
.then( chain => { | ||
if ( !chain ) { | ||
throw new Error( 'Could not resolve sourcemap for ' + owner ); | ||
} | ||
|
||
return chain.apply().toString(); | ||
}); | ||
} | ||
|
||
return sourcemapPromises[ filepath ].then( map => { | ||
response.statusCode = 200; | ||
response.setHeader( 'Content-Type', 'application/json' ); | ||
|
||
response.write( map ); | ||
response.end(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
let SOURCEMAPPING_URL = 'sourceMa'; | ||
SOURCEMAPPING_URL += 'ppingURL'; | ||
|
||
const SOURCEMAP_COMMENT = new RegExp( `\n*(?:` + | ||
`\\/\\/[@#]\\s*${SOURCEMAPPING_URL}=([^'"]+)|` + // js | ||
`\\/\\*#?\\s*${SOURCEMAPPING_URL}=([^'"]+)\\s\\+\\/)` + // css | ||
`\\s*$`, 'g' ); | ||
|
||
function getSourcemapComment ( url, ext ) { | ||
if ( ext === '.css' ) { | ||
return `\n/*# ${SOURCEMAPPING_URL}=${url} */\n`; | ||
} | ||
|
||
return `\n//# ${SOURCEMAPPING_URL}=${url}\n`; | ||
} | ||
|
||
export { getSourcemapComment, SOURCEMAP_COMMENT, SOURCEMAPPING_URL }; |
Oops, something went wrong.