@danielx/civet 0.6.26 → 0.6.28

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.
@@ -1,124 +1,109 @@
1
- /**
2
- @file esbuild plugin for Civet language
3
-
4
- Simple zero config example
5
-
6
- @example
7
- ```javascript
8
- import esbuild from 'esbuild'
9
- import civetPlugin from '@danielx/civet/esbuild-plugin'
10
-
11
- esbuild.build({
12
- ...,
13
- plugins: [
14
- civetPlugin
15
- ]
16
- }).catch(() => process.exit(1))
17
- ```
18
-
19
- Chain civet output into another esbuild plugin, solid for example
20
-
21
- @example
22
- ```javascript
23
- import esbuild from 'esbuild'
24
- const { solidPlugin } = require('esbuild-plugin-solid');
25
- import civetPlugin from '@danielx/civet/esbuild-plugin'
26
-
27
- esbuild.build({
28
- ...,
29
- plugins: [
30
- civetPlugin(
31
- next: solidPlugin()
32
- )
33
- ]
34
- }).catch(() => process.exit(1))
35
- ```
36
- */
37
-
38
-
39
-
40
- ;
41
-
42
- const { readFile, writeFile, mkdtemp, rmdir } = require('fs/promises')
43
- const path = require('path');
44
-
45
- // NOTE: this references the built version of the module, not the source
46
- const { compile } = require("../dist/main.js")
47
-
48
- // NOTE: this function is named civet so esbuild gets "civet" as the name of the plugin
49
- function civet (options = {}) {
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+
26
+ // source/esbuild-plugin.civet
27
+ var esbuild_plugin_exports = {};
28
+ __export(esbuild_plugin_exports, {
29
+ default: () => esbuild_plugin_default
30
+ });
31
+ module.exports = __toCommonJS(esbuild_plugin_exports);
32
+ var import_promises = require("fs/promises");
33
+ var import_path = __toESM(require("path"));
34
+ var import_main = require("./main.js");
35
+ function civet(options = {}) {
50
36
  const {
51
- filter=/\.civet$/,
52
- inlineMap=true,
53
- js=true,
37
+ filter = /\.civet$/,
38
+ inlineMap = true,
39
+ js = true,
54
40
  next
55
- } = options
56
-
57
- let nextTransform
58
- let tmpPath
59
-
41
+ } = options;
42
+ let nextTransform;
43
+ let tmpPath;
60
44
  if (next) {
61
45
  next.setup({
62
- onEnd() {; },
63
- onStart() {; },
64
- resolve() {; },
65
- onResolve() {; },
66
- initialOptions() {; },
67
- esbuild() {; },
46
+ onEnd() {
47
+ ;
48
+ },
49
+ onStart() {
50
+ ;
51
+ },
52
+ resolve() {
53
+ ;
54
+ },
55
+ onResolve() {
56
+ ;
57
+ },
58
+ initialOptions() {
59
+ ;
60
+ },
61
+ esbuild() {
62
+ ;
63
+ },
68
64
  onLoad(_, handler) {
69
- return nextTransform = handler
65
+ return nextTransform = handler;
70
66
  }
71
- })
67
+ });
72
68
  }
