UNPKG

safe-webpack-define-plugin

Version:

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

40 lines 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SafeDefinePlugin = void 0; const webpack_1 = require("webpack"); const ExposureFunction_1 = require("./ExposureFunction"); const escapeValues = (definitions, { exposureStrategy, rootExposureStrategy = [] }) => { /** * Define how variables should be exposed */ const exposureFunction = (0, ExposureFunction_1.resolveExposureFunction)(exposureStrategy); /** * Expose the root if there are any * They are more limited in scope, as only an array of names is sensible here */ const rootExposure = rootExposureStrategy.reduce((escaped, rootName) => (Object.assign(Object.assign({}, escaped), { [rootName]: JSON.stringify(definitions) })), {}); if (rootExposureStrategy.length && !exposureStrategy) { /** * Stop if there is root exposure but no definitions exposure */ return rootExposure; } /** * Expose regular defnitions if there are any */ return Object.entries(definitions).reduce((escaped, [propName, value]) => (Object.assign(Object.assign({}, escaped), { [exposureFunction(propName)]: JSON.stringify(value) })), rootExposure); }; /** * This plugin exposes as variables any objects given to it to the code in the outputted webpack bundle * * It is just a wrapper of the webpack `DefinePlugin` with some typescript/value escaping rails * This can be used along side the `DefinePlugin` */ class SafeDefinePlugin extends webpack_1.DefinePlugin { constructor(definitions, options = {}) { super(escapeValues(definitions, options)); } } exports.SafeDefinePlugin = SafeDefinePlugin; exports.default = SafeDefinePlugin; //# sourceMappingURL=Plugin.js.map