From 1e4708e90f5aa40dde420b1cbfed236c23da7f3f Mon Sep 17 00:00:00 2001 From: Yama-Tomo Date: Fri, 10 May 2019 19:18:44 +0900 Subject: [PATCH] feat(webpack): suppress not found typescript warnings (#5635) --- packages/config/src/config/build.js | 3 +- .../test/__snapshots__/options.test.js.snap | 1 + .../config/__snapshots__/index.test.js.snap | 2 + packages/config/types/build.d.ts | 3 +- packages/webpack/src/config/base.js | 22 +++++++++- packages/webpack/src/plugins/warnfix.js | 9 ++-- test/unit/typescript-build.test.js | 44 +++++++++++++++++++ 7 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 test/unit/typescript-build.test.js diff --git a/packages/config/src/config/build.js b/packages/config/src/config/build.js index 8e5d348c66..e0f8a282bc 100644 --- a/packages/config/src/config/build.js +++ b/packages/config/src/config/build.js @@ -55,7 +55,8 @@ export default () => ({ vueStyle: {} }, typescript: { - typeCheck: true + typeCheck: true, + ignoreNotFoundWarnings: false }, styleResources: {}, plugins: [], diff --git a/packages/config/test/__snapshots__/options.test.js.snap b/packages/config/test/__snapshots__/options.test.js.snap index b35b63ec58..b8d3dd436a 100644 --- a/packages/config/test/__snapshots__/options.test.js.snap +++ b/packages/config/test/__snapshots__/options.test.js.snap @@ -144,6 +144,7 @@ Object { "terser": Object {}, "transpile": Array [], "typescript": Object { + "ignoreNotFoundWarnings": false, "typeCheck": true, }, "watch": Array [], diff --git a/packages/config/test/config/__snapshots__/index.test.js.snap b/packages/config/test/config/__snapshots__/index.test.js.snap index 85d4eecb79..87026a0d0e 100644 --- a/packages/config/test/config/__snapshots__/index.test.js.snap +++ b/packages/config/test/config/__snapshots__/index.test.js.snap @@ -124,6 +124,7 @@ Object { "terser": Object {}, "transpile": Array [], "typescript": Object { + "ignoreNotFoundWarnings": false, "typeCheck": true, }, "watch": Array [], @@ -457,6 +458,7 @@ Object { "terser": Object {}, "transpile": Array [], "typescript": Object { + "ignoreNotFoundWarnings": false, "typeCheck": true, }, "watch": Array [], diff --git a/packages/config/types/build.d.ts b/packages/config/types/build.d.ts index db05940209..c7bcaf594d 100644 --- a/packages/config/types/build.d.ts +++ b/packages/config/types/build.d.ts @@ -60,7 +60,8 @@ export interface NuxtConfigurationBuild { terser?: TerserPluginOptions | boolean transpile?: (string | RegExp)[] typescript?: { - typeCheck?: { [key: string]: any } | boolean // TBD - Couldn't find typedefs for the forkTsCheckerWebpackPlugin options + typeCheck?: { [key: string]: any } | boolean, // TBD - Couldn't find typedefs for the forkTsCheckerWebpackPlugin options + ignoreNotFoundWarnings?: boolean } watch?: string[] } diff --git a/packages/webpack/src/config/base.js b/packages/webpack/src/config/base.js index 301eaeab50..29a8fa301a 100644 --- a/packages/webpack/src/config/base.js +++ b/packages/webpack/src/config/base.js @@ -368,8 +368,7 @@ export default class WebpackBaseConfig { plugins.push(...(buildOptions.plugins || [])) - // Hide warnings about plugins without a default export (#1179) - plugins.push(new WarnFixPlugin()) + plugins.push(new WarnFixPlugin(this.warningFixFilter())) // Build progress indicator plugins.push(new WebpackBar({ @@ -418,6 +417,25 @@ export default class WebpackBaseConfig { return plugins } + warningFixFilter() { + const { buildOptions, options: { _typescript = {} } } = this.buildContext + const filters = [ + // Hide warnings about plugins without a default export (#1179) + warn => warn.name === 'ModuleDependencyWarning' && + warn.message.includes(`export 'default'`) && + warn.message.includes('nuxt_plugin_') + ] + + if (_typescript.build && buildOptions.typescript && buildOptions.typescript.ignoreNotFoundWarnings) { + filters.push( + warn => warn.name === 'ModuleDependencyWarning' && + /export .* was not found in /.test(warn.message) + ) + } + + return warn => !filters.some(ignoreFilter => ignoreFilter(warn)) + } + extendConfig(config) { const { extend } = this.buildContext.buildOptions if (typeof extend === 'function') { diff --git a/packages/webpack/src/plugins/warnfix.js b/packages/webpack/src/plugins/warnfix.js index 582fc422be..40dc6c7642 100644 --- a/packages/webpack/src/plugins/warnfix.js +++ b/packages/webpack/src/plugins/warnfix.js @@ -1,10 +1,11 @@ export default class WarnFixPlugin { + constructor(filter) { + this.filter = filter + } + apply(compiler) /* istanbul ignore next */ { compiler.hooks.done.tap('warnfix-plugin', (stats) => { - stats.compilation.warnings = stats.compilation.warnings.filter(warn => - !(warn.name === 'ModuleDependencyWarning' && - warn.message.includes(`export 'default'`) && - warn.message.includes('nuxt_plugin_'))) + stats.compilation.warnings = stats.compilation.warnings.filter(this.filter) }) } } diff --git a/test/unit/typescript-build.test.js b/test/unit/typescript-build.test.js new file mode 100644 index 0000000000..3e9d8fe59c --- /dev/null +++ b/test/unit/typescript-build.test.js @@ -0,0 +1,44 @@ +import WebpackBaseConfig from '../../packages/webpack/src/config/base' + +const createWebpackBaseConfig = (typescriptBuild, ignoreNotFoundWarnings) => { + const builder = { + buildContext: { + buildOptions: { + typescript: { ignoreNotFoundWarnings }, + transpile: [] + }, + options: { + _typescript: { build: typescriptBuild } + } + } + } + + return new WebpackBaseConfig(builder) +} + +describe('warningFixFilter', () => { + let filters + const name = 'ModuleDependencyWarning' + + describe('disabled ignoreNotFoundWarnings', () => { + beforeEach(() => { + filters = createWebpackBaseConfig(true, false).warningFixFilter() + }) + + test('should be true', () => expect(filters({})).toBe(true)) + test('should be true', () => expect(filters({ name, message: 'error!' })).toBe(true)) + test('should be false', () => expect(filters({ name, message: `export 'default' nuxt_plugin_xxxx` })).toBe(false)) + test('should be true', () => expect(filters({ name, message: `export 'xxx' was not found in ` })).toBe(true)) + }) + + describe('enabled ignoreNotFoundWarnings', () => { + beforeEach(() => { + filters = createWebpackBaseConfig(true, true).warningFixFilter() + }) + + test('should be true', () => expect(filters({})).toBe(true)) + test('should be true', () => expect(filters({ name, message: 'error!' })).toBe(true)) + test('should be false', () => expect(filters({ name, message: `export 'default' nuxt_plugin_xxxx` })).toBe(false)) + test('should be false', () => expect(filters({ name, message: `export 'xxx' was not found in ` })).toBe(false)) + }) +})