Giter Club home page Giter Club logo

piped-webpack's Introduction

Piped Webpack

License Travis Status npm No Maintenance Intended

webpack as a Gulp plugin.

Why?

Why Gulp?

Webpack already function as a great build tool, and in many cases you don't even need Gulp.

Combining Gulp with webpack, however, allow you to do many more things without writing webpack plugins:

  • Separate CSS workflow that does not go into the bundle (eg. for non-SPA apps)
  • Mangle other type of assets with the vast collection of Gulp plugins
  • Run webpack output through Gulp plugins

Why not webpack-stream?

At TipMe we started out with webpack-stream. However, we found that it doesn't work with DllPlugin (#149). So we set out to create a new implementation:

  • Webpack is now on peerDependencies, so you can use any version you wanted without passing the instance.
  • Dump memory-fs to stream directly, so all output files will always be discovered
  • No unnecessary config mangling:
    • We don't set output for you, make sure that your output.path is not set or set to process.cwd()
    • We don't add any plugins for you (webpack-stream can add ProgressPlugin). If you want any plugin you can add them manually.
  • Extensible class-based design
  • Use webpack's watch system for performance

The reason we name this as piped-webpack is because webpack-stream also appear as gulp-webpack on npm.

Migrating from webpack-stream is simple: just change your require to piped-webpack and, if you're passing webpack instance, remove it. Also remove callback argument if you're using it. We'll implement something later.

Usage

Install this module from npm:

npm install --save-dev piped-webpack

Pipe your entrypoint files to piped-webpack:

const gulp = require('gulp');
const pipedWebpack = require('piped-webpack');

gulp.task('webpack', function(){
	return gulp.src(['js/entry1.js', 'js/entry2.js'])
		.pipe(pipedWebpack({
			// your webpack config
		}))
		.pipe(gulp.dest(__dirname + '/static/'));
});

In the above case, the webpack config can omit the entry object.

If you already have entry set, you can pipe an empty stream to pipedWebpack:

gulp.src([])
	.pipe(pipedWebpack({
		entry: {
			// your entrypoints
		},
		// your webpack config
	}))
	.pipe(gulp.dest(__dirname + '/static/'));

Note that due to webpack's limitation we don't actually use the files from stream, only path. Therefore, don't pipe anything else but gulp.src into this plugin.

Tips

Additional entries

If you need to use load something in your entrypoints (eg. babel-polyfill) you can use additionalEntries option:

const gulp = require('gulp');
const pipedWebpack = require('piped-webpack');

