Skip to content

Commit 3abf38b

Browse files
committed
ignore custom blocks with no matching loader
1 parent 5d35c65 commit 3abf38b

File tree

3 files changed

+29
-66
lines changed

3 files changed

+29
-66
lines changed

docs/en/configurations/custom-blocks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
You can define custom language blocks inside `*.vue` files. The content of a custom block will be processed by the loaders specified in the `loaders` object of `vue-loader` options and then required by the component module. The configuration is similar to what is described in [Advanced Loader Configuration](../configurations/advanced.md), except the matching uses the tag name instead of the `lang` attribute.
66

7+
If a matching loader is found for a custom block, it will be processed; otherwise the custom block will simply be ignored.
8+
79
## Example
810

911
Here's an example of extracting all `<docs>` custom blocks into a single docs file:
@@ -63,5 +65,3 @@ module.exports = {
6365
}
6466
}
6567
```
66-
67-
Note that custom blocks would require specific configuration to work properly, so you probably want to avoid distributing reusable components with custom blocks in source form.

lib/loader.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ module.exports = function (content) {
174174
loader = addCssModulesToLoader(loader, part, index)
175175
}
176176
// if user defines custom loaders for html, add template compiler to it
177-
if (type === 'template' && loader !== defaultLoaders.html) {
177+
if (type === 'template' && loader.indexOf(defaultLoaders.html) < 0) {
178178
loader = defaultLoaders.html + '!' + loader
179179
}
180180
// inject rewriter before css/html loader for
@@ -281,32 +281,6 @@ module.exports = function (content) {
281281
output += '\n'
282282
}
283283

284-
// add requires for customBlocks
285-
if (parts.customBlocks && parts.customBlocks.length) {
286-
var addedPrefix = false
287-
288-
parts.customBlocks.forEach(function (customBlock, i) {
289-
if (!loaders[customBlock.type]) {
290-
loaderContext.emitWarning('Loader for custom block type "' + customBlock.type + '" not found in webpack configuration')
291-
} else {
292-
// require customBlock
293-
customBlock.src = customBlock.attrs.src
294-
var requireString = customBlock.src
295-
? getRequireForImport(customBlock.type, customBlock)
296-
: getRequire(customBlock.type, customBlock, i)
297-
298-
if (!addedPrefix) {
299-
output += '\n/* customBlocks */\n'
300-
addedPrefix = true
301-
}
302-
303-
output += requireString + '\n'
304-
}
305-
})
306-
307-
output += '\n'
308-
}
309-
310284
// we require the component normalizer function, and call it like so:
311285
// normalizeComponent(
312286
// scriptExports,
@@ -385,6 +359,30 @@ module.exports = function (content) {
385359
}
386360
}
387361

362+
// add requires for customBlocks
363+
if (parts.customBlocks && parts.customBlocks.length) {
364+
var addedPrefix = false
365+
366+
parts.customBlocks.forEach(function (customBlock, i) {
367+
if (loaders[customBlock.type]) {
368+
// require customBlock
369+
customBlock.src = customBlock.attrs.src
370+
var requireString = customBlock.src
371+
? getRequireForImport(customBlock.type, customBlock)
372+
: getRequire(customBlock.type, customBlock, i)
373+
374+
if (!addedPrefix) {
375+
output += '\n/* customBlocks */\n'
376+
addedPrefix = true
377+
}
378+
379+
output += requireString + '\n'
380+
}
381+
})
382+
383+
output += '\n'
384+
}
385+
388386
if (!query.inject) {
389387
// hot reload
390388
if (

test/test.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -617,50 +617,15 @@ describe('vue-loader', function () {
617617
})
618618
})
619619

620-
it('custom blocks can be removed from the output', function (done) {
620+
it('custom blocks can be ignored', function (done) {
621621
bundle({
622-
entry: './test/fixtures/custom-language.vue',
623-
vue: {
624-
loaders: {
625-
'unit-test': 'null-loader'
626-
}
627-
}
622+
entry: './test/fixtures/custom-language.vue'
628623
}, function (code, warnings) {
629624
expect(code).not.to.contain('describe(\'example\', function () {\n it(\'basic\', function (done) {\n done();\n });\n})')
630625
done()
631626
})
632627
})
633628

634-
it('no warnings when loaders are specified for all custom blocks', function (done) {
635-
bundle({
636-
entry: './test/fixtures/custom-language.vue',
637-
vue: {
638-
loaders: {
639-
'documentation': 'null-loader',
640-
'unit-test': 'null-loader'
641-
}
642-
}
643-
}, function (code, warnings) {
644-
expect(warnings.length).to.equal(0)
645-
done()
646-
})
647-
})
648-
649-
it('warning should be raised when a loader is not specified for a custom block', function (done) {
650-
bundle({
651-
entry: './test/fixtures/custom-language.vue',
652-
vue: {
653-
loaders: {
654-
'documentation': 'null-loader'
655-
}
656-
}
657-
}, function (code, warnings) {
658-
expect(warnings.length).to.equal(1)
659-
expect(warnings[0].message).to.equal('Loader for custom block type "unit-test" not found in webpack configuration')
660-
done()
661-
})
662-
})
663-
664629
it('passes attributes as options to the loader', function (done) {
665630
bundle({
666631
entry: './test/fixtures/custom-options.vue',

0 commit comments

Comments
 (0)