0.0.1 • Published 7 years ago

webpack-transform v0.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

Simple string-to-string in-memory webpack transformation; no file system involved.

Installation

npm: npm i webpack-transform
yarn: yarn add webpack-transform

API

const webpackTransform = require('webpack-transform');

The transform function has two overloads:
function webpackTransform(code, callback)
function webpackTransform(code, config, callback)

Arguments:
code - A string containing the code that should be bundled, equivalent to the contents of the entry file in a "standard" webpack compilation process.
config - Webpack configuration. Avoid any file-system related options.
callback - A standard callback function with (error, output) signature, where output is a string containing the entire code of the bundle generated by webpack.

Use cases

The transformation function was created for the purpose of testing the output of webpack plugins but can be used for other things as well.

Examples

A simple transformation

webpackTransform('console.log("Hello, world!");', (error, output) => {
  if (error) {
    throw error;
  }
  console.log(output);
});

The last few lines of the output string (the webpack polyfill at the start of the file is too big to show in its entirety):

/******/ ([
/* 0 */
/***/ (function(module, exports) {

console.log("Hello, world!");

/***/ })
/******/ ]);

Extra configuration

var code =
  'function main() { console.log("Hello, world!"); }\n' +
  'main();';

var config = {
  output: {
    pathinfo: true,
  }
};

webpackTransform(code, config, (error, output) => {
  if (error) {
    throw error;
  }

  console.log(output);
});

Last few lines of output string:

/******/ ([
/* 0 */
/*!*****************!*\
  !*** /entry.js ***!
  \*****************/
/*! dynamic exports provided */
/*! all exports used */
/***/ (function(module, exports) {

function main() { console.log("Hello, world!"); }
main();

/***/ })
/******/ ]);

Webpack compatibility

Should work with all major versions of webpack.

License

ISC