gulp.task('webpack', function(){
	return gulp.src(['js/entry1.js', 'js/entry2.js'])
		.pipe(pipedWebpack({
			// your webpack config
			additionalEntries: ['babel-polyfill'],
		}))
		.pipe(gulp.dest(__dirname + '/static/'));

You can also specify a function that returns an array. The function will receive Vinyl file object as argument.

Submit source maps to Sentry

Here's how we submit source maps to Sentry, and removing it from production servers

const gulp = require('gulp');
const filter = require('gulp-filter');
const sentryRelease = require('gulp-sentry-release');
const merge = require('merge-stream');
const pipedWebpack = require('piped-webpack');

const SENTRY_URL = 'https://app.getsentry.com/api/0/projects/mycompany/myapp/';
const SENTRY_API_KEY = 'apikeygoeshere'; // see gulp-sentry-release docs on how to get this key

const webpackConfig = {
	// ...
	// sentry requires that your source map have a visible comment
	devtool: 'source-map',
};

gulp.task('webpack', function(){
	// filter out source maps
	let mapFilter = filter(['**', '!**/*.map'], {restore: true, passthrough: false});

	let codeStream = gulp.src(['*/js/*.js', '!static/**'])
		.pipe(pipedWebpack(webpackConfig))
		.pipe(mapFilter) // remove all map files
		.pipe(gulp.dest(__dirname + '/static/'));

	let mapStream = mapFilter.restore
		.pipe(sentryRelease({
			API_URL: SENTRY_URL,
			API_KEY: SENTRY_API_KEY,
			DOMAIN: '~',
			version: '1.0.0', // you can use git-rev to update this automatically
		}).release());

	return merge(codeStream, mapStream);
});

Watching

Set watch: true in your configuration to use webpack's watch system. This can be done like this:

gulp.task('webpack', function(){
	// invoke your webpack normally
});
gulp.task('watch', function(){
	webpackConfig.watch = true;
	gulp.start('webpack');
});

License

piped-webpack is licensed under the MIT License

This project is unmaintained. You may use it, but issues and pull requests might be ignored.

piped-webpack's People

Contributors

sjones6 avatar whs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

piped-webpack's Issues

Does not output anything on Windows (no files generated)

Our project must be buildable on both *nix and Windows. However I just noticed that piped-webpack outputs nothing on Windows while it works fine on macOS. No files are generated, but the usual webpack logging seems to indicate that compilation was performed successfully. Is there something I can do to fix this? How can I diagnose what's happening?

Running my webpack config using the webpack command directly generates files as expected.

Here is my webpack output when run by gulp:

Version: webpack 3.4.1
Time: 4460ms
     Asset     Size  Chunks                    Chunk Names
lib.dll.js  9.25 MB       0  [emitted]  [big]  lib
   [0] ./node_modules/process/browser.js 5.42 kB {0} [built]
   [1] ./node_modules/react/react.js 56 bytes {0} [built]
...
  [17] ./node_modules/react-redux/lib/index.js 475 bytes {0} [built]
  [98] ./node_modules/redux/es/index.js 1.08 kB {0} [built]
 [123] ./node_modules/react-dom/index.js 59 bytes {0} [built]
 [140] ./node_modules/react-router/es/index.js 1.46 kB {0} [built]
 [342] dll lib 12 bytes {0} [built]
...
    + 824 hidden modules
[13:23:49] Finished '_scripts_lib' after 4.7 s

Here is my gulp task:

gulp.task("_scripts_lib", function () {
  return gulp.src([])
    .pipe(pipedWebpack(require("./webpack.lib.config.js")))
    .pipe(gulp.dest(SCRIPTS_OUTPUT_DIR));
});

and here is my webpack config file:

/* global __dirname process */

let path = require("path");
let Webpack = require("webpack");

const libName = "[name]_[hash]";

let config = {
  resolve: {
    modules: [
      __dirname,
      "node_modules"
    ],
    extensions: [".js", ".jsx"]
  },
  entry: {
    lib: [
      "react",
      "react-dom",
      "react-router",
      "redux",
      "react-redux"
    ]
  },
  module: {
    rules: [
      // Process JS(X) files using Babel
      { test: /\.jsx?$/, loader: "babel-loader", include: [/scripts/], query: { presets: ["react", "es2015"] } },
    ]
  },
  plugins: [
    new Webpack.DllPlugin({
      name: libName,
      path: path.join(__dirname, "[name].dll-manifest.json")
    })
  ],
  output: {
    filename: "[name].dll.js",
    library: libName
  }
};

module.exports = config;

Watch mode emitting ALL components

I’m using this webpack config to test the issue:

{
    watch: true,
    entry: {
        react1: ['./src/components/react1.jsx'],
        react2: ['./src/components/react2.jsx']
    },
    output: {
        filename: 'components/[name].js',
        path: path.resolve(__dirname, 'build-dev')
    },
    module: {
        loaders: [
            { test: /\.jsx$/, loader: 'babel-loader' }
        ]
    },
    plugins: [
        new webpack.DllReferencePlugin({
            manifest: path.join('build-dev', 'components/common-manifest.json')
        })
    ]
}

This works fine when run with webpack --watch (only changed modules are emitted, and you must actually change the file, i.e. saving it with no changes doesn’t fire webpack). But using the same config with gulp ALL the components are always emitted.

This is the gulp code:

return gulp.src([])
    .pipe(pipedWebpack(<config here>))
    .pipe(gulp.dest(config.dst))

Thank You!

Just wanted to say that this plugin was very useful to us and fixed the issues we were having with DllPlugin and webpack-stream (the manifest json file was never generated). This saved us a whole bunch of time.

Also, I thought this comment might help other people find a solution to similar issues! I didn't find much info about DllPlugin not working as expected.

Anyway, thanks for making this plugin available! 💯

Crashes on empty pipe

If gulp-changed results in an empty pipe the plugin crashes.

I recently replaced it from webpack-stream due to the DllPlugin glitch. The latest in this situation was showing a message informing that webpack would not run because of an empty entry, but this plugin crashes instead telling WebpackOptionsValidationError: Invalid configuration object. ....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.