-
Notifications
You must be signed in to change notification settings - Fork 31
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
try our hand at editing some next configs #45
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { builders, loadFile, generateCode, writeFile } from 'magicast'; | ||
|
||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
import { PACKAGE_NAME } from '../../constants.js'; | ||
|
||
const COMMON_TEMPLATE = ` | ||
|
||
// Everything below here added by the ${PACKAGE_NAME} CLI wizard. | ||
// You should probably clean this up. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is pretty creative. Does your average next.js user actually care, or should this be more like "feel free to clean this up"? |
||
|
||
const originalConfig = module.exports; | ||
|
||
const withNextVideo = require('${path.join(PACKAGE_NAME, 'process')}'); | ||
|
||
module.exports = withNextVideo(originalConfig); | ||
`; | ||
|
||
function extensionToType(filePath: string) { | ||
if (filePath.endsWith('.mjs')) { | ||
return 'module'; | ||
} | ||
|
||
return 'commonjs'; | ||
} | ||
|
||
export default async function updateNextConfigFile(parentDir: string = './') { | ||
let type: 'module' | 'commonjs' = 'commonjs'; | ||
let configPath: string | undefined = undefined; | ||
let configContents: string = ''; | ||
|
||
const pathsToCheck = ['next.config.js', 'next.config.mjs']; | ||
|
||
for (let i = 0; i < pathsToCheck.length; i++) { | ||
console.log('asdf'); | ||
mmcc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const filePath = path.join(parentDir, pathsToCheck[i]); | ||
let exists; | ||
try { | ||
exists = await fs.stat(filePath); | ||
} catch (e) { | ||
exists = false; | ||
} | ||
|
||
if (exists) { | ||
type = extensionToType(pathsToCheck[i]); | ||
configPath = filePath; | ||
configContents = await fs.readFile(filePath, 'utf-8'); | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (!configPath) { | ||
throw { error: 'not_found' }; | ||
} | ||
|
||
if (configContents.includes(PACKAGE_NAME)) { | ||
throw { error: 'already_added' }; | ||
} | ||
|
||
if (type === 'commonjs') { | ||
await fs.appendFile(configPath, COMMON_TEMPLATE); | ||
|
||
return { type, configPath }; | ||
} | ||
|
||
if (type === 'module') { | ||
const mod = await loadFile(configPath); | ||
|
||
mod.imports.$add({ | ||
from: PACKAGE_NAME, | ||
imported: 'withNextVideo', | ||
local: 'withNextVideo', | ||
}); | ||
|
||
const expressionToWrap = generateCode(mod.exports.default.$ast).code; | ||
mod.exports.default = builders.raw(`withNextVideo(${expressionToWrap})`); | ||
|
||
// @ts-ignore | ||
writeFile(mod, configPath); | ||
|
||
return { type, configPath }; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
|
||
const packageJsonContents = await fs.readFile(path.join(process.cwd(), 'package.json'), 'utf-8'); | ||
Check failure on line 4 in src/constants.ts GitHub Actions / build (18.x)
Check failure on line 4 in src/constants.ts GitHub Actions / build (20.x)
|
||
const packageJson = JSON.parse(packageJsonContents); | ||
|
||
export const VIDEOS_PATH = path.join(process.cwd(), '/videos'); | ||
export const PACKAGE_NAME = '@mux/next-video'; | ||
export const PACKAGE_NAME = packageJson.name; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import assert from 'node:assert'; | ||
import { after, describe, it } from 'node:test'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
import updateNextConfigFile from '../../../src/cli/lib/next-config.js'; | ||
|
||
function outputConfigName(configName: string) { | ||
if (configName.endsWith('.mjs')) { | ||
return 'next.config.mjs'; | ||
} | ||
|
||
// We have to return cjs files so we can import them async in a test. | ||
return 'next.config.js'; | ||
} | ||
|
||
describe('updateNextConfig', () => { | ||
let tmpDirs: string[] = []; | ||
|
||
async function createTempDirWithConfig(configName: string): Promise<string> { | ||
const dir = await fs.mkdtemp(path.join('tests', 'tmp-configs-')); | ||
tmpDirs.push(dir); | ||
|
||
const outputName = outputConfigName(configName); | ||
await fs.copyFile(path.join('tests', 'factories', configName), path.join(dir, outputName)); | ||
|
||
return dir; | ||
} | ||
|
||
after(() => { | ||
tmpDirs.forEach(async (dir) => { | ||
// await fs.rm(dir, { recursive: true, force: true }); | ||
}); | ||
}); | ||
|
||
it('should add next-video to the next.config.js file', async () => { | ||
const dirPath = await createTempDirWithConfig('next.config.js'); | ||
await updateNextConfigFile(dirPath); | ||
|
||
const updatedContents = await fs.readFile(path.join(dirPath, 'next.config.js'), 'utf-8'); | ||
|
||
assert(updatedContents.includes('next-video')); | ||
}); | ||
|
||
it('should add next-video to the next.config.mjs file', async () => { | ||
const dirPath = await createTempDirWithConfig('next.config.mjs'); | ||
await updateNextConfigFile(dirPath); | ||
|
||
console.log({ dirPath }); | ||
|
||
const updatedContents = await fs.readFile(path.join(dirPath, 'next.config.mjs'), 'utf-8'); | ||
|
||
assert(updatedContents.includes('next-video')); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
/* config options here */ | ||
}; | ||
|
||
module.exports = nextConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
/* config options here */ | ||
}; | ||
|
||
export default nextConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = (phase, { defaultConfig }) => { | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
/* config options here */ | ||
}; | ||
return nextConfig; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = async (phase, { defaultConfig }) => { | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
...defaultConfig, | ||
}; | ||
return nextConfig; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
|
||
import withNextVideo from '../src/with-next-video.js'; | ||
|
||
describe('withNextVideo', () => { | ||
it('should handle nextConfig being a function', async () => { | ||
const nextConfig = (phase, { defaultConfig }) => { | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
...defaultConfig, | ||
}; | ||
return nextConfig; | ||
}; | ||
|
||
const result = await withNextVideo(nextConfig); | ||
|
||
const configResult = await result('phase', { defaultConfig: {} }); | ||
|
||
assert(typeof configResult.webpack === 'function'); | ||
}); | ||
|
||
it('should handle nextConfig being a promise', async () => { | ||
const nextConfig = async (phase, { defaultConfig }) => { | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
...defaultConfig, | ||
}; | ||
return nextConfig; | ||
}; | ||
|
||
const result = await withNextVideo(nextConfig); | ||
|
||
const configResult = await result('phase', { defaultConfig: {} }); | ||
|
||
assert(typeof configResult.webpack === 'function'); | ||
}); | ||
|
||
it('should handle nextConfig being an object', async () => { | ||
const nextConfig = {}; | ||
|
||
const result = await withNextVideo(nextConfig); | ||
|
||
assert(typeof result.webpack === 'function'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we have the "manual install guide" we should add a link to that here.