Skip to content

Commit

Permalink
Inline SVG images for easier bundler setup
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jan 11, 2024
1 parent 367e0ff commit 5c9c317
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Fix syntax label from "Javascript" to "JavaScript".
- Fix typing errors for emitter.
- Inline SVG images for easier bundler setup.

# v2.0.0-beta.0

Expand Down
146 changes: 146 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/quill/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ const pkg = require('./package.json');

module.exports = {
presets: ['@babel/preset-env', '@babel/preset-typescript'],
plugins: [['transform-define', { QUILL_VERSION: pkg.version }]],
plugins: [
['transform-define', { QUILL_VERSION: pkg.version }],
'./scripts/babel-svg-inline-import',
],
};
1 change: 1 addition & 0 deletions packages/quill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"style-loader": "^3.3.3",
"stylus": "^0.62.0",
"stylus-loader": "^7.1.3",
"svgo": "^3.2.0",
"terser-webpack-plugin": "^5.3.9",
"transpile-webpack-plugin": "^1.1.3",
"ts-loader": "^9.5.1",
Expand Down
43 changes: 43 additions & 0 deletions packages/quill/scripts/babel-svg-inline-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs');
const { dirname, resolve } = require('path');
const { optimize } = require('svgo');

module.exports = ({ types: t }) => {
class BabelSVGInlineImport {
constructor() {
return {
visitor: {
ImportDeclaration: {
exit(path, state) {
const givenPath = path.node.source.value;
if (!givenPath.endsWith('.svg')) {
return;
}
const specifier = path.node.specifiers[0];
const id = specifier.local.name;
const reference = state && state.file && state.file.opts.filename;
const absolutePath = resolve(dirname(reference), givenPath);
const content = optimize(
fs.readFileSync(absolutePath).toString(),
).data;

const variableValue = t.stringLiteral(content);
const variable = t.variableDeclarator(
t.identifier(id),
variableValue,
);

path.replaceWith({
type: 'VariableDeclaration',
kind: 'const',
declarations: [variable],
});
},
},
},
};
}
}

return new BabelSVGInlineImport();
};

0 comments on commit 5c9c317

Please sign in to comment.