73
-
74
69
  return {
75
70
  name: "civet",
76
-
77
71
  setup(build) {
78
-
79
72
  build.onStart(async function() {
80
73
  if (next) {
81
- const { tmpdir } = require('os')
82
- tmpPath = await mkdtemp(path.join(tmpdir(), "civet-"))
74
+ const { tmpdir } = require("os");
75
+ tmpPath = await (0, import_promises.mkdtemp)(import_path.default.join(tmpdir(), "civet-"));
83
76
  }
84
- return
85
- })
86
-
77
+ return;
78
+ });
87
79
  build.onEnd(async function() {
88
80
  if (tmpPath) {
89
- await rmdir(tmpPath, { recursive: true })
81
+ await (0, import_promises.rmdir)(tmpPath, { recursive: true });
90
82
  }
91
- return
92
- })
93
-
83
+ return;
84
+ });
94
85
  return build.onLoad({ filter }, async function(args) {
95
86
  try {
96
- const source = await readFile(args.path, 'utf8')
97
- const filename = path.relative(process.cwd(), args.path)
98
- const compiled = compile(source, {
87
+ const source = await (0, import_promises.readFile)(args.path, "utf8");
88
+ const filename = import_path.default.relative(process.cwd(), args.path);
89
+ const compiled = (0, import_main.compile)(source, {
99
90
  filename,
100
91
  inlineMap,
101
92
  js
102
- })
103
-
93
+ });
104
94
  if (next && tmpPath) {
105
- const outputFileName = filename + js ? '.jsx' : '.tsx'
106
- const outputFilePath = path.join(tmpPath, outputFileName)
107
-
108
- // I'd prefer not to use temp files but I can't find a way to pass a stream to fs.readFile which is what
109
- // most esbuild plugins use
110
- await writeFile(outputFilePath, compiled)
95
+ const outputFileName = filename + js ? ".jsx" : ".tsx";
96
+ const outputFilePath = import_path.default.join(tmpPath, outputFileName);
97
+ await (0, import_promises.writeFile)(outputFilePath, compiled);
111
98
  return await nextTransform({
112
99
  ...args,
113
100
  path: outputFilePath
114
- })
101
+ });
115
102
  }
116
-
117
103
  return {
118
- contents: compiled,
119
- }
120
- }
121
- catch (e) {
104
+ contents: compiled
105
+ };
106
+ } catch (e) {
122
107
  return {
123
108
  errors: [{
124
109
  text: e.message,
@@ -126,20 +111,18 @@ function civet (options = {}) {
126
111
  file: args.path,
127
112
  namespace: args.namespace,
128
113
  line: e.line,
129
- column: e.column,
114
+ column: e.column
130
115
  },
131
116
  detail: e
132
117
  }]
133
- }
118
+ };
134
119
  }
135
- })
120
+ });
136
121
  }
137
- }
122
+ };
138
123
  }
