-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add warning for .svg files that are imported by css files
- Loading branch information
Showing
9 changed files
with
123 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
background-image: url(./icon.svg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module.exports = function() { | ||
this.extendBuild(config => { | ||
const imageRule = config.module.rules.find( | ||
rule => rule.test && rule.test.test('.svg') | ||
) | ||
if (!imageRule) { | ||
throw new Error('Cannot find the existing webpack rule for .svg files') | ||
} | ||
// Don't process .svg files in default image rule | ||
// from https://github.com/nuxt/nuxt.js/blob/76b10d2d3f4e5352f1c9d14c52008f234e66d7d5/lib/builder/webpack/base.js#L203 | ||
imageRule.test = /\.(png|jpe?g|gif|webp)$/ | ||
|
||
// Handle svg files imported by css files | ||
const FILE_RE = /\.(vue|js|ts)$/ | ||
config.module.rules.push( | ||
Object.assign({}, imageRule, { | ||
issuer: file => !FILE_RE.test(file) | ||
}) | ||
) | ||
|
||
// Handle svg files imported by js files | ||
config.module.rules.push({ | ||
test: /\.svg$/, | ||
issuer: file => FILE_RE.test(file), | ||
use: ['vue-loader', 'svg-to-vue-component/loader'] | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
exports.name = 'svg-to-vue-component' | ||
|
||
exports.apply = api => { | ||
api.hook('createWebpackChain', config => { | ||
// Only convert .svg files that are imported by these files as Vue component | ||
const FILE_RE = /\.(vue|js|ts)$/ | ||
|
||
// Use vue-cli's default rule for svg in non .vue .js .ts files | ||
config.module.rule('svg').issuer(file => !FILE_RE.test(file)) | ||
|
||
// Use our loader to handle svg imported by other files | ||
config.module | ||
.rule('svg-component') | ||
.test(/\.svg$/) | ||
.issuer(file => FILE_RE.test(file)) | ||
.use('vue') | ||
.loader('vue-loader') | ||
.end() | ||
.use('svg-to-vue-component') | ||
.loader(require.resolve('./loader')) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters