Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/bootstrap/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const TAG_URL
= 'https://github.com/MavonEngine/Core/archive/refs/tags/0.0.9-alpha.zip'
= 'https://github.com/MavonEngine/Core/archive/refs/tags/0.0.10-alpha.zip'

export const TEMPLATE_SUBPATH = 'packages/multiplayer-template'
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mavonengine/core",
"type": "module",
"version": "0.0.8",
"version": "0.0.9",
"description": "",
"author": "",
"license": "MIT",
Expand Down
70 changes: 70 additions & 0 deletions packages/core/tests/vite/plugins/packageJsonPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { readFileSync } from 'node:fs'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createPackageJsonPlugin } from '../../../vite/plugins/packageJsonPlugin.js'

vi.mock('node:fs', () => ({
readFileSync: vi.fn(),
}))

describe('createPackageJsonPlugin', () => {
let resolveHandler: (args: { path: string }) => { path: string, namespace: string }
let loadHandler: (args: { path: string }) => { contents: string, loader: string }

beforeEach(() => {
const plugin = createPackageJsonPlugin('/fake/core/root')
const mockBuild = {
onResolve: vi.fn((_: unknown, handler: typeof resolveHandler) => {
resolveHandler = handler
}),
onLoad: vi.fn((_: unknown, handler: typeof loadHandler) => {
loadHandler = handler
}),
}
plugin.setup(mockBuild)
})

describe('onResolve', () => {
it('resolves @mavonengine/core package.json to coreRoot', () => {
const result = resolveHandler({ path: '@mavonengine/core/package.json' })
expect(result.path).toBe('/fake/core/root/package.json')
expect(result.namespace).toBe('mavonengine-pkg-json')
})

it('returns null for non-core package.json paths', () => {
const result = resolveHandler({ path: '/some/other/package.json' })
expect(result).toBeNull()
})
})

describe('onLoad', () => {
it('generates named exports for valid identifier keys', () => {
vi.mocked(readFileSync).mockReturnValue(JSON.stringify({ name: 'test-pkg', version: '1.0.0' }))
const result = loadHandler({ path: '/fake/package.json' })
expect(result.contents).toContain('export const name = "test-pkg";')
expect(result.contents).toContain('export const version = "1.0.0";')
})

it('filters out non-identifier keys from named exports', () => {
vi.mocked(readFileSync).mockReturnValue(
JSON.stringify({ 'hyphen-key': 'value', '@scoped': 'foo', 'valid': 'yes' }),
)
const result = loadHandler({ path: '/fake/package.json' })
expect(result.contents).not.toContain('export const hyphen-key')
expect(result.contents).not.toContain('export const @scoped')
expect(result.contents).toContain('export const valid = "yes";')
})

it('includes a default export of the full package object', () => {
const pkg = { 'name': 'test', 'version': '1.0.0', 'non-identifier': true }
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(pkg))
const result = loadHandler({ path: '/fake/package.json' })
expect(result.contents).toContain(`export default ${JSON.stringify(pkg)};`)
})

it('sets loader to js', () => {
vi.mocked(readFileSync).mockReturnValue(JSON.stringify({}))
const result = loadHandler({ path: '/fake/package.json' })
expect(result.loader).toBe('js')
})
})
})
28 changes: 28 additions & 0 deletions packages/core/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import { readFileSync } from 'node:fs'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vite'
import glsl from 'vite-plugin-glsl'
import { createPackageJsonPlugin } from './vite/plugins/packageJsonPlugin.js'

const GLSL_FILTER = /\.glsl$/

const coreRoot = dirname(fileURLToPath(import.meta.url))

const mavonEngineGlslPlugin = {
name: 'glsl',
setup(build) {
build.onLoad({ filter: GLSL_FILTER }, (args) => {
return {
contents: `export default ${JSON.stringify(readFileSync(args.path, 'utf8'))}`,
loader: 'js',
}
})
},
}