139
-
140
- const defaultPlugin = civet()
141
-
142
- // Default zero-config plugin
143
- civet.setup = defaultPlugin.setup
144
-
145
- module.exports = civet
124
+ var defaultPlugin = civet();
125
+ civet.setup = defaultPlugin.setup;
126
+ var esbuild_plugin_default = civet;
127
+ // Annotate the CommonJS export names for ESM import in node:
128
+ 0 && (module.exports = {});
@@ -0,0 +1,7 @@
1
+ import * as esbuild from 'esbuild';
2
+ import { PluginOptions } from './unplugin.mjs';
3
+ import 'unplugin';
4
+
5
+ declare const _default: (options?: PluginOptions | undefined) => esbuild.Plugin;
6
+
7
+ export { _default as default };
@@ -0,0 +1,7 @@
1
+ import * as esbuild from 'esbuild';
2
+ import { PluginOptions } from './unplugin.js';
3
+ import 'unplugin';
4
+
5
+ declare const _default: (options?: PluginOptions | undefined) => esbuild.Plugin;
6
+
7
+ export { _default as default };
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/esbuild.ts
31
+ var esbuild_exports = {};
32
+ __export(esbuild_exports, {
33
+ default: () => esbuild_default
34
+ });
35
+ module.exports = __toCommonJS(esbuild_exports);
36
+
37
+ // src/index.ts
38
+ var import_unplugin = require("unplugin");
39
+ var import_civet = __toESM(require("@danielx/civet"));
40
+ var fs = __toESM(require("fs"));
41
+ var import_path = __toESM(require("path"));
42
+ var import_typescript = __toESM(require("typescript"));
43
+ var tsvfs = __toESM(require("@typescript/vfs"));
44
+ var formatHost = {
45
+ getCurrentDirectory: () => import_typescript.default.sys.getCurrentDirectory(),
46
+ getNewLine: () => import_typescript.default.sys.newLine,
47
+ getCanonicalFileName: import_typescript.default.sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
48
+ };
49
+ var isCivet = (id) => /\.civet$/.test(id);
50
+ var isCivetTranspiled = (id) => /\.civet\.(m?)(j|t)s(x?)$/.test(id);
51
+ var civetUnplugin = (0, import_unplugin.createUnplugin)((options = {}) => {
52
+ if (options.dts && options.js) {
53
+ throw new Error("Can't have both `dts` and `js` be set to `true`.");
54
+ }
55
+ const transpileToJS = options.js ?? !options.dts;
56
+ const outExt = options.outputExtension ?? (transpileToJS ? ".jsx" : ".tsx");
57
+ let fsMap = /* @__PURE__ */ new Map();
58
+ let compilerOptions;
59
+ return {
60
+ name: "unplugin-civet",
61
+ enforce: "pre",
62
+ async buildStart() {
63
+ if (options.dts) {
64
+ const configPath = import_typescript.default.findConfigFile(process.cwd(), import_typescript.default.sys.fileExists);
65
+ if (!configPath) {
66
+ throw new Error("Could not find 'tsconfig.json'");
67
+ }
68
+ const { config, error } = import_typescript.default.readConfigFile(
69
+ configPath,
70
+ import_typescript.default.sys.readFile
71
+ );
72
+ if (error) {
73
+ console.error(import_typescript.default.formatDiagnostic(error, formatHost));
74
+ throw error;
75
+ }
76
+ const configContents = import_typescript.default.parseJsonConfigFileContent(
77
+ config,
78
+ import_typescript.default.sys,
79
+ process.cwd()
80
+ );
81
+ compilerOptions = {
82
+ ...configContents.options,
83
+ target: import_typescript.default.ScriptTarget.ESNext
84
+ };
85
+ fsMap = /* @__PURE__ */ new Map();
86
+ }
87
+ },
88
+ buildEnd() {
89
+ if (options.dts) {
90
+ const system = tsvfs.createFSBackedSystem(fsMap, process.cwd(), import_typescript.default);
91
+ const host = tsvfs.createVirtualCompilerHost(
92
+ system,
93
+ compilerOptions,
94
+ import_typescript.default
95
+ );
96
+ const program = import_typescript.default.createProgram({
97
+ rootNames: [...fsMap.keys()],
98
+ options: compilerOptions,
99
+ host: host.compilerHost
100
+ });
101
+ for (const file of fsMap.keys()) {
102
+ const sourceFile = program.getSourceFile(file);
103
+ program.emit(
104
+ sourceFile,
105
+ (filePath, content) => {
106
+ this.emitFile({
107
+ source: content,
108
+ fileName: import_path.default.relative(process.cwd(), filePath),
109
+ type: "asset"
110
+ });
111
+ },
112
+ void 0,
113
+ true
114
+ );
115
+ }
116
+ }
117
+ },
118
+ resolveId(id, importer) {
119
+ if (/\0/.test(id))
120
+ return null;
121
+ if (!isCivet(id))
122
+ return null;
123
+ const relativeId = import_path.default.relative(
124
+ process.cwd(),
125
+ import_path.default.resolve(import_path.default.dirname(importer ?? ""), id)
126
+ );
127
+ const relativePath = relativeId + outExt;
128
+ return relativePath;
129
+ },
130
+ loadInclude(id) {
131
+ return isCivetTranspiled(id);
132
+ },
133
+ async load(id) {
134
+ if (!isCivetTranspiled(id))
135
+ return null;
136
+ const filename = import_path.default.resolve(process.cwd(), id.slice(0, -outExt.length));
137
+ const code = await fs.promises.readFile(filename, "utf-8");
138
+ let transformed = {
139
+ code: import_civet.default.compile(code, {
140
+ inlineMap: true,
141
+ filename: id,
142
+ js: transpileToJS
143
+ }),
144
+ map: null
145
+ };
146
+ if (options.transformOutput)
147
+ transformed = await options.transformOutput(transformed.code, id);
148
+ return transformed;
149
+ },
150
+ transform(code, id) {
151
+ if (!/\.civet\.tsx?$/.test(id))
152
+ return null;
153
+ if (options.dts) {
154
+ fsMap.set(import_path.default.resolve(process.cwd(), id), code);
155
+ }
156
+ return null;
157
+ },
158
+ vite: {
159
+ config(_config, { command }) {
160
+ if (command === "build") {
161
+ return {
162
+ esbuild: {
163
+ include: [/\.civet$/],
164
+ loader: "tsx"
165
+ }
166
+ };
167
+ }
168
+ return null;
169
+ }
170
+ }
171
+ };
172
+ });
173
+ var src_default = civetUnplugin;
174
+
175
+ // src/esbuild.ts
176
+ var esbuild_default = src_default.esbuild;
@@ -0,0 +1,9 @@
1
+ import {
2
+ src_default
3
+ } from "./unplugin-shared.mjs";
4
+
5
+ // src/esbuild.ts
6
+ var esbuild_default = src_default.esbuild;
7
+ export {
8
+ esbuild_default as default
9
+ };
package/dist/esm.mjs CHANGED
@@ -1,132 +1,85 @@
1
- /**
2
- @file Civet ESM loader.
3
-
4
- Currently depends on ts-node esm loader being downstream
5
-
6
- @example
7
- ```bash
8
- node --loader ts-node/esm --loader @danielx/civet/esm source.civet
9
- ```
10
- */
11
-
1
+ // source/esm.civet
12
2
  import { readFileSync } from "fs";
