Skip to content

Commit f0de24c

Browse files
chore: init
0 parents  commit f0de24c

18 files changed

+10276
-0
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_size = 2
6+
indent_style = space
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.eslintrc.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
root: true,
3+
parserOptions: {
4+
parser: 'babel-eslint',
5+
sourceType: 'module'
6+
},
7+
extends: [
8+
'@nuxtjs'
9+
]
10+
}

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
*.iml
3+
.idea
4+
*.log*
5+
.nuxt
6+
.vscode
7+
.DS_STORE
8+
coverage
9+
dist

.travis.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
node_js:
3+
- "8"
4+
- "9"
5+
- "10"
6+
cache:
7+
yarn: true
8+
directories:
9+
- node_modules
10+
install:
11+
- yarn
12+
script:
13+
- yarn test
14+
after_success:
15+
- yarn coverage

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Alexander Lichter <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Nuxt SVG Loader - SVGs as components, also on the server side!
2+
[![npm (scoped with tag)](https://img.shields.io/npm/v/nuxt-svg-loader/latest.svg?style=flat-square)](https://npmjs.com/package/nuxt-svg-loader)
3+
[![npm](https://img.shields.io/npm/dt/nuxt-svg-loader.svg?style=flat-square)](https://npmjs.com/package/nuxt-svg-loader)
4+
[![Build Status](https://travis-ci.org/Developmint/nuxt-svg-loader.svg?branch=master)](https://travis-ci.org/Developmint/nuxt-svg-loader)
5+
[![codecov](https://codecov.io/gh/Developmint/nuxt-svg-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/Developmint/nuxt-svg-loader)
6+
[![Dependencies](https://david-dm.org/Developmint/nuxt-svg-loader/status.svg?style=flat-square)](https://david-dm.org/Developmint/nuxt-svg-loader)
7+
[![js-standard-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com)
8+
9+
>
10+
11+
[📖 **Release Notes**](./CHANGELOG.md)
12+
13+
## Features
14+
15+
* Full support of SVGs as components. Import them like your Vue SFCs
16+
* Built on top of [vue-svg-loader](https://github.com/visualfanatic/vue-svg-loader)
17+
* Nuxt 2 (and only Nuxt 2) support
18+
* Fully tested!
19+
20+
## Setup
21+
22+
- Add `nuxt-svg-loader` dependency using yarn or npm to your project
23+
- Add `nuxt-svg-loader` to `modules` section of `nuxt.config.js`
24+
25+
```js
26+
{
27+
modules: [
28+
'nuxt-svg-loader',
29+
],
30+
}
31+
```
32+
33+
- No more options are needed. It'll simply work
34+
35+
## Development
36+
37+
- Clone this repository
38+
- Install dependencies using `yarn install` or `npm install`
39+
- Start development server using `npm run dev`
40+
41+
## License
42+
43+
[MIT License](./LICENSE)
44+
45+
Copyright (c) Alexander Lichter

commitlint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { extends: ['@commitlint/config-conventional'] }

lib/module.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const logger = require('consola').withScope('nuxt-svg-loader')
2+
3+
export default function nuxtSvgLoader() {
4+
const oldExtendFunction = this.options.build.extend
5+
6+
this.options.build.extend = (...args) => {
7+
setupVueSvgLoader(...args)
8+
if (oldExtendFunction) {
9+
oldExtendFunction(...args)
10+
}
11+
}
12+
}
13+
14+
const setupVueSvgLoader = (config) => {
15+
const imageLoaderRule = config.module.rules.find(rule => rule.test && /svg/.test(rule.test.toString()))
16+
17+
if (!imageLoaderRule) {
18+
logger.error('Could not modify image loader rule!')
19+
return
20+
}
21+
// from https://github.com/nuxt/nuxt.js/blob/76b10d2d3f4e5352f1c9d14c52008f234e66d7d5/lib/builder/webpack/base.js#L203
22+
imageLoaderRule.test = /\.(png|jpe?g|gif|webp)$/
23+
24+
config.module.rules.push({
25+
test: /\.svg$/,
26+
loader: 'vue-svg-loader'
27+
})
28+
}
29+
30+
module.exports.meta = require('../package.json')

package.json

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"name": "nuxt-svg-loader",
3+
"version": "0.0.1",
4+
"description": "",
5+
"license": "MIT",
6+
"contributors": [
7+
{
8+
"name": "Alexander Lichter <[email protected]>"
9+
}
10+
],
11+
"main": "lib/module.js",
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/Developmint/nuxt-svg-loader"
15+
},
16+
"bugs": {
17+
"url": "https://github.com/Developmint/nuxt-svg-loader/issues"
18+
},
19+
"publishConfig": {
20+
"access": "public"
21+
},
22+
"scripts": {
23+
"dev": "nuxt --config-file test/fixture/configs/default.js",
24+
"lint": "eslint lib test",
25+
"test": "yarn run lint && jest",
26+
"release": "standard-version && git push --follow-tags && npm publish",
27+
"commitlint": "commitlint -e $GIT_PARAMS",
28+
"coverage": "codecov"
29+
},
30+
"eslintIgnore": [
31+
"lib/templates/*.*"
32+
],
33+
"files": [
34+
"lib"
35+
],
36+
"keywords": [
37+
"nuxtjs",
38+
"nuxt",
39+
"nuxt-module",
40+
"svg",
41+
"loader",
42+
"svg-loader"
43+
],
44+
"engines": {
45+
"node": ">=8.0.0",
46+
"npm": ">=5.0.0"
47+
},
48+
"jest": {
49+
"testEnvironment": "node",
50+
"collectCoverage": true,
51+
"coveragePathIgnorePatterns": [
52+
"/node_modules/",
53+
"/test/fixture"
54+
],
55+
"forceExit": true
56+
},
57+
"dependencies": {
58+
"consola": "^1.4.4",
59+
"vue-svg-loader": "^0.11.0"
60+
},
61+
"devDependencies": {
62+
"@commitlint/cli": "^6.1.3",
63+
"@commitlint/config-conventional": "^6.1.3",
64+
"@nuxtjs/eslint-config": "^0.0.1",
65+
"babel-eslint": "^10.0.1",
66+
"codecov": "^3.1.0",
67+
"eslint": "^5.7.0",
68+
"eslint-config-standard": "^12.0.0",
69+
"eslint-plugin-import": "^2.14.0",
70+
"eslint-plugin-jest": "^21.24.1",
71+
"eslint-plugin-node": "^7.0.1",
72+
"eslint-plugin-promise": "^4.0.1",
73+
"eslint-plugin-standard": "^4.0.0",
74+
"eslint-plugin-vue": "^5.0.0-beta.3",
75+
"get-port": "^4.0.0",
76+
"husky": "^1.0.0-rc.1",
77+
"jest": "^23.6.0",
78+
"jsdom": "^12.2.0",
79+
"nuxt": "^2.2.0",
80+
"standard-version": "^4.4.0"
81+
},
82+
"husky": {
83+
"hooks": {
84+
"pre-commit": "yarn run lint",
85+
"commit-msg": "yarn run commitlint"
86+
}
87+
}
88+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ssr correctly register SVG loader and load SVG correctly 1`] = `
4+
"<!doctype html>
5+
<html data-n-head-ssr data-n-head=\\"\\">
6+
<head>
7+
<title data-n-head=\\"true\\"></title><style data-vue-ssr-id=\\"fd547dac:0\\">
8+
.nuxt-progress{position:fixed;top:0;left:0;right:0;height:2px;width:0;transition:width .2s,opacity .4s;opacity:1;background-color:#efc14e;z-index:999999
9+
}</style>
10+
</head>
11+
<body data-n-head=\\"\\">
12+
<div data-server-rendered=\\"true\\" id=\\"__nuxt\\"><div class=\\"nuxt-progress\\" style=\\"width:0%;height:2px;background-color:black;opacity:0;\\"></div><div id=\\"__layout\\"><svg xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 1000 1000\\"><style></style><g id=\\"nuxt\\"><path d=\\"M317.9 852H3.7l408.1-704 408.1 704H507.7\\" fill=\\"#41b883\\"></path><path d=\\"M779.8 852h216.5l-354-608.5-351 608.5h216.5\\" fill=\\"#328170\\"></path><path d=\\"M651.2 852h159.5L549.9 403.8 291.3 852h159.5\\" fill=\\"#35495e\\"></path></g></svg></div></div><script>window.__NUXT__={layout:\\"default\\",data:[{}],error:null,serverRendered:true};</script><script src=\\"/_nuxt/687e6be1d38a624eb82e.js\\" defer></script><script src=\\"/_nuxt/b56a2d15b853e7513d21.js\\" defer></script><script src=\\"/_nuxt/f338aafc6d9996bc3a8d.js\\" defer></script><script src=\\"/_nuxt/c34084ccf58500f72ab8.js\\" defer></script>
13+
</body>
14+
</html>
15+
"
16+
`;
17+
18+
exports[`ssr honor custom build.extend function 1`] = `
19+
"<!doctype html>
20+
<html data-n-head-ssr data-n-head=\\"\\">
21+
<head>
22+
<title data-n-head=\\"true\\"></title><style data-vue-ssr-id=\\"fd547dac:0\\">
23+
.nuxt-progress{position:fixed;top:0;left:0;right:0;height:2px;width:0;transition:width .2s,opacity .4s;opacity:1;background-color:#efc14e;z-index:999999
24+
}</style>
25+
</head>
26+
<body data-n-head=\\"\\">
27+
<div data-server-rendered=\\"true\\" id=\\"__nuxt\\"><div class=\\"nuxt-progress\\" style=\\"width:0%;height:2px;background-color:black;opacity:0;\\"></div><div id=\\"__layout\\"><svg xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 1000 1000\\"><style></style><g id=\\"nuxt\\"><path d=\\"M317.9 852H3.7l408.1-704 408.1 704H507.7\\" fill=\\"#41b883\\"></path><path d=\\"M779.8 852h216.5l-354-608.5-351 608.5h216.5\\" fill=\\"#328170\\"></path><path d=\\"M651.2 852h159.5L549.9 403.8 291.3 852h159.5\\" fill=\\"#35495e\\"></path></g></svg></div></div><script>window.__NUXT__={layout:\\"default\\",data:[{}],error:null,serverRendered:true};</script><script src=\\"/_nuxt/687e6be1d38a624eb82e.js\\" defer></script><script src=\\"/_nuxt/b56a2d15b853e7513d21.js\\" defer></script><script src=\\"/_nuxt/f338aafc6d9996bc3a8d.js\\" defer></script><script src=\\"/_nuxt/c34084ccf58500f72ab8.js\\" defer></script>
28+
</body>
29+
</html>
30+
"
31+
`;

test/fixture/components/Nuxt.svg

+17
Loading

test/fixture/configs/default.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { resolve } = require('path')
2+
3+
module.exports = {
4+
dev: false,
5+
rootDir: resolve(__dirname, '../../../'),
6+
srcDir: resolve(__dirname, '../'),
7+
render: {
8+
resourceHints: false
9+
},
10+
modules: ['@@']
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { resolve } = require('path')
2+
3+
module.exports = {
4+
dev: false,
5+
rootDir: resolve(__dirname, '../../../'),
6+
srcDir: resolve(__dirname, '../'),
7+
render: {
8+
resourceHints: false
9+
},
10+
modules: ['@@', '@/modules/error'],
11+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { resolve } = require('path')
2+
const consola = require('consola')
3+
module.exports = {
4+
dev: false,
5+
rootDir: resolve(__dirname, '../../../'),
6+
srcDir: resolve(__dirname, '../'),
7+
render: {
8+
resourceHints: false
9+
},
10+
modules: ['@@'],
11+
build: {
12+
extend() {
13+
consola.log('Build fn')
14+
}
15+
}
16+
}

test/fixture/modules/error.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = function () {
2+
const oldExtendFunction = this.options.build.extend
3+
4+
this.options.build.extend = (config, options) => {
5+
const imageLoaderRule = config.module.rules.find(rule => rule.test && /svg/.test(rule.test.toString()))
6+
imageLoaderRule.test = /^$/
7+
if (oldExtendFunction) {
8+
oldExtendFunction(config, options)
9+
}
10+
}
11+
}

test/fixture/pages/index.vue

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<nuxt-logo/>
3+
</template>
4+
5+
<script>
6+
import NuxtLogo from '../components/Nuxt.svg'
7+
export default {
8+
components: {
9+
NuxtLogo
10+
}
11+
}
12+
</script>

0 commit comments

Comments
 (0)