Skip to content

Commit

Permalink
feat: allow external dependencies to be mapped to a specific file
Browse files Browse the repository at this point in the history
Closes #66
  • Loading branch information
Jamie Couperwhite committed Jun 30, 2019
1 parent 996f118 commit 3e53c4b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ commonjsPreprocessor: {
```
When not specified the root folder defaults to the `karma.basePath/node_modules` configuration option.

#### Externals

When external dependencies have prebundled sources.
You can specify what file should be included from the root folder

```js
// karma.conf.js
module.exports = function(config) {
config.set({
// ...
commonjsPreprocessor: {
modulesRoot: 'components',
externals: {
'angular': 'angular/angular.min.js'
}
},
// ...
});
};
```

This effectively translates any `require('angular')` statements into `require('./components/angular/angular.min.js')`.

----

For an example project, check out Karma's [client tests](https://github.com/karma-runner/karma/tree/master/test/client).

----
Expand Down
44 changes: 43 additions & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@ var os = require('os');

var BRIDGE_FILE_PATH = path.normalize(__dirname + '/../client/commonjs_bridge.js');

var initCommonJS = function(/* config.files */ files) {
var initCommonJS = function(
files,
config,
basePath,
preprocessors,
) {

if (config.externals) {
const keys = Object.keys(config.externals);
for (const key of keys) {
const pattern = path.resolve(config.modulesRoot || 'node_modules', config.externals[key]);
console.log(pattern);
files.push({
pattern: pattern,
included: true,
served: true,
watched: false,
});
preprocessors[pattern] = preprocessors[pattern] || [];
preprocessors[pattern].push("commonjs");
}
}

// Include the file that resolves all the dependencies on the client.
files.push({
Expand All @@ -13,6 +34,12 @@ var initCommonJS = function(/* config.files */ files) {
watched: false
});
};
initCommonJS.$inject = [
"config.files",
"config.commonjsPreprocessor",
"config.basePath",
"config.preprocessors",
];

var createPreprocesor = function(logger, config, basePath) {
var log = logger.create('preprocessor.commonjs');
Expand All @@ -24,6 +51,17 @@ var createPreprocesor = function(logger, config, basePath) {

log.debug('Configured root path for modules "%s".', modulesRootPath);

const externalAugment = {};
if (config.externals) {
const keys = Object.keys(config.externals);

for (const key of keys) {
const expectedPath = path.resolve(modulesRootPath, key, "index.js");
const actualPath = path.resolve(modulesRootPath, config.externals[key]);
externalAugment[actualPath] = expectedPath;
}
}

return function(content, file, done) {

if (file.originalPath === BRIDGE_FILE_PATH) {
Expand All @@ -32,6 +70,10 @@ var createPreprocesor = function(logger, config, basePath) {

log.debug('Processing "%s".', file.originalPath);

if (externalAugment[file.path]) {
file.path = externalAugment[file.path];
}

if (path.extname(file.originalPath) === '.json') {
return done('window.__cjs_module__["' + file.path + '"] = ' + content + ';' + os.EOL);
}
Expand Down

0 comments on commit 3e53c4b

Please sign in to comment.