This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(source-maps): always generate source map, then purge them if not …
…needed in postprocess step
- Loading branch information
1 parent
02dfff8
commit d26b44c
Showing
10 changed files
with
176 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Logger } from './logger/logger'; | ||
import { BuildContext } from './util/interfaces'; | ||
import { purgeSourceMapsIfNeeded } from './util/source-maps'; | ||
|
||
|
||
export function postprocess(context: BuildContext) { | ||
const logger = new Logger(`postprocess`); | ||
return postprocessWorker(context).then(() => { | ||
logger.finish(); | ||
}) | ||
.catch((err: Error) => { | ||
throw logger.fail(err); | ||
}); | ||
} | ||
|
||
|
||
function postprocessWorker(context: BuildContext) { | ||
return purgeSourceMapsIfNeeded(context); | ||
} |
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,42 @@ | ||
import * as helpers from './helpers'; | ||
|
||
describe('helpers', () => { | ||
describe('getBooleanPropertyValue', () => { | ||
it('should return true when value is "true"', () => { | ||
// arrange | ||
const propertyName = 'test'; | ||
const propertyValue = 'true'; | ||
let environment: any = { }; | ||
environment[propertyName] = propertyValue; | ||
process.env = environment; | ||
// act | ||
const result = helpers.getBooleanPropertyValue(propertyName); | ||
// assert | ||
expect(result).toEqual(true); | ||
}); | ||
|
||
it('should return false when value is undefined/null', () => { | ||
// arrange | ||
const propertyName = 'test'; | ||
let environment: any = { }; | ||
process.env = environment; | ||
// act | ||
const result = helpers.getBooleanPropertyValue(propertyName); | ||
// assert | ||
expect(result).toEqual(false); | ||
}); | ||
|
||
it('should return false when value is not "true"', () => { | ||
// arrange | ||
const propertyName = 'test'; | ||
const propertyValue = 'taco'; | ||
let environment: any = { }; | ||
environment[propertyName] = propertyValue; | ||
process.env = environment; | ||
// act | ||
const result = helpers.getBooleanPropertyValue(propertyName); | ||
// assert | ||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
}); |
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,40 @@ | ||
import { join } from 'path'; | ||
import * as Constants from './constants'; | ||
import * as sourceMaps from './source-maps'; | ||
import * as helpers from './helpers'; | ||
|
||
describe('source maps', () => { | ||
describe('purgeSourceMapsIfNeeded', () => { | ||
it('should return a resolved promise when purging source maps isnt needed', () => { | ||
// arrange | ||
let env: any = {}; | ||
env[Constants.ENV_VAR_GENERATE_SOURCE_MAP] = 'true'; | ||
process.env = env; | ||
// act | ||
const resultPromise = sourceMaps.purgeSourceMapsIfNeeded(null); | ||
// assert | ||
return resultPromise; | ||
}); | ||
|
||
it('should return a promise call unlink on all files with a .map extensin', () => { | ||
// arrange | ||
let env: any = {}; | ||
env[Constants.ENV_VAR_GENERATE_SOURCE_MAP] = null; | ||
process.env = env; | ||
const buildDir = '/some/fake/build/dir'; | ||
const context = { buildDir: buildDir }; | ||
|
||
spyOn(helpers, helpers.readDirAsync.name).and.returnValue(Promise.resolve(['test.js', 'test.js.map', 'test2.js', 'test2.js.map'])); | ||
const unlinkSpy = spyOn(helpers, helpers.unlinkAsync.name).and.returnValue(Promise.resolve()); | ||
|
||
// act | ||
const resultPromise = sourceMaps.purgeSourceMapsIfNeeded(context); | ||
|
||
// assert | ||
return resultPromise.then(() => { | ||
expect(unlinkSpy.calls.argsFor(0)[0]).toEqual(join(buildDir, 'test.js.map')); | ||
expect(unlinkSpy.calls.argsFor(1)[0]).toEqual(join(buildDir, 'test2.js.map')); | ||
}); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import { join } from 'path'; | ||
import * as Constants from './constants'; | ||
import { getBooleanPropertyValue, readDirAsync, unlinkAsync } from './helpers'; | ||
import { BuildContext } from './interfaces'; | ||
|
||
export function purgeSourceMapsIfNeeded(context: BuildContext) { | ||
if (getBooleanPropertyValue(Constants.ENV_VAR_GENERATE_SOURCE_MAP)) { | ||
// keep the source maps and just return | ||
return Promise.resolve(); | ||
} | ||
return readDirAsync(context.buildDir).then((fileNames: string[]) => { | ||
const sourceMaps = fileNames.filter(fileName => fileName.endsWith('.map')); | ||
const fullPaths = sourceMaps.map(sourceMap => join(context.buildDir, sourceMap)); | ||
const promises: Promise<any>[] = []; | ||
for (const fullPath of fullPaths) { | ||
promises.push(unlinkAsync(fullPath)); | ||
} | ||
return Promise.all(promises); | ||
}); | ||
} |