index.ts 543 Bytes
interface InjectEnvType {
  globalEnvName?: string
  variable?: {
    mode?: string
    base?: string
    shortName?: string
    enableOss?: boolean
    ossAddress?: string
  }
}

export function injectEnv(options: InjectEnvType) {
  const { globalEnvName = 'PROJECT_ENV', variable = {} } = options
  const projectEnv = `window.${globalEnvName}`
  return `${projectEnv}=${JSON.stringify(variable)};
  Object.freeze(${projectEnv});
  Object.defineProperty(window, "${globalEnvName}", {
    configurable: false,
    writable: false,
  });
  `
}