This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source maps for generated code (#7)
- Loading branch information
Showing
11 changed files
with
239 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import global from '@dojo/core/global'; | ||
import has, { add as hasAdd } from '@dojo/has/has'; | ||
|
||
hasAdd('btoa', 'btoa' in global); | ||
hasAdd('atob', 'atob' in global); | ||
|
||
/** | ||
* Take a string encoded in base64 and decode it | ||
* @param encodedString The base64 encoded string | ||
*/ | ||
export const decode: (encodedString: string) => string = has('atob') ? function (encodedString: string) { | ||
/* this allows for utf8 characters to be decoded properly */ | ||
return decodeURIComponent(Array.prototype.map.call(atob(encodedString), (char: string) => '%' + ('00' + char.charCodeAt(0).toString(16)).slice(-2)).join('')); | ||
} : function (encodedString: string): string { | ||
return new Buffer(encodedString.toString(), 'base64').toString('utf8'); | ||
}; | ||
|
||
/** | ||
* Take a string and encode it to base64 | ||
* @param rawString The string to encode | ||
*/ | ||
export const encode: (rawString: string) => string = has('btoa') ? function (decodedString: string) { | ||
/* this allows for utf8 characters to be encoded properly */ | ||
return btoa(encodeURIComponent(decodedString).replace(/%([0-9A-F]{2})/g, (match, code: string) => String.fromCharCode(Number('0x' + code)))); | ||
} : function (rawString: string): string { | ||
return new Buffer(rawString.toString(), 'utf8').toString('base64'); | ||
}; |
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,23 @@ | ||
import { CodeWithSourceMap, SourceMapConsumer, SourceNode } from 'source-map'; | ||
|
||
const SOURCE_MAP_REGEX = /(?:\/{2}[#@]{1,2}|\/\*)\s+sourceMappingURL\s*=\s*(data:(?:[^;]+;)+base64,)?(\S+)(?:\n\s*)?$/; | ||
|
||
/** | ||
* Wrap code, which has a source map, with a preamble and a postscript and return the wrapped code with an updated | ||
* map. | ||
* @param preamble A string to append before the code | ||
* @param code The code, with an optional source map in string format | ||
* @param postscript A string to append after the code | ||
*/ | ||
export function wrapCode(preamble: string, code: { map?: string, code: string }, postscript: string): CodeWithSourceMap { | ||
const result = new SourceNode(); | ||
result.add(preamble); | ||
if (code.map) { | ||
result.add(SourceNode.fromStringWithSourceMap(code.code.replace(SOURCE_MAP_REGEX, ''), new SourceMapConsumer(code.map))); | ||
} | ||
else { | ||
result.add(code.code); | ||
} | ||
result.add(postscript); | ||
return result.toStringWithSourceMap(); | ||
} |
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,8 +1,10 @@ | ||
import './base64'; | ||
import './css'; | ||
import './DOMParser'; | ||
import './gists'; | ||
import './json'; | ||
import './postcss'; | ||
import './postcssCssnext'; | ||
import './postcssModules'; | ||
import './sourceMap'; | ||
import './providers/all'; |
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,27 @@ | ||
import * as registerSuite from 'intern!object'; | ||
import * as assert from 'intern/chai!assert'; | ||
import * as base64 from '../../../src/support/base64'; | ||
|
||
registerSuite({ | ||
name: 'support/base64', | ||
|
||
'encode()': { | ||
'normal string'() { | ||
assert.strictEqual(base64.encode('foo bar baz'), 'Zm9vIGJhciBiYXo=', 'should have encoded properly'); | ||
}, | ||
|
||
'utf8 string'() { | ||
assert.strictEqual(base64.encode('💩😱🦄'), '8J+SqfCfmLHwn6aE', 'should have encoded properly'); | ||
} | ||
}, | ||
|
||
'decode()': { | ||
'normal string'() { | ||
assert.strictEqual(base64.decode('Zm9vIGJhciBiYXo='), 'foo bar baz', 'should have decoded properly'); | ||
}, | ||
|
||
'utf8 string'() { | ||
assert.strictEqual(base64.decode('8J+SqfCfmLHwn6aE'), '💩😱🦄', 'should have decoded properly'); | ||
} | ||
} | ||
}); |
Oops, something went wrong.