diff --git a/package-lock.json b/package-lock.json index e61c62541..d39e10823 100644 --- a/package-lock.json +++ b/package-lock.json @@ -44820,7 +44820,7 @@ }, "packages/gasket-plugin-https-proxy": { "name": "@gasket/plugin-https-proxy", - "version": "7.0.9", + "version": "7.0.0-canary.0", "license": "MIT", "dependencies": { "http-proxy": "^1.18.1" @@ -45407,6 +45407,7 @@ "@gasket/plugin-cypress": "^7.0.9", "@gasket/plugin-express": "^7.0.14", "@gasket/plugin-https": "^7.0.9", + "@gasket/plugin-https-proxy": "7.0.0-canary.0", "@gasket/plugin-intl": "^7.0.14", "@gasket/plugin-jest": "^7.0.10", "@gasket/plugin-lint": "^7.0.3", diff --git a/packages/gasket-plugin-https-proxy/package.json b/packages/gasket-plugin-https-proxy/package.json index d0acaf740..8344b56b9 100644 --- a/packages/gasket-plugin-https-proxy/package.json +++ b/packages/gasket-plugin-https-proxy/package.json @@ -1,6 +1,6 @@ { "name": "@gasket/plugin-https-proxy", - "version": "7.0.9", + "version": "7.0.0-canary.0", "description": "Adds support for running an https proxy", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/gasket-plugin-nextjs/generator/markdown/app-router.md b/packages/gasket-plugin-nextjs/generator/markdown/app-router.md index f7ee676d0..e2de65812 100644 --- a/packages/gasket-plugin-nextjs/generator/markdown/app-router.md +++ b/packages/gasket-plugin-nextjs/generator/markdown/app-router.md @@ -3,9 +3,9 @@ This Gasket app uses Next.js 14 with [App Router] which allows for intuitive, file-based routing within the app directory. The integration with Next.js 14 enhances development by leveraging features like automatic static optimization and server-side rendering, ensuring a scalable and efficient web application. {{#if nextDevProxy}} -### Development Proxy +### HTTPS Proxy -The development proxy in this Gasket app forwards requests to the default Next.js server, enabling local development with HTTPS. +The HTTPS proxy in this Gasket app forwards requests to the default Next.js server, enabling HTTPS for development and support on deployed servers. {{/if}} {{#if typescript}} diff --git a/packages/gasket-plugin-nextjs/generator/markdown/page-router.md b/packages/gasket-plugin-nextjs/generator/markdown/page-router.md index 5c1f9d8e0..6321caf16 100644 --- a/packages/gasket-plugin-nextjs/generator/markdown/page-router.md +++ b/packages/gasket-plugin-nextjs/generator/markdown/page-router.md @@ -5,7 +5,7 @@ This Gasket app uses Next.js 14 with [Page Router] which relies on the tradition {{#if nextDevProxy}} ### Development Proxy -The development proxy in this Gasket app forwards requests to the default Next.js server, enabling local development with HTTPS. +The HTTPS proxy in this Gasket app forwards requests to the default Next.js server, enabling HTTPS for development and support on deployed servers. {{/if}} {{#if typescript}} {{#if (eq nextServerType 'pageRouter')}} diff --git a/packages/gasket-plugin-nextjs/generator/next/server.js b/packages/gasket-plugin-nextjs/generator/next/server.js index a54b15ba0..ff0a67b78 100644 --- a/packages/gasket-plugin-nextjs/generator/next/server.js +++ b/packages/gasket-plugin-nextjs/generator/next/server.js @@ -1,2 +1,7 @@ import gasket from './gasket.js'; +{{/if}} +{{#if nextDevProxy }} +gasket.actions.startProxyServer(); +{{else}} gasket.actions.startServer(); +{{/if}} diff --git a/packages/gasket-plugin-nextjs/lib/create.js b/packages/gasket-plugin-nextjs/lib/create.js index d09e2f676..b2b0cea4c 100644 --- a/packages/gasket-plugin-nextjs/lib/create.js +++ b/packages/gasket-plugin-nextjs/lib/create.js @@ -153,20 +153,24 @@ function addNpmScripts({ pkg, nextServerType, nextDevProxy, typescript, hasGaske }; if (nextServerType === 'customServer') { + scripts.start = 'node server.js'; + scripts.local = `GASKET_DEV=1 ${watcher} server.${fileExtension}`; if (typescript) { - scripts.start = 'node dist/server.js'; scripts['build:tsc'] = 'tsc -p ./tsconfig.server.json'; scripts.build = 'npm run build:tsc && next build'; + scripts.start = 'node dist/server.js'; scripts.local = `npm run build:tsc && GASKET_DEV=1 ${watcher} server.${fileExtension}`; - } else { - scripts.start = 'node server.js'; - scripts.build = 'next build'; - scripts.local = `GASKET_DEV=1 ${watcher} server.${fileExtension}`; } } else if (nextDevProxy) { - scripts.local = `${scripts.local} & ${watcher} server.${fileExtension}`; - scripts['start:local'] = `${scripts.start} & ${bin} server.${fileExtension}`; - scripts.preview = `${scripts.preview} & ${bin} server.${fileExtension}`; + scripts['start:https'] = `node server.js`; + scripts['local:https'] = `${watcher} server.${fileExtension}`; + scripts.start = `next start & npm run start:https`; + scripts.local = `next dev & npm run local:https`; + if (typescript) { + scripts['build:tsc'] = 'tsc -p ./tsconfig.server.json'; + scripts.build = 'npm run build:tsc && next build'; + scripts['start:https'] = `node dist/server.js`; + } } pkg.add('scripts', scripts); @@ -176,7 +180,7 @@ function addConfig({ gasketConfig, nextDevProxy, nextServerType }) { gasketConfig.addPlugin('pluginNextjs', name); if (nextDevProxy && nextServerType !== 'customServer') { - gasketConfig.add('devProxy', { + gasketConfig.add('httpsProxy', { protocol: 'http', hostname: 'localhost', port: 80, diff --git a/packages/gasket-plugin-nextjs/lib/prompt.js b/packages/gasket-plugin-nextjs/lib/prompt.js index a8c37af82..43fabbfad 100644 --- a/packages/gasket-plugin-nextjs/lib/prompt.js +++ b/packages/gasket-plugin-nextjs/lib/prompt.js @@ -33,7 +33,7 @@ async function promptNextDevProxy(context, prompt) { const { nextDevProxy } = await prompt([ { name: 'nextDevProxy', - message: 'Do you want to add a proxy for the Next.js dev server?', + message: 'Do you want an HTTPS proxy for the Next.js server?', type: 'confirm', default: false } diff --git a/packages/gasket-plugin-nextjs/test/create.test.js b/packages/gasket-plugin-nextjs/test/create.test.js index d1bf21d54..0c7c133c3 100644 --- a/packages/gasket-plugin-nextjs/test/create.test.js +++ b/packages/gasket-plugin-nextjs/test/create.test.js @@ -289,11 +289,12 @@ describe('create hook', () => { mockContext.nextDevProxy = true; await create.handler({}, mockContext); expect(mockContext.pkg.add).toHaveBeenCalledWith('scripts', { - 'local': 'next dev & nodemon server.js', - 'start:local': 'next start & node server.js', - 'preview': 'npm run build && npm run start & node server.js', - 'build': 'next build', - 'start': 'next start' + 'start:https': 'node server.js', + 'local:https': 'nodemon server.js', + 'start': 'next start & npm run start:https', + 'local': 'next dev & npm run local:https', + 'preview': 'npm run build && npm run start', + 'build': 'next build' }); }); @@ -332,11 +333,13 @@ describe('create hook', () => { mockContext.hasGasketIntl = true; await create.handler({}, mockContext); expect(mockContext.pkg.add).toHaveBeenCalledWith('scripts', { - 'local': 'next dev & tsx watch server.ts', - 'start:local': 'next start & tsx server.ts', - 'preview': 'npm run build && npm run start & tsx server.ts', - 'build': 'next build', - 'start': 'next start', + 'start:https': 'node dist/server.js', + 'local:https': 'tsx watch server.ts', + 'start': 'next start & npm run start:https', + 'local': 'next dev & npm run local:https', + 'build:tsc': 'tsc -p ./tsconfig.server.json', + 'build': 'npm run build:tsc && next build', + 'preview': 'npm run build && npm run start', 'prebuild': 'tsx gasket.ts build' }); }); @@ -349,10 +352,10 @@ describe('create hook', () => { expect(mockContext.gasketConfig.addPlugin).toHaveBeenCalledWith('pluginNextjs', name); }); - it('adds devProxy config', async function () { + it('adds httpsProxy config', async function () { mockContext.nextDevProxy = true; await create.handler({}, mockContext); - expect(mockContext.gasketConfig.add).toHaveBeenCalledWith('devProxy', { + expect(mockContext.gasketConfig.add).toHaveBeenCalledWith('httpsProxy', { protocol: 'http', hostname: 'localhost', port: 80, diff --git a/packages/gasket-plugin-nextjs/test/prompt.test.js b/packages/gasket-plugin-nextjs/test/prompt.test.js index c0de13b17..c62e49f76 100644 --- a/packages/gasket-plugin-nextjs/test/prompt.test.js +++ b/packages/gasket-plugin-nextjs/test/prompt.test.js @@ -34,7 +34,7 @@ describe('prompt hook', () => { expect(askNextServer.message).toEqual('Which server type would you like to use?'); expect(askNextServer.type).toEqual('list'); expect(askDevServer.name).toEqual('nextDevProxy'); - expect(askDevServer.message).toEqual('Do you want to add a proxy for the Next.js dev server?'); + expect(askDevServer.message).toEqual('Do you want an HTTPS proxy for the Next.js server?'); expect(askDevServer.type).toEqual('confirm'); expect(askSitemap.name).toEqual('addSitemap'); expect(askSitemap.message).toEqual('Do you want to add a sitemap?'); @@ -102,7 +102,7 @@ describe('prompt hook', () => { expect(mockPrompt).toHaveBeenCalledWith([ { name: 'nextDevProxy', - message: 'Do you want to add a proxy for the Next.js dev server?', + message: 'Do you want an HTTPS proxy for the Next.js server?', type: 'confirm', default: false } diff --git a/packages/gasket-plugin-typescript/generator/shared/server.ts b/packages/gasket-plugin-typescript/generator/shared/server.ts index bf585d2b8..aef15afa2 100644 --- a/packages/gasket-plugin-typescript/generator/shared/server.ts +++ b/packages/gasket-plugin-typescript/generator/shared/server.ts @@ -4,5 +4,8 @@ import gasket from './gasket.js'; {{#if apiApp }} import './routes/index.js'; {{/if}} - +{{#if nextDevProxy }} +gasket.actions.startProxyServer(); +{{else}} gasket.actions.startServer(); +{{/if}} diff --git a/packages/gasket-plugin-typescript/lib/create.js b/packages/gasket-plugin-typescript/lib/create.js index b83f55a9e..299047261 100644 --- a/packages/gasket-plugin-typescript/lib/create.js +++ b/packages/gasket-plugin-typescript/lib/create.js @@ -47,19 +47,19 @@ module.exports = async function create(gasket, context) { await readme.markdownFile(path.join(generatorDir, 'markdown/README.md')); } - // Files for customServer + // Files for Next.js customServer if (nextServerType === 'customServer') { files.add(`${generatorDir}/next/*`, `${generatorDir}/shared/*`); gitignore?.add('dist', 'TypeScript build output'); pkg.add('eslintIgnore', ['dist']); } - // Files for dev proxy w/o customServer + // Files for Next.js default server w/ https proxy if (nextDevProxy) { - files.add(`${generatorDir}/next/*(tsconfig).json`, `${generatorDir}/shared/*`); + files.add(`${generatorDir}/next/*`, `${generatorDir}/shared/*`); } - // Files for defaultServer w/o dev proxy + // Files for Next.js default server w/o dev proxy if (nextDevProxy === false && nextServerType !== 'customServer') { files.add(`${generatorDir}/next/*(tsconfig).json`); } diff --git a/packages/gasket-plugin-typescript/test/create.test.js b/packages/gasket-plugin-typescript/test/create.test.js index 4dd489225..18489624c 100644 --- a/packages/gasket-plugin-typescript/test/create.test.js +++ b/packages/gasket-plugin-typescript/test/create.test.js @@ -100,7 +100,10 @@ describe('create hook', () => { it('adds files for customServer', () => { mockContext.nextServerType = 'customServer'; create({}, mockContext); - expect(mockContext.files.add).toHaveBeenCalledWith(expect.stringMatching(/generator\/next\/\*$/), expect.stringMatching(/generator\/shared\/\*$/)); + expect(mockContext.files.add).toHaveBeenCalledWith( + expect.stringMatching(/generator\/next\/\*$/), + expect.stringMatching(/generator\/shared\/\*$/) + ); }); it('adds files for defaultServer w/o dev proxy', () => { @@ -126,7 +129,10 @@ describe('create hook', () => { it('adds files for dev proxy w/o customServer', () => { mockContext.nextDevProxy = true; create({}, mockContext); - expect(mockContext.files.add).toHaveBeenCalledWith(expect.stringMatching(/generator\/next\/\*\(tsconfig\).json$/), expect.stringMatching(/generator\/shared\/\*$/)); + expect(mockContext.files.add).toHaveBeenCalledWith( + expect.stringMatching(/generator\/next\/\*$/), + expect.stringMatching(/generator\/shared\/\*$/) + ); }); }); }); diff --git a/packages/gasket-preset-nextjs/lib/preset-config.js b/packages/gasket-preset-nextjs/lib/preset-config.js index b745725c5..aaafe308d 100644 --- a/packages/gasket-preset-nextjs/lib/preset-config.js +++ b/packages/gasket-preset-nextjs/lib/preset-config.js @@ -1,4 +1,5 @@ import pluginHttps from '@gasket/plugin-https'; +import pluginHttpsProxy from '@gasket/plugin-https-proxy'; import pluginNext from '@gasket/plugin-nextjs'; import pluginIntl from '@gasket/plugin-intl'; import pluginWebpack from '@gasket/plugin-webpack'; @@ -15,7 +16,6 @@ export default async function presetConfig(gasket, context) { let typescriptPlugin; const plugins = [ - pluginHttps, pluginNext, pluginIntl, pluginWebpack, @@ -23,13 +23,17 @@ export default async function presetConfig(gasket, context) { pluginLint ]; - if (context.nextServerType === 'customServer') { const frameworkPlugin = await import('@gasket/plugin-express'); + plugins.push(pluginHttps); plugins.push(frameworkPlugin.default || frameworkPlugin); } + if (context.nextDevProxy) { + plugins.push(pluginHttpsProxy); + } + if ('testPlugins' in context && context.testPlugins.length > 0) { await Promise.all(context.testPlugins.map(async (testPlugin) => { const plugin = await import(testPlugin); diff --git a/packages/gasket-preset-nextjs/package.json b/packages/gasket-preset-nextjs/package.json index baaf7feab..a512768fc 100644 --- a/packages/gasket-preset-nextjs/package.json +++ b/packages/gasket-preset-nextjs/package.json @@ -34,6 +34,7 @@ "@gasket/plugin-cypress": "^7.0.9", "@gasket/plugin-express": "^7.0.14", "@gasket/plugin-https": "^7.0.9", + "@gasket/plugin-https-proxy": "7.0.0-canary.0", "@gasket/plugin-intl": "^7.0.14", "@gasket/plugin-jest": "^7.0.10", "@gasket/plugin-lint": "^7.0.3", diff --git a/packages/gasket-preset-nextjs/test/preset-config.test.js b/packages/gasket-preset-nextjs/test/preset-config.test.js index c290718f4..ba41c6e98 100644 --- a/packages/gasket-preset-nextjs/test/preset-config.test.js +++ b/packages/gasket-preset-nextjs/test/preset-config.test.js @@ -28,7 +28,6 @@ describe('presetConfig', () => { const config = await presetConfig({}, mockContext); const expected = [ expect.objectContaining({ name: '@gasket/plugin-webpack' }), - expect.objectContaining({ name: '@gasket/plugin-https' }), expect.objectContaining({ name: '@gasket/plugin-nextjs' }), expect.objectContaining({ name: '@gasket/plugin-winston' }), expect.objectContaining({ name: '@gasket/plugin-lint' }) @@ -56,15 +55,45 @@ describe('presetConfig', () => { ); }); - describe('adds server framework plugin', () => { + describe('no http or https-proxy plugin', () => { + it('express', async () => { + mockContext.nextServerType = 'appRouter'; + const config = await presetConfig({}, mockContext); + expect(config.plugins).not.toEqual( + expect.arrayContaining([ + expect.objectContaining({ name: '@gasket/plugin-https' }) + ]) + ); + expect(config.plugins).not.toEqual( + expect.arrayContaining([ + expect.objectContaining({ name: '@gasket/plugin-https-proxy' }) + ]) + ); + }); + }); + + describe('adds http w/ server framework plugins', () => { it('express', async () => { mockContext.nextServerType = 'customServer'; const config = await presetConfig({}, mockContext); expect(config.plugins).toEqual( expect.arrayContaining([ + expect.objectContaining({ name: '@gasket/plugin-https' }), expect.objectContaining({ name: '@gasket/plugin-express' }) ]) ); }); }); + + describe('adds https-proxy plugin', () => { + it('express', async () => { + mockContext.nextDevProxy = true; + const config = await presetConfig({}, mockContext); + expect(config.plugins).toEqual( + expect.arrayContaining([ + expect.objectContaining({ name: '@gasket/plugin-https-proxy' }) + ]) + ); + }); + }); });