Skip to content

Commit

Permalink
fix: warn missing plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 25, 2018
1 parent 949ba66 commit 068bb81
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 26 additions & 2 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -175,3 +196,6 @@ function cleanIdent (use) {
}
return use
}

VueLoaderPlugin.NS = NS
module.exports = VueLoaderPlugin

0 comments on commit 068bb81

Please sign in to comment.