feat(webpack): suppress not found typescript warnings (#5635)

This commit is contained in:
Yama-Tomo 2019-05-10 19:18:44 +09:00 committed by Pooya Parsa
parent cc00469409
commit 1e4708e90f
7 changed files with 76 additions and 8 deletions

View File

@ -55,7 +55,8 @@ export default () => ({
vueStyle: {}
},
typescript: {
typeCheck: true
typeCheck: true,
ignoreNotFoundWarnings: false
},
styleResources: {},
plugins: [],

View File

@ -144,6 +144,7 @@ Object {
"terser": Object {},
"transpile": Array [],
"typescript": Object {
"ignoreNotFoundWarnings": false,
"typeCheck": true,
},
"watch": Array [],

View File

@ -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 [],

View File

@ -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[]
}

View File

@ -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') {

View File

@ -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)
})
}
}

View File

@ -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))
})
})