Skip to content

Commit

Permalink
allow custom externals/globals in karma rollup plugin
Browse files Browse the repository at this point in the history
- `unexpected`, `unexpected-eventemitter`, and `unexpected-sinon` are now external (not bundled) and loaded via their globals
- modified `test/browser-specific/setup.js` to use `require()` since we can do that now
- bundle now has an inline source map since I couldn't figure out any other way to load it
- removed duplication of work in the karma plugin; no call to `bundle.generate()` is needed. furthermore the `code` prop did not include the inline source maps. so we just write the file and read the result. could probably avoid reading the file by manually stitching the `code` and `map` props together, but I'm unsure how
- loads `mocha.js.map`
  • Loading branch information
boneskull committed Jul 16, 2020
1 parent 8fb5fb4 commit 0cce38e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
23 changes: 22 additions & 1 deletion karma.conf.js
Expand Up @@ -123,6 +123,12 @@ module.exports = config => {
cfg = addSauceTests(cfg, sauceConfig);
cfg = chooseTestSuite(cfg, env.MOCHA_TEST);

// include sourcemap
cfg = {
...cfg,
files: [...cfg.files, {pattern: './mocha.js.map', included: false}]
};

config.set(cfg);
};

Expand Down Expand Up @@ -232,7 +238,22 @@ const addStandardDependencies = cfg => ({
require.resolve('unexpected-eventemitter/dist/unexpected-eventemitter.js'),
require.resolve('./test/browser-specific/setup'),
...cfg.files
]
],
rollup: {
...cfg.rollup,
external: [
'sinon',
'unexpected',
'unexpected-eventemitter',
'unexpected-sinon'
],
globals: {
sinon: 'sinon',
unexpected: 'weknowhow.expect',
'unexpected-sinon': 'weknowhow.unexpectedSinon',
'unexpected-eventemitter': 'unexpectedEventEmitter'
}
}
});

/**
Expand Down
13 changes: 6 additions & 7 deletions scripts/karma-rollup-plugin.js
Expand Up @@ -109,7 +109,7 @@ framework.$inject = [
function bundlePreprocessor(config) {
const {
basePath,
rollup: {configFile}
rollup: {configFile, globals = {}, external = []}
} = config;

const configPromise = loadConfigFile(path.resolve(basePath, configFile));
Expand All @@ -127,25 +127,24 @@ function bundlePreprocessor(config) {
const outputConfig = {
...((config.output || [])[0] || {}),
file: file.path,
globals: {
sinon: 'sinon'
}
globals,
sourcemap: 'inline'
};

warnings.flush();

const bundle = await rollup.rollup({
input: fileMap.get(file.path),
plugins: pluginConfig,
external: ['sinon'],
external,
onwarn: config.onwarn
});
const {output} = await bundle.generate(outputConfig);

await bundle.write(outputConfig);
console.error(`wrote bundle to ${file.path}`);
const code = fs.readFileSync(outputConfig.file, 'utf8');

done(null, output[0].code);
done(null, code);
};
}

Expand Down
6 changes: 3 additions & 3 deletions test/browser-specific/setup.js
Expand Up @@ -2,7 +2,7 @@

process.stdout = require('browser-stdout')();

global.expect = global.weknowhow.expect
global.expect = require('unexpected')
.clone()
.use(global.weknowhow.unexpectedSinon)
.use(global.unexpectedEventEmitter);
.use(require('unexpected-sinon'))
.use(require('unexpected-eventemitter'));

0 comments on commit 0cce38e

Please sign in to comment.