-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try our hand at editing some next configs
- Loading branch information
Showing
16 changed files
with
572 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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'); | ||
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |