|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Florent Cailhol @ooflorent |
| 4 | +*/ |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +const WebpackError = require("./WebpackError"); |
| 8 | + |
| 9 | +class WarnAmbiguousSourceTypePlugin { |
| 10 | + apply(compiler) { |
| 11 | + compiler.hooks.compilation.tap( |
| 12 | + "WarnAmbiguousSourceTypePlugin", |
| 13 | + (compilation, { normalModuleFactory }) => { |
| 14 | + normalModuleFactory.hooks.parser |
| 15 | + .for("javascript/auto") |
| 16 | + .tap("WarnAmbiguousSourceTypePlugin", parser => { |
| 17 | + function flagAsDynamic() { |
| 18 | + const module = parser.state.module; |
| 19 | + if (!module.buildInfo) module.buildInfo = {}; |
| 20 | + module.buildInfo.dynamic = true; |
| 21 | + } |
| 22 | + parser.hooks.call |
| 23 | + .for("define") |
| 24 | + .tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic); |
| 25 | + parser.hooks.call |
| 26 | + .for("require") |
| 27 | + .tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic); |
| 28 | + parser.hooks.call |
| 29 | + .for("require.resolve") |
| 30 | + .tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic); |
| 31 | + parser.hooks.expression |
| 32 | + .for("module") |
| 33 | + .tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic); |
| 34 | + parser.hooks.expression |
| 35 | + .for("exports") |
| 36 | + .tap("WarnAmbiguousSourceTypePlugin", flagAsDynamic); |
| 37 | + }); |
| 38 | + compilation.hooks.finishModules.tap( |
| 39 | + "WarnAmbiguousSourceTypePlugin", |
| 40 | + modules => { |
| 41 | + for (const module of modules) { |
| 42 | + if (module.type === "javascript/auto") { |
| 43 | + if ( |
| 44 | + module.buildMeta && |
| 45 | + module.buildMeta.exportsType && |
| 46 | + module.buildInfo && |
| 47 | + module.buildInfo.dynamic |
| 48 | + ) { |
| 49 | + compilation.warnings.push( |
| 50 | + new AmbiguousSourceTypeWarning(module) |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + ); |
| 57 | + } |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class AmbiguousSourceTypeWarning extends WebpackError { |
| 63 | + constructor(module) { |
| 64 | + super(); |
| 65 | + |
| 66 | + this.name = "AmbiguousSourceTypeWarning"; |
| 67 | + this.message = "Source type is ambiguous"; |
| 68 | + this.origin = this.module = module; |
| 69 | + |
| 70 | + Error.captureStackTrace(this, this.constructor); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +module.exports = WarnAmbiguousSourceTypePlugin; |
0 commit comments