svg-sprite-generation-loader 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,72 @@
1
+ # For most projects, this workflow file will not need changing; you simply need
2
+ # to commit it to your repository.
3
+ #
4
+ # You may wish to alter this file to override the set of languages analyzed,
5
+ # or to provide custom queries or build logic.
6
+ #
7
+ # ******** NOTE ********
8
+ # We have attempted to detect the languages in your repository. Please check
9
+ # the `language` matrix defined below to confirm you have the correct set of
10
+ # supported CodeQL languages.
11
+ #
12
+ name: "CodeQL"
13
+
14
+ on:
15
+ push:
16
+ branches: [ "main" ]
17
+ pull_request:
18
+ # The branches below must be a subset of the branches above
19
+ branches: [ "main" ]
20
+ schedule:
21
+ - cron: '41 5 * * 1'
22
+
23
+ jobs:
24
+ analyze:
25
+ name: Analyze
26
+ runs-on: ubuntu-latest
27
+ permissions:
28
+ actions: read
29
+ contents: read
30
+ security-events: write
31
+
32
+ strategy:
33
+ fail-fast: false
34
+ matrix:
35
+ language: [ 'javascript' ]
36
+ # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37
+ # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
38
+
39
+ steps:
40
+ - name: Checkout repository
41
+ uses: actions/checkout@v3
42
+
43
+ # Initializes the CodeQL tools for scanning.
44
+ - name: Initialize CodeQL
45
+ uses: github/codeql-action/init@v2
46
+ with:
47
+ languages: ${{ matrix.language }}
48
+ # If you wish to specify custom queries, you can do so here or in a config file.
49
+ # By default, queries listed here will override any specified in a config file.
50
+ # Prefix the list here with "+" to use these queries and those in the config file.
51
+
52
+ # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
53
+ # queries: security-extended,security-and-quality
54
+
55
+
56
+ # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
57
+ # If this step fails, then you should remove it and run the build manually (see below)
58
+ - name: Autobuild
59
+ uses: github/codeql-action/autobuild@v2
60
+
61
+ # ℹ️ Command-line programs to run using the OS shell.
62
+ # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
63
+
64
+ # If the Autobuild fails above, remove it and uncomment the following three lines.
65
+ # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
66
+
67
+ # - run: |
68
+ # echo "Run, Build Application using script"
69
+ # ./location_of_script_within_repo/buildscript.sh
70
+
71
+ - name: Perform CodeQL Analysis
72
+ uses: github/codeql-action/analyze@v2
@@ -0,0 +1,34 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ release:
8
+ types: [created]
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - uses: actions/setup-node@v1
16
+ with:
17
+ always-auth: true
18
+ node-version: 14
19
+ registry-url: "https://registry.npmjs.org"
20
+ - run: yarn install --frozen-lockfile
21
+
22
+ publish-npm:
23
+ needs: build
24
+ runs-on: ubuntu-latest
25
+ steps:
26
+ - uses: actions/checkout@v2
27
+ - uses: actions/setup-node@v1
28
+ with:
29
+ node-version: 14
30
+ registry-url: https://registry.npmjs.org/
31
+ - run: yarn install --frozen-lockfile
32
+ - run: yarn publish
33
+ env:
34
+ NODE_AUTH_TOKEN: ${{secrets.npm_token}}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Vadym Shymko
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.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # svg-sprite-generation-loader
2
+
3
+ [![npm version](https://img.shields.io/npm/v/svg-sprite-generation-loader)](https://www.npmjs.com/package/svg-sprite-generation-loader)
4
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/vadymshymko/svg-sprite-generation-loader/blob/master/LICENSE)
5
+
6
+ Webpack plugin and loader for generating svg sprites
7
+
8
+ ## Installation:
9
+
10
+ ```bash
11
+ npm install svg-sprite-generation-loader --save-dev
12
+ ```
13
+
14
+ **yarn**
15
+
16
+ ```bash
17
+ yarn add svg-sprite-generation-loader --dev
18
+ ```
19
+
20
+ ## Usage:
21
+
22
+ In your webpack config:
23
+
24
+ ```javascript
25
+ const SvgSpriteGenerationPlugin = require("svg-sprite-generation-loader/plugin.js");
26
+
27
+ module.exports = {
28
+ plugins: [
29
+ new SvgSpriteGenerationPlugin.Plugin({
30
+ spriteFilePath: `path/to/sprite/file.svg`,
31
+ }),
32
+ ],
33
+ module: {
34
+ rules: [
35
+ {
36
+ test: /\.svg$/,
37
+ use: "svg-sprite-generation-loader",
38
+ },
39
+ ],
40
+ },
41
+ };
42
+ ```
43
+
44
+ In some source code:
45
+
46
+ ```javascript
47
+ import someIcon from 'path/to/iconFileName.svg'
48
+
49
+ export default function SomeIcon() {
50
+ return (
51
+ <svg {...someIcon}>
52
+ <use href="path/to/sprite/file.svg#iconFileName">
53
+ </svg>
54
+ );
55
+ }
56
+ ```
package/index.js ADDED
@@ -0,0 +1,17 @@
1
+ const path = require("path");
2
+
3
+ const svgSpriteState = require("./utils/spriteState.js");
4
+ const parseSvg = require("./utils/parseSvg.js");
5
+
6
+ module.exports = function (source) {
7
+ const { attributes, content } = parseSvg(source);
8
+ const symbolId = path.basename(this.resourcePath, ".svg");
9
+
10
+ if (this.target === "web") {
11
+ svgSpriteState.addSpriteIcon({ symbolId, attributes, content });
12
+ }
13
+
14
+ return `
15
+ export default ${JSON.stringify(attributes)}
16
+ `;
17
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "svg-sprite-generation-loader",
3
+ "version": "0.0.1",
4
+ "description": "Webpack loader for generating svg sprites",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/vadymshymko/svg-sprite-generation-loader.git"
12
+ },
13
+ "keywords": [
14
+ "svg",
15
+ "sprite",
16
+ "svg-sprite",
17
+ "svg-symbol",
18
+ "svg-symbol-sprite",
19
+ "webpack",
20
+ "webpack-svg-sprite",
21
+ "webpack-loader",
22
+ "webpack-plugin"
23
+ ],
24
+ "author": "Vadym Shymko <vadymshymko@gmail.com>",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/vadymshymko/svg-sprite-generation-loader/issues"
28
+ },
29
+ "homepage": "https://github.com/vadymshymko/svg-sprite-generation-loader#readme",
30
+ "dependencies": {
31
+ "svgo": "^2.8.0"
32
+ }
33
+ }
package/plugin.js ADDED
@@ -0,0 +1,62 @@
1
+ const { optimize } = require("svgo");
2
+
3
+ const svgSpriteState = require("./utils/spriteState.js");
4
+
5
+ class SvgSpriteGeneratorPlugin {
6
+ static defaultOptions = {
7
+ spriteFilePath: "sprite.svg",
8
+ svgo: {
9
+ plugins: [
10
+ {
11
+ name: "preset-default",
12
+ params: {
13
+ overrides: {
14
+ removeUselessDefs: false,
15
+ removeViewBox: false,
16
+ cleanupIDs: false,
17
+ },
18
+ },
19
+ },
20
+ ],
21
+ },
22
+ };
23
+
24
+ constructor(options = {}) {
25
+ this.options = { ...SvgSpriteGeneratorPlugin.defaultOptions, ...options };
26
+ }
27
+
28
+ apply(compiler) {
29
+ // webpack module instance can be accessed from the compiler object,
30
+ // this ensures that correct version of the module is used
31
+ // (do not require/import the webpack or any symbols from it directly).
32
+ const { webpack } = compiler;
33
+
34
+ // RawSource is one of the "sources" classes that should be used
35
+ // to represent asset sources in compilation.
36
+ const { RawSource } = webpack.sources;
37
+
38
+ compiler.hooks.thisCompilation.tap(
39
+ SvgSpriteGeneratorPlugin.name,
40
+ (compilation) => {
41
+ compilation.hooks.processAssets.tap(
42
+ SvgSpriteGeneratorPlugin.name,
43
+ () => {
44
+ if (compilation.options.loader.target === "web") {
45
+ compilation.emitAsset(
46
+ `${this.options.spriteFilePath}`,
47
+ new RawSource(
48
+ optimize(
49
+ svgSpriteState.getSpriteContent(),
50
+ this.options.svgo
51
+ ).data
52
+ )
53
+ );
54
+ }
55
+ }
56
+ );
57
+ }
58
+ );
59
+ }
60
+ }
61
+
62
+ module.exports = SvgSpriteGeneratorPlugin;
@@ -0,0 +1,22 @@
1
+ module.exports = function (source) {
2
+ const [_, parsedAttributes, parsedContent] = source.match(
3
+ /<svg(.{0,})>([\s\S]+)<\/svg>/i
4
+ );
5
+
6
+ const attributes = parsedAttributes
7
+ .match(/([\w-:]+)(=)?("[^<>"]*"|'[^<>']*'|[\w-:]+)/g)
8
+ .reduce(function (result, attribute) {
9
+ const [name, unformattedValue] = attribute.split("=");
10
+
11
+ result[name] = unformattedValue
12
+ ? unformattedValue.replace(/['"]/g, "")
13
+ : true;
14
+
15
+ return result;
16
+ }, {});
17
+
18
+ return {
19
+ attributes,
20
+ content: parsedContent.replace(/\n/g, " ").trim(),
21
+ };
22
+ };
@@ -0,0 +1,17 @@
1
+ class SvgSpriteGeneratorState {
2
+ constructor() {
3
+ this.spriteIcons = {};
4
+ }
5
+
6
+ addSpriteIcon(icon) {
7
+ this.spriteIcons[icon.symbolId] = icon;
8
+ }
9
+
10
+ getSpriteContent() {
11
+ return `${Object.keys(this.spriteIcons).reduce((result, iconId) => {
12
+ return `${result}<symbol viewBox="${this.spriteIcons[iconId].attributes.viewBox}" fill="none" id="${iconId}">${this.spriteIcons[iconId].content}</symbol>`;
13
+ }, '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">')}</svg>`;
14
+ }
15
+ }
16
+
17
+ module.exports = new SvgSpriteGeneratorState();