unplugin-env 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/webpack.cjs CHANGED
@@ -4309,41 +4309,41 @@ var require_queue = __commonJS({
4309
4309
  queue.drained = drained;
4310
4310
  return queue;
4311
4311
  function push(value) {
4312
- var p = new Promise(function(resolve2, reject) {
4312
+ var p = new Promise(function(resolve, reject) {
4313
4313
  pushCb(value, function(err, result) {
4314
4314
  if (err) {
4315
4315
  reject(err);
4316
4316
  return;
4317
4317
  }
4318
- resolve2(result);
4318
+ resolve(result);
4319
4319
  });
4320
4320
  });
4321
4321
  p.catch(noop);
4322
4322
  return p;
4323
4323
  }
4324
4324
  function unshift(value) {
4325
- var p = new Promise(function(resolve2, reject) {
4325
+ var p = new Promise(function(resolve, reject) {
4326
4326
  unshiftCb(value, function(err, result) {
4327
4327
  if (err) {
4328
4328
  reject(err);
4329
4329
  return;
4330
4330
  }
4331
- resolve2(result);
4331
+ resolve(result);
4332
4332
  });
4333
4333
  });
4334
4334
  p.catch(noop);
4335
4335
  return p;
4336
4336
  }
4337
4337
  function drained() {
4338
- var p = new Promise(function(resolve2) {
4338
+ var p = new Promise(function(resolve) {
4339
4339
  process.nextTick(function() {
4340
4340
  if (queue.idle()) {
4341
- resolve2();
4341
+ resolve();
4342
4342
  } else {
4343
4343
  var previousDrain = queue.drain;
4344
4344
  queue.drain = function() {
4345
4345
  if (typeof previousDrain === "function") previousDrain();
4346
- resolve2();
4346
+ resolve();
4347
4347
  queue.drain = previousDrain;
4348
4348
  };
4349
4349
  }
@@ -4829,9 +4829,9 @@ var require_stream3 = __commonJS({
4829
4829
  });
4830
4830
  }
4831
4831
  _getStat(filepath) {
4832
- return new Promise((resolve2, reject) => {
4832
+ return new Promise((resolve, reject) => {
4833
4833
  this._stat(filepath, this._fsStatSettings, (error, stats) => {
4834
- return error === null ? resolve2(stats) : reject(error);
4834
+ return error === null ? resolve(stats) : reject(error);
4835
4835
  });
4836
4836
  });
4837
4837
  }
@@ -4855,10 +4855,10 @@ var require_async5 = __commonJS({
4855
4855
  this._readerStream = new stream_1.default(this._settings);
4856
4856
  }
4857
4857
  dynamic(root, options) {
4858
- return new Promise((resolve2, reject) => {
4858
+ return new Promise((resolve, reject) => {
4859
4859
  this._walkAsync(root, options, (error, entries) => {
4860
4860
  if (error === null) {
4861
- resolve2(entries);
4861
+ resolve(entries);
4862
4862
  } else {
4863
4863
  reject(error);
4864
4864
  }
@@ -4868,10 +4868,10 @@ var require_async5 = __commonJS({
4868
4868
  async static(patterns, options) {
4869
4869
  const entries = [];
4870
4870
  const stream = this._readerStream.static(patterns, options);
4871
- return new Promise((resolve2, reject) => {
4871
+ return new Promise((resolve, reject) => {
4872
4872
  stream.once("error", reject);
4873
4873
  stream.on("data", (entry) => entries.push(entry));
4874
- stream.once("end", () => resolve2(entries));
4874
+ stream.once("end", () => resolve(entries));
4875
4875
  });
4876
4876
  }
4877
4877
  };
@@ -5541,127 +5541,10 @@ var import_node_fs3 = require("fs");
5541
5541
  var import_node_process4 = __toESM(require("process"), 1);
5542
5542
  var import_unplugin = require("unplugin");
5543
5543
 
5544
- // src/core/generate.ts
5545
- var import_node_path = __toESM(require("path"), 1);
5546
- var import_node_fs = require("fs");
5547
- var import_node_process = __toESM(require("process"), 1);
5548
- var import_fast_glob = __toESM(require_out4(), 1);
5549
- var import_recast = __toESM(require("recast"), 1);
5550
- function mergeObjects(prodObj, devObj) {
5551
- const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
5552
- for (const prop of devObj.properties) {
5553
- const key = prop.key.name || prop.key.value;
5554
- if (prodProps.has(key)) {
5555
- const index = prodObj.properties.indexOf(prodProps.get(key));
5556
- if (index >= 0)
5557
- prodObj.properties[index] = prop;
5558
- } else {
5559
- prodObj.properties.push(prop);
5560
- }
5561
- }
5562
- }
5563
- async function generateScript(options, mode, base2) {
5564
- const { dir, fileName, globalName, serve, build } = options.env;
5565
- const folder = await findFolder(import_node_process.default.cwd(), dir);
5566
- const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
5567
- absolute: true,
5568
- cwd: folder
5569
- });
5570
- const testReg = mode === "serve" ? serve : build;
5571
- let target = "";
5572
- let source = "";
5573
- let code = "";
5574
- const name = fileName;
5575
- for (const file of files) {
5576
- try {
5577
- const mod = await import_node_fs.promises.readFile(file, "utf-8");
5578
- if (testReg == null ? void 0 : testReg.test(file))
5579
- target = mod;
5580
- else
5581
- source = mod;
5582
- } catch (error) {
5583
- console.error(`Error loading file ${file}:`, error);
5584
- }
5585
- }
5586
- const targetAst = import_recast.default.parse(target);
5587
- const sourceAst = import_recast.default.parse(source);
5588
- const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5589
- const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5590
- mergeObjects(sourceExport, targetExport);
5591
- const mergedCode = import_recast.default.print(sourceExport).code;
5592
- const returnedTarget = mergedCode;
5593
- const versionInfo = await generateVersion(options, mode);
5594
- code = `window.${globalName}=${returnedTarget};${versionInfo}`;
5595
- const formatCode = code;
5596
- return {
5597
- code,
5598
- script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
5599
- </head>`,
5600
- emit: {
5601
- type: "asset",
5602
- fileName: name,
5603
- source: formatCode
5604
- },
5605
- watchFiles: files
5606
- };
5607
- }
5608
- async function generateVersion(options, mode) {
5609
- const packageFile = await (0, import_fast_glob.default)("package.json", {
5610
- absolute: true,
5611
- cwd: (0, import_node_path.resolve)(import_node_process.default.cwd())
5612
- });
5613
- const packageString = await import_node_fs.promises.readFile(packageFile[0], "utf8");
5614
- const packageJson = JSON.parse(packageString);
5615
- return `console.info("Version: ${packageJson.version} - ${mode === "serve" ? "runtime" : "built"} on ${options.date}")`;
5616
- }
5617
- async function findFolder(directoryPath, dir) {
5618
- const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
5619
- const files = await import_node_fs.promises.readdir(directoryPath);
5620
- const filePaths = files.filter((item) => !ignore.has(item));
5621
- let nestedFolder = "";
5622
- for (const file of filePaths) {
5623
- const fullFilePath = import_node_path.default.join(directoryPath, file);
5624
- const stat = await import_node_fs.promises.stat(fullFilePath);
5625
- if (stat.isDirectory()) {
5626
- if (file.toLowerCase() === dir) {
5627
- return fullFilePath;
5628
- } else {
5629
- nestedFolder = await findFolder(fullFilePath, dir);
5630
- if (nestedFolder)
5631
- return nestedFolder;
5632
- }
5633
- }
5634
- }
5635
- return "";
5636
- }
5637
-
5638
- // src/core/options.ts
5639
- var import_utils = require("@antfu/utils");
5640
- function resolveOptions(options) {
5641
- const defaults = {
5642
- env: {
5643
- dir: "config",
5644
- fileName: "manifest.js",
5645
- globalName: "manifest",
5646
- serve: /dev|development/i,
5647
- build: /prod|production/i
5648
- },
5649
- compress: {
5650
- outDir: "dist",
5651
- ignoreBase: true
5652
- }
5653
- };
5654
- const mergeOptions = (0, import_utils.deepMerge)(defaults, options);
5655
- return {
5656
- ...mergeOptions,
5657
- date: (/* @__PURE__ */ new Date()).toLocaleString()
5658
- };
5659
- }
5660
-
5661
5544
  // src/core/compress.ts
5662
- var import_node_fs2 = __toESM(require("fs"), 1);
5663
- var import_node_path2 = __toESM(require("path"), 1);
5664
- var import_node_process3 = __toESM(require("process"), 1);
5545
+ var import_node_fs = __toESM(require("fs"), 1);
5546
+ var import_node_path = __toESM(require("path"), 1);
5547
+ var import_node_process2 = __toESM(require("process"), 1);
5665
5548
  var import_archiver = __toESM(require("archiver"), 1);
5666
5549
 
5667
5550
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.js
@@ -5851,16 +5734,16 @@ var ansiStyles = assembleStyles();
5851
5734
  var ansi_styles_default = ansiStyles;
5852
5735
 
5853
5736
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/supports-color/index.js
5854
- var import_node_process2 = __toESM(require("process"), 1);
5737
+ var import_node_process = __toESM(require("process"), 1);
5855
5738
  var import_node_os = __toESM(require("os"), 1);
5856
5739
  var import_node_tty = __toESM(require("tty"), 1);
5857
- function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : import_node_process2.default.argv) {
5740
+ function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : import_node_process.default.argv) {
5858
5741
  const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
5859
5742
  const position = argv.indexOf(prefix + flag);
5860
5743
  const terminatorPosition = argv.indexOf("--");
5861
5744
  return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
5862
5745
  }
5863
- var { env } = import_node_process2.default;
5746
+ var { env } = import_node_process.default;
5864
5747
  var flagForceColor;
5865
5748
  if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
5866
5749
  flagForceColor = 0;
@@ -5916,7 +5799,7 @@ function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
5916
5799
  if (env.TERM === "dumb") {
5917
5800
  return min;
5918
5801
  }
5919
- if (import_node_process2.default.platform === "win32") {
5802
+ if (import_node_process.default.platform === "win32") {
5920
5803
  const osRelease = import_node_os.default.release().split(".");
5921
5804
  if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
5922
5805
  return Number(osRelease[2]) >= 14931 ? 3 : 2;
@@ -6174,17 +6057,19 @@ var Log = class {
6174
6057
  };
6175
6058
 
6176
6059
  // src/core/compress.ts
6177
- async function createCompress(options) {
6178
- const { outDir, ignoreBase } = options;
6179
- const zipFilePath = import_node_path2.default.resolve(import_node_process3.default.cwd(), `${import_node_path2.default.basename(outDir)}.zip`);
6180
- Log.log("Compressing the directory", outDir);
6181
- return new Promise((resolve2, reject) => {
6060
+ async function createCompress(options, outDir2) {
6061
+ const { ignoreBase } = options;
6062
+ const zipFilePath = import_node_path.default.resolve(import_node_process2.default.cwd(), `${import_node_path.default.basename(outDir2)}.zip`);
6063
+ Log.log("Compressing the directory", outDir2);
6064
+ return new Promise((resolve, reject) => {
6182
6065
  try {
6183
- const output = import_node_fs2.default.createWriteStream(zipFilePath);
6066
+ const output = import_node_fs.default.createWriteStream(zipFilePath);
6184
6067
  const archive = (0, import_archiver.default)("zip");
6185
6068
  output.on("close", () => {
6186
- Log.success("Successfully compressed to", `${zipFilePath} (${archive.pointer() / 1024 / 1024} MB)`);
6187
- resolve2(null);
6069
+ const size = archive.pointer();
6070
+ const sizeInMB = size > 1024 * 1024 ? `${(size / 1024 / 1024).toFixed(1)} MB` : `${(size / 1024).toFixed(1)} KB`;
6071
+ Log.success("Successfully compressed to", `${zipFilePath} (${sizeInMB})`);
6072
+ resolve(null);
6188
6073
  });
6189
6074
  archive.on("warning", (err) => {
6190
6075
  if (err.code === "ENOENT")
@@ -6197,8 +6082,8 @@ async function createCompress(options) {
6197
6082
  reject(err);
6198
6083
  });
6199
6084
  archive.pipe(output);
6200
- const absoluteOutDir = import_node_path2.default.resolve(outDir);
6201
- archive.directory(absoluteOutDir, ignoreBase ? false : import_node_path2.default.basename(outDir));
6085
+ const absoluteOutDir = import_node_path.default.resolve(outDir2);
6086
+ archive.directory(absoluteOutDir, ignoreBase ? false : import_node_path.default.basename(outDir2));
6202
6087
  archive.finalize();
6203
6088
  } catch (error) {
6204
6089
  Log.error("Error compressing the directory", error);
@@ -6207,10 +6092,128 @@ async function createCompress(options) {
6207
6092
  });
6208
6093
  }
6209
6094
 
6095
+ // src/core/generate.ts
6096
+ var import_node_fs2 = require("fs");
6097
+ var import_node_path2 = __toESM(require("path"), 1);
6098
+ var import_node_process3 = __toESM(require("process"), 1);
6099
+ var import_fast_glob = __toESM(require_out4(), 1);
6100
+ var import_local_pkg = require("local-pkg");
6101
+ var import_recast = __toESM(require("recast"), 1);
6102
+ function mergeObjects(prodObj, devObj) {
6103
+ const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
6104
+ for (const prop of devObj.properties) {
6105
+ const key = prop.key.name || prop.key.value;
6106
+ if (prodProps.has(key)) {
6107
+ const index = prodObj.properties.indexOf(prodProps.get(key));
6108
+ if (index >= 0)
6109
+ prodObj.properties[index] = prop;
6110
+ } else {
6111
+ prodObj.properties.push(prop);
6112
+ }
6113
+ }
6114
+ }
6115
+ async function generateScript(options, mode, base2) {
6116
+ const { dir, fileName, globalName, serve, build } = options.env;
6117
+ const folder = await findFolder(import_node_process3.default.cwd(), dir);
6118
+ const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
6119
+ absolute: true,
6120
+ cwd: folder
6121
+ });
6122
+ const testReg = mode === "serve" ? serve : build;
6123
+ let target = "";
6124
+ let source = "";
6125
+ let code = "";
6126
+ const name = fileName;
6127
+ for (const file of files) {
6128
+ try {
6129
+ const mod = await import_node_fs2.promises.readFile(file, "utf-8");
6130
+ if (testReg?.test(file))
6131
+ target = mod;
6132
+ else
6133
+ source = mod;
6134
+ } catch (error) {
6135
+ console.error(`Error loading file ${file}:`, error);
6136
+ }
6137
+ }
6138
+ const targetAst = import_recast.default.parse(target);
6139
+ const sourceAst = import_recast.default.parse(source);
6140
+ const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6141
+ const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6142
+ mergeObjects(sourceExport, targetExport);
6143
+ const mergedCode = import_recast.default.print(sourceExport).code;
6144
+ const returnedTarget = mergedCode;
6145
+ const versionInfo = await generateVersion(options, mode);
6146
+ code = `window.${globalName}=${returnedTarget};
6147
+ ${versionInfo}`;
6148
+ const formatCode = code;
6149
+ return {
6150
+ code,
6151
+ script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
6152
+ </head>`,
6153
+ emit: {
6154
+ type: "asset",
6155
+ fileName: name,
6156
+ source: formatCode
6157
+ },
6158
+ watchFiles: files
6159
+ };
6160
+ }
6161
+ async function generateVersion(options, mode) {
6162
+ const pkg = await (0, import_local_pkg.getPackageInfo)(import_node_process3.default.cwd());
6163
+ return `console.info("Version: %c${pkg?.version}%c - ${mode === "serve" ? "runtime" : "built"} on %c${options.datetime}%c", "color: green;", '', "color: blue;", '')`;
6164
+ }
6165
+ async function findFolder(directoryPath, dir) {
6166
+ const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
6167
+ const files = await import_node_fs2.promises.readdir(directoryPath);
6168
+ const filePaths = files.filter((item) => !ignore.has(item));
6169
+ let nestedFolder = "";
6170
+ for (const file of filePaths) {
6171
+ const fullFilePath = import_node_path2.default.join(directoryPath, file);
6172
+ const stat = await import_node_fs2.promises.stat(fullFilePath);
6173
+ if (stat.isDirectory()) {
6174
+ if (file.toLowerCase() === dir) {
6175
+ return fullFilePath;
6176
+ } else {
6177
+ nestedFolder = await findFolder(fullFilePath, dir);
6178
+ if (nestedFolder)
6179
+ return nestedFolder;
6180
+ }
6181
+ }
6182
+ }
6183
+ return "";
6184
+ }
6185
+
6186
+ // src/core/options.ts
6187
+ var import_utils = require("@antfu/utils");
6188
+ function resolveOptions(options) {
6189
+ const defaults = {
6190
+ env: {
6191
+ dir: "config",
6192
+ fileName: "manifest.js",
6193
+ globalName: "manifest",
6194
+ serve: /dev/i,
6195
+ build: /prod/i
6196
+ },
6197
+ compress: {
6198
+ ignoreBase: false
6199
+ }
6200
+ };
6201
+ const mergeOptions = (0, import_utils.deepMerge)(defaults, options);
6202
+ const formatter = new Intl.DateTimeFormat("zh-CN", {
6203
+ dateStyle: "full",
6204
+ timeStyle: "medium"
6205
+ });
6206
+ return {
6207
+ ...mergeOptions,
6208
+ datetime: formatter.format(/* @__PURE__ */ new Date())
6209
+ };
6210
+ }
6211
+
6210
6212
  // src/index.ts
6211
6213
  var virtualEnvId = "virtual:env";
6212
6214
  var resolvedVirtualEnvId = `\0${virtualEnvId}`;
6213
6215
  var base = "";
6216
+ var outDir = "";
6214
6217
  var unpluginFactory = (options = {}) => {
6215
6218
  const resolved = resolveOptions(options);
6216
6219
  return [{
@@ -6218,6 +6221,7 @@ var unpluginFactory = (options = {}) => {
6218
6221
  apply: "serve",
6219
6222
  enforce: "post",
6220
6223
  configResolved(config) {
6224
+ outDir = config.build.outDir;
6221
6225
  base = config.base;
6222
6226
  },
6223
6227
  async resolveId(id) {
@@ -6239,6 +6243,7 @@ var unpluginFactory = (options = {}) => {
6239
6243
  enforce: "post",
6240
6244
  configResolved(config) {
6241
6245
  base = config.base;
6246
+ outDir = config.build.outDir;
6242
6247
  },
6243
6248
  resolveId(id) {
6244
6249
  if (id.startsWith(virtualEnvId))
@@ -6262,7 +6267,7 @@ var unpluginFactory = (options = {}) => {
6262
6267
  buildEnd: () => {
6263
6268
  import_node_process4.default.on("beforeExit", async () => {
6264
6269
  const { compress } = resolved;
6265
- await createCompress(compress);
6270
+ await createCompress(compress, outDir);
6266
6271
  import_node_process4.default.exit(0);
6267
6272
  });
6268
6273
  }
package/dist/webpack.js CHANGED
@@ -5,8 +5,8 @@
5
5
 
6
6
  import {
7
7
  webpack_default
8
- } from "./chunk-OQXUTP45.js";
9
- import "./chunk-U7BMFDAG.js";
8
+ } from "./chunk-664Z6SFJ.js";
9
+ import "./chunk-V2T5VQQ7.js";
10
10
  export {
11
11
  webpack_default as default
12
12
  };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "unplugin-env",
3
3
  "type": "module",
4
- "version": "1.0.0",
5
- "packageManager": "pnpm@7.1.1",
4
+ "version": "1.0.2",
5
+ "packageManager": "pnpm@9.15.4",
6
6
  "description": "Register global imports on demand for Vite and Webpack",
7
7
  "license": "MIT",
8
8
  "homepage": "https://github.com/nikkolast88/unplugin-env#readme",
@@ -72,6 +72,15 @@
72
72
  "files": [
73
73
  "dist"
74
74
  ],
75
+ "engines": {
76
+ "node": "^20.19.0 || >=22.12.0",
77
+ "pnpm": ">=9.0.0"
78
+ },
79
+ "browserslist": [
80
+ "defaults",
81
+ "not IE 11",
82
+ "maintained node versions"
83
+ ],
75
84
  "scripts": {
76
85
  "build": "tsup",
77
86
  "dev": "tsup --watch src",
@@ -110,30 +119,29 @@
110
119
  }
111
120
  },
112
121
  "dependencies": {
113
- "@antfu/utils": "^0.7.8",
122
+ "@antfu/utils": "^9.3.0",
114
123
  "archiver": "^7.0.1",
115
- "local-pkg": "^0.5.0",
124
+ "local-pkg": "^1.1.2",
116
125
  "recast": "^0.23.11",
117
- "unplugin": "^1.10.1"
126
+ "unplugin": "^2.3.10"
118
127
  },
119
128
  "devDependencies": {
120
- "@antfu/eslint-config": "^2.17.0",
121
- "@nuxt/kit": "^3.11.2",
122
- "@nuxt/schema": "^3.11.2",
123
- "@types/archiver": "^6.0.2",
124
- "@types/node": "^20.12.11",
125
- "bumpp": "^9.4.1",
126
- "chalk": "^5.3.0",
127
- "eslint": "^8.57.0",
128
- "esno": "^4.7.0",
129
- "fast-glob": "^3.3.2",
130
- "nodemon": "^3.1.0",
131
- "rimraf": "^5.0.5",
132
- "rollup": "^4.17.2",
133
- "tsup": "^8.0.2",
134
- "typescript": "^4.9.5",
135
- "vite": "^5.2.11",
136
- "vitest": "^1.6.0",
137
- "webpack": "^5.91.0"
129
+ "@antfu/eslint-config": "^6.2.0",
130
+ "@nuxt/kit": "^4.2.0",
131
+ "@nuxt/schema": "^4.2.0",
132
+ "@types/archiver": "^7.0.0",
133
+ "@types/node": "^24.10.0",
134
+ "bumpp": "^10.3.1",
135
+ "chalk": "^5.6.2",
136
+ "eslint": "^9.39.0",
137
+ "esno": "^4.8.0",
138
+ "fast-glob": "^3.3.3",
139
+ "nodemon": "^3.1.10",
140
+ "rollup": "^4.52.5",
141
+ "tsup": "^8.5.0",
142
+ "typescript": "^5.9.3",
143
+ "vite": "^7.1.12",
144
+ "vitest": "^4.0.6",
145
+ "webpack": "^5.102.1"
138
146
  }
139
147
  }