From 068bb8125f4c6559406917e406eec36a2c92ca34 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 25 Apr 2018 09:39:06 -0400 Subject: [PATCH] fix: warn missing plugin --- lib/index.js | 9 +++++++++ lib/plugin.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1fc558c41..7ff308b0d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,9 +10,18 @@ const genStylesCode = require('./codegen/styleInjection') const { genHotReloadCode } = require('./codegen/hotReload') const genCustomBlocksCode = require('./codegen/customBlocks') const componentNormalizerPath = require.resolve('./runtime/componentNormalizer') +const { NS } = require('./plugin') module.exports = function (source) { const loaderContext = this + + if (!loaderContext[NS]) { + loaderContext.emitError(new Error( + `vue-loader was used without the corresponding plugin. ` + + `Make sure to include VueLoaderPlugin in your webpack config.` + )) + } + const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r) const { diff --git a/lib/plugin.js b/lib/plugin.js index afe5d3e15..c6def8453 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -1,9 +1,30 @@ +const fs = require('fs') +const path = require('path') const qs = require('querystring') const RuleSet = require('webpack/lib/RuleSet') -// TODO handle vueRule with oneOf -module.exports = class VueLoaderPlugin { +const id = 'vue-loader-plugin' +const NS = path.dirname(fs.realpathSync(__filename)) + +class VueLoaderPlugin { apply (compiler) { + // add NS marker so that the loader can detect and report missing plugin + if (compiler.hooks) { + // webpack 4 + compiler.hooks.compilation.tap(id, compilation => { + compilation.hooks.normalModuleLoader.tap(id, loaderContext => { + loaderContext[NS] = true + }) + }) + } else { + // webpack < 4 + compiler.plugin('compilation', compilation => { + compilation.plugin('normal-module-loader', loaderContext => { + loaderContext[NS] = true + }) + }) + } + // get a hold of the raw rules const rawRules = compiler.options.module.rules // use webpack's RuleSet utility to normalize user rules @@ -175,3 +196,6 @@ function cleanIdent (use) { } return use } + +VueLoaderPlugin.NS = NS +module.exports = VueLoaderPlugin