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

fix(webpack): use static babel config #240

Merged
merged 1 commit into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
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
34 changes: 18 additions & 16 deletions packages/rollup/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import fs from 'fs'
import convert from '@svgr/core'
import { createFilter } from 'rollup-pluginutils'
import { transform as babelTransform } from '@babel/core'
import { transform as babelTransform, createConfigItem } from '@babel/core'
import svgo from '@svgr/plugin-svgo'
import jsx from '@svgr/plugin-jsx'
import presetReact from '@babel/preset-react'
import presetEnv from '@babel/preset-env'
import pluginTransformReactConstantElements from '@babel/plugin-transform-react-constant-elements'

const babelOptions = {
babelrc: false,
configFile: false,
presets: [
createConfigItem(presetReact, { type: 'preset' }),
createConfigItem([presetEnv, { modules: false }], { type: 'preset' }),
],
plugins: [createConfigItem(pluginTransformReactConstantElements)],
}

function svgrPlugin(options = {}) {
const filter = createFilter(options.include || '**/*.svg', options.exclude)
Expand Down Expand Up @@ -42,21 +55,10 @@ function svgrPlugin(options = {}) {

const pBabelTransform = async code =>
new Promise((resolve, reject) => {
babelTransform(
code,
{
babelrc: false,
presets: [
'@babel/preset-react',
['@babel/preset-env', { modules: false }],
],
plugins: ['@babel/plugin-transform-react-constant-elements'],
},
(err, result) => {
if (err) reject(err)
else resolve(result.code)
},
)
babelTransform(code, babelOptions, (err, result) => {
if (err) reject(err)
else resolve(result.code)
})
})

if (babel) {
Expand Down
36 changes: 18 additions & 18 deletions packages/webpack/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { getOptions } from 'loader-utils'
import { transform as babelTransform } from '@babel/core'
import { transform as babelTransform, createConfigItem } from '@babel/core'
import convert from '@svgr/core'
import svgo from '@svgr/plugin-svgo'
import jsx from '@svgr/plugin-jsx'
import presetReact from '@babel/preset-react'
import presetEnv from '@babel/preset-env'
import pluginTransformReactConstantElements from '@babel/plugin-transform-react-constant-elements'

const babelOptions = {
babelrc: false,
configFile: false,
presets: [
createConfigItem(presetReact, { type: 'preset' }),
createConfigItem([presetEnv, { modules: false }], { type: 'preset' }),
],
plugins: [createConfigItem(pluginTransformReactConstantElements)],
}

function svgrLoader(source) {
const callback = this.async()
Expand All @@ -25,23 +38,10 @@ function svgrLoader(source) {

const pBabelTransform = async jsCode =>
new Promise((resolve, reject) => {
babelTransform(
jsCode,
{
babelrc: false,
// Unless having this, babel will merge the config with global 'babel.config.js' which may causes some problems such as using react-hot-loader/babel in babel.config.js
configFile: false,
presets: [
'@babel/preset-react',
['@babel/preset-env', { modules: false }],
],
plugins: ['@babel/plugin-transform-react-constant-elements'],
},
(err, result) => {
if (err) reject(err)
else resolve(result.code)
},
)
babelTransform(jsCode, babelOptions, (err, result) => {
if (err) reject(err)
else resolve(result.code)
})
})

const tranformSvg = svg =>
Expand Down