13
3
  import { createRequire } from "module";
14
- import { pathToFileURL, fileURLToPath } from "url"
15
-
16
- import sourceMapSupport from "@cspotcode/source-map-support"
17
-
18
- // NOTE: this references the built version of the module, not the source
19
- import Civet from "../dist/main.js";
20
- const { compile, util } = Civet;
21
- const { SourceMap } = util
22
-
23
- const baseURL = pathToFileURL(process.cwd() + '/').href
24
- const extensionsRegex = /\.civet$/
25
-
26
- let registered = false;
27
- const outputCache = new Map
28
-
29
- const directorySeparator = '/'
30
- const backslashRegExp = /\\/g
31
- /**
32
- * Replace backslashes with forward slashes.
33
- */
4
+ import { pathToFileURL, fileURLToPath } from "url";
5
+ import sourceMapSupport from "@cspotcode/source-map-support";
6
+ import Civet from "./main.js";
7
+ var { compile, util } = Civet;
8
+ var { SourceMap } = util;
9
+ var baseURL = pathToFileURL(process.cwd() + "/").href;
10
+ var extensionsRegex = /\.civet$/;
11
+ var registered = false;
12
+ var outputCache = /* @__PURE__ */ new Map();
13
+ var directorySeparator = "/";
14
+ var backslashRegExp = /\\/g;
34
15
  function normalizeSlashes(value) {
35
16
  return value.replace(backslashRegExp, directorySeparator);
36
17
  }
37
-
38
- function ensureRegister () {
39
- if (registered) { return }
40
-
18
+ function ensureRegister() {
19
+ if (registered) {
20
+ return;
21
+ }
41
22
  const installation = {
42
- environment: 'node',
23
+ environment: "node",
43
24
  retrieveFile(pathOrUrl) {
44
- let path = pathOrUrl
45
- // If it's a file URL, convert to local path
46
- // I could not find a way to handle non-URLs except to swallow an error
47
- if (path.startsWith('file:')) {
25
+ let path = pathOrUrl;
26
+ if (path.startsWith("file:")) {
48
27
  try {
49
- path = fileURLToPath(path)
50
- } catch(e) {}
28
+ path = fileURLToPath(path);
29
+ } catch (e) {
30
+ }
51
31
  }
52
- path = normalizeSlashes(path)
53
-
54
- return outputCache.get(path)
32
+ path = normalizeSlashes(path);
33
+ return outputCache.get(path);
55
34
  }
56
- }
57
-
58
- sourceMapSupport.install(installation)
59
-
60
- return registered = true
35
+ };
36
+ sourceMapSupport.install(installation);
37
+ return registered = true;
61
38
  }
