• Stars
    star
    198
  • Rank 196,898 (Top 4 %)
  • Language
    JavaScript
  • Created over 9 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

webpack-require

WARNING: this is beta software.

This lets you require() a module in Node that needs to have a specific webpack config. This is useful rendering a component that uses webpack loaders server-side in Node.js.

How to use it

var webpackRequire = require('webpack-require');

webpackRequire(
  require('./path/to/webpack.config.js'),
  require.resolve('./path/to/yourmodule.js'),
  function(err, factory, stats, fs) {
    if (err) {
      console.error(err);
    }

    // Invoke factory() to actually get an instance of your module.
    // You can call it multiple times to get multiple independent copies
    // of your module (useful for multiple requests in a single process).
    // Note that this is fairly performant, since the file is only parsed
    // once even if you call factory() multiple times.
    var yourmodule = factory();

    // You can inspect the build process by looking at the stats object

    // fs can be used to read static assets from the bundle. They are mounted
    // in the root directory. This is useful for extracting a static CSS
    // stylesheet with ExtractTextPlugin by doing: fs.readFileSync('/style.css')
  }
);

How does it work?

This bundles your app with webpack and evaluates the bundle in a contextify sandbox. It also tweaks your webpack config to remove plugins that are detrimental server-side (i.e. optimization plugins).

API docs

webpackRequire(webpackConfig, modulePath, [overrideModules], [globals], callback)

  • webpackConfig: the webpack config as a JavaScript object.
  • modulePath: the absolute path to the module you want to require()
  • overrideModules: optional array of absolute paths to modules that you do NOT want bundled. Instead, when these modules are required inside of the webpack bundle, a reference to the module as required by Node.js will be returned instead. This is useful when you have a stateful module, and you want to share that state between your Node.js code and your webpacked code. If that module is not included in this array, then when that module is required you'll get a fresh copy without the shared state. This array is also useful for sharing native modules, like fs with your webpack bundle.
  • globals: optional object to serve as global scope
  • callback(err, factory, stats, fs): callback that executes when the build process is complete. factory is a function that takes no arguments and returns a fresh copy of the required module. Multiple invocations of this function should be fairly inexpensive since parsing only happens once. stats is the webpack stats object and can return information about the build process (see the webpack docs on stats). fs is a mock file system module that you can use to read static assets like images from the bundle. All files are placed at the root (i.e. if your bundle outputs a myImage.png asset, you can use fs.readFileSync('/myImage.png') to read it).

More Repositories

1

react-howto

Your guide to the (sometimes overwhelming!) React ecosystem.
11,612
star
2

webpack-howto

JavaScript
10,119
star
3

rwb

JavaScript
727
star
4

react-webpack-template

JavaScript
549
star
5

react-boilerplate

Boilerplate for creating a React npm package
JavaScript
544
star
6

react-touch

React photo viewer for mobile
JavaScript
446
star
7

ReactHack

React+Parse+Bootstrap Hackathon toolkit
JavaScript
249
star
8

react-server-rendering-example

Super-basic example of how server rendering works
JavaScript
221
star
9

react-one-hour-email

Build a very simple email client in an hour, step by step.
JavaScript
174
star
10

node-jsx

transparently require() jsx from within node
JavaScript
165
star
11

react-gss

JavaScript
155
star
12

langchain-github-bot

Python
143
star
13

react-raf-batching

A game-like batching strategy for React
JavaScript
134
star
14

loris

High-performance JavaScript UIs
JavaScript
133
star
15

react-tutorial

Code from the React tutorial
JavaScript
124
star
16

jsx-loader

JSX loader for webpack
JavaScript
108
star
17

famous-react

JavaScript
78
star
18

dagster-poor-mans-data-lake

Python
74
star
19

react-browserify-template

Quick template for building with Browserify + React
HTML
67
star
20

react-touch-lib

React component library for building inertial touch applications.
JavaScript
63
star
21

yologram

