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

fix: build not working in Es modules projects #69

Closed
wants to merge 6 commits into from
Closed
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
11 changes: 10 additions & 1 deletion src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function build(cliOptions: Partial<ViteSSGOptions> = {}) {
mock = false,
entry = await detectEntry(root),
formatting = null,
buildCjs = false,
includedRoutes = DefaultIncludedRoutes,
onBeforePageRender,
onPageRendered,
Expand Down Expand Up @@ -72,10 +73,18 @@ export async function build(cliOptions: Partial<ViteSSGOptions> = {}) {
mode: config.mode,
})

let outputFile = join(ssgOut, 'main.js')
// rename to cjs extension
if (buildCjs === true) {
let pos = outputFile.lastIndexOf('.');
outputFile = outputFile.substr(0, pos < 0 ? outputFile.length : pos) + '.cjs';
await fs.rename(join(ssgOut, 'main.js'), outputFile);
}

const ssrManifest: Manifest = JSON.parse(await fs.readFile(join(out, 'ssr-manifest.json'), 'utf-8'))

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { createApp } = require(join(ssgOut, 'main.js')) as { createApp: CreateAppFactory }
const { createApp } = require(outputFile) as { createApp: CreateAppFactory }

let indexHTML = await fs.readFile(join(out, 'index.html'), 'utf-8')

Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export interface ViteSSGOptions {
*/
mode?: string

/**
* Uses .cjs file extension for temporary build file.
* This should be set to true for es-module environments
* with type: "module" set in package.json.
*
* @default false
*/
buildCjs?: boolean

/**
* Custom functions to modified the routes to do the SSG.
*
Expand Down