UNPKG

1.81 kBJavaScriptView Raw
1/**
2 * This is the original RegExp cloned from the original Nuxt.js configuration
3 * files, with only the search for ".svg" files removed. Keep tabs on this in
4 * case the core decides to add additional qualifiers to the pattern.
5 */
6const ORIGINAL_TEST = /\.(png|jpe?g|gif|svg|webp)$/
7const REPLACEMENT_TEST = /\.(png|jpe?g|gif|webp)$/
8
9module.exports = function (options) {
10 this.extendBuild(setup)
11}
12
13/**
14 * Perform the primary setup for the nuxt-svg module by removing and replacing
15 * all of the rules that match ".svg" with the new one.
16 *
17 * @param config The webpack configuration object to extend
18 */
19function setup (config) {
20 const rules = config.module.rules
21
22 // Remove any original svg rules
23 const svgRules = rules.filter(rule => rule.test.test('.svg'))
24
25 for (const rule of svgRules) {
26 if (
27 rule.test.source !== ORIGINAL_TEST.source &&
28 rule.test.source !== REPLACEMENT_TEST.source
29 ) {
30 throw new Error("nuxt-svg: Unexpected '.svg' rule in the webpack configuration")
31 }
32 rule.test = REPLACEMENT_TEST
33 }
34
35 const vueSvgLoader = [
36 {
37 loader: 'vue-svg-loader',
38 options: {
39 svgo: false
40 }
41 }
42 ]
43
44 if (config.name === 'client') {
45 const jsxRule = config.module.rules.find(r => r.test.test('.jsx'))
46 const babelLoader = jsxRule.use[jsxRule.use.length - 1]
47 vueSvgLoader.unshift(babelLoader)
48 }
49
50 // Create the custom SVG rule
51 const rule = {
52 test: /\.svg$/,
53 oneOf: [
54 {
55 resourceQuery: /inline/,
56 use: vueSvgLoader
57 },
58 {
59 resourceQuery: /data/,
60 loader: 'url-loader'
61 },
62 {
63 loader: 'file-loader' // By default, always use file-loader
64 }
65 ]
66 }
67
68 rules.push(rule) // Actually add the rule
69}
70
71module.exports.meta = require('../package.json')