Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make this plugin works with tsup's bundle: false option #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const svgrPlugin = (options = {}) => ({
name: 'svgr',
setup(build) {
build.onResolve({ filter: /\.svg$/ }, async (args) => {
if (build.bundle) {
switch (args.kind) {
case 'import-statement':
case 'require-call':
Expand All @@ -16,15 +17,14 @@ const svgrPlugin = (options = {}) => ({
external: true,
}
}
}
})

build.onLoad({ filter: /\.svg$/ }, async (args) => {
const svg = await readFile(args.path, { encoding: 'utf8' })

if (options.plugins && !options.plugins.includes('@svgr/plugin-jsx')) {
const svg = await readFile(args.path, 'utf8')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can provide encoding as string in 2nd argument

options.plugins ||= []
if (!options.plugins.includes('@svgr/plugin-jsx')) {
options.plugins.push('@svgr/plugin-jsx')
} else if (!options.plugins) {
options.plugins = ['@svgr/plugin-jsx']
Comment on lines +25 to -27
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to look nicer

}

const contents = await transform(svg, { ...options }, { filePath: args.path })
Expand Down