From 240c4ce77c8b8f8e4797c4360588f89c83ec3f98 Mon Sep 17 00:00:00 2001 From: JerryZou Date: Thu, 29 Oct 2020 18:05:04 +0800 Subject: [PATCH] fix(ssr): avoid throw error after HMR in SsrModule --- src/ssr/ssr-module.ts | 12 ++++++++++-- test/specs/ssr.spec.tsx | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ssr/ssr-module.ts b/src/ssr/ssr-module.ts index 7c943cf2..28a32cd9 100644 --- a/src/ssr/ssr-module.ts +++ b/src/ssr/ssr-module.ts @@ -11,13 +11,13 @@ export const SSRModule = (config: string | InjectableConfig & { name: string }) let name: string if (typeof config === 'string') { if (configSets.has(config)) { - throw new Error(`Duplicated Module name: ${config}`) + reportDuplicated(config) } name = config configSets.add(config) } else if (config && typeof config.name === 'string') { if (configSets.has(config.name)) { - throw new Error(`Duplicated Module name: ${config.name}`) + reportDuplicated(config.name) } configSets.add(config.name) name = config.name @@ -33,3 +33,11 @@ export const SSRModule = (config: string | InjectableConfig & { name: string }) return Injectable(injectableConfig)(target) } } + +function reportDuplicated(moduleName: string) { + if (process.env.NODE_ENV === 'production') { + throw new Error(`Duplicated Module name: ${moduleName}`) + } + // avoid to throw error after HMR + console.warn(`Duplicated Module name: ${moduleName}`) +} diff --git a/test/specs/ssr.spec.tsx b/test/specs/ssr.spec.tsx index 0cf11960..f52c55d5 100644 --- a/test/specs/ssr.spec.tsx +++ b/test/specs/ssr.spec.tsx @@ -107,11 +107,13 @@ describe('SSR specs:', () => { beforeAll(() => { // @ts-ignore process.env.ENABLE_AYANAMI_SSR = 'true' + process.env.NODE_ENV = 'production' }) afterAll(() => { // @ts-ignore process.env.ENABLE_AYANAMI_SSR = 'false' + delete process.env.NODE_ENV }) it('should throw if module name not given', () => { @@ -172,6 +174,9 @@ describe('SSR specs:', () => { expect(generateException1).toThrow() expect(generateException2).toThrow() expect(generateException3).toThrow() + process.env.NODE_ENV = 'development' + expect(generateException1).not.toThrow() + expect(generateException2).not.toThrow() }) it('should run ssr effects', async () => {