Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dev": "tsc --watch",
"build": "tsc",
"dev-example": "webpack-dev-server --config example/webpack.config.js --inline --hot",
"build-example": "rm -rf example/dist && webpack --config example/webpack.config.js --env.prod",
"build-example": "rimraf -rf example/dist && webpack --config example/webpack.config.js --env.prod",
"lint": "prettier --write --parser typescript \"src/**/*.ts\"",
"prepublishOnly": "tsc"
},
Expand Down Expand Up @@ -54,6 +54,7 @@
"prettier": "^1.19.1",
"pug": "^2.0.4",
"pug-plain-loader": "^1.0.0",
"rimraf": "^3.0.2",
"stylus": "^0.54.7",
"stylus-loader": "^3.0.2",
"ts-jest": "^24.2.0",
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
parse,
TemplateCompiler,
CompilerOptions,
CompilerError,
SFCBlock,
SFCTemplateCompileOptions,
SFCStyleBlock
Expand Down Expand Up @@ -82,7 +83,7 @@ const loader: webpack.loader.Loader = function(source: string) {
})

if (errors.length) {
errors.forEach(err => {
errors.forEach((err: CompilerError) => {
formatError(err, source, resourcePath)
loaderContext.emitError(err)
})
Expand All @@ -109,7 +110,7 @@ const loader: webpack.loader.Loader = function(source: string) {
const id = hash(isProduction ? shortFilePath + '\n' + source : shortFilePath)

// feature information
const hasScoped = descriptor.styles.some(s => s.scoped)
const hasScoped = descriptor.styles.some((s: SFCStyleBlock) => s.scoped)
const needsHotReload =
!isServer &&
!isProduction &&
Expand Down Expand Up @@ -207,7 +208,7 @@ const loader: webpack.loader.Loader = function(source: string) {
code += `\n/* custom blocks */\n`
code +=
descriptor.customBlocks
.map((block, i) => {
.map((block: SFCBlock, i: number) => {
const src = block.attrs.src || resourcePath
const attrsQuery = attrsToQuery(block.attrs)
const blockTypeQuery = `&blockType=${qs.escape(block.type)}`
Expand Down
35 changes: 20 additions & 15 deletions src/pitcher.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import * as webpack from 'webpack'
import qs from 'querystring'
import loaderUtils from 'loader-utils'
import qs from 'querystring'
import * as webpack from 'webpack'

const selfPath = require.resolve('./index')
// const templateLoaderPath = require.resolve('./templateLoader')
const stylePostLoaderPath = require.resolve('./stylePostLoader')

// @types/webpack doesn't provide the typing for loaderContext.loaders...
interface Loader {
type Loader = string | ObjectLoader

interface ObjectLoader {
request: string
path: string
query: string
pitchExecuted: boolean
}

const isESLintLoader = (l: Loader) => /(\/|\\|@)eslint-loader/.test(l.path)
const isNullLoader = (l: Loader) => /(\/|\\|@)null-loader/.test(l.path)
const isCSSLoader = (l: Loader) => /(\/|\\|@)css-loader/.test(l.path)
const isCacheLoader = (l: Loader) => /(\/|\\|@)cache-loader/.test(l.path)
const isNotPitcher = (l: Loader) => l.path !== __filename
function getLoaderPath(loader: Loader): string {
return typeof loader === 'string' ? loader : loader.path
}

const isESLintLoader = (l: Loader) =>
/([\/\\@])eslint-loader/.test(getLoaderPath(l))
const isNullLoader = (l: Loader) =>
/([\/\\@])null-loader/.test(getLoaderPath(l))
const isCSSLoader = (l: Loader) => /([\/\\@])css-loader/.test(getLoaderPath(l))
const isCacheLoader = (l: Loader) =>
/([\/\\@])cache-loader/.test(getLoaderPath(l))
const isNotPitcher = (l: Loader) => getLoaderPath(l) !== __filename

const pitcher: webpack.loader.Loader = code => code

Expand All @@ -28,8 +37,7 @@ module.exports = pitcher
// and transform it into appropriate requests.
pitcher.pitch = function() {
const context = this as webpack.loader.LoaderContext
const rawLoaders = context.loaders.filter(isNotPitcher)
let loaders = rawLoaders
let loaders = context.loaders.filter(isNotPitcher)

// do not inject if user uses null-loader to void the type (#1239)
if (loaders.some(isNullLoader)) {
Expand Down Expand Up @@ -115,14 +123,11 @@ function genRequest(loaders: Loader[], context: webpack.loader.LoaderContext) {
function shouldIgnoreCustomBlock(loaders: Loader[]) {
const actualLoaders = loaders.filter(loader => {
// vue-loader
if (loader.path === selfPath) {
if (getLoaderPath(loader) === selfPath) {
return false
}
// cache-loader
if (isCacheLoader(loader)) {
return false
}
return true
return !isCacheLoader(loader)
})
return actualLoaders.length === 0
}
13 changes: 4 additions & 9 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class VueLoaderPlugin implements webpack.Plugin {
const vueUse = vueRule.use as webpack.RuleSetLoader[]
// get vue-loader options
const vueLoaderUseIndex = vueUse.findIndex(u => {
return /^vue-loader|(\/|\\|@)vue-loader/.test(u.loader || '')
return /^vue-loader|([\/\\@])vue-loader/.test(u.loader || '')
})

if (vueLoaderUseIndex < 0) {
Expand Down Expand Up @@ -143,10 +143,8 @@ function cloneRule(rule: webpack.RuleSetRule) {
if (resource && !resource(fakeResourcePath)) {
return false
}
if (resourceQuery && !resourceQuery(query)) {
return false
}
return true

return !(resourceQuery && !resourceQuery(query))
}
}

Expand Down Expand Up @@ -182,10 +180,7 @@ function cloneRuleForRenderFn(rule: webpack.RuleSetRule) {
if (resource && !resource(fakeResourcePath)) {
return false
}
if (resourceQuery && !resourceQuery(query)) {
return false
}
return true
return !(resourceQuery && !resourceQuery(query))
}
}

Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6637,6 +6637,13 @@ rimraf@^3.0.0:
dependencies:
glob "^7.1.3"

rimraf@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==
dependencies:
glob "^7.1.3"

ripemd160@^2.0.0, ripemd160@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.2.tgz#a1c1a6f624751577ba5d07914cbc92850585890c"
Expand Down