purus 0.6.1 → 0.8.0

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/lib/purus-core.js CHANGED
@@ -22,7 +22,7 @@ const VERSION = require("../package.json").version;
22
22
  * @returns {string} Generated JavaScript code
23
23
  */
24
24
  function compile(source, options = {}) {
25
- const { header = true, strict = true } = options;
25
+ const { header = true, strict = true, type: moduleType } = options;
26
26
  const tmpFile = path.join(
27
27
  require("os").tmpdir(),
28
28
  `purus_${Date.now()}_${Math.random().toString(36).slice(2)}.purus`
@@ -34,6 +34,9 @@ function compile(source, options = {}) {
34
34
  if (!strict) {
35
35
  args.push("--strict", "false");
36
36
  }
37
+ if (moduleType === "commonjs") {
38
+ args.push("--type", "commonjs");
39
+ }
37
40
  args.push(tmpFile);
38
41
  const result = execFileSync(process.execPath, [COMPILER_JS, ...args], {
39
42
  encoding: "utf8",
@@ -11,6 +11,7 @@ const args = process.argv.slice(3);
11
11
  let entry = null;
12
12
  let noHeader = false;
13
13
  let strict = null;
14
+ let moduleType = null;
14
15
 
15
16
  for (let i = 0; i < args.length; i++) {
16
17
  if (args[i] === "--no-header") {
@@ -21,6 +22,10 @@ for (let i = 0; i < args.length; i++) {
21
22
  } else {
22
23
  strict = true;
23
24
  }
25
+ } else if (args[i] === "--type" || args[i] === "-t") {
26
+ if (i + 1 < args.length) {
27
+ moduleType = args[++i];
28
+ }
24
29
  } else if (args[i] === "--entry" || args[i] === "-e") {
25
30
  entry = args[++i];
26
31
  } else if (!args[i].startsWith("-")) {
@@ -28,11 +33,34 @@ for (let i = 0; i < args.length; i++) {
28
33
  }
29
34
  }
30
35
 
36
+ function resolveModuleType(filePath, cliModuleType) {
37
+ if (filePath && filePath.endsWith(".cpurus")) return "commonjs";
38
+ if (filePath && filePath.endsWith(".mpurus")) return "module";
39
+ if (cliModuleType) return cliModuleType;
40
+
41
+ const configResult = loadConfig();
42
+ if (configResult && configResult.config.type) return configResult.config.type;
43
+
44
+ try {
45
+ const pkgDir = configResult ? configResult.configDir : process.cwd();
46
+ const pkgPath = path.join(pkgDir, "package.json");
47
+ if (fs.existsSync(pkgPath)) {
48
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
49
+ if (pkg.type) return pkg.type;
50
+ }
51
+ } catch {
52
+ // ignore
53
+ }
54
+
55
+ return "module";
56
+ }
57
+
31
58
  if (entry && fs.existsSync(entry) && fs.statSync(entry).isFile()) {
32
59
  // Single file - compile and run
33
60
  const source = fs.readFileSync(entry, "utf8");
34
61
  const useStrict = strict !== null ? strict : true;
35
- const js = compile(source, { header: false, strict: useStrict });
62
+ const resolvedModule = resolveModuleType(entry, moduleType);
63
+ const js = compile(source, { header: false, strict: useStrict, module: resolvedModule });
36
64
  const m = new (require("module"))();
37
65
  m._compile(js, entry);
38
66
  } else {
@@ -81,7 +109,8 @@ if (entry && fs.existsSync(entry) && fs.statSync(entry).isFile()) {
81
109
  for (const f of files) {
82
110
  const source = fs.readFileSync(f, "utf8");
83
111
  const useStrict2 = strict !== null ? strict : true;
84
- const js = compile(source, { header: false, strict: useStrict2 });
112
+ const resolvedModule = resolveModuleType(f, moduleType);
113
+ const js = compile(source, { header: false, strict: useStrict2, module: resolvedModule });
85
114
 
86
115
  const tmpFile = path.join(
87
116
  require("os").tmpdir(),
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "purus",
3
- "version": "0.6.1",
3
+ "version": "0.8.0",
4
4
  "description": "A language that compiles to JavaScript — no Shift key required",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
7
+ "types": "index.d.ts",
7
8
  "exports": {
8
9
  ".": {
10
+ "types": "./index.d.ts",
9
11
  "import": "./index.mjs",
10
12
  "require": "./index.js"
11
13
  }
@@ -16,6 +18,7 @@
16
18
  "files": [
17
19
  "index.js",
18
20
  "index.mjs",
21
+ "index.d.ts",
19
22
  "bin/",
20
23
  "lib/",
21
24
  "LICENSE",