@shopify/react-server-webpack-plugin
A webpack plugin that generates entrypoints for @shopify/react-server based applications
Last updated 4 days ago by shopify-dep .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @shopify/react-server-webpack-plugin 
SYNC missed versions from official npm registry.

@shopify/react-server-webpack-plugin

Build Status License: MIT npm version

A webpack plugin which generates "virtual" in-memory entrypoints for @shopify/react-server based applications. This plugin allows you to run a universal React application without needing any client/server-specific code.

Installation

$ yarn add @shopify/react-server-webpack-plugin

Usage

With sewing-kit

As of version 0.102.0 sewing-kit consumes this plugin by default if you have @shopify/react-server in your package.json.

For detailed instructions on usage with Rails and sewing-kit see the documentation for quilt_rails.

Without sewing-kit

First you will need to install all of the dependencies you'll need for your application

yarn add react react-dom
yarn add webpack @shopify/react-server @shopify/react-server-webpack-plugin @shopify/webpack-asset-metadata-plugin --dev

Since @shopify/react-server relies on @shopify/webpack-asset-metadata-plugin, you will need to setup both plugins in your webpack configuration. A simple starter (not production optimized) webpack setup is as follows:

// webpack.config.js

const {ReactServerPlugin} = require('@shopify/react-server-webpack-plugin');
const {AssetMetadataPlugin} = require('@shopify/webpack-asset-metadata-plugin');

const universal = {
  mode: 'development',
  optimization: {
    minimize: false,
  },
  plugins: [new AssetMetadataPlugin(), new ReactServerPlugin()],
};

const server = {
  ...universal,
  name: 'server',
  target: 'node',
  entry: './server',
  externals: [
    (context, request, callback) => {
      if (/node_modules/.test(context)) {
        return callback(null, `commonjs ${request}`);
      }
      callback();
    },
  ],
};

const client = {
  ...universal,
  name: 'client',
  target: 'web',
  entry: './client',
};

module.exports = [server, client];

By default, this plugin expects the entrypoint to your application to be in the root directory.

// index.jsx
import React from 'react';

export default function App() {
  return <div>I am an app</div>;
}

Next you can run webpack && node dist/server.js. When the server starts up you should see:

started react-server on localhost:PORT

If you point your browser at localhost:PORT you should see "I am an app". :)

Automatic Props

The plugin passes some props to your application automatically.

interface DefaultProps {
  /*
   The full WHATWG URL object for the initial request. On the client this will *not* update reactively. It is always the URL for the initial request.
  */
  url: URL;

  /*
   An object containing any data sent via the `x-quilt-data` header.
   This header is used by `quilt_rails` for sending data directly from ruby to React.
  */
  data: Record<string, any>;
}

These props are provided during both server-side and client-side rendering, so they can be used without any special logic around what environment your app is in.

These props can be used to feed initial data into your application for use with libraries such as react-router, as well as for simple app logic.

// index.jsx
import React from 'react';

export default function App({url, data}: {url: Url, data: Record<string, any>}) {
  const {href} = url;
  if (href.endsWith('/about')) {
    return <div>this is an about page</div>;
  }

  return (
    <div>
      I am an app, here are some items:
      <ul>
        {data.items.forEach(({content, id}) => (
          <li key={id}>{content}</li>;
        ))}
      </ul>
    </div>
  );
}

API

The plugin is exported as a named export.

import {ReactServerPlugin} from '@shopify/react-server-webpack-plugin';

It accepts a configuration object with the following interface:

interface Options {
  /*
   The base-path to use for the `client.js` and `server.js` virtual entry files,
   this should also be where your index.tsx/jsx is.

   default: '.'
  */

  basePath: string;

  /*
   The host to use when calling `createServer` from `@shopify/react-server`,
   this should also be where your index.tsx/jsx is.

   default: process.env.REACT_SERVER_IP || "localhost"
  */
  host: string;

  /*
    The port to use when calling `createServer` from `@shopify/react-server`

    default: process.env.REACT_SERVER_PORT || 8081
  */
  port: number;