export default defineConfig({
plugins: [
glsl(),
],
optimizeDeps: {
esbuildOptions: {
plugins: [
mavonEngineGlslPlugin,
createPackageJsonPlugin(coreRoot),
],
},
},
server: {
watch: {
// For local npm link dev mode in @mavonengine/core
Expand Down
31 changes: 31 additions & 0 deletions packages/core/vite/plugins/packageJsonPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'

const PACKAGE_JSON_FILTER = /[/\\]package\.json$/
const ALL_FILTER = /.*/
const IDENTIFIER_FILTER = /^[a-z_$][\w$]*$/i

/*
* Allows importing package.json as an ES module with named exports so the
* client package can display the correct engine version at runtime.
*/
export function createPackageJsonPlugin(coreRoot) {
return {
name: 'package-json',
setup(build) {
build.onResolve({ filter: PACKAGE_JSON_FILTER }, (args) => {
if (!args.path.includes('@mavonengine/core'))
return null
return { path: resolve(coreRoot, 'package.json'), namespace: 'mavonengine-pkg-json' }
})
build.onLoad({ filter: ALL_FILTER, namespace: 'mavonengine-pkg-json' }, (args) => {
const pkg = JSON.parse(readFileSync(args.path, 'utf8'))
const named = Object.entries(pkg)
.filter(([k]) => IDENTIFIER_FILTER.test(k))
.map(([k, v]) => `export const ${k} = ${JSON.stringify(v)};`)
.join('\n')
return { contents: `${named}\nexport default ${JSON.stringify(pkg)};`, loader: 'js' }
})
},
}
}
39 changes: 1 addition & 38 deletions packages/multiplayer-template/client/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,18 @@
import { existsSync, readFileSync } from 'node:fs'
import { existsSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import config from '@mavonengine/core/vite.config'
import vue from '@vitejs/plugin-vue'

const GLSL_FILTER = /\.glsl$/
const PACKAGE_JSON_FILTER = /[/\\]package\.json$/
const ALL_FILTER = /.*/
const IDENTIFIER_FILTER = /^[a-z_$][\w$]*$/i

const clientRoot = dirname(fileURLToPath(import.meta.url))
const templateRoot = resolve(clientRoot, '..')
const coreRoot = resolve(clientRoot, '../../core')
const coreSrc = resolve(coreRoot, 'src')
const editorRoot = resolve(clientRoot, '../../editor')
const editorSrc = resolve(editorRoot, 'src/Editor.ts')

// esbuild plugin: handles .glsl imports during dep pre-bundling
const glslPlugin = {
name: 'glsl',
setup(build) {
build.onLoad({ filter: GLSL_FILTER }, (args) => {
return { contents: `export default ${JSON.stringify(readFileSync(args.path, 'utf8'))}`, loader: 'js' }
})
},
}

// esbuild plugin: handles `import { version } from 'package.json' with { type: 'json' }`
// esbuild rejects this because @mavonengine/core's exports field doesn't list package.json
const packageJsonPlugin = {
name: 'package-json',
setup(build) {
build.onResolve({ filter: PACKAGE_JSON_FILTER }, args => ({ path: args.path, namespace: 'pkg-json' }))
build.onLoad({ filter: ALL_FILTER, namespace: 'pkg-json' }, (args) => {
const pkg = JSON.parse(readFileSync(args.path, 'utf8'))
const named = Object.entries(pkg)
.filter(([k]) => IDENTIFIER_FILTER.test(k))
.map(([k, v]) => `export const ${k} = ${JSON.stringify(v)};`)
.join('\n')
return { contents: `${named}\nexport default ${JSON.stringify(pkg)};`, loader: 'js' }
})
},
}

export default {
...config,
optimizeDeps: {
esbuildOptions: {
plugins: [glslPlugin, packageJsonPlugin],
},
},
server: {
...config.server,
fs: {
Expand Down
Loading
Loading