Skip to content

Commit

Permalink
fix: make standalone builds easier (vikejs/vike#1165, vikejs/vike#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Dec 11, 2023
1 parent ee07acb commit 3765c56
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
31 changes: 22 additions & 9 deletions telefunc/node/vite/plugins/importBuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@ function importBuild(): Plugin[] {
}

function getImporterCode(config: ResolvedConfig, telefuncFilesEntry: string) {
// Current file: node_modules/telefunc/dist/cjs/node/vite/plugins/importBuild/index.js
const importPathAbsolute = toPosixPath(
require.resolve(`../../../../../../dist/cjs/node/vite/plugins/importBuild/loadBuild.js`)
)
const importPath = getImportPath(config)

assert(config.build.ssr) // outDir needs to be the outDir of the server-side
const outDir = getOutDirAbsolute(config)
assertPosixPath(outDir)
assertPosixPath(importPathAbsolute)
const importPath = path.posix.relative(outDir, importPathAbsolute)
// console.log(`\n importPath: ${importPath}\n outDirServer: ${outDirServer}\n importPathAbsolute: ${importPathAbsolute}\n config.build.outDir: ${config.build.outDir}`)
const importerCode = [
'{',
Expand All @@ -51,3 +43,24 @@ function getImporterCode(config: ResolvedConfig, telefuncFilesEntry: string) {
].join('\n')
return importerCode
}

function getImportPath(config: ResolvedConfig) {
// We resolve filePathAbsolute even if we don't use it: we use require.resolve() as an assertion that the relative path is correct
const filePathAbsolute = toPosixPath(
// [RELATIVE_PATH_FROM_DIST] Current file: node_modules/telefunc/dist/cjs/node/vite/plugins/importBuild/index.js
require.resolve(`../../../../../../dist/cjs/node/vite/plugins/importBuild/loadBuild.js`)
)
if (
// Let's implement a new config if a user needs the import to be a relative path instead of 'telefunc/__internal/loadImportBuild' (AFAIK there is no use case for relative paths for Telefunc)
true as boolean
) {
return 'telefunc/__internal/loadBuild'
} else {
assert(config.build.ssr) // outDir needs to be the outDir of the server-side
const outDir = getOutDirAbsolute(config)
assertPosixPath(outDir)
assertPosixPath(filePathAbsolute)
const filePathRelative = path.posix.relative(outDir, filePathAbsolute)
return filePathRelative
}
}
4 changes: 4 additions & 0 deletions telefunc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
},
"./babel": {
"node": "./dist/cjs/node/babel/plugin.js"
},
"./__internal/loadBuild": {
"worker": "./dist/cjs/node/vite/plugins/importBuild/loadBuild.js",
"node": "./dist/cjs/node/vite/plugins/importBuild/loadBuild.js"
}
},
"devDependencies": {
Expand Down

0 comments on commit 3765c56

Please sign in to comment.