npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

@repay/scripts

0.3.1 • Public • Published

@repay/scripts

The CLI tools used for building and bundling javascript packages at REPAY. Uses the @repay/babel-preset, overrides not currently available except through customizing the config.

Usage

Add @repay/scripts to your project with yarn add --dev @repay/scripts

Web application

To run a web application with reloading, call the dev command with your entry file:

// src/index.js
console.log("running");

In the command line:

repay-scripts dev src/index.js

Open your browser to https://localhost:3434

When development is finished, call the build command with your entry file to generate a distributable:

repay-scripts build src/index.js

CLI Commands

Output from repay-scripts --help

repay-scripts <command>

Commands:
  repay-scripts build <entry>  build a javascript library or front-end
                               application
  repay-scripts dev <entry>    build a javascript library or front-end
                               application for local development

Options:
  --lib           is this a javascript library        [boolean] [default: false]
  --babel-env     set the babel environment
               [string] [choices: "development", "test", "production"] [default:
                                                                   "production"]
  --config, -c    path to override configuration        [string] [default: null]
  --debug         adds extra logging for debugging purposes
                                                      [boolean] [default: false]
  --tree-shaking  enables treeshaking for libraries   [boolean] [default: false]
  -h, --help      Show help                                            [boolean]
  -v, --version   Show version number                                  [boolean]

Using the tree-shaking option

Only applicable to libraries.

Adding the --tree-shaking option will create a commonjs bundle normally, but then use Babel directly instead of bundling the ES modules. This allows for more aggressive tree shaking or code splitting when the library is used for the front-end. Additionally, the library should implement the sideEffects property in it's package.json to allow bundlers to confidently remove unused files.

Example:

repay-scripts build --tree-shaking --lib src/index.ts

Using config option

The config options is passed a file which should export a single function as the module.exports which will be called with the pre-made configuration and the options provided to repay-scripts as the arguments, and must return a configuration.

web app example

# shell command
repay-scripts dev --config webpack.config.js src/index.js
// webpack.config.js
module.exports = (config, options) => {
  // access options to determine what changes to make
  if (options.command === "dev") {
    config.performance = { hints: false };
  }
  // returns config whether modified or not
  return config;
};

library example

# shell command
repay-scripts dev --config rollup.config.js --lib src/index.ts
// rollup.config.js
module.exports = (config, options) => {
  // remove ESM build
  config.outputs.splice(0, 1);
  // always return config
  return config;
};

Optimizing Bundles

By default, @repay/scripts splits out the webpack runtime as a bundle to prevent unnecessary content hash updates. You can read more about the option here.

When generating bundles for client facing applications, it's ideal to take libraries used and host them in a separate "vendor" or "common" bundle. This bundle can be cached between releases when the libraries are not upgraded. Here is an example implementation when using @repay/scripts and the config option.

// repay-scripts.config.js
module.exports = (baseConfig, options) => {
  if (options.env === "production") {
    // adds library bundles for cactus and react libraries
    baseConfig.optimization.splitChunks.cacheGroups = {
      cactus: {
        test: /[\\/]node_modules[\\/]cactus-/,
        name: "cactus",
        chunks: "all"
      },
      react: {
        // selects both react and react-dom into a separate bundle
        test: /[\\/]node_modules[\\/]react(-dom)?[\\/]/,
        name: "react",
        chunks: "all"
      }
    };
  }
  return baseConfig;
};

You can learn more about the splitChunks object and options in the webpack docs.

Keywords

none

install

npm i @repay/scripts

Downloadsweekly downloads

28

version

0.3.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability