-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,84 @@ | ||
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. | ||
// 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++) { | ||
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
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,53 @@ | ||
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); | ||
|
||
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'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.