Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐈🐈🐈 #657

Merged
merged 10 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
set -euo pipefail

# normal files
coffee build/esbuild.coffee
civet --no-config build/esbuild.civet

# cli
BIN="dist/civet"
echo "#!/usr/bin/env node" | cat - dist/cli.js > "$BIN"
chmod +x "$BIN"
rm dist/cli.js

# esbuild-plugin
./dist/civet --no-config --js -c source/esbuild-plugin.civet -o dist/esbuild-plugin.js

# esm loader
./dist/civet --no-config --js -c source/esm.civet -o dist/esm.mjs

# babel plugin
cp source/babel-plugin.mjs dist/babel-plugin.mjs

Expand Down
47 changes: 0 additions & 47 deletions build/coffee-esm.mjs

This file was deleted.

123 changes: 123 additions & 0 deletions build/esbuild.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"civet coffeeCompat"

esbuild = require "esbuild"
heraPlugin = require "@danielx/hera/esbuild-plugin"
# Need to use the packaged version because we may not have built our own yet
civetPlugin = require "../node_modules/@danielx/civet/dist/esbuild-plugin.js"

watch = process.argv.includes '--watch'
minify = false
sourcemap = false

path = require "path"
{access} = require "fs/promises"
exists = (p) ->
access(p)
.then ->
true
.catch ->
false
extensionResolverPlugin = (extensions) ->
name: "extension-resolve"
setup: (build) ->
# For relative requires that don't contain a '.'
build.onResolve { filter: /\/[^.]*$/ }, (r) ->
for extension in extensions
{path: resolvePath, resolveDir} = r
p = path.join(resolveDir, resolvePath + ".#{extension}")

# see if a .coffee file exists
found = await exists(p)
if found
return path: p

return undefined

resolveExtensions = extensionResolverPlugin(["civet", "hera"])

# To get proper extension resolution for non-bundled files, we need to use
# a plugin hack: https://github.com/evanw/esbuild/issues/622#issuecomment-769462611
# set bundle: true, then rewrite .coffee -> .js and mark as external
# Also marking everything else as external since we don't want to bundle anything
rewriteCivetImports = {
name: 'rewrite-civet',
setup: (build) ->
build.onResolve { filter: /.*/ }, (args) ->
if (args.importer)
path: args.path.replace(/\.civet$/, ".js")
external: true
}

# Files that need civet imports re-written
# since they aren't actually bundled
["cli", "config", "esbuild-plugin"].forEach (name) ->
esbuild.build({
entryPoints: ["source/#{name}.civet"]
bundle: true
platform: 'node'
format: 'cjs'
outfile: "dist/#{name}.js"
plugins: [
rewriteCivetImports
civetPlugin()
]
}).catch -> process.exit 1

# esm needs to be a module for import.meta
["esm"].forEach (name) ->
esbuild.build({
entryPoints: ["source/#{name}.civet"]
bundle: true
platform: 'node'
format: 'esm'
outfile: "dist/#{name}.mjs"
plugins: [
rewriteCivetImports
civetPlugin()
]
}).catch -> process.exit 1

for esm in [false, true]
esbuild.build({
entryPoints: ['source/main.civet']
bundle: true
watch
platform: 'node'
format: if esm then 'esm' else 'cjs'
outfile: "dist/main.#{if esm then 'mjs' else 'js'}"
plugins: [
resolveExtensions
civetPlugin()
heraPlugin
]
}).catch -> process.exit 1

esbuild.build({
entryPoints: ['source/main.civet']
globalName: "Civet"
bundle: true
sourcemap
watch
platform: 'browser'
outfile: 'dist/browser.js'
plugins: [
resolveExtensions
civetPlugin()
heraPlugin
]
}).catch -> process.exit 1

esbuild.build({
entryPoints: ['source/bun-civet.civet']
bundle: false
sourcemap
minify
watch
platform: 'node'
format: 'esm'
target: "esNext"
outfile: 'dist/bun-civet.mjs'
plugins: [
civetPlugin()
]
}).catch -> process.exit 1
120 changes: 0 additions & 120 deletions build/esbuild.coffee

This file was deleted.

2 changes: 1 addition & 1 deletion build/hera-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function resolve(specifier, context, defaultResolve) {

export async function load(url, context, next) {
if (extensionsRegex.test(url)) {
return next(url.replace(extensionsRegex, ".cjs"), {
return next(url, {
format: "commonjs"
});
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"./esm": "./dist/esm.mjs",
"./esbuild-plugin": "./dist/esbuild-plugin.js",
"./register": "./register.js",
"./config": "./dist/config.js",
"./*": "./*",
"./dist/*": "./dist/*"
},
Expand Down Expand Up @@ -43,15 +42,14 @@
"@cspotcode/source-map-support": "^0.8.1"
},
"devDependencies": {
"@danielx/civet": "^0.6.26",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd feel safer if this were a pinned dep (no ^). Given how we tend to change things and just bump the patch number. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the lockfile should take care of it but being explicit here would also be fine.

"@danielx/hera": "^0.8.10",
"@types/assert": "^1.5.6",
"@types/coffeescript": "^2.5.2",
"@types/mocha": "^9.1.1",
"@types/node": "^18.7.8",
"axios": "^1.2.2",
"c8": "^7.12.0",
"esbuild": "^0.14.49",
"esbuild-coffeescript": "^2.1.0",
"marked": "^4.2.4",
"mocha": "^10.0.0",
"prettier": "^2.8.1",
Expand All @@ -71,7 +69,10 @@
"extension": [
".civet",
".coffee",
".hera",
".js",
".mjs",
".mts",
".ts"
],
"include": [
Expand All @@ -85,9 +86,8 @@
],
"loader": [
"ts-node/esm",
"./build/coffee-esm.mjs",
"./build/hera-esm.mjs",
"./dist/esm.mjs"
"./node_modules/@danielx/civet/dist/esm.mjs"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this use the correct version of Civet? We need tests to run in the repo version, not in the released version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the version that will parse the source and test files. Once parsed the tests are run against main.civet, lib.civet, parser.hera, etc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. (If only we could add a comment to a JSON file. 😉)

That seems fine, because we don't generally use fancy features in the test infrastructure, only inside the triple quotes. Actually, in some ways, it will be an improvement; I've run into issues where I break the Civet compiler (and ran yarn build) and then yarn test crashes, with unhelpful error messages. This should avoid that sort of issue.

],
"reporter": "dot",
"recursive": true,
Expand Down
File renamed without changes.
8 changes: 5 additions & 3 deletions source/cli.coffee → source/cli.civet
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {parse, compile, generate} from "./main"
import {findConfig, loadConfig} from "./config"
"civet coffeeCompat"

import {parse, compile, generate} from "./main.civet"
import {findConfig, loadConfig} from "./config.civet"
{prune} = generate

version = -> require("../package.json").version
Expand Down Expand Up @@ -196,7 +198,7 @@ cli = ->

if options.config isnt false # --no-config
options.config ?= await findConfig(process.cwd())

if options.config
options = {
...(await loadConfig options.config),
Expand Down
Loading