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

TypeScript support for .blits files #244

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions packages/create-blits/boilerplate/ts/default/src/App.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Blits from '@lightningjs/blits'

import Home from './pages/Home.js'
import Home from './pages/Home'

export default Blits.Application({
template: `
<Element>
<RouterView />
</Element>
`,
</Element>`,
routes: [{ path: '/', component: Home }],
})
2 changes: 1 addition & 1 deletion packages/create-blits/boilerplate/ts/default/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Blits from '@lightningjs/blits'
import App from './App.js'
import App from './App'

Blits.Launch(App, 'app', {
w: 1920,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Blits from '@lightningjs/blits'

import Loader from '../components/Loader.js'
import Loader from '../components/Loader'

const colors = ['#f5f3ff', '#ede9fe', '#ddd6fe', '#c4b5fd', '#a78bfa']

Expand Down Expand Up @@ -38,8 +38,7 @@ export default Blits.Component('Home', {
/>
</Element>
</Element>
</Element>
`,
</Element>`,
state() {
return {
/**
Expand Down
45 changes: 37 additions & 8 deletions vite/blitsFileConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,52 @@
*/

import blitsfileconverter from '../src/lib/blitsfileconverter/blitsfileconverter.js'
import path from 'node:path'
import fs from 'node:fs'
import { createRequire } from 'module'

export default function blitsFileType() {
return {
name: 'vite-plugin-blits-file-type',
enforce: 'pre',
transform(src, id) {

resolveId(source, importer) {
if (source.endsWith('.blits')) {
return importer ? path.resolve(path.dirname(importer), source) : path.resolve(source)
}
return null
},

load(id) {
if (id.endsWith('.blits')) {
try {
const transformedCode = blitsfileconverter(src)
return {
code: transformedCode,
map: null, // no source map
const source = fs.readFileSync(id, 'utf-8')
let code = blitsfileconverter(source)

// Check for TypeScript and transpile to JS if needed
if (/<script\s+lang=["']ts["']/.test(source)) {
// Resolve the local typescript dependency
const userRequire = createRequire(process.cwd() + '/')
let ts
try {
ts = userRequire('typescript')
} catch (err) {
throw new Error(
`\n\nThe file "${id}" contains \`lang="ts"\`, indicating it uses TypeScript. \nTo enable TypeScript support, please install the 'typescript' package as a dev dependency by running:\n\n` +
' npm install --save-dev typescript\n\n'
)
}
} catch (error) {
this.error(error)
const transpiled = ts.transpileModule(code, {
compilerOptions: { target: ts.ScriptTarget.ESNext, module: ts.ModuleKind.ESNext },
})
code = transpiled.outputText
}

return {
code,
map: null,
}
}
return null
},
}
}
9 changes: 5 additions & 4 deletions vite/preCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export default function () {

const fileExtension = path.extname(filePath)

// we should only precompile .js and .ts files
if (fileExtension === '.js' || fileExtension === '.ts') {
// we should only precompile .blits, .js and .ts files
if (fileExtension === '.js' || fileExtension === '.ts' || fileExtension === '.blits') {
return compiler(source, filePath)
}
const relativePath = path.relative(process.cwd(), filePath)
return compiler(source, relativePath)

// vite expects null if there is no modification
return null
},
}
}