JavaScript
42
star
22

react-multiplayer

Multiplayer React with Firebase
JavaScript
40
star
23

morimodel

Models for Mori
JavaScript
38
star
24

react-xhr

JavaScript
37
star
25

react-tween

JavaScript
29
star
26

react-meteor-preso

JavaScript
26
star
27

react-classset

React.addons.classSet() packaged in npm
JavaScript
22
star
28

react-jqueryui

Wrap jQuery UI widgets in React components
JavaScript
22
star
29

django-reactify

Python
21
star
30

generator-react-library

Yeoman generator for React components
JavaScript
17
star
31

use-state-singleton

yet another redux alternative
TypeScript
15
star
32

dagster-github-stars-example

Python
15
star
33

rust-benchmark

JavaScript
14
star
34

js-css-loader

Define CSS in JS
JavaScript
13
star
35

commonjs-asset-spec

12
star
36

lockless

STM for Python
Python
11
star
37

ratelimiter

The easiest way to add rate limiting to your app
JavaScript
11
star
38

reactify-server-rendering

JavaScript
9
star
39

jsxc

experimental jsx command-line tool
JavaScript
9
star
40

staticify

Browserify all of your static resources
JavaScript
9
star
41

unrouter

JavaScript
8
star
42

zod-args

zod-args is the fastest way to throw together a simple CLI with TypeScript type checking.
JavaScript
8
star
43

sweetjs-loader

webpack loader for sweet.js
JavaScript
8
star
44

reacthack-core

JavaScript
8
star
45

dagster-script-to-assets

Python
6
star
46

sqlconfig

Python
4
star
47

oredev-workshop

JavaScript
4
star
48

rerequire

JavaScript
4
star
49

statics

static assets in npm
JavaScript
4
star
50

zod-json-rpc

TypeScript
4
star
51

generator-react-quickstart

Yeoman generator for React apps and libraries
JavaScript
3
star
52

sharable-components-prototype

JavaScript
3
star
53

react-ember-demo

JavaScript
3
star
54

react-main

JavaScript
3
star
55

ts-json-rpc

TypeScript
3
star
56

stylesheets

Client/server stylesheet management
JavaScript
3
star
57

reactify-server-rendering-tools

JavaScript
3
star
58

react-freezer

JavaScript
3
star
59

omgnosql

OMG! NoSQL!!!
Python
2
star
60

reactbars

DONT EVEN THINK ABOUT IT
JavaScript
2
star
61

dslpy

Domain specific languages embedded in Python
Python
2
star
62

jsxnode

JavaScript
2
star
63

react-graph

immutable client-side graph abstraction with great react integration. fulfills similar use cases to backbone.model
JavaScript
2
star
64

dagster-ml-example

Python
2
star
65

objectid

Python
2
star
66

reactpad

JavaScript
2
star
67

meteor-leaderboard-react

Meteor leaderboard example using React
JavaScript
2
star
68

htmldry

Don't Repeat your HTML
JavaScript
2
star
69

react-meteor

Meteor bindings for React, packaged for npm
JavaScript
2
star
70

browserify-bundler

JavaScript
1
star
71

dagster-data-sources

1
star
72

jsbs

JavaScript boot strap, or JavaScript bullshit, depending on what you prefer
JavaScript
1
star
73

jsonrpc2.0-cli

JavaScript
1
star
74

sketchpad

Python
1
star
75

text-to-pydantic

Python
1
star
76

odsc-talk

Python
1
star
77

inboxfeed

JavaScript
1
star
78

browserify-transform-server-side

JavaScript
1
star
79

ts-json-rpc-client

TypeScript
1
star
80

statics-stylesheets

JavaScript
1
star
81

rx-spinner

A simple reusable spinner component example
JavaScript
1
star
82

react-profiler

A simpler interface to ReactDefaultPerf
JavaScript
1
star
83

react-core

JavaScript
1
star
84

dagster-gitpod

Python
1
star