62
-
63
- export function resolve(specifier, context, next) {
64
- const { parentURL = baseURL } = context
65
-
39
+ function resolve(specifier, context, next) {
40
+ const { parentURL = baseURL } = context;
66
41
  if (extensionsRegex.test(specifier)) {
67
42
  return {
68
43
  shortCircuit: true,
69
44
  format: "civet",
70
- url: new URL(specifier, parentURL).href,
71
- }
45
+ url: new URL(specifier, parentURL).href
46
+ };
72
47
  }
73
-
74
- // Let Node.js handle all other specifiers.
75
- return next(specifier, context)
48
+ return next(specifier, context);
76
49
  }
77
-
78
- export async function load(url, context, next) {
50
+ async function load(url, context, next) {
79
51
  if (context.format === "civet") {
80
- const path = fileURLToPath(url)
52
+ const path = fileURLToPath(url);
81
53
  const source = readFileSync(path, "utf8");
82
- const {code: tsSource, sourceMap} = compile(source, {
54
+ const { code: tsSource, sourceMap } = compile(source, {
83
55
  filename: path,
84
56
  sourceMap: true,
85
- js: true,
86
- })
87
-
88
- // NOTE: Append .tsx to URL so ts-node treats as TypeScript
89
- const transpiledUrl = url + ".tsx"
90
-
91
- // NOTE: Assuming ts-node hook follows load hook
57
+ js: true
58
+ });
59
+ const transpiledUrl = url + ".tsx";
92
60
  const result = await next(transpiledUrl, {
93
- // ts-node won't transpile unless this is module
94
- // can't use commonjs since we don't rewrite imports
95
61
  format: "module",
96
- // NOTE: Setting the source in the context makes it available when ts-node uses defaultLoad
97
- source: tsSource,
98
- })
99
-
100
- // NOTE: we must install our source map support after ts-node does to take priority
101
- ensureRegister()
102
-
103
- // Remove .tsx extension for final URL
104
- result.responseURL = (result.responseURL ?? transpiledUrl)
105
- .replace(/.tsx$/, '')
106
-
107
- // parse source map from downstream (ts-node) result
108
- // compose with civet source map
109
- result.source = SourceMap.remap(result.source, sourceMap, url, result.responseURL)
110
- // NOTE: This path needs to match what ts-node uses so we can override the source map
111
- outputCache.set(normalizeSlashes(path), result.source)
112
-
113
- return result
62
+ source: tsSource
63
+ });
64
+ ensureRegister();
65
+ result.responseURL = (result.responseURL ?? transpiledUrl).replace(/.tsx$/, "");
66
+ result.source = SourceMap.remap(result.source, sourceMap, url, result.responseURL);
67
+ outputCache.set(normalizeSlashes(path), result.source);
68
+ return result;
114
69
  }
115
-
116
- // Other URLs continue unchanged.
117
- return next(url, context)
70
+ return next(url, context);
118
71
  }
119
-
120
- // commonjs hook
121
- const require = createRequire(import.meta.url)
122
- require.extensions[".civet"] = function(m, filename) {
123
- const source = readFileSync(filename, "utf8")
72
+ var require2 = createRequire(import.meta.url);
73
+ require2.extensions[".civet"] = function(m, filename) {
74
+ const source = readFileSync(filename, "utf8");
124
75
  const code = compile(source, {
125
76
  filename,
126
77
  inlineMap: true,
127
78
  js: true
128
- })
129
-
130
- //@ts-ignore TODO: Figure out how to load types from inculde folders in Civet LSP
131
- return m._compile(code, filename)
132
- }
79
+ });
80
+ return m._compile(code, filename);
81
+ };
82
+ export {
83
+ load,
84
+ resolve
85
+ };