Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. const webpack = require("webpack");
  2. const Vinyl = require("vinyl");
  3. const MemoryFs = require("memory-fs");
  4. const { Transform } = require("stream");
  5. const path = require("path");
  6.  
  7. class Webpack extends Transform {
  8. constructor(outputPath) {
  9. super({
  10. objectMode: true
  11. });
  12. this.outputPath = outputPath;
  13. }
  14.  
  15. _transform(file, encoding, done) {
  16. const compiler = webpack({
  17. entry: file.path,
  18. output: {
  19. path: "/build",
  20. filename: "bundle.js"
  21. },
  22. module: {
  23. rules: [
  24. {
  25. test: /\.jsx?$/,
  26. exclude: /node_modules/,
  27. loader: "babel-loader",
  28. options: {
  29. presets: [
  30. "react",
  31. "flow"
  32. ]
  33. }
  34. }
  35. ]
  36. }
  37. });
  38. const fs = new MemoryFs();
  39. compiler.outputFileSystem = fs;
  40. const self = this;
  41. compiler.plugin("after-emit", (compilation, callback) => {
  42. fs.readFile("/build/bundle.js", (err, data) => {
  43. if (err) {
  44. console.error(err);
  45. return;
  46. }
  47. self.push(new Vinyl({
  48. cwd: process.cwd(),
  49. path: self.outputPath,
  50. contents: Buffer.from(data, "utf8")
  51. }));
  52. callback();
  53. });
  54. });
  55. compiler.run((err, stats) => {
  56. if (err) {
  57. console.error(err);
  58. }
  59. done();
  60. });
  61. }
  62. }
  63.  
  64. module.exports = (outputPath) => {
  65. return new Webpack(outputPath);
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement