🚀 Socket Launch Week 🚀 Day 1: Introducing .NET Support in Socket.Learn More
Socket
Sign inDemoInstall
Socket

safe-webpack-define-plugin

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-webpack-define-plugin

A webpack (typescript ready) plugin to define global variables on outputted bundles

1.0.9
latest
Source
npm
Version published
Weekly downloads
105
47.89%
Maintainers
1
Weekly downloads
 
Created
Source

Webpack Safe Define Plugin

A typescript ready webpack plugin to easily include global variables on your outputted bundles

It is much alike the DotEnv plugin but also comes with the bonus of

  • safely introducing your plain javascript objects (instances of Classes will get stringified in the process) to your webpack projects (such as configuration) on the front-end.
  • having the flexibiliy on how your output is built, no process.env shackles
  • leveraging typescript's type checking

Usage:

webpack.config.ts
export default {
    // ....
    plugins: [
        /**
         * Any primitive values are allowed through
         **/
        new SafeDefinePlugin({
            variables: 'of',
            the: ['world', { unite: 1 }],
            bourgeoisie: null,
        }),
    ],
};
Your entrypoint.ts file
declare const variables: unknown;
declare const the: unknown;

console.log(variables, the);
Your output main.js file
console.log('of', ['world', { unite: 1 }]);

Further notes

  • Feel free to use this with other Define Plugin instances in the same webpack configuration, they work just as well (as long as outputed definitions do not compete)
  • Do you want your exposed variables to be under a specific path such as process.env? Just check out SafeDefinePluginOptions['exposureStrategy'], we accept custom names generators too!
  • Not all possible types have been introduced, if you have any issues, feel free to open a ticket

Future improvements

  • Leverage ts-morph to generate typescript defintion files for your declared variables
  • Introduce a JSON.stringify spy to allows for more freedom
  • More data types allowed for input

LICENSE

MIT

Keywords

webpack

FAQs

Package last updated on 21 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts