From 9ea5da421e593739970d5e5de9974c4c69ac3c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Thu, 11 Nov 2021 11:47:47 +0100 Subject: [PATCH] refactor(core): use exportName transform (#616) BREAKING CHANGE: @svgr/core now exposes { transform } instead of default export. --- packages/cli/src/util.ts | 4 ++-- packages/core/README.md | 4 ++-- .../{convert.test.ts.snap => transform.test.ts.snap} | 0 packages/core/src/index.ts | 2 +- packages/core/src/{convert.test.ts => transform.test.ts} | 6 +++--- packages/core/src/{convert.ts => transform.ts} | 6 ++---- packages/rollup/src/index.ts | 4 ++-- packages/webpack/src/index.ts | 4 ++-- 8 files changed, 14 insertions(+), 16 deletions(-) rename packages/core/src/__snapshots__/{convert.test.ts.snap => transform.test.ts.snap} (100%) rename packages/core/src/{convert.test.ts => transform.test.ts} (99%) rename packages/core/src/{convert.ts => transform.ts} (93%) diff --git a/packages/cli/src/util.ts b/packages/cli/src/util.ts index d090133e..9c9a58e4 100644 --- a/packages/cli/src/util.ts +++ b/packages/cli/src/util.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import { promises as fs } from 'fs' import { red } from 'chalk' -import svgrConvert, { Config, State } from '@svgr/core' +import { transform, Config, State } from '@svgr/core' import svgo from '@svgr/plugin-svgo' import jsx from '@svgr/plugin-jsx' import prettier from '@svgr/plugin-prettier' @@ -31,7 +31,7 @@ export const convert = ( config: Config, state: Partial, ): string => { - return svgrConvert.sync(code, config, { + return transform.sync(code, config, { ...state, caller: { name: '@svgr/cli', diff --git a/packages/core/README.md b/packages/core/README.md index efb31756..69fa864a 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -13,7 +13,7 @@ npm install @svgr/core ## Usage ```js -import svgr from '@svgr/core' +import { transform } from '@svgr/core' const svgCode = ` ` -svgr(svgCode, { icon: true }, { componentName: 'MyComponent' }).then( +transform(svgCode, { icon: true }, { componentName: 'MyComponent' }).then( (jsCode) => { console.log(jsCode) }, diff --git a/packages/core/src/__snapshots__/convert.test.ts.snap b/packages/core/src/__snapshots__/transform.test.ts.snap similarity index 100% rename from packages/core/src/__snapshots__/convert.test.ts.snap rename to packages/core/src/__snapshots__/transform.test.ts.snap diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 065bfa8e..998ca93f 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,4 +1,4 @@ -export { default } from './convert' +export { transform } from './transform' export * from './config' export type { State } from './state' export type { Plugin, ConfigPlugin } from './plugins' diff --git a/packages/core/src/convert.test.ts b/packages/core/src/transform.test.ts similarity index 99% rename from packages/core/src/convert.test.ts rename to packages/core/src/transform.test.ts index 4b112283..33e1a0f2 100644 --- a/packages/core/src/convert.test.ts +++ b/packages/core/src/transform.test.ts @@ -1,11 +1,11 @@ -import convert, { Config, State } from '.' +import { transform, Config, State } from '.' function convertWithAllPlugins( code: string, config?: Config, state?: Partial, ) { - return convert( + return transform( code, { plugins: [ @@ -24,7 +24,7 @@ function convertSyncWithAllPlugins( config?: Config, state?: Partial, ) { - return convert.sync( + return transform.sync( code, { plugins: [ diff --git a/packages/core/src/convert.ts b/packages/core/src/transform.ts similarity index 93% rename from packages/core/src/convert.ts rename to packages/core/src/transform.ts index f68954c9..f176e0c4 100644 --- a/packages/core/src/convert.ts +++ b/packages/core/src/transform.ts @@ -15,7 +15,7 @@ const run = (code: string, config: Config, state: Partial): string => { return nextCode } -const convert = async ( +export const transform = async ( code: string, config: Config = {}, state: Partial = {}, @@ -24,7 +24,7 @@ const convert = async ( return run(code, config, state) } -convert.sync = ( +transform.sync = ( code: string, config: Config = {}, state: Partial = {}, @@ -32,5 +32,3 @@ convert.sync = ( config = loadConfig.sync(config, state) return run(code, config, state) } - -export default convert diff --git a/packages/rollup/src/index.ts b/packages/rollup/src/index.ts index 3d511a82..7d0438fa 100644 --- a/packages/rollup/src/index.ts +++ b/packages/rollup/src/index.ts @@ -1,5 +1,5 @@ import * as fs from 'fs' -import convert, { Config } from '@svgr/core' +import { transform, Config } from '@svgr/core' import { createFilter, CreateFilter } from 'rollup-pluginutils' import { transformAsync, createConfigItem } from '@babel/core' import svgo from '@svgr/plugin-svgo' @@ -58,7 +58,7 @@ const plugin: PluginImpl = (options = {}) => { const previousExport = exportMatches ? data : null - const jsCode = await convert(load, options, { + const jsCode = await transform(load, options, { filePath: id, caller: { name: '@svgr/rollup', diff --git a/packages/webpack/src/index.ts b/packages/webpack/src/index.ts index 6cd9be1a..62c6efe5 100644 --- a/packages/webpack/src/index.ts +++ b/packages/webpack/src/index.ts @@ -1,6 +1,6 @@ import { callbackify } from 'util' import { transformAsync, createConfigItem } from '@babel/core' -import convert, { Config, State } from '@svgr/core' +import { transform, Config, State } from '@svgr/core' import { normalize } from 'path' import svgo from '@svgr/plugin-svgo' import jsx from '@svgr/plugin-jsx' @@ -42,7 +42,7 @@ interface LoaderOptions extends Config { const tranformSvg = callbackify( async (contents: string, options: LoaderOptions, state: Partial) => { const { babel = true, ...config } = options - const jsCode = await convert(contents, config, state) + const jsCode = await transform(contents, config, state) if (!babel) return jsCode const result = await transformAsync( jsCode,