Skip to content

Commit cda1ec3

Browse files
committed
feat: expose all block attrs via query
1 parent 6b73b74 commit cda1ec3

File tree

8 files changed

+50
-36
lines changed

8 files changed

+50
-36
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ The good news is that you can now configure `localIdentName` in one place:
128128
}
129129
```
130130

131-
If you only want to use CSS Modules in some of your Vue components, you can use a `oneOf` rule and check for the `cssModules` string in resourceQuery:
131+
If you only want to use CSS Modules in some of your Vue components, you can use a `oneOf` rule and check for the `module` string in resourceQuery:
132132

133133
``` js
134134
{
135135
test: /\.css$/,
136136
oneOf: [
137137
// this matches <style module>
138138
{
139-
resourceQuery: /cssModules/,
139+
resourceQuery: /module/,
140140
use: [
141141
'vue-style-loader',
142142
{

Diff for: lib/customBlocks.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
module.exports = function genCustomBlocksCode () {
1+
// const { attrsToQuery } = require('./utils')
22

3+
module.exports = function genCustomBlocksCode (
4+
blocks,
5+
resourcePath
6+
) {
7+
// blocks.map((block, i) => {
8+
// const src = block.src || resourcePath
9+
// const langQuery = getLangQuery(block)
10+
11+
// })
312
}

Diff for: lib/index.js

+14-22
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const qs = require('querystring')
55
const plugin = require('./plugin')
66
const selectBlock = require('./select')
77
const loaderUtils = require('loader-utils')
8+
const { attrsToQuery } = require('./utils')
9+
const genStylesCode = require('./styles')
810
const { genHotReloadCode } = require('./hotReload')
9-
const genStyleInjectionCode = require('./styleInjection')
11+
const genCustomBlocksCode = require('./customBlocks')
1012
const componentNormalizerPath = require.resolve('./runtime/componentNormalizer')
1113

1214
module.exports = function (source) {
@@ -59,9 +61,7 @@ module.exports = function (source) {
5961

6062
// feature information
6163
const hasScoped = descriptor.styles.some(s => s.scoped)
62-
const templateAttrs = (descriptor.template && descriptor.template.attrs) || {}
63-
const hasFunctional = templateAttrs.functional
64-
const hasComment = templateAttrs.comments
64+
const hasFunctional = descriptor.template && descriptor.template.attrs.functional
6565
const needsHotReload = (
6666
!isServer &&
6767
!isProduction &&
@@ -73,12 +73,10 @@ module.exports = function (source) {
7373
let templateImport = `var render, staticRenderFns`
7474
if (descriptor.template) {
7575
const src = descriptor.template.src || resourcePath
76-
const langQuery = getLangQuery(descriptor.template)
7776
const idQuery = `&id=${id}`
78-
const scopedQuery = hasScoped ? `&scoped` : ``
79-
const fnQuery = hasFunctional ? `&functional` : ``
80-
const commentQuery = hasComment ? `&comment` : ``
81-
const query = `?vue&type=template${scopedQuery}${idQuery}${langQuery}${fnQuery}${commentQuery}`
77+
const scopedQuery = hasScoped ? `&scoped=true` : ``
78+
const attrsQuery = attrsToQuery(descriptor.template.attrs)
79+
const query = `?vue&type=template${idQuery}${scopedQuery}${attrsQuery}`
8280
const request = stringifyRequest(src + query)
8381
templateImport = `import { render, staticRenderFns } from ${request}`
8482
}
@@ -87,8 +85,8 @@ module.exports = function (source) {
8785
let scriptImport = `var script = {}`
8886
if (descriptor.script) {
8987
const src = descriptor.script.src || resourcePath
90-
const langQuery = getLangQuery(descriptor.script, 'js')
91-
const query = `?vue&type=script${langQuery}`
88+
const attrsQuery = attrsToQuery(descriptor.script.attrs, 'js')
89+
const query = `?vue&type=script${attrsQuery}`
9290
const request = stringifyRequest(src + query)
9391
scriptImport = (
9492
`import script from ${request}\n` +
@@ -97,15 +95,14 @@ module.exports = function (source) {
9795
}
9896

9997
// styles
100-
let styleInjectionCode = ``
98+
let stylesCode = ``
10199
if (descriptor.styles.length) {
102-
styleInjectionCode = genStyleInjectionCode(
100+
stylesCode = genStylesCode(
103101
loaderContext,
104102
descriptor.styles,
105103
id,
106104
resourcePath,
107105
stringifyRequest,
108-
getLangQuery,
109106
needsHotReload,
110107
isServer || isShadow // needs explicit injection?
111108
)
@@ -114,22 +111,22 @@ module.exports = function (source) {
114111
let code = `
115112
${templateImport}
116113
${scriptImport}
117-
${styleInjectionCode}
114+
${stylesCode}
118115
import normalizer from ${stringifyRequest(`!${componentNormalizerPath}`)}
119116
var component = normalizer(
120117
script,
121118
render,
122119
staticRenderFns,
123120
${hasFunctional ? `true` : `false`},
124-
${/injectStyles/.test(styleInjectionCode) ? `injectStyles` : `null`},
121+
${/injectStyles/.test(stylesCode) ? `injectStyles` : `null`},
125122
${hasScoped ? JSON.stringify(id) : `null`},
126123
${isServer ? JSON.stringify(hash(request)) : `null`}
127124
${isShadow ? `,true` : ``}
128125
)
129126
`.trim()
130127

131128
if (descriptor.customBlocks && descriptor.customBlocks.length) {
132-
// TODO custom blocks
129+
code += genCustomBlocksCode(descriptor.customBlocks)
133130
}
134131

135132
// Expose filename. This is used by the devtools and vue runtime warnings.
@@ -146,9 +143,4 @@ var component = normalizer(
146143
return code
147144
}
148145

149-
function getLangQuery (block, fallback) {
150-
const lang = block.lang || fallback
151-
return lang ? `&lang=${lang}` : ``
152-
}
153-
154146
module.exports.VueLoaderPlugin = plugin

Diff for: lib/style-post-loader/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (source, map) {
1313
const id = `data-v-${query.id}`
1414
const plugins = [trim()]
1515

16-
if (query.scoped != null) {
16+
if (query.scoped) {
1717
plugins.push(scoped({ id }))
1818
}
1919

Diff for: lib/styleInjection.js renamed to lib/styles.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { attrsToQuery } = require('./utils')
12
const hotReloadAPIPath = require.resolve('vue-hot-reload-api')
23

34
module.exports = function genStyleInjectionCode (
@@ -6,7 +7,6 @@ module.exports = function genStyleInjectionCode (
67
id,
78
resourcePath,
89
stringifyRequest,
9-
getLangQuery,
1010
needsHotReload,
1111
needsExplicitInjection
1212
) {
@@ -19,12 +19,11 @@ module.exports = function genStyleInjectionCode (
1919

2020
function genStyleRequest (style, i) {
2121
const src = style.src || resourcePath
22-
const langQuery = getLangQuery(style, 'css')
22+
const attrsQuery = attrsToQuery(style.attrs, 'css')
2323
// make sure to only pass id when necessary so that we don't inject
2424
// duplicate tags when multiple components import the same css file
25-
const scopedQuery = style.scoped ? `&scoped&id=${id}` : ``
26-
const moduleQuery = style.module ? `&cssModules` : ``
27-
const query = `?vue&type=style&index=${i}${langQuery}${scopedQuery}${moduleQuery}`
25+
const idQuery = style.scoped ? `&id=${id}` : ``
26+
const query = `?vue&type=style&index=${i}${idQuery}${attrsQuery}`
2827
return stringifyRequest(src + query)
2928
}
3029

Diff for: lib/template-loader/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,14 @@ function actuallyCompile (sourceTemplate, options, loaderContext, query) {
6464
const isProduction = loaderContext.minimize || process.env.NODE_ENV === 'production'
6565
const needsHotReload = !isServer && !isProduction && options.hotReload !== false
6666
const defaultModules = [transformAssetUrl(options.transformAssetUrl), transformSrcset()]
67-
const hasComment = query.comment != null
68-
const hasFunctionalTemplate = query.functional != null
67+
const hasFunctionalTemplate = query.functional
6968

7069
const userCompilerOptions = options.compilerOptions || {}
7170

7271
const compilerOptions = Object.assign({}, userCompilerOptions, {
73-
scopeId: query.scoped != null ? `data-v-${id}` : null,
72+
scopeId: query.scoped ? `data-v-${id}` : null,
7473
modules: defaultModules.concat(userCompilerOptions.modules || []),
75-
comments: hasComment
74+
comments: query.comments
7675
})
7776

7877
// support user compiler

Diff for: lib/utils.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const qs = require('querystring')
2+
3+
exports.attrsToQuery = (attrs, langFallback) => {
4+
let query = ``
5+
for (const name in attrs) {
6+
const value = attrs[name]
7+
if (name !== 'src') {
8+
query += `&${qs.escape(name)}=${value ? qs.escape(value) : ``}`
9+
}
10+
}
11+
if (langFallback && !(`lang` in attrs)) {
12+
query += `&lang=${langFallback}`
13+
}
14+
return query
15+
}

Diff for: test/advanced.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test('support rules with oneOf', async () => {
117117
use: 'vue-style-loader',
118118
oneOf: [
119119
{
120-
resourceQuery: /cssModules/,
120+
resourceQuery: /module/,
121121
use: [
122122
{
123123
loader: 'css-loader',

0 commit comments

Comments
 (0)