implement standard function SFC.

This commit is contained in:
蓝莓小果冻 2024-08-22 19:07:03 +08:00 committed by bemly
parent cf3b277a39
commit f9e2f238d6
3 changed files with 130 additions and 17 deletions

View File

@ -27,11 +27,65 @@ This is ESM module support.
```coffeescript
import * as cfs from "coffeescript"
import * as pug from "pug"
import sty from "stylus"
console.log "[luolita] use ES Modules loader."
console.log "[luolita] #{ cfs.VERSION }"
PATH = "temp/test.luoli"
NAME = "[luolita]"
ENCODING = "utf-8"
COFFEE_OPTIONS =
bare: true,
header: false,
sourceMap: true,
inlineMap: true,
export default "????"
console.log "#{ NAME } use ES Modules loader."
console.log "#{ NAME } CoffeeScript ver: #{ cfs.VERSION }"
import { readFileSync, createReadStream, writeFileSync } from "fs"
import JSON from 'json5'
import readline from "readline"
DEBUG = true
{ config: CONFIG } = JSON.parse readFileSync "package.json5", "utf-8"
file = readline.createInterface
input: createReadStream PATH, encoding: ENCODING
if DEBUG then console.log "#{ NAME } Reading file: #{ PATH }"
# 0: coffeescript, 1: pug, 2: stylus
file_segments = {}
file_segment_status = ""
output_segments = {}
sfc_var_bridge = ->
output2file = (json) ->
writeFileSync "temp/test.css", json.style
writeFileSync "temp/test.js", json.coffee.js
writeFileSync "temp/test.html", json.template
file.on "line", (line) ->
if DEBUG then console.log "#{ NAME } #{ PATH }> #{ line }"
switch line.trimEnd()
when "coffee:" then file_segment_status = "coffee"
when "template:" then file_segment_status = "template"
when "style:" then file_segment_status = "style"
else
file_segments[file_segment_status] ?= ""
file_segments[file_segment_status] += line + '\n'
file.on "close", ->
if DEBUG then console.log "#{ NAME } Close file: #{ PATH }"
sfc_var_bridge()
output_segments.coffee = cfs.compile file_segments.coffee, COFFEE_OPTIONS
output_segments.template = pug.render file_segments.template
output_segments.style = await sty file_segments.style
.render()
if DEBUG then console.log output_segments
output2file output_segments
export default output_segments
```
@ -45,6 +99,9 @@ This is ESM module support.
### [0.1.-2] - 2024-08-22
- 完成所有打包工作,测试导出是否正常 Completed all packaging, test export work
### [0.1.3] - 2024-08-22
- 简单分割文件为三段类型分开解释 Simple split files into three types to explain
## 许可证 License
WTFPL - Do What The F*ck You Want To Public License

View File

@ -9,16 +9,70 @@ This repository adopts CJS+ESM hybrid development to fight for the future!
BUT, coffeescript support ES2015 modules' syntactic sugar : /
###
require 'coffeescript/register'
# This is Node.js CommonJS support.
console.log "[luolita] use CommonJS loader."
console.log "[luolita] start converting..."
args = require("minimist") process.argv.slice 2
a = => 3
b = -> 4
console.log(a(), b(), __dirname, args)
cfs = require "coffeescript"
pug = require "pug"
sty = require "stylus"
PATH = "temp/test.luoli"
NAME = "[luolita]"
ENCODING = "utf-8"
COFFEE_OPTIONS =
bare: true,
header: false,
sourceMap: true,
inlineMap: true,
console.log "#{ NAME } use ES Modules loader."
console.log "#{ NAME } CoffeeScript ver: #{ cfs.VERSION }"
{ readFileSync, createReadStream, writeFileSync } = require "fs"
JSON = require 'json5'
readline = require "readline"
DEBUG = true
{ config: CONFIG } = JSON.parse readFileSync "package.json5", "utf-8"
file = readline.createInterface
input: createReadStream PATH, encoding: ENCODING
if DEBUG then console.log "#{ NAME } Reading file: #{ PATH }"
# 0: coffeescript, 1: pug, 2: stylus
file_segments = {}
file_segment_status = ""
output_segments = {}
sfc_var_bridge = ->
output2file = (json) ->
writeFileSync "temp/test.css", json.style
writeFileSync "temp/test.js", json.coffee.js
writeFileSync "temp/test.html", json.template
file.on "line", (line) ->
if DEBUG then console.log "#{ NAME } #{ PATH }> #{ line }"
switch line.trimEnd()
when "coffee:" then file_segment_status = "coffee"
when "template:" then file_segment_status = "template"
when "style:" then file_segment_status = "style"
else
file_segments[file_segment_status] ?= ""
file_segments[file_segment_status] += line + '\n'
file.on "close", ->
if DEBUG then console.log "#{ NAME } Close file: #{ PATH }"
sfc_var_bridge()
output_segments.coffee = cfs.compile file_segments.coffee, COFFEE_OPTIONS
output_segments.template = pug.render file_segments.template
output_segments.style = await sty file_segments.style
.render()
if DEBUG then console.log output_segments
output2file output_segments
console.log(__dirname, args)

View File

@ -1,7 +1,7 @@
{
// 爱死 pnpm 惹,支持 JSON5 真是大好评!
name: 'luolita',
version: '0.1.2',
version: '0.1.3',
// TODO: 目前 COFFEE 不支持 ESM 模块化55555
type: 'commonjs',
main: 'luolita.mjs',
@ -14,9 +14,10 @@
start: 'ver && echo %npm_package_config_Windows% || echo $npm_package_config_Posix',
test: './luolita.coffee',
prebuild: 'coffee -bl -o temp/index.mjs -c README.md',
build: 'coffee -bl -o luolita.mjs -c README.md',
readme: 'coffee -bl -o luolita.mjs -c README.md && node luolita.mjs',
// 释放 package.json5还原配置文件数据编译 ".litcoffee"
preinstall: 'coffee --bare --no-header --literate --output luolita.mjs --compile README.md \
&& mv package.bemly.json5 package.json5',
preinstall: 'coffee --bare --no-header --literate --output luolita.mjs --compile README.md && mv package.bemly.json5 package.json5',
// pnpm 发布的时候强制无法上传 package.json5改成 package.bemly.json 规避
prepack: 'cp package.json5 package.bemly.json5',
},
@ -27,22 +28,23 @@
'tools',
'pug',
'jade',
'stylus'
'stylus',
],
author: 'Bemly_ <bemly_@petalmail.com> (https://bemly.moe)',
contributors: [
'thank you for everyone!'
'thank you for everyone!',
],
license: 'WTFPL',
dependencies: {
coffeescript: '^2.7.0',
json5: '^2.2.3',
minimist: '^1.2.8',
pug: '^3.0.3',
stylus: '^0.63.0'
stylus: '^0.63.0',
},
config: {
drink: 'coffee --bare --no-header --literate --output luolita.mjs --compile README.md',
Windows: '[luolita] detected program work on Windows',
Posix: '[luolita] detected program work on Posix',
}
},
}