Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const webpack = require("webpack");
- const Vinyl = require("vinyl");
- const MemoryFs = require("memory-fs");
- const { Transform } = require("stream");
- const path = require("path");
- class Webpack extends Transform {
- constructor(outputPath) {
- super({
- objectMode: true
- });
- this.outputPath = outputPath;
- }
- _transform(file, encoding, done) {
- const compiler = webpack({
- entry: file.path,
- output: {
- path: "/build",
- filename: "bundle.js"
- },
- module: {
- rules: [
- {
- test: /\.jsx?$/,
- exclude: /node_modules/,
- loader: "babel-loader",
- options: {
- presets: [
- "react",
- "flow"
- ]
- }
- }
- ]
- }
- });
- const fs = new MemoryFs();
- compiler.outputFileSystem = fs;
- const self = this;
- compiler.plugin("after-emit", (compilation, callback) => {
- fs.readFile("/build/bundle.js", (err, data) => {
- if (err) {
- console.error(err);
- return;
- }
- self.push(new Vinyl({
- cwd: process.cwd(),
- path: self.outputPath,
- contents: Buffer.from(data, "utf8")
- }));
- callback();
- });
- });
- compiler.run((err, stats) => {
- if (err) {
- console.error(err);
- }
- done();
- });
- }
- }
- module.exports = (outputPath) => {
- return new Webpack(outputPath);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement