Skip to content

Commit

Permalink
Use TS 5.3 import attributes (#9591)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Dec 18, 2023
1 parent 9e43b62 commit cb4aba6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 51 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module.exports = {
version: packageJSON.devDependencies['@babel/runtime-corejs3'],
},
],
'@babel/plugin-syntax-import-attributes',
],
overrides: [
// ** WEB PACKAGES **
Expand Down
36 changes: 10 additions & 26 deletions packages/vite/src/buildRouteManifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs/promises'
import path from 'path'
import fs from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'

import type { Manifest as ViteBuildManifest } from 'vite'

Expand All @@ -13,30 +14,12 @@ import type { RWRouteManifest } from './types'
* Generate a route manifest file for the web server side.
*/
export async function buildRouteManifest() {
const webRouteManifest = getPaths().web.routeManifest

// TODO When https://github.com/tc39/proposal-import-attributes and
// https://github.com/microsoft/TypeScript/issues/53656 have both landed we
// should try to do this instead:
// const clientBuildManifest: ViteBuildManifest = await import(
// path.join(getPaths().web.dist, 'client-build-manifest.json'),
// { with: { type: 'json' } }
// )
// NOTES:
// * There's a related babel plugin here
// https://babeljs.io/docs/babel-plugin-syntax-import-attributes
// * Included in `preset-env` if you set `shippedProposals: true`
// * We had this before, but with `assert` instead of `with`. We really
// should be using `with`. See motivation in issues linked above.
// * With `assert` and `@babel/plugin-syntax-import-assertions` the
// code compiled and ran properly, but Jest tests failed, complaining
// about the syntax.
const manifestPath = path.join(
getPaths().web.dist,
'client-build-manifest.json'
)
const buildManifestStr = await fs.readFile(manifestPath, 'utf-8')
const clientBuildManifest: ViteBuildManifest = JSON.parse(buildManifestStr)
const buildManifestUrl = url.pathToFileURL(
path.join(getPaths().web.dist, 'client-build-manifest.json')
).href
const clientBuildManifest: ViteBuildManifest = (
await import(buildManifestUrl, { with: { type: 'json' } })
).default

const routesList = getProjectRoutes()

Expand Down Expand Up @@ -66,6 +49,7 @@ export async function buildRouteManifest() {

console.log('routeManifest', JSON.stringify(routeManifest, null, 2))

const webRouteManifest = getPaths().web.routeManifest
return fs.writeFile(webRouteManifest, JSON.stringify(routeManifest, null, 2))
}

Expand Down
39 changes: 14 additions & 25 deletions packages/vite/src/runFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// UPDATE: We decided to name the package @redwoodjs/web-server instead of
// fe-server. And it's already created, but this hasn't been moved over yet.

import fs from 'fs/promises'
import path from 'path'
import path from 'node:path'
import url from 'node:url'

import { createServerAdapter } from '@whatwg-node/server'
// @ts-expect-error We will remove dotenv-defaults from this package anyway
Expand Down Expand Up @@ -61,32 +61,21 @@ export async function runFeServer() {
}
}

// TODO When https://github.com/tc39/proposal-import-attributes and
// https://github.com/microsoft/TypeScript/issues/53656 have both landed we
// should try to do this instead:
// const routeManifest: RWRouteManifest = await import(
// rwPaths.web.routeManifest, { with: { type: 'json' } }
// )
// NOTES:
// * There's a related babel plugin here
// https://babeljs.io/docs/babel-plugin-syntax-import-attributes
// * Included in `preset-env` if you set `shippedProposals: true`
// * We had this before, but with `assert` instead of `with`. We really
// should be using `with`. See motivation in issues linked above.
// * With `assert` and `@babel/plugin-syntax-import-assertions` the
// code compiled and ran properly, but Jest tests failed, complaining
// about the syntax.
const routeManifestStr = await fs.readFile(rwPaths.web.routeManifest, 'utf-8')
const routeManifest: RWRouteManifest = JSON.parse(routeManifestStr)

// TODO See above about using `import { with: { type: 'json' } }` instead
const manifestPath = path.join(rwPaths.web.dist, 'client-build-manifest.json')
const buildManifestStr = await fs.readFile(manifestPath, 'utf-8')
const buildManifest: ViteBuildManifest = JSON.parse(buildManifestStr)
const routeManifestUrl = url.pathToFileURL(rwPaths.web.routeManifest).href
const routeManifest: RWRouteManifest = (
await import(routeManifestUrl, { with: { type: 'json' } })
).default

const buildManifestUrl = url.pathToFileURL(
path.join(rwPaths.web.dist, 'client-build-manifest.json')
).href
const buildManifest: ViteBuildManifest = (
await import(buildManifestUrl, { with: { type: 'json' } })
).default

if (rwConfig.experimental?.rsc?.enabled) {
console.log('='.repeat(80))
console.log('buildManifest', buildManifest)
console.log('buildManifest', buildManifest.default)
console.log('='.repeat(80))
}

Expand Down

0 comments on commit cb4aba6

Please sign in to comment.