Skip to content

Commit

Permalink
fix(init): issue computmaxer#162
Browse files Browse the repository at this point in the history
  • Loading branch information
m-a-r-c-e-l-i-n-o committed Jul 10, 2016
1 parent 7915f75 commit 96943a5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
27 changes: 16 additions & 11 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,23 @@
System.bundles = [];
}

var systemInstantiate = System.instantiate;
var http = location.protocol;
var slashes = http.concat('//');
var host = slashes.concat(window.location.host) + '/base/';
System.instantiate = function(load) {
var fileKey = load.name.replace(host, '');
if (karma.config.jspm.coverageFiles[fileKey]) {
var re = new RegExp('file://' + karma.config.jspm.basePath + '/','g');
load.source = karma.config.jspm.coverageFiles[fileKey].replace(re, host);
if (typeof karma.config.jspm.coverage === 'object') {
var systemInstantiate = System.instantiate;
var http = location.protocol;
var slashes = http.concat('//');
var host = slashes.concat(window.location.host);
if (host.split('').reverse()[0] !== '/') {
host = host.concat('/');
}
return systemInstantiate.call(System, load);
};
System.instantiate = function(load) {
var fileKey = load.name.replace(host, '');
if (karma.config.jspm.coverage.files[fileKey]) {
var re = new RegExp('file://' + karma.config.jspm.coverage.basePath + '/','g');
load.source = karma.config.jspm.coverage.files[fileKey].replace(re, host);
}
return systemInstantiate.call(System, load);
};
}

// Load everything specified in loadFiles in the specified order
var promiseChain = Promise.resolve();
Expand Down
8 changes: 5 additions & 3 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ module.exports = function(files, basePath, jspm, client, emitter) {
return packagesPath + fileName + '.js';
}
}

Array.prototype.unshift.apply(files,
configPaths.map(function(configPath) {
return createPattern(configPath)
})
);

// Needed for JSPM 0.17 beta
if(jspm.browser) {
files.unshift(createPattern(browserPath));
Expand Down Expand Up @@ -139,7 +139,9 @@ module.exports = function(files, basePath, jspm, client, emitter) {

// Allow Karma to serve all files within jspm_packages.
// This allows jspm/SystemJS to load them
var jspmPattern = createServedPattern(packagesPath + '**/*', {nocache: jspm.cachePackages !== true});
var jspmPattern = createServedPattern(
packagesPath + '!(system-polyfills.src.js|system.src.js)/**', {nocache: jspm.cachePackages !== true}
);
jspmPattern.watched = false;
files.push(jspmPattern);
};
18 changes: 11 additions & 7 deletions src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ function CoveragePreprocessor(basePath, client, reporters, helper, logger) {
// create a jspm namespace to pass data to the browsers
if (!client.jspm) client.jspm = {};
// store instrumented sources to be executed by browser
client.jspm.coverageFiles = {};
client.jspm.coverage = {
files: {}
};
// store basePath used to generate the key for each source in the obj above
client.jspm.basePath = basePath;
client.jspm.coverage.basePath = basePath;
// temp folder to store instrumented files for sourcemap remapping
client.jspm.tempDirectory = path.join(__dirname, '../no-source-map/');
client.jspm.coverage.tempDirectory = path.join(__dirname, '../no-source-map/');

// make temp directory
var tempDirectory = client.jspm.tempDirectory;
var tempDirectory = client.jspm.coverage.tempDirectory;
if (!fs.existsSync(tempDirectory)) {
fs.mkdirSync(tempDirectory);
}
Expand All @@ -50,7 +52,7 @@ function CoveragePreprocessor(basePath, client, reporters, helper, logger) {
// create a unique key to store the sources of modules for the browser
var fileKey = getFileKey(getOSFilePath(load.address), basePath);
// exclude the dependency modules (i.e. libraries) from instrumentation
if (client.jspm.coverageFiles[fileKey]) {
if (client.jspm.coverage.files[fileKey]) {
// put file's transpiled counterpart in temp folder
var filename;
var sourceMap = '';
Expand All @@ -63,6 +65,8 @@ function CoveragePreprocessor(basePath, client, reporters, helper, logger) {
delete load.metadata.sourceMap.sourcesContent;
// this is the file being "instrumented"
load.metadata.sourceMap.file = filename;
// normalize the main source filename
load.metadata.sourceMap.sources[0] = load.address;
// removing "file://" from paths
load.metadata.sourceMap.sources = load.metadata.sourceMap.sources.map(function (filename) {
return getOSFilePath(filename);
Expand All @@ -87,7 +91,7 @@ function CoveragePreprocessor(basePath, client, reporters, helper, logger) {
}

// instrument with istanbul
client.jspm.coverageFiles[fileKey] = instrument.instrumentSync(
client.jspm.coverage.files[fileKey] = instrument.instrumentSync(
load.source,
// make the path-like file key into something that can be used as a name
filename
Expand All @@ -106,7 +110,7 @@ function CoveragePreprocessor(basePath, client, reporters, helper, logger) {
// only files to be instrumented pass through here
// store this information to allow "instantiate" to exclude
// dependency modules (i.e. libraries)
client.jspm.coverageFiles[getFileKey(file.path, basePath)] = true;
client.jspm.coverage.files[getFileKey(file.path, basePath)] = true;
// import modules
systemJS.import(file.path).then(function () {
done(content);
Expand Down
2 changes: 1 addition & 1 deletion src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function CoverageReporter(rootConfig, helper, logger, emitter) {
};
config._onExit = function (done) {
try {
rimraf.sync(rootConfig.client.jspm.tempDirectory);
rimraf.sync(rootConfig.client.jspm.coverage.tempDirectory);
} catch(e) {
log.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion test/testInit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('jspm plugin init', function(){
});

it('should use the configured jspm_packages path and include it at the end of the files array', function(){
expect(normalPath(files[files.length - 1].pattern)).toEqual(normalPath(path.resolve(cwd, './custom_packages/**/*')));
expect(normalPath(files[files.length - 1].pattern)).toEqual(normalPath(path.resolve(cwd, './custom_packages/!(system-polyfills.src.js|system.src.js)/**')));
expect(files[files.length - 1].included).toEqual(false);
expect(files[files.length - 1].served).toEqual(true);
expect(files[files.length - 1].watched).toEqual(false);
Expand Down

0 comments on commit 96943a5

Please sign in to comment.