Skip to content

Instantly share code, notes, and snippets.

@theKashey
Created October 19, 2018 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theKashey/07090691c0a4680ed773375d8dbeebc1 to your computer and use it in GitHub Desktop.
Save theKashey/07090691c0a4680ed773375d8dbeebc1 to your computer and use it in GitHub Desktop.
injecting-mock-window
// this package empowers babel-register
const addHook = require('pirates').addHook;
function matcher(filename) {
// do not apply to mocks
return fileName.indexOf('src/mocks') < 0;
}
const PREPEND_TEXT = `
const window = require('src/mocks/window.js');
const document = require('src/mocks/document.js');
`;
// you may use `require-control` to teach node.js to understand aliases.
// just prepend each js file with a few imports
addHook(
(code, filename) => PREPEND_TEXT + code;
{ exts: ['.js'], matcher }
);
// just add a ProvidePlugin
// it will inject imports to the files
// where those variables are NOT defined.
new webpack.ProvidePlugin({
window: 'give-me-a-window',
document: 'give-me-a-document',
// ...
});
// just spin up alias
resolve: {
alias: {
'give-me-a-window': 'src/mocks/window.js',
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment