@dumlj/compare-envs-webpack-plugin
Advanced tools
Comparing version 2.5.23 to 2.5.24
{ | ||
"name": "@dumlj-example/compare-envs-webpack-plugin", | ||
"version": "2.5.23", | ||
"version": "2.5.24", | ||
"private": true, | ||
@@ -11,4 +11,4 @@ "main": "webpack.config.ts", | ||
"dependencies": { | ||
"@dumlj/compare-envs-webpack-plugin": "workspace:^2.5.23", | ||
"@dumlj/memfs-webpack-plugin": "workspace:^2.5.23" | ||
"@dumlj/compare-envs-webpack-plugin": "workspace:^2.5.24", | ||
"@dumlj/memfs-webpack-plugin": "workspace:^2.5.24" | ||
}, | ||
@@ -15,0 +15,0 @@ "devDependencies": { |
@@ -1,1 +0,26 @@ | ||
export * from './CompareEnvsWebpackPlugin'; | ||
import { SeedWebpackPlugin, type SeedWebpackPluginOptions } from '@dumlj/seed-webpack-plugin'; | ||
import type { Compiler } from 'webpack'; | ||
export interface CompareEnvsWebpackPluginOptions extends SeedWebpackPluginOptions { | ||
/** | ||
* 对比其他环境变量文件 | ||
* @description | ||
* 此部分环境变量会作为 `{ [key]: '' }` 存储到环境变量中 | ||
*/ | ||
compare?: string | string[]; | ||
/** | ||
* 跳过 Dev 模式对比 | ||
* @description | ||
* 默认为 false | ||
*/ | ||
skipCompareDevMode?: boolean; | ||
} | ||
export declare class CompareEnvsWebpackPlugin extends SeedWebpackPlugin { | ||
static PLUGIN_NAME: string; | ||
protected compare: string | string[]; | ||
protected skipCompareDevMode: boolean; | ||
constructor(options?: CompareEnvsWebpackPluginOptions); | ||
/** 查找 dotenv 文件 */ | ||
protected lookupDotenvFiles(pattern: string | string[], context?: string): Promise<string[]>; | ||
applyCompare(compiler: Compiler): void; | ||
apply(compiler: Compiler): void; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CompareEnvsWebpackPlugin = void 0; | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./CompareEnvsWebpackPlugin"), exports); | ||
const seed_webpack_plugin_1 = require("@dumlj/seed-webpack-plugin"); | ||
const feature_dotenv_1 = require("@dumlj/feature-dotenv"); | ||
const chalk_1 = tslib_1.__importDefault(require("chalk")); | ||
const glob_1 = require("glob"); | ||
class CompareEnvsWebpackPlugin extends seed_webpack_plugin_1.SeedWebpackPlugin { | ||
static PLUGIN_NAME = 'compare-envs-webpack-plugin'; | ||
compare; | ||
skipCompareDevMode; | ||
constructor(options) { | ||
super(options); | ||
this.compare = options?.compare || []; | ||
this.skipCompareDevMode = typeof options?.skipCompareDevMode === 'boolean' ? options?.skipCompareDevMode : false; | ||
} | ||
/** 查找 dotenv 文件 */ | ||
async lookupDotenvFiles(pattern, context = process.cwd()) { | ||
if (!((typeof pattern === 'string' || Array.isArray(pattern)) && pattern.length > 0)) { | ||
return []; | ||
} | ||
try { | ||
return (0, glob_1.glob)(pattern, { cwd: context, absolute: true }); | ||
} | ||
catch (error) { | ||
return []; | ||
} | ||
} | ||
applyCompare(compiler) { | ||
const { webpack, options } = compiler; | ||
const { WebpackError } = webpack; | ||
const { mode } = options; | ||
const compare = async () => { | ||
const files = await this.lookupDotenvFiles(this.compare); | ||
if (files.length === 0) { | ||
this.logger.warn('Not found any dotenv files.'); | ||
return true; | ||
} | ||
const invalids = await (0, feature_dotenv_1.validateDotEnv)(files); | ||
return invalids.length > 0 | ||
? `The environment variables (dotenv) is incorrectly configured \n - ${invalids.join('\n - ')} \nPlease set the missing envs to empty string in the above file. e.g \`ABC=\`` | ||
: true; | ||
}; | ||
if (mode === 'production') { | ||
compiler.hooks.beforeCompile.tapPromise(this.pluginName, async () => { | ||
const result = await compare(); | ||
if (result !== true) { | ||
this.logger.error(result); | ||
const error = new WebpackError(`[${this.pluginName}] Compare dotenv files failed.`); | ||
return Promise.reject(error); | ||
} | ||
}); | ||
} | ||
else { | ||
compiler.hooks.done.tapPromise(this.pluginName, async ({ compilation }) => { | ||
/** 跳过 */ | ||
if (this.skipCompareDevMode) { | ||
return; | ||
} | ||
const result = await compare(); | ||
if (result !== true) { | ||
this.logger.warn(result); | ||
const warning = new WebpackError(chalk_1.default.yellow(result)); | ||
compilation.warnings.push(warning); | ||
} | ||
}); | ||
} | ||
} | ||
apply(compiler) { | ||
super.apply(compiler); | ||
this.applyCompare(compiler); | ||
} | ||
} | ||
exports.CompareEnvsWebpackPlugin = CompareEnvsWebpackPlugin; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dumlj/compare-envs-webpack-plugin", | ||
"version": "2.5.23", | ||
"version": "2.5.24", | ||
"description": "Check whether the contents of dotenv files are consistent.", | ||
@@ -13,5 +13,11 @@ "repository": { | ||
"types": "./libs/index.d.ts", | ||
"scripts": { | ||
"clear": "rimraf ./libs", | ||
"compile": "tsc --project ./tsconfig.compile.json", | ||
"dev": "jest --watch", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"@dumlj/feature-dotenv": "^2.5.23", | ||
"@dumlj/seed-webpack-plugin": "^2.5.23", | ||
"@dumlj/feature-dotenv": "^2.5.24", | ||
"@dumlj/seed-webpack-plugin": "^2.5.24", | ||
"chalk": "^4.1.2", | ||
@@ -32,3 +38,3 @@ "glob": "^10.3.10", | ||
}, | ||
"gitHead": "af7572cfa6c1e5d6ce8c57588b581e881b1ca679" | ||
"gitHead": "4a0c12e0d7d041313b742ec998b6959a03d91c47" | ||
} |
@@ -8,3 +8,2 @@ <!-- This file is dynamically generated. please edit in __readme__ --> | ||
[![codecov](https://codecov.io/gh/dumlj/dumlj-build/graph/badge.svg?token=ELV5W1H0C0)](https://codecov.io/gh/dumlj/dumlj-build) | ||
[![DeepSource](https://app.deepsource.com/gh/dumlj/dumlj-build.svg/?label=active+issues&show_trend=true&token=YtSFFZ702Q016pjWlBWT30Iy)](https://app.deepsource.com/gh/dumlj/dumlj-build/) | ||
@@ -51,9 +50,9 @@ # Compare Envs Webpack Plugin | ||
<b>@dumlj/compare-envs-webpack-plugin</b> | ||
├─┬ <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/seed-webpack-plugin%22,%22version%22:%222.5.22%22,%22description%22:%22Basic%20webpack%20plugins%22,%22isPrivate%22:false,%22location%22:%22@webpack-plugin/seed-webpack-plugin%22,%22dependencies%22:%5B%22@dumlj/feature-updater%22,%22chalk%22,%22tslib%22,%22utility-types%22,%22@dumlj/mock-lib%22,%22@jest/types%22,%22@types/webpack%22,%22ts-jest%22,%22webpack%22%5D,%22workspaceDependencies%22:%5B%22@dumlj/feature-updater%22,%22@dumlj/mock-lib%22%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@webpack-plugin/seed-webpack-plugin">@dumlj/seed-webpack-plugin</a> | ||
│ ├─┬─ <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/feature-updater%22,%22version%22:%222.5.22%22,%22description%22:%22updater%20for%20packages.%22,%22isPrivate%22:false,%22location%22:%22@feature/feature-updater%22,%22dependencies%22:%5B%22@dumlj/shell-lib%22,%22@dumlj/util-lib%22,%22fs-extra%22,%22semver%22,%22tslib%22,%22@dumlj/mock-lib%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%22@dumlj/shell-lib%22,%22@dumlj/util-lib%22,%22@dumlj/mock-lib%22%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@feature/feature-updater">@dumlj/feature-updater</a> | ||
│ │ ├─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/shell-lib%22,%22version%22:%222.5.22%22,%22description%22:%22shell%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:false,%22location%22:%22@lib/shell-lib%22,%22dependencies%22:%5B%22@jest/types%22,%22chokidar%22,%22command-exists%22,%22lodash%22,%22tslib%22,%22ts-jest%22,%22tsd-lite%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/shell-lib">@dumlj/shell-lib</a> | ||
│ │ ├─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/util-lib%22,%22version%22:%222.5.22%22,%22description%22:%22util%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:false,%22location%22:%22@lib/util-lib%22,%22dependencies%22:%5B%22fs-extra%22,%22glob%22,%22tslib%22,%22@jest/types%22,%22memfs%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/util-lib">@dumlj/util-lib</a> | ||
│ │ └─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/mock-lib%22,%22version%22:%222.5.22%22,%22description%22:%22mock%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:true,%22location%22:%22@lib/mock-lib%22,%22dependencies%22:%5B%22memfs%22,%22tslib%22,%22webpack%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/mock-lib">@dumlj/mock-lib</a><sup><small><i>PRIVATE</i></small></sup> | ||
│ └─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/mock-lib%22,%22version%22:%222.5.22%22,%22description%22:%22mock%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:true,%22location%22:%22@lib/mock-lib%22,%22dependencies%22:%5B%22memfs%22,%22tslib%22,%22webpack%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/mock-lib">@dumlj/mock-lib</a><sup><small><i>PRIVATE</i></small></sup> | ||
└── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/feature-dotenv%22,%22version%22:%222.5.22%22,%22description%22:%22dotenv%20utilities.%22,%22isPrivate%22:false,%22location%22:%22@feature/feature-dotenv%22,%22dependencies%22:%5B%22dotenv%22,%22fs-extra%22,%22tslib%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@feature/feature-dotenv">@dumlj/feature-dotenv</a> | ||
├─┬ <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/seed-webpack-plugin%22,%22version%22:%222.5.23%22,%22description%22:%22Basic%20webpack%20plugins%22,%22isPrivate%22:false,%22location%22:%22@webpack-plugin/seed-webpack-plugin%22,%22dependencies%22:%5B%22@dumlj/feature-updater%22,%22chalk%22,%22tslib%22,%22utility-types%22,%22@dumlj/mock-lib%22,%22@jest/types%22,%22@types/webpack%22,%22ts-jest%22,%22webpack%22%5D,%22workspaceDependencies%22:%5B%22@dumlj/feature-updater%22,%22@dumlj/mock-lib%22%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@webpack-plugin/seed-webpack-plugin">@dumlj/seed-webpack-plugin</a> | ||
│ ├─┬─ <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/feature-updater%22,%22version%22:%222.5.23%22,%22description%22:%22updater%20for%20packages.%22,%22isPrivate%22:false,%22location%22:%22@feature/feature-updater%22,%22dependencies%22:%5B%22@dumlj/shell-lib%22,%22@dumlj/util-lib%22,%22fs-extra%22,%22semver%22,%22tslib%22,%22@dumlj/mock-lib%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%22@dumlj/shell-lib%22,%22@dumlj/util-lib%22,%22@dumlj/mock-lib%22%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@feature/feature-updater">@dumlj/feature-updater</a> | ||
│ │ ├─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/shell-lib%22,%22version%22:%222.5.23%22,%22description%22:%22shell%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:false,%22location%22:%22@lib/shell-lib%22,%22dependencies%22:%5B%22@jest/types%22,%22chokidar%22,%22command-exists%22,%22lodash%22,%22tslib%22,%22ts-jest%22,%22tsd-lite%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/shell-lib">@dumlj/shell-lib</a> | ||
│ │ ├─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/util-lib%22,%22version%22:%222.5.23%22,%22description%22:%22util%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:false,%22location%22:%22@lib/util-lib%22,%22dependencies%22:%5B%22fs-extra%22,%22glob%22,%22tslib%22,%22@jest/types%22,%22memfs%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/util-lib">@dumlj/util-lib</a> | ||
│ │ └─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/mock-lib%22,%22version%22:%222.5.23%22,%22description%22:%22mock%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:true,%22location%22:%22@lib/mock-lib%22,%22dependencies%22:%5B%22memfs%22,%22tslib%22,%22webpack%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/mock-lib">@dumlj/mock-lib</a><sup><small><i>PRIVATE</i></small></sup> | ||
│ └─── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/mock-lib%22,%22version%22:%222.5.23%22,%22description%22:%22mock%20%E5%B7%A5%E5%85%B7%E5%BA%93%22,%22isPrivate%22:true,%22location%22:%22@lib/mock-lib%22,%22dependencies%22:%5B%22memfs%22,%22tslib%22,%22webpack%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@lib/mock-lib">@dumlj/mock-lib</a><sup><small><i>PRIVATE</i></small></sup> | ||
└── <a is="dumlj-link" data-project="%7B%22name%22:%22@dumlj/feature-dotenv%22,%22version%22:%222.5.23%22,%22description%22:%22dotenv%20utilities.%22,%22isPrivate%22:false,%22location%22:%22@feature/feature-dotenv%22,%22dependencies%22:%5B%22dotenv%22,%22fs-extra%22,%22tslib%22,%22@jest/types%22,%22ts-jest%22%5D,%22workspaceDependencies%22:%5B%5D%7D" href="https://github.com/dumlj/dumlj-build/tree/main/@feature/feature-dotenv">@dumlj/feature-dotenv</a> | ||
</pre> |
101
src/index.ts
@@ -1,1 +0,100 @@ | ||
export * from './CompareEnvsWebpackPlugin' | ||
import { SeedWebpackPlugin, type SeedWebpackPluginOptions } from '@dumlj/seed-webpack-plugin' | ||
import { validateDotEnv } from '@dumlj/feature-dotenv' | ||
import chalk from 'chalk' | ||
import { glob } from 'glob' | ||
import type { Compiler } from 'webpack' | ||
export interface CompareEnvsWebpackPluginOptions extends SeedWebpackPluginOptions { | ||
/** | ||
* 对比其他环境变量文件 | ||
* @description | ||
* 此部分环境变量会作为 `{ [key]: '' }` 存储到环境变量中 | ||
*/ | ||
compare?: string | string[] | ||
/** | ||
* 跳过 Dev 模式对比 | ||
* @description | ||
* 默认为 false | ||
*/ | ||
skipCompareDevMode?: boolean | ||
} | ||
export class CompareEnvsWebpackPlugin extends SeedWebpackPlugin { | ||
static PLUGIN_NAME = 'compare-envs-webpack-plugin' | ||
protected compare: string | string[] | ||
protected skipCompareDevMode: boolean | ||
constructor(options?: CompareEnvsWebpackPluginOptions) { | ||
super(options) | ||
this.compare = options?.compare || [] | ||
this.skipCompareDevMode = typeof options?.skipCompareDevMode === 'boolean' ? options?.skipCompareDevMode : false | ||
} | ||
/** 查找 dotenv 文件 */ | ||
protected async lookupDotenvFiles(pattern: string | string[], context: string = process.cwd()) { | ||
if (!((typeof pattern === 'string' || Array.isArray(pattern)) && pattern.length > 0)) { | ||
return [] | ||
} | ||
try { | ||
return glob(pattern, { cwd: context, absolute: true }) | ||
} catch (error) { | ||
return [] | ||
} | ||
} | ||
public applyCompare(compiler: Compiler) { | ||
const { webpack, options } = compiler | ||
const { WebpackError } = webpack | ||
const { mode } = options | ||
const compare = async () => { | ||
const files = await this.lookupDotenvFiles(this.compare) | ||
if (files.length === 0) { | ||
this.logger.warn('Not found any dotenv files.') | ||
return true | ||
} | ||
const invalids = await validateDotEnv(files) | ||
return invalids.length > 0 | ||
? `The environment variables (dotenv) is incorrectly configured \n - ${invalids.join( | ||
'\n - ' | ||
)} \nPlease set the missing envs to empty string in the above file. e.g \`ABC=\`` | ||
: true | ||
} | ||
if (mode === 'production') { | ||
compiler.hooks.beforeCompile.tapPromise(this.pluginName, async () => { | ||
const result = await compare() | ||
if (result !== true) { | ||
this.logger.error(result) | ||
const error = new WebpackError(`[${this.pluginName}] Compare dotenv files failed.`) | ||
return Promise.reject(error) | ||
} | ||
}) | ||
} else { | ||
compiler.hooks.done.tapPromise(this.pluginName, async ({ compilation }) => { | ||
/** 跳过 */ | ||
if (this.skipCompareDevMode) { | ||
return | ||
} | ||
const result = await compare() | ||
if (result !== true) { | ||
this.logger.warn(result) | ||
const warning = new WebpackError(chalk.yellow(result)) | ||
compilation.warnings.push(warning) | ||
} | ||
}) | ||
} | ||
} | ||
public apply(compiler: Compiler) { | ||
super.apply(compiler) | ||
this.applyCompare(compiler) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
0
59995
25
306
57
1