  /*
   The assetPrefix to use when calling `createServer` from `@shopify/react-server`.

   default: process.env.CDN_URL || "localhost:8080/assets/webpack"
  */
  assetPrefix: string;
}

An example configuration for a sewing-kit app named cool-app might look like this:

new ReactServerPlugin({
  assetPrefix: process.env.CDN_URL || 'https://localhost:8080/webpack/assets/';
});

Current Tags

  • 3.0.3                                ...           latest (4 days ago)
  • 2.2.23-core-js-beta.2                                ...           next (2 months ago)

72 Versions

  • 3.0.3                                ...           4 days ago
  • 3.0.1                                ...           11 days ago
  • 2.2.34                                ...           21 days ago
  • 2.2.33                                ...           24 days ago
  • 2.2.32                                ...           a month ago
  • 2.2.31                                ...           a month ago
  • 2.2.30                                ...           a month ago
  • 2.2.29                                ...           2 months ago
  • 2.2.28                                ...           2 months ago
  • 2.2.27                                ...           2 months ago
  • 2.2.26                                ...           2 months ago
  • 2.2.25                                ...           2 months ago
  • 2.2.24                                ...           2 months ago
  • 2.2.23-core-js-beta.2                                ...           2 months ago
  • 2.2.23-core-js-beta.1                                ...           2 months ago
  • 2.2.23                                ...           2 months ago
  • 2.2.22                                ...           4 months ago
  • 2.2.21                                ...           4 months ago
  • 2.2.20                                ...           4 months ago
  • 2.2.19                                ...           5 months ago
  • 2.2.18                                ...           5 months ago
  • 2.2.17                                ...           6 months ago
  • 2.2.16                                ...           6 months ago
  • 2.2.15                                ...           6 months ago
  • 2.2.14                                ...           7 months ago
  • 2.2.13                                ...           7 months ago
  • 2.2.12                                ...           7 months ago
  • 2.2.11                                ...           7 months ago
  • 2.2.10                                ...           7 months ago
  • 2.2.9                                ...           7 months ago
  • 2.2.8                                ...           7 months ago
  • 2.2.7                                ...           7 months ago
  • 2.2.6                                ...           7 months ago
  • 2.2.5                                ...           7 months ago
  • 2.2.4                                ...           8 months ago
  • 2.2.3                                ...           8 months ago
  • 2.2.2                                ...           8 months ago
  • 2.2.1                                ...           8 months ago
  • 2.2.0                                ...           8 months ago
  • 2.1.15                                ...           8 months ago
  • 2.1.16                                ...           8 months ago
  • 2.1.14                                ...           8 months ago
  • 2.1.13                                ...           8 months ago
  • 2.1.12                                ...           8 months ago
  • 2.1.11                                ...           8 months ago
  • 2.1.10                                ...           8 months ago
  • 2.1.9                                ...           8 months ago
  • 2.1.8                                ...           8 months ago
  • 2.1.7                                ...           8 months ago
  • 2.1.6                                ...           9 months ago
  • 2.1.5                                ...           9 months ago
  • 2.1.4                                ...           9 months ago
  • 2.1.3                                ...           9 months ago
  • 2.1.2                                ...           9 months ago
  • 2.1.1                                ...           9 months ago
  • 2.1.0                                ...           9 months ago
  • 2.0.11                                ...           9 months ago
  • 2.0.10                                ...           9 months ago
  • 2.0.9                                ...           9 months ago
  • 2.0.8                                ...           9 months ago
  • 2.0.6                                ...           9 months ago
  • 2.0.4                                ...           9 months ago
  • 2.0.3                                ...           9 months ago
  • 2.0.2                                ...           9 months ago
  • 2.0.1                                ...           9 months ago
  • 2.0.0                                ...           9 months ago
  • 1.0.5                                ...           9 months ago
  • 1.0.3                                ...           9 months ago
  • 1.0.4                                ...           9 months ago
  • 1.0.2                                ...           9 months ago
  • 1.0.1                                ...           9 months ago
  • 1.0.0                                ...           9 months ago

Copyright 2014 - 2016 © taobao.org |