@automattic/webpack-inline-constant-exports-plugin

1.0.0 • Public • Published

webpack-inline-constant-exports-plugin

Detects that bindings exported from a module are constants and inlines them at the usage site.

constants.js

export const BLOGGER = 'BLOGGER_PLAN'; // strings
export const PREMIUM = 'PREMIUM_PLAN';
export const MONTHS_IN_YEAR = 12; // numbers
export const MONTLY_BILLING = false; // boolean
export const NO_PLAN = null; // null

app.js

import { BLOGGER, PREMIUM } from './constants';

console.log( BLOGGER, PREMIUM );

If the constants.js file is marked as constants module, eligible constants (strings, numbers, booleans, anything that's ===-equal to each other even for different instances) will be inlined into the importing module and the actual import will be removed:

bundled-app.js

console.log( 'BLOGGER_PLAN', 'PREMIUM_PLAN' );

Usage

webpack.config.js

const InlineConstantExportsPlugin = require( '@automattic/webpack-inline-constant-exports-plugin' );

module.exports = {
	plugins: [ new InlineConstantExportsPlugin( [ /\/constants.js/ ] ) ],
};

The constructor argument is an array of regexp matchers: if a matcher matches a module resource path, the module will be treated as an constant-exporting one and these exports will be inlined.

Side Effects

If you import bindings from a constants module:

import { BLOGGER, PREMIUM } from './constants';

it is assumed that the module is imported only to get the constant bindings, and not for its side effects. The import will be removed if all the imported bindings were inlined and the potential side effects will not be performed.

In other words, it's like the module declared sideEffects: false in its package.json.

Readme

Keywords

Package Sidebar

Install

npm i @automattic/webpack-inline-constant-exports-plugin

Weekly Downloads

115

Version

1.0.0

License

GPL-2.0-or-later

Unpacked Size

12.3 kB

Total Files

4

Last publish

Collaborators

  • porada
  • imranh920
  • gmjuhasz
  • rcarvalho
  • briowill
  • bgrgicak
  • dhenridev
  • daledupreez-a8c
  • jeherve
  • yuliyan
  • micbosia8c
  • jeremy.yip
  • wpvip-bot
  • etobiesen
  • kzoschke
  • brunobasto
  • kat3samsin
  • fmfernandes
  • newspack
  • robertsreberski_a8c
  • msurdi-a8c
  • chihsuan
  • manzoorwanijk
  • rjchow
  • andrea-sdl
  • scjr
  • spsiddarthan
  • t2dw4t
  • ehg_
  • wwa
  • sirreal
  • elazzabi
  • royho
  • luismulinari
  • macbre
  • mjangda
  • matticbot
  • a8c
  • blowery
  • noahtallen
  • hanifn
  • sgomes
  • tyxla
  • saroshaga
  • parkcityj
  • nejclovrencic
  • sirbrillig
  • chriszarate
  • robersongomes
  • johngodley