Skip to content

Commit

Permalink
fix(webpack): use static babel config
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
gregberge committed Nov 24, 2018
1 parent 81e0d6b commit 14f6d73
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
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

0 comments on commit 14f6d73

Please sign in to comment.