support esm modules

This commit is contained in:
MichalTarasiuk 2024-03-08 19:36:06 +01:00
parent 765cc0c7cf
commit dcbdb0ccb7
11 changed files with 43 additions and 38 deletions

View file

@ -7,13 +7,13 @@
// edited to work with the appdir by @raphaelbadia
const gzSize = require("gzip-size")
const mkdirp = require("mkdirp")
const fs = require("fs")
const path = require("path")
import gzSize from "gzip-size"
import { mkdirp } from "mkdirp"
import fs from "fs"
import path from "path"
// Pull options from `package.json`
const options = getOptions()
const options = await getOptions()
const BUILD_OUTPUT_DIRECTORY = getBuildOutputDirectory(options)
// first we check to make sure that the build output directory exists
@ -28,8 +28,12 @@ try {
}
// if so, we can import the build manifest
const buildMeta = require(path.join(nextMetaRoot, "build-manifest.json"))
const appDirMeta = require(path.join(nextMetaRoot, "app-build-manifest.json"))
const { default: buildMeta } = await import(path.join(nextMetaRoot, "build-manifest.json"), {
assert: { type: "json" },
})
const { default: appDirMeta } = await import(path.join(nextMetaRoot, "app-build-manifest.json"), {
assert: { type: "json" },
})
// this memory cache ensures we dont read any script file more than once
// bundles are often shared between pages
@ -116,8 +120,8 @@ function getScriptSize(scriptPath) {
/**
* Reads options from `package.json`
*/
function getOptions(pathPrefix = process.cwd()) {
const pkg = require(path.join(pathPrefix, "package.json"))
async function getOptions(pathPrefix = process.cwd()) {
const pkg = await import(path.join(pathPrefix, "package.json"), { assert: { type: "json" } })
return { ...pkg.nextBundleAnalysis, name: pkg.name }
}