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.
@@ -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
  };
@@ -5533,127 +5533,10 @@ import { promises as fs3 } from "fs";
5533
5533
  import process5 from "process";
5534
5534
  import { createUnplugin } from "unplugin";
5535
5535
 
5536
- // src/core/generate.ts
5537
- var import_fast_glob = __toESM(require_out4(), 1);
5538
- import path, { resolve } from "path";
5539
- import { promises as fs } from "fs";
5540
- import process2 from "process";
5541
- import recast from "recast";
5542
- function mergeObjects(prodObj, devObj) {
5543
- const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
5544
- for (const prop of devObj.properties) {
5545
- const key = prop.key.name || prop.key.value;
5546
- if (prodProps.has(key)) {
5547
- const index = prodObj.properties.indexOf(prodProps.get(key));
5548
- if (index >= 0)
5549
- prodObj.properties[index] = prop;
5550
- } else {
5551
- prodObj.properties.push(prop);
5552
- }
5553
- }
5554
- }
5555
- async function generateScript(options, mode, base2) {
5556
- const { dir, fileName, globalName, serve, build } = options.env;
5557
- const folder = await findFolder(process2.cwd(), dir);
5558
- const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
5559
- absolute: true,
5560
- cwd: folder
5561
- });
5562
- const testReg = mode === "serve" ? serve : build;
5563
- let target = "";
5564
- let source = "";
5565
- let code = "";
5566
- const name = fileName;
5567
- for (const file of files) {
5568
- try {
5569
- const mod = await fs.readFile(file, "utf-8");
5570
- if (testReg == null ? void 0 : testReg.test(file))
5571
- target = mod;
5572
- else
5573
- source = mod;
5574
- } catch (error) {
5575
- console.error(`Error loading file ${file}:`, error);
5576
- }
5577
- }
5578
- const targetAst = recast.parse(target);
5579
- const sourceAst = recast.parse(source);
5580
- const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5581
- const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
5582
- mergeObjects(sourceExport, targetExport);
5583
- const mergedCode = recast.print(sourceExport).code;
5584
- const returnedTarget = mergedCode;
5585
- const versionInfo = await generateVersion(options, mode);
5586
- code = `window.${globalName}=${returnedTarget};${versionInfo}`;
5587
- const formatCode = code;
5588
- return {
5589
- code,
5590
- script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
5591
- </head>`,
5592
- emit: {
5593
- type: "asset",
5594
- fileName: name,
5595
- source: formatCode
5596
- },
5597
- watchFiles: files
5598
- };
5599
- }
5600
- async function generateVersion(options, mode) {
5601
- const packageFile = await (0, import_fast_glob.default)("package.json", {
5602
- absolute: true,
5603
- cwd: resolve(process2.cwd())
5604
- });
5605
- const packageString = await fs.readFile(packageFile[0], "utf8");
5606
- const packageJson = JSON.parse(packageString);
5607
- return `console.info("Version: ${packageJson.version} - ${mode === "serve" ? "runtime" : "built"} on ${options.date}")`;
5608
- }
5609
- async function findFolder(directoryPath, dir) {
5610
- const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
5611
- const files = await fs.readdir(directoryPath);
5612
- const filePaths = files.filter((item) => !ignore.has(item));
5613
- let nestedFolder = "";
5614
- for (const file of filePaths) {
5615
- const fullFilePath = path.join(directoryPath, file);
5616
- const stat = await fs.stat(fullFilePath);
5617
- if (stat.isDirectory()) {
5618
- if (file.toLowerCase() === dir) {
5619
- return fullFilePath;
5620
- } else {
5621
- nestedFolder = await findFolder(fullFilePath, dir);
5622
- if (nestedFolder)
5623
- return nestedFolder;
5624
- }
5625
- }
5626
- }
5627
- return "";
5628
- }
5629
-
5630
- // src/core/options.ts
5631
- import { deepMerge } from "@antfu/utils";
5632
- function resolveOptions(options) {
5633
- const defaults = {
5634
- env: {
5635
- dir: "config",
5636
- fileName: "manifest.js",
5637
- globalName: "manifest",
5638
- serve: /dev|development/i,
5639
- build: /prod|production/i
5640
- },
5641
- compress: {
5642
- outDir: "dist",
5643
- ignoreBase: true
5644
- }
5645
- };
5646
- const mergeOptions = deepMerge(defaults, options);
5647
- return {
5648
- ...mergeOptions,
5649
- date: (/* @__PURE__ */ new Date()).toLocaleString()
5650
- };
5651
- }
5652
-
5653
5536
  // src/core/compress.ts
5654
- import fs2 from "fs";
5655
- import path2 from "path";
5656
- import process4 from "process";
5537
+ import fs from "fs";
5538
+ import path from "path";
5539
+ import process3 from "process";
5657
5540
  import archiver from "archiver";
5658
5541
 
5659
5542
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/ansi-styles/index.js
@@ -5843,16 +5726,16 @@ var ansiStyles = assembleStyles();
5843
5726
  var ansi_styles_default = ansiStyles;
5844
5727
 
5845
5728
  // node_modules/.pnpm/chalk@5.6.2/node_modules/chalk/source/vendor/supports-color/index.js
5846
- import process3 from "process";
5729
+ import process2 from "process";
5847
5730
  import os from "os";
5848
5731
  import tty from "tty";
5849
- function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process3.argv) {
5732
+ function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process2.argv) {
5850
5733
  const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
5851
5734
  const position = argv.indexOf(prefix + flag);
5852
5735
  const terminatorPosition = argv.indexOf("--");
5853
5736
  return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
5854
5737
  }
5855
- var { env } = process3;
5738
+ var { env } = process2;
5856
5739
  var flagForceColor;
5857
5740
  if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
5858
5741
  flagForceColor = 0;
@@ -5908,7 +5791,7 @@ function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
5908
5791
  if (env.TERM === "dumb") {
5909
5792
  return min;
5910
5793
  }
5911
- if (process3.platform === "win32") {
5794
+ if (process2.platform === "win32") {
5912
5795
  const osRelease = os.release().split(".");
5913
5796
  if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
5914
5797
  return Number(osRelease[2]) >= 14931 ? 3 : 2;
@@ -6166,17 +6049,19 @@ var Log = class {
6166
6049
  };
6167
6050
 
6168
6051
  // src/core/compress.ts
6169
- async function createCompress(options) {
6170
- const { outDir, ignoreBase } = options;
6171
- const zipFilePath = path2.resolve(process4.cwd(), `${path2.basename(outDir)}.zip`);
6172
- Log.log("Compressing the directory", outDir);
6173
- return new Promise((resolve2, reject) => {
6052
+ async function createCompress(options, outDir2) {
6053
+ const { ignoreBase } = options;
6054
+ const zipFilePath = path.resolve(process3.cwd(), `${path.basename(outDir2)}.zip`);
6055
+ Log.log("Compressing the directory", outDir2);
6056
+ return new Promise((resolve, reject) => {
6174
6057
  try {
6175
- const output = fs2.createWriteStream(zipFilePath);
6058
+ const output = fs.createWriteStream(zipFilePath);
6176
6059
  const archive = archiver("zip");
6177
6060
  output.on("close", () => {
6178
- Log.success("Successfully compressed to", `${zipFilePath} (${archive.pointer() / 1024 / 1024} MB)`);
6179
- resolve2(null);
6061
+ const size = archive.pointer();
6062
+ const sizeInMB = size > 1024 * 1024 ? `${(size / 1024 / 1024).toFixed(1)} MB` : `${(size / 1024).toFixed(1)} KB`;
6063
+ Log.success("Successfully compressed to", `${zipFilePath} (${sizeInMB})`);
6064
+ resolve(null);
6180
6065
  });
6181
6066
  archive.on("warning", (err) => {
6182
6067
  if (err.code === "ENOENT")
@@ -6189,8 +6074,8 @@ async function createCompress(options) {
6189
6074
  reject(err);
6190
6075
  });
6191
6076
  archive.pipe(output);
6192
- const absoluteOutDir = path2.resolve(outDir);
6193
- archive.directory(absoluteOutDir, ignoreBase ? false : path2.basename(outDir));
6077
+ const absoluteOutDir = path.resolve(outDir2);
6078
+ archive.directory(absoluteOutDir, ignoreBase ? false : path.basename(outDir2));
6194
6079
  archive.finalize();
6195
6080
  } catch (error) {
6196
6081
  Log.error("Error compressing the directory", error);
@@ -6199,10 +6084,128 @@ async function createCompress(options) {
6199
6084
  });
6200
6085
  }
6201
6086
 
6087
+ // src/core/generate.ts
6088
+ var import_fast_glob = __toESM(require_out4(), 1);
6089
+ import { promises as fs2 } from "fs";
6090
+ import path2 from "path";
6091
+ import process4 from "process";
6092
+ import { getPackageInfo } from "local-pkg";
6093
+ import recast from "recast";
6094
+ function mergeObjects(prodObj, devObj) {
6095
+ const prodProps = new Map(prodObj.properties.map((p) => [p.key.name || p.key.value, p]));
6096
+ for (const prop of devObj.properties) {
6097
+ const key = prop.key.name || prop.key.value;
6098
+ if (prodProps.has(key)) {
6099
+ const index = prodObj.properties.indexOf(prodProps.get(key));
6100
+ if (index >= 0)
6101
+ prodObj.properties[index] = prop;
6102
+ } else {
6103
+ prodObj.properties.push(prop);
6104
+ }
6105
+ }
6106
+ }
6107
+ async function generateScript(options, mode, base2) {
6108
+ const { dir, fileName, globalName, serve, build } = options.env;
6109
+ const folder = await findFolder(process4.cwd(), dir);
6110
+ const files = await (0, import_fast_glob.default)("*.+(js|ts)", {
6111
+ absolute: true,
6112
+ cwd: folder
6113
+ });
6114
+ const testReg = mode === "serve" ? serve : build;
6115
+ let target = "";
6116
+ let source = "";
6117
+ let code = "";
6118
+ const name = fileName;
6119
+ for (const file of files) {
6120
+ try {
6121
+ const mod = await fs2.readFile(file, "utf-8");
6122
+ if (testReg?.test(file))
6123
+ target = mod;
6124
+ else
6125
+ source = mod;
6126
+ } catch (error) {
6127
+ console.error(`Error loading file ${file}:`, error);
6128
+ }
6129
+ }
6130
+ const targetAst = recast.parse(target);
6131
+ const sourceAst = recast.parse(source);
6132
+ const targetExport = targetAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6133
+ const sourceExport = sourceAst.program.body.find((n) => n.type === "ExportDefaultDeclaration").declaration;
6134
+ mergeObjects(sourceExport, targetExport);
6135
+ const mergedCode = recast.print(sourceExport).code;
6136
+ const returnedTarget = mergedCode;
6137
+ const versionInfo = await generateVersion(options, mode);
6138
+ code = `window.${globalName}=${returnedTarget};
6139
+ ${versionInfo}`;
6140
+ const formatCode = code;
6141
+ return {
6142
+ code,
6143
+ script: ` <script type="text/javascript" src="${base2}${fileName}"></script>
6144
+ </head>`,
6145
+ emit: {
6146
+ type: "asset",
6147
+ fileName: name,
6148
+ source: formatCode
6149
+ },
6150
+ watchFiles: files
6151
+ };
6152
+ }
6153
+ async function generateVersion(options, mode) {
6154
+ const pkg = await getPackageInfo(process4.cwd());
6155
+ return `console.info("Version: %c${pkg?.version}%c - ${mode === "serve" ? "runtime" : "built"} on %c${options.datetime}%c", "color: green;", '', "color: blue;", '')`;
6156
+ }
6157
+ async function findFolder(directoryPath, dir) {
6158
+ const ignore = /* @__PURE__ */ new Set(["dist", "node_modules", "playground", "example", "test", "jest", "tests", "locales", "public", ".git", ".github", ".vscode"]);
6159
+ const files = await fs2.readdir(directoryPath);
6160
+ const filePaths = files.filter((item) => !ignore.has(item));
6161
+ let nestedFolder = "";
6162
+ for (const file of filePaths) {
6163
+ const fullFilePath = path2.join(directoryPath, file);
6164
+ const stat = await fs2.stat(fullFilePath);
6165
+ if (stat.isDirectory()) {
6166
+ if (file.toLowerCase() === dir) {
6167
+ return fullFilePath;
6168
+ } else {
6169
+ nestedFolder = await findFolder(fullFilePath, dir);
6170
+ if (nestedFolder)
6171
+ return nestedFolder;
6172
+ }
6173
+ }
6174
+ }
6175
+ return "";
6176
+ }
6177
+
6178
+ // src/core/options.ts
6179
+ import { deepMerge } from "@antfu/utils";
6180
+ function resolveOptions(options) {
6181
+ const defaults = {
6182
+ env: {
6183
+ dir: "config",
6184
+ fileName: "manifest.js",
6185
+ globalName: "manifest",
6186
+ serve: /dev/i,
6187
+ build: /prod/i
6188
+ },
6189
+ compress: {
6190
+ ignoreBase: false
6191
+ }
6192
+ };
6193
+ const mergeOptions = deepMerge(defaults, options);
6194
+ const formatter = new Intl.DateTimeFormat("zh-CN", {
6195
+ dateStyle: "full",
6196
+ timeStyle: "medium"
6197
+ });
6198
+ return {
6199
+ ...mergeOptions,
6200
+ datetime: formatter.format(/* @__PURE__ */ new Date())
6201
+ };
6202
+ }
6203
+
6202
6204
  // src/index.ts
6203
6205
  var virtualEnvId = "virtual:env";
6204
6206
  var resolvedVirtualEnvId = `\0${virtualEnvId}`;
6205
6207
  var base = "";
6208
+ var outDir = "";
6206
6209
  var unpluginFactory = (options = {}) => {
6207
6210
  const resolved = resolveOptions(options);
6208
6211
  return [{
@@ -6210,6 +6213,7 @@ var unpluginFactory = (options = {}) => {
6210
6213
  apply: "serve",
6211
6214
  enforce: "post",
6212
6215
  configResolved(config) {
6216
+ outDir = config.build.outDir;
6213
6217
  base = config.base;
6214
6218
  },
6215
6219
  async resolveId(id) {
@@ -6231,6 +6235,7 @@ var unpluginFactory = (options = {}) => {
6231
6235
  enforce: "post",
6232
6236
  configResolved(config) {
6233
6237
  base = config.base;
6238
+ outDir = config.build.outDir;
6234
6239
  },
6235
6240
  resolveId(id) {
6236
6241
  if (id.startsWith(virtualEnvId))
@@ -6254,7 +6259,7 @@ var unpluginFactory = (options = {}) => {
6254
6259
  buildEnd: () => {
6255
6260
  process5.on("beforeExit", async () => {
6256
6261
  const { compress } = resolved;
6257
- await createCompress(compress);
6262
+ await createCompress(compress, outDir);
6258
6263
  process5.exit(0);
6259
6264
  });
6260
6265
  }