Skip to content

Commit

Permalink
fix: adding video JSON to the Next 15 build + Turbopack warning (#316)
Browse files Browse the repository at this point in the history
* fix: adding video JSON to the build
fix #315

* fix: add Turbopack warning
  • Loading branch information
luwes authored Nov 1, 2024
1 parent 85e6458 commit 199e158
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 31 deletions.
12 changes: 6 additions & 6 deletions examples/default-provider/package-lock.json

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

66 changes: 63 additions & 3 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@types/dotenv-flow": "^3.3.3",
"@types/node": "^20.14.9",
"@types/react": "18.3.3",
"@types/resolve": "^1.20.6",
"@types/yargs": "^17.0.32",
"c8": "^9.1.0",
"esbuild": "^0.21.5",
Expand All @@ -99,6 +100,7 @@
"chalk": "^4.1.2",
"chokidar": "^3.6.0",
"magicast": "^0.3.4",
"resolve": "^1.22.8",
"symlink-dir": "^5.2.1",
"undici": "^5.28.4",
"yargs": "^17.7.2"
Expand Down
10 changes: 0 additions & 10 deletions src/cli/lib/json-configs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { PACKAGE_NAME } from '../../constants.js';

Expand Down Expand Up @@ -44,11 +42,3 @@ export async function checkPackageJsonForNextVideo(packagePath: string = './pack

return !!(json.devDependencies?.[PACKAGE_NAME] || json.dependencies?.[PACKAGE_NAME]);
}

export async function getNextVideoVersion() {
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const scriptRoot = path.join(scriptDir, '..', '..', '..');
const packagePath = path.join(scriptRoot, 'package.json');
const pkg = JSON.parse(await readFile(packagePath, 'utf-8'));
return pkg.version;
}
6 changes: 3 additions & 3 deletions src/cli/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { cwd } from 'node:process';
import { readdir } from 'node:fs/promises';
import path from 'node:path';

import log from '../utils/logger.js';
import { callHandler } from '../process.js';
import { createAsset, getAsset } from '../assets.js';
import { getVideoConfig } from '../config.js';
import { getNextVideoVersion } from './lib/json-configs.js';
import { getPackageVersion } from '../utils/utils.js';
import log from '../utils/logger.js';

export const command = 'sync';
export const desc =
Expand Down Expand Up @@ -56,7 +56,7 @@ function watcher(dir: string) {
export async function handler(argv: Arguments) {
const directoryPath = path.join(cwd(), argv.dir as string);

const version = await getNextVideoVersion();
const version = getPackageVersion('next-video');
log.space(log.label(`▶︎ next-video ${version}`));
log.space();

Expand Down
2 changes: 1 addition & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function add(...messages: any[]) {
}

export function warning(...messages: any[]) {
base('log', chalk.yellow.bold('!'), ...messages);
base('log', chalk.yellow.bold(''), ...messages);
}

export function error(...messages: any[]) {
Expand Down
29 changes: 29 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
import fs from 'node:fs';
import resolve from 'resolve';

export function getPackageVersion(packageName: string) {
const packageJsonPath = resolvePackageJson(packageName);
if (packageJsonPath) {
try {
const packageJson: { version: string } = JSON.parse(
fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }),
);
return packageJson.version;
} catch {
// noop
}
}
return undefined;
}

function resolvePackageJson(packageName: string) {
try {
return resolve.sync(`${packageName}/package.json`, {
basedir: process.cwd(),
preserveSymlinks: true,
});
} catch {
return undefined;
}
}

export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand Down
44 changes: 36 additions & 8 deletions src/with-next-video.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import symlinkDir from 'symlink-dir';
import { join, dirname } from 'node:path';
import fs from 'node:fs/promises';
import fs from 'node:fs';
import { env } from 'node:process';
import { fileURLToPath } from 'node:url';
import logger from './utils/logger.js';
import { getPackageVersion } from './utils/utils.js';
import { setVideoConfig } from './config.js';
import type { VideoConfig } from './config.js';

let hasWarned = false;

export function withNextVideo(nextConfig: any, videoConfig?: VideoConfig) {
const videoConfigComplete = setVideoConfig(videoConfig);
const { path, folder, provider } = videoConfigComplete;
Expand Down Expand Up @@ -36,19 +40,43 @@ export function withNextVideo(nextConfig: any, videoConfig?: VideoConfig) {
symlinkDir(VIDEOS_PATH, TMP_PUBLIC_VIDEOS_PATH);

process.on('exit', async () => {
await fs.unlink(TMP_PUBLIC_VIDEOS_PATH);
fs.unlinkSync(TMP_PUBLIC_VIDEOS_PATH);
});
}

const experimental = { ...nextConfig.experimental };
const nextVersion = getPackageVersion('next');

if (nextVersion && nextVersion.startsWith('15.')) {
nextConfig.outputFileTracingIncludes = {
...nextConfig.outputFileTracingIncludes,
[path]: [`./${folder}/**/*.json`],
};
} else {
const experimental = { ...nextConfig.experimental };
experimental.outputFileTracingIncludes = {
...experimental.outputFileTracingIncludes,
[path]: [`./${folder}/**/*.json`],
};
nextConfig.experimental = experimental;
}

if (!hasWarned && process.env.TURBOPACK && !process.env.NEXT_VIDEO_SUPPRESS_TURBOPACK_WARNING) {
hasWarned = true;

experimental.outputFileTracingIncludes = {
...experimental.outputFileTracingIncludes,
[path]: [`./${folder}/**/*.json`],
};
const nextVideoVersion = getPackageVersion('next-video');

logger.space(logger.label(`▶︎ next-video ${nextVideoVersion}\n`));
logger.warning(
`You are using next-video with \`next ${
process.env.NODE_ENV === 'development' ? 'dev' : 'build'
} --turbo\`. next-video doesn't yet fully support Turbopack.\n We recommend temporarily removing the \`--turbo\` flag for use with next-video.\n`
);
logger.warning(
`Follow this issue for progress on next-video + Turbopack: https://github.com/muxinc/next-video/issues/266.\n (You can suppress this warning by setting NEXT_VIDEO_SUPPRESS_TURBOPACK_WARNING=1 as environment variable)\n`
);
}

return Object.assign({}, nextConfig, {
experimental,
webpack(config: any, options: any) {
if (!options.defaultLoaders) {
throw new Error(
Expand Down

0 comments on commit 199e158

Please sign in to comment.