@37bytes/vite-dynamic-environments
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -7,6 +7,6 @@ import { Plugin } from 'vite'; | ||
dynamicEnvironmentsDir?: string; | ||
outputDir?: string; | ||
envOutputDir?: string; | ||
} | ||
declare const dynamicEnvironmentsSupport: ({ scriptLink, ignorePrefixes, dynamicEnvironmentsDir, outputDir }?: Params) => Plugin; | ||
declare const dynamicEnvironmentsSupport: ({ scriptLink, ignorePrefixes, dynamicEnvironmentsDir, envOutputDir }?: Params) => Plugin; | ||
export { dynamicEnvironmentsSupport as default }; |
@@ -111,4 +111,10 @@ import { createRequire } from 'module';const require = createRequire(import.meta.url) | ||
import { resolve, join } from "node:path"; | ||
import { createLogger } from "vite"; | ||
var logger = createLogger("info", { prefix: "[dynamicEnvironmentsSupport]" }); | ||
var { FORCE_ENVIRONMENT } = process.env; | ||
var getLast = (array) => array[array.length - 1]; | ||
var getDynamicEnvironment = ({ path, ignorePrefixes }) => { | ||
var getDynamicEnvironment = ({ path, ignorePrefixes = [] }) => { | ||
if (!existsSync(path)) { | ||
throw new Error(`File does not exist (${path})`); | ||
} | ||
const config = import_dotenv.default.parse(readFileSync(path)); | ||
@@ -120,5 +126,3 @@ const dynamicEnvironment = Object.fromEntries( | ||
}; | ||
var generateScript = (environment) => ` | ||
window.dynamicEnvironment = Object.freeze(${JSON.stringify(environment)}); | ||
`; | ||
var generateScript = (environment) => `window.dynamicEnvironment = Object.freeze(${JSON.stringify(environment)});`; | ||
var DEFAULT_SCRIPT_LINK = "/dynamicEnvironment.js"; | ||
@@ -131,53 +135,81 @@ var DEFAULT_DYNAMIC_ENVIRONMENTS_DIR = resolve(process.cwd(), "environments/dynamic"); | ||
dynamicEnvironmentsDir = DEFAULT_DYNAMIC_ENVIRONMENTS_DIR, | ||
outputDir = DEFAULT_OUTPUT_DIR | ||
} = {}) => ({ | ||
name: "@37bytes/vite-dynamic-environments", | ||
// режим разработки | ||
configureServer(server) { | ||
server.middlewares.use(scriptLink, (request, response) => { | ||
const dynamicEnvironment = getDynamicEnvironment({ | ||
ignorePrefixes, | ||
path: resolve(process.cwd(), ".env.development") | ||
envOutputDir = DEFAULT_OUTPUT_DIR | ||
} = {}) => { | ||
let outDir; | ||
let envDir; | ||
let envConfig; | ||
let forceEnvironmentScriptContent; | ||
return { | ||
name: "@37bytes/vite-dynamic-environments", | ||
config: (config, nextEnvConfig) => { | ||
outDir = config.build?.outDir ?? "dist"; | ||
envConfig = nextEnvConfig; | ||
if (config.envDir) { | ||
envDir = resolve(process.cwd(), config.envDir); | ||
} else { | ||
envDir = process.cwd(); | ||
} | ||
}, | ||
// режим разработки | ||
configureServer(server) { | ||
server.middlewares.use(scriptLink, (request, response) => { | ||
if (!envDir) { | ||
throw new Error("envDir is falsy"); | ||
} | ||
const dynamicEnvironment = getDynamicEnvironment({ | ||
ignorePrefixes, | ||
path: resolve(envDir, `.env.${envConfig.mode}`) | ||
}); | ||
response.writeHead(200, { "Content-Type": "application/json" }); | ||
response.end(generateScript(dynamicEnvironment)); | ||
}); | ||
response.writeHead(200, { "Content-Type": "application/json" }); | ||
response.end(generateScript(dynamicEnvironment)); | ||
}); | ||
}, | ||
// очистка outputDir | ||
buildStart: () => { | ||
rmSync(outputDir, { recursive: true, force: true }); | ||
}, | ||
// генерация .js для каждого динамического окружения | ||
buildEnd: () => { | ||
const fullPathToDynamicEnvironments = resolve(process.cwd(), dynamicEnvironmentsDir); | ||
readdirSync(fullPathToDynamicEnvironments).forEach((fileName) => { | ||
if (!fileName.startsWith(".env.")) { | ||
return; | ||
}, | ||
// очистка outputDir | ||
buildStart: () => { | ||
rmSync(envOutputDir, { recursive: true, force: true }); | ||
}, | ||
// генерация .js для каждого динамического окружения | ||
buildEnd: () => { | ||
const fullPathToDynamicEnvironments = resolve(process.cwd(), dynamicEnvironmentsDir); | ||
readdirSync(fullPathToDynamicEnvironments).forEach((fileName) => { | ||
if (!fileName.startsWith(".env.")) { | ||
return; | ||
} | ||
const fullPathToFile = join(fullPathToDynamicEnvironments, fileName); | ||
if (statSync(fullPathToFile).isDirectory()) { | ||
return; | ||
} | ||
const environmentName = getLast(fileName.split(".")); | ||
const dynamicEnvironment = getDynamicEnvironment({ | ||
ignorePrefixes, | ||
path: fullPathToFile | ||
}); | ||
if (!existsSync(envOutputDir)) { | ||
mkdirSync(envOutputDir, { recursive: true }); | ||
} | ||
const scriptContent = generateScript(dynamicEnvironment); | ||
writeFileSync(join(envOutputDir, `dynamicEnvironment.${environmentName}.js`), scriptContent); | ||
if (FORCE_ENVIRONMENT === environmentName) { | ||
logger.info( | ||
`process.env.FORCE_ENVIRONMENT defined! preparing dynamicEnvironment.js for "${environmentName}"...` | ||
); | ||
forceEnvironmentScriptContent = scriptContent; | ||
} | ||
}); | ||
}, | ||
// автоматическое добавление скрипта в html | ||
transformIndexHtml: (html) => { | ||
if (!html.includes("<head>")) { | ||
throw new Error("dynamicEnvironmentsPlugin: '<head>' not found in html"); | ||
} | ||
const fullPathToFile = join(fullPathToDynamicEnvironments, fileName); | ||
if (statSync(fullPathToFile).isDirectory()) { | ||
return; | ||
return html.replace("<head>", `<head><script src='${scriptLink}'></script>`); | ||
}, | ||
writeBundle: () => { | ||
if (forceEnvironmentScriptContent) { | ||
logger.info(`writing prepared dynamicEnvironment.js in "${outDir}"...`); | ||
writeFileSync(join(resolve(outDir, "dynamicEnvironment.js")), forceEnvironmentScriptContent); | ||
} | ||
const environmentName = getLast(fileName.split(".")); | ||
const dynamicEnvironment = getDynamicEnvironment({ | ||
ignorePrefixes, | ||
path: fullPathToFile | ||
}); | ||
if (!existsSync(outputDir)) { | ||
mkdirSync(outputDir, { recursive: true }); | ||
} | ||
writeFileSync( | ||
join(outputDir, `dynamicEnvironment.${environmentName}.js`), | ||
generateScript(dynamicEnvironment) | ||
); | ||
}); | ||
}, | ||
// автоматическое добавление скрипта в html | ||
transformIndexHtml: (html) => { | ||
if (!html.includes("<head>")) { | ||
throw new Error("dynamicEnvironmentsPlugin: '<head>' not found in html"); | ||
} | ||
return html.replace("<head>", `<head><script src="${scriptLink}"></script>`); | ||
} | ||
}); | ||
}; | ||
}; | ||
var dynamicEnvironmentsSupport_default = dynamicEnvironmentsSupport; | ||
@@ -184,0 +216,0 @@ |
{ | ||
"name": "@37bytes/vite-dynamic-environments", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Vite plugin for generating dynamic JavaScript files for each environment, allowing seamless integration and management of environment-specific configurations.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
19940
447
13