vite-plugin-env-compatible
Advanced tools
Comparing version 1.1.1 to 2.0.0
@@ -28,2 +28,2 @@ import { Plugin } from 'vite'; | ||
export { UserOptions as EnvCompatibleOptions, envCompatible as default }; | ||
export { type UserOptions as EnvCompatibleOptions, envCompatible as default }; |
@@ -1,241 +0,2 @@ | ||
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true}); | ||
var __commonJS = (callback, module) => () => { | ||
if (!module) { | ||
module = {exports: {}}; | ||
callback(module.exports, module); | ||
} | ||
return module.exports; | ||
}; | ||
var __exportStar = (target, module, desc) => { | ||
if (module && typeof module === "object" || typeof module === "function") { | ||
for (let key of __getOwnPropNames(module)) | ||
if (!__hasOwnProp.call(target, key) && key !== "default") | ||
__defProp(target, key, {get: () => module[key], enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable}); | ||
} | ||
return target; | ||
}; | ||
var __toModule = (module) => { | ||
return __exportStar(__markAsModule(__defProp(module != null ? __create(__getProtoOf(module)) : {}, "default", module && module.__esModule && "default" in module ? {get: () => module.default, enumerable: true} : {value: module, enumerable: true})), module); | ||
}; | ||
// node_modules/dotenv/lib/main.js | ||
var require_main = __commonJS((exports, module) => { | ||
var fs2 = require("fs"); | ||
var path3 = require("path"); | ||
function log(message) { | ||
console.log(`[dotenv][DEBUG] ${message}`); | ||
} | ||
var NEWLINE = "\n"; | ||
var RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/; | ||
var RE_NEWLINES = /\\n/g; | ||
var NEWLINES_MATCH = /\n|\r|\r\n/; | ||
function parse(src, options) { | ||
const debug = Boolean(options && options.debug); | ||
const obj = {}; | ||
src.toString().split(NEWLINES_MATCH).forEach(function(line, idx) { | ||
const keyValueArr = line.match(RE_INI_KEY_VAL); | ||
if (keyValueArr != null) { | ||
const key = keyValueArr[1]; | ||
let val = keyValueArr[2] || ""; | ||
const end = val.length - 1; | ||
const isDoubleQuoted = val[0] === '"' && val[end] === '"'; | ||
const isSingleQuoted = val[0] === "'" && val[end] === "'"; | ||
if (isSingleQuoted || isDoubleQuoted) { | ||
val = val.substring(1, end); | ||
if (isDoubleQuoted) { | ||
val = val.replace(RE_NEWLINES, NEWLINE); | ||
} | ||
} else { | ||
val = val.trim(); | ||
} | ||
obj[key] = val; | ||
} else if (debug) { | ||
log(`did not match key and value when parsing line ${idx + 1}: ${line}`); | ||
} | ||
}); | ||
return obj; | ||
} | ||
function config(options) { | ||
let dotenvPath = path3.resolve(process.cwd(), ".env"); | ||
let encoding = "utf8"; | ||
let debug = false; | ||
if (options) { | ||
if (options.path != null) { | ||
dotenvPath = options.path; | ||
} | ||
if (options.encoding != null) { | ||
encoding = options.encoding; | ||
} | ||
if (options.debug != null) { | ||
debug = true; | ||
} | ||
} | ||
try { | ||
const parsed = parse(fs2.readFileSync(dotenvPath, {encoding}), {debug}); | ||
Object.keys(parsed).forEach(function(key) { | ||
if (!Object.prototype.hasOwnProperty.call(process.env, key)) { | ||
process.env[key] = parsed[key]; | ||
} else if (debug) { | ||
log(`"${key}" is already defined in \`process.env\` and will not be overwritten`); | ||
} | ||
}); | ||
return {parsed}; | ||
} catch (e) { | ||
return {error: e}; | ||
} | ||
} | ||
module.exports.config = config; | ||
module.exports.parse = parse; | ||
}); | ||
// node_modules/dotenv-expand/lib/main.js | ||
var require_main2 = __commonJS((exports, module) => { | ||
"use strict"; | ||
var dotenvExpand2 = function(config) { | ||
var environment = config.ignoreProcessEnv ? {} : process.env; | ||
var interpolate = function(envValue) { | ||
var matches = envValue.match(/(.?\${?(?:[a-zA-Z0-9_]+)?}?)/g) || []; | ||
return matches.reduce(function(newEnv, match) { | ||
var parts = /(.?)\${?([a-zA-Z0-9_]+)?}?/g.exec(match); | ||
var prefix = parts[1]; | ||
var value2, replacePart; | ||
if (prefix === "\\") { | ||
replacePart = parts[0]; | ||
value2 = replacePart.replace("\\$", "$"); | ||
} else { | ||
var key = parts[2]; | ||
replacePart = parts[0].substring(prefix.length); | ||
value2 = environment.hasOwnProperty(key) ? environment[key] : config.parsed[key] || ""; | ||
value2 = interpolate(value2); | ||
} | ||
return newEnv.replace(replacePart, value2); | ||
}, envValue); | ||
}; | ||
for (var configKey in config.parsed) { | ||
var value = environment.hasOwnProperty(configKey) ? environment[configKey] : config.parsed[configKey]; | ||
config.parsed[configKey] = interpolate(value); | ||
} | ||
for (var processKey in config.parsed) { | ||
environment[processKey] = config.parsed[processKey]; | ||
} | ||
return config; | ||
}; | ||
module.exports = dotenvExpand2; | ||
}); | ||
// src/lib/env.ts | ||
var import_dotenv = __toModule(require_main()); | ||
var import_dotenv_expand = __toModule(require_main2()); | ||
var _path = require('path'); var _path2 = _interopRequireDefault(_path); | ||
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); | ||
function lookupFile(dir, formats, pathOnly = false) { | ||
for (const format of formats) { | ||
const fullPath = _path2.default.join(dir, format); | ||
if (_fs2.default.existsSync(fullPath) && _fs2.default.statSync(fullPath).isFile()) { | ||
return pathOnly ? fullPath : _fs2.default.readFileSync(fullPath, "utf-8"); | ||
} | ||
} | ||
const parentDir = _path2.default.dirname(dir); | ||
if (parentDir !== dir) { | ||
return lookupFile(parentDir, formats, pathOnly); | ||
} | ||
} | ||
function loadEnv(loadOptions) { | ||
const {mode, envDir, prefix, ignoreProcessEnv} = loadOptions; | ||
if (mode === "local") { | ||
throw new Error(`"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.`); | ||
} | ||
const env = {}; | ||
const envFiles = [ | ||
`.env.${mode}.local`, | ||
`.env.${mode}`, | ||
`.env.local`, | ||
`.env` | ||
]; | ||
for (const key in process.env) { | ||
if (key.startsWith(prefix) && env[key] === void 0) { | ||
env[key] = process.env[key]; | ||
} | ||
} | ||
for (const file of envFiles) { | ||
const path3 = lookupFile(envDir, [file], true); | ||
if (path3) { | ||
const parsed = import_dotenv.default.parse(_fs2.default.readFileSync(path3), { | ||
debug: !!process.env.DEBUG || void 0 | ||
}); | ||
(0, import_dotenv_expand.default)({ | ||
parsed, | ||
ignoreProcessEnv | ||
}); | ||
for (const [key, value] of Object.entries(parsed)) { | ||
if (key.startsWith(prefix) && env[key] === void 0) { | ||
env[key] = value; | ||
} else if (key === "NODE_ENV") { | ||
process.env.VITE_USER_NODE_ENV = value; | ||
} | ||
} | ||
} | ||
} | ||
return env; | ||
} | ||
function loadDynamicInjectedEnv(prefix) { | ||
const env = {}; | ||
Object.keys(process.env).map((envKey) => { | ||
if (String(envKey).includes(prefix)) { | ||
env[envKey] = process.env[envKey] || ""; | ||
} | ||
}); | ||
return env; | ||
} | ||
// package.json | ||
var name = "vite-plugin-env-compatible"; | ||
// src/index.ts | ||
function envCompatible(userOptions = {}) { | ||
const options = { | ||
mountedPath: "process.env", | ||
...userOptions | ||
}; | ||
return { | ||
name, | ||
enforce: "pre", | ||
config(config, {mode}) { | ||
const root = config.root || process.cwd(); | ||
let envDir = config.envDir || "./"; | ||
if (!_path2.default.isAbsolute(envDir || "")) { | ||
envDir = _path2.default.join(root, envDir); | ||
} | ||
const prefix = typeof options.prefix === "undefined" ? "VUE_APP_" : options.prefix; | ||
const env = loadEnv({ | ||
mode, | ||
envDir, | ||
prefix, | ||
ignoreProcessEnv: _nullishCoalesce(options.ignoreProcessEnv, () => ( false)) | ||
}); | ||
const dynamicInjectedEnv = loadDynamicInjectedEnv(prefix); | ||
const myDefine = {}; | ||
if (_optionalChain([options, 'access', _ => _.mountedPath, 'optionalAccess', _2 => _2.startsWith, 'call', _3 => _3("process.env")])) { | ||
myDefine["process.env.VITE"] = JSON.stringify(true); | ||
} | ||
Object.keys({...env, ...dynamicInjectedEnv}).map((key) => { | ||
const value = env[key]; | ||
myDefine[`${options.mountedPath}.${key}`] = JSON.stringify(value); | ||
}); | ||
config.define = { | ||
...config.define || {}, | ||
...myDefine | ||
}; | ||
} | ||
}; | ||
} | ||
exports.default = envCompatible; | ||
"use strict";var U=Object.create;var g=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var R=Object.getPrototypeOf,V=Object.prototype.hasOwnProperty;var y=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),F=(e,t)=>{for(var n in t)g(e,n,{get:t[n],enumerable:!0})},j=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of L(t))!V.call(e,s)&&s!==n&&g(e,s,{get:()=>t[s],enumerable:!(i=W(t,s))||i.enumerable});return e};var f=(e,t,n)=>(n=e!=null?U(R(e)):{},j(t||!e||!e.__esModule?g(n,"default",{value:e,enumerable:!0}):n,e)),T=e=>j(g({},"__esModule",{value:!0}),e);var S=y((K,h)=>{"use strict";var C=require("fs"),Q=require("path");function P(e){console.log(`[dotenv][DEBUG] ${e}`)}var B=` | ||
`,q=/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/,G=/\\n/g,M=/\n|\r|\r\n/;function O(e,t){let n=!!(t&&t.debug),i={};return e.toString().split(M).forEach(function(s,r){let p=s.match(q);if(p!=null){let c=p[1],o=p[2]||"",l=o.length-1,a=o[0]==='"'&&o[l]==='"';o[0]==="'"&&o[l]==="'"||a?(o=o.substring(1,l),a&&(o=o.replace(G,B))):o=o.trim(),i[c]=o}else n&&P(`did not match key and value when parsing line ${r+1}: ${s}`)}),i}function H(e){let t=Q.resolve(process.cwd(),".env"),n="utf8",i=!1;e&&(e.path!=null&&(t=e.path),e.encoding!=null&&(n=e.encoding),e.debug!=null&&(i=!0));try{let s=O(C.readFileSync(t,{encoding:n}),{debug:i});return Object.keys(s).forEach(function(r){Object.prototype.hasOwnProperty.call(process.env,r)?i&&P(`"${r}" is already defined in \`process.env\` and will not be overwritten`):process.env[r]=s[r]}),{parsed:s}}catch(s){return{error:s}}}h.exports.config=H;h.exports.parse=O});var _=y((ee,w)=>{"use strict";var J=function(e){var t=e.ignoreProcessEnv?{}:process.env,n=function(p){var c=p.match(/(.?\${?(?:[a-zA-Z0-9_]+)?}?)/g)||[];return c.reduce(function(o,l){var a=/(.?)\${?([a-zA-Z0-9_]+)?}?/g.exec(l),d=a[1],u,v;if(d==="\\")v=a[0],u=v.replace("\\$","$");else{var b=a[2];v=a[0].substring(d.length),u=t.hasOwnProperty(b)?t[b]:e.parsed[b]||"",u=n(u)}return o.replace(v,u)},p)};for(var i in e.parsed){var s=t.hasOwnProperty(i)?t[i]:e.parsed[i];e.parsed[i]=n(s)}for(var r in e.parsed)t[r]=e.parsed[r];return e};w.exports=J});var Z={};F(Z,{default:()=>A});module.exports=T(Z);var E=f(require("path")),m=f(require("fs")),D=f(S()),I=f(_());function $(e,t,n=!1){for(let s of t){let r=E.default.join(e,s);if(m.default.existsSync(r)&&m.default.statSync(r).isFile())return n?r:m.default.readFileSync(r,"utf-8")}let i=E.default.dirname(e);if(i!==e)return $(i,t,n)}function N(e){let{mode:t,envDir:n,prefix:i,ignoreProcessEnv:s}=e;if(t==="local")throw new Error('"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.');let r={},p=[`.env.${t}.local`,`.env.${t}`,".env.local",".env"];for(let c in process.env)c.startsWith(i)&&r[c]===void 0&&(r[c]=process.env[c]);for(let c of p){let o=$(n,[c],!0);if(o){let l=D.default.parse(m.default.readFileSync(o),{debug:!!process.env.DEBUG||void 0});(0,I.default)({parsed:l,ignoreProcessEnv:s});for(let[a,d]of Object.entries(l))a.startsWith(i)&&r[a]===void 0?r[a]=d:a==="NODE_ENV"&&(process.env.VITE_USER_NODE_ENV=d)}}return r}function z(e){let t={};return Object.keys(process.env).map(n=>{String(n).includes(e)&&(t[n]=process.env[n]||"")}),t}var k="vite-plugin-env-compatible";var x=f(require("path"));function A(e={}){let t={mountedPath:"process.env",...e};return{name:k,enforce:"pre",config(n,{mode:i}){let s=n.root||process.cwd(),r=n.envDir||"./";x.default.isAbsolute(r||"")||(r=x.default.join(s,r));let p=typeof t.prefix>"u"?"VUE_APP_":t.prefix,c=N({mode:i,envDir:r,prefix:p,ignoreProcessEnv:t.ignoreProcessEnv??!1}),o=z(p),l={};t.mountedPath?.startsWith("process.env")&&(l["process.env.VITE"]=JSON.stringify(!0)),Object.keys({...c,...o}).map(a=>{let d=c[a];l[`${t.mountedPath}.${a}`]=JSON.stringify(d)}),n.define={...n.define||{},...l}}}} |
{ | ||
"name": "vite-plugin-env-compatible", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Environment Variables Compatible", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"files": [ | ||
@@ -12,3 +19,3 @@ "dist" | ||
"scripts": { | ||
"build": "tsup src/index.ts --dts", | ||
"build": "tsup", | ||
"lint": "prettier --write --parser typescript \"src/**/*.ts\"", | ||
@@ -28,3 +35,7 @@ "prepublishOnly": "npm run build" | ||
"devDependencies": { | ||
"@commitlint/cli": "7.2.0", | ||
"@commitlint/config-conventional": "7.1.2", | ||
"@types/node": "14.14.25", | ||
"commitizen": "2.10.1", | ||
"cz-customizable": "5.2.0", | ||
"dotenv": "8.2.0", | ||
@@ -34,9 +45,5 @@ "dotenv-expand": "5.1.0", | ||
"prettier": "2.2.1", | ||
"tsup": "3.12.1", | ||
"tsup": "^8.0.1", | ||
"typescript": "4.1.3", | ||
"vite": "2.5.1", | ||
"@commitlint/cli": "7.2.0", | ||
"@commitlint/config-conventional": "7.1.2", | ||
"commitizen": "2.10.1", | ||
"cz-customizable": "5.2.0", | ||
"yorkie": "2.0.0" | ||
@@ -43,0 +50,0 @@ }, |
# vite-plugin-env-compatible | ||
> inject to process.env like vue-cli or create-react-app | ||
> inject to process.env like vue-cli or create-react-app and also define client `process.env.XXX` for you. | ||
@@ -22,5 +22,5 @@ <p align="center"> | ||
## Motivation | ||
- `vite` expose `VITE_XXX` to `import.meta.env.VITE_XXX`, but not loaded to process.env like vue-cli or create-react-app | ||
- this plugin support setting prefix like `VUE_APP_` or `REACT_APP_` and loaded to process.env | ||
- just for compatibility | ||
- `vite` expose env to `import.meta.env.PREFIX_XXX`, but not loaded to process.env like vue-cli or create-react-app. | ||
- ~~this plugin support setting prefix like `VUE_APP_` or `REACT_APP_` and loaded to process.env~~(support by vite@2.5.1). | ||
- built-in with [vue-cli-plugin-vite](https://github.com/IndexXuan/vue-cli-plugin-vite) just for compatibility. | ||
@@ -34,3 +34,3 @@ ## Usage | ||
// vite.config.ts | ||
import envCompatible from 'vite-plugin-env-compatible' | ||
import env from 'vite-plugin-env-compatible' | ||
@@ -41,3 +41,3 @@ // @see https://vitejs.dev/config/ | ||
// ...other plugins | ||
envCompatible(/* options */) | ||
env(/* options */) | ||
], | ||
@@ -44,0 +44,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6
13010
56
1
24
1