Skip to content

Inherit existing queries #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 4, 2018
Merged
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
4 changes: 3 additions & 1 deletion lib/codegen/customBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const { attrsToQuery } = require('./utils')
module.exports = function genCustomBlocksCode (
blocks,
resourcePath,
resourceQuery,
stringifyRequest
) {
return `\n/* custom blocks */\n` + blocks.map((block, i) => {
const src = block.attrs.src || resourcePath
const attrsQuery = attrsToQuery(block.attrs)
const issuerQuery = block.attrs.src ? `&issuerPath=${qs.escape(resourcePath)}` : ''
const query = `?vue&type=custom&index=${i}&blockType=${qs.escape(block.type)}${issuerQuery}${attrsQuery}`
const inheritQuery = resourceQuery ? `&${resourceQuery.slice(1)}` : ''
const query = `?vue&type=custom&index=${i}&blockType=${qs.escape(block.type)}${issuerQuery}${attrsQuery}${inheritQuery}`
return (
`import block${i} from ${stringifyRequest(src + query)}\n` +
`if (typeof block${i} === 'function') block${i}(component)`
Expand Down
3 changes: 2 additions & 1 deletion lib/codegen/styleInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ module.exports = function genStyleInjectionCode (
function genStyleRequest (style, i) {
const src = style.src || resourcePath
const attrsQuery = attrsToQuery(style.attrs, 'css')
const inheritQuery = `&${loaderContext.resourceQuery.slice(1)}`
// make sure to only pass id when necessary so that we don't inject
// duplicate tags when multiple components import the same css file
const idQuery = style.scoped ? `&id=${id}` : ``
const query = `?vue&type=style&index=${i}${idQuery}${attrsQuery}`
const query = `?vue&type=style&index=${i}${idQuery}${attrsQuery}${inheritQuery}`
return stringifyRequest(src + query)
}

Expand Down
13 changes: 9 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ module.exports = function (source) {
resourceQuery
} = loaderContext

const incomingQuery = qs.parse(resourceQuery.slice(1))
const rawQuery = resourceQuery.slice(1)
const inheritQuery = `&${rawQuery}`
const incomingQuery = qs.parse(rawQuery)
const options = loaderUtils.getOptions(loaderContext) || {}

const isServer = target === 'node'
Expand All @@ -61,7 +63,9 @@ module.exports = function (source) {
// module id for scoped CSS & hot-reload
const shortFilePath = path
.relative(context, resourcePath)
.replace(/^(\.\.[\\\/])+/, '')
.replace(/^(\.\.[\\\/])+/, '') +
resourceQuery

const id = hash(
isProduction
? (shortFilePath + '\n' + source)
Expand All @@ -86,7 +90,7 @@ module.exports = function (source) {
const idQuery = `&id=${id}`
const scopedQuery = hasScoped ? `&scoped=true` : ``
const attrsQuery = attrsToQuery(descriptor.template.attrs)
const query = `?vue&type=template${idQuery}${scopedQuery}${attrsQuery}`
const query = `?vue&type=template${idQuery}${scopedQuery}${attrsQuery}${inheritQuery}`
const request = templateRequest = stringifyRequest(src + query)
templateImport = `import { render, staticRenderFns } from ${request}`
}
Expand All @@ -96,7 +100,7 @@ module.exports = function (source) {
if (descriptor.script) {
const src = descriptor.script.src || resourcePath
const attrsQuery = attrsToQuery(descriptor.script.attrs, 'js')
const query = `?vue&type=script${attrsQuery}`
const query = `?vue&type=script${attrsQuery}${inheritQuery}`
const request = stringifyRequest(src + query)
scriptImport = (
`import script from ${request}\n` +
Expand Down Expand Up @@ -141,6 +145,7 @@ var component = normalizer(
code += genCustomBlocksCode(
descriptor.customBlocks,
resourcePath,
resourceQuery,
stringifyRequest
)
}
Expand Down
18 changes: 18 additions & 0 deletions test/advanced.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ test('support chaining with other loaders', done => {
})
})

test('inherit queries on files', done => {
mockBundleAndRun({
entry: 'basic.vue?change',
modify: config => {
config.module.rules[0] = {
test: /\.vue$/,
use: [
'vue-loader',
require.resolve('./mock-loaders/query')
]
}
}
}, ({ module }) => {
expect(module.data().msg).toBe('Changed!')
done()
})
})

test('expose filename', done => {
mockBundleAndRun({
entry: 'basic.vue'
Expand Down
22 changes: 22 additions & 0 deletions test/mock-loaders/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = function (content) {
const query = this.resourceQuery.slice(1)

if (/change/.test(query)) {
return `
<template>
<div>Changed!</div>
</template>
<script>
export default {
data () {
return {
msg: 'Changed!'
}
}
}
</script>
`
}

return content
}
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function bundle (options, cb, wontThrowError) {
config.module.rules[vueIndex] = Object.assign({}, vueRule, { options: vueOptions })
}

if (/\.vue$/.test(config.entry)) {
if (/\.vue/.test(config.entry)) {
const vueFile = config.entry
config = merge(config, {
entry: require.resolve('./fixtures/entry'),
Expand Down