Skip to content

Commit

Permalink
fix: import next.config.js in Windows (#159)
Browse files Browse the repository at this point in the history
* chore: add windows ci test

* fix: dynamic import file path in Windows

* fix: try forward slashes

* chore: log out file path

* fix: pathToFileURL().href
related issue nodejs/node#34765

* chore: log out error

* fix: try adding cwd()

* fix: cross platform write file script
related shelljs/shx#192

* fix: try next-video/process import

* fix: cross platform glob expansion

* chore: use codecov v4

* chore: move next.config.js mock test file
  • Loading branch information
luwes committed Feb 2, 2024
1 parent 6a6da17 commit 969d6e8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ env:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x]

os: [ubuntu-latest, windows-latest]
node-version: [18, 20]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -29,6 +28,6 @@ jobs:
- run: npm test
- run: npm run coverage
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"types:cjs": "tsc --outDir dist/cjs",
"prebuild": "npm run clean && npm run types",
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "esbuild src/*.ts* src/*/*.ts* src/*/*/*.ts* --outdir=dist --format=esm --target=es2020",
"build:cjs": "esbuild src/*.ts* src/*/*.ts* src/*/*/*.ts* --outdir=dist/cjs --platform=node --format=cjs --target=es2020 --define:import.meta.url=\\\"\\\"",
"postbuild:cjs": "echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
"build:esm": "esbuild \"src/**/*.ts*\" --outdir=dist --format=esm --target=es2020",
"build:cjs": "esbuild \"src/**/*.ts*\" --outdir=dist/cjs --platform=node --format=cjs --target=es2020 --define:import.meta.url=\\\"\\\"",
"postbuild:cjs": "node --eval \"fs.writeFileSync('./dist/cjs/package.json', '{\\\"type\\\": \\\"commonjs\\\"}')\"",
"prepare": "npm run build",
"cli": "node --loader tsx --no-warnings ./src/cli",
"test": "glob -c \"c8 --src src --exclude 'tests/**' node --loader tsx --no-warnings --test\" \"./tests/**/*.test.{ts,tsx}\"",
"test": "glob -c \"c8 --src src --exclude 'next.config.js' --exclude 'tests/**' --exclude 'dist/**' node --loader tsx --no-warnings --test\" \"./tests/**/*.test.{ts,tsx}\"",
"coverage": "c8 report --reporter=text-lcov > ./coverage/lcov.info"
},
"peerDependencies": {
Expand Down
17 changes: 13 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { env } from 'node:process';
import { env, cwd } from 'node:process';
import path from 'node:path';
import { pathToFileURL } from 'node:url';

/**
* Video configurations
Expand Down Expand Up @@ -48,14 +49,22 @@ export async function getVideoConfig(): Promise<VideoConfigComplete> {
// Import the app's next.config.(m)js file so the env variable
// __NEXT_VIDEO_OPTS set in with-next-video.ts can be used.
try {
await import(/* webpackIgnore: true */ path.resolve('next.config.js'));
} catch {
await importConfig('next.config.js');
} catch (err) {
console.error(err);

try {
await import(/* webpackIgnore: true */ path.resolve('next.config.mjs'));
await importConfig('next.config.mjs');
} catch {
console.error('Failed to load next.config.js or next.config.mjs');
}
}
}
return JSON.parse(env['__NEXT_VIDEO_OPTS'] ?? '{}');
}

async function importConfig(file: string) {
const absFilePath = path.resolve(cwd(), file);
const fileUrl = pathToFileURL(absFilePath).href;
return import(/* webpackIgnore: true */ fileUrl);
}
14 changes: 8 additions & 6 deletions tests/cli/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ describe('cli', () => {

async function createTempDir(): Promise<string> {
// Create a temporary directory and populate it with some files.
const dir = await fs.mkdtemp(path.join('tests', 'tmp-videos-'));

const dir = await fs.mkdtemp('tmp-videos-');
tmpDirs.push(dir);

return dir;
}

before(() => {
process.chdir('tests');

process.env.MUX_TOKEN_ID = 'fake-token-id';
process.env.MUX_TOKEN_SECRET = 'fake-token-secret';

Expand All @@ -79,12 +79,14 @@ describe('cli', () => {
});
});

after(() => {
after(async () => {
server.close();

tmpDirs.forEach(async (dir) => {
for (const dir of tmpDirs) {
await fs.rm(dir, { recursive: true, force: true });
});
}

process.chdir('../');
});

describe('sync', () => {
Expand Down
22 changes: 22 additions & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'node:assert';
import { describe, it, before, after } from 'node:test';
import { getVideoConfig } from '../src/config.js';

describe('config', () => {
before(() => {
process.chdir('tests');
});

after(() => {
process.chdir('../');
});

it('getVideoConfig', async () => {
// Test that the default config is returned if no next.config.js file is found.
const videoConfig = await getVideoConfig();
assert.equal(videoConfig.folder, 'videos');
assert.equal(videoConfig.path, '/api/video');
assert.equal(videoConfig.provider, 'mux');
assert.deepStrictEqual(videoConfig.providerConfig, {});
});
});
7 changes: 7 additions & 0 deletions tests/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is used in the tests to mock the next.config.js file!!!
import { withNextVideo } from 'next-video/process';

/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withNextVideo(nextConfig);

0 comments on commit 969d6e8

Please sign in to comment.