Skip to content

Commit

Permalink
Fix Parcel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Nov 24, 2018
1 parent b9436b5 commit dc084a9
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 226 deletions.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
watchPathIgnorePatterns: ['__fixtures__', '__fixtures__build__'],
rootDir: 'packages',
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@svgr/parcel",
"name": "@svgr/parcel-plugin-svgr",
"description": "SVGR Parcel plugin.",
"version": "4.0.0",
"version": "4.0.4",
"main": "lib/index.js",
"repository": "[email protected]:smooth-code/svgr.git",
"author": "Mario Pabon <[email protected]>",
Expand All @@ -28,11 +28,13 @@
"parcel-bundler": "^1.10.0"
},
"dependencies": {
"@babel/core": "^7.1.2",
"@babel/core": "^7.1.6",
"@babel/plugin-transform-react-constant-elements": "^7.0.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@svgr/core": "^4.0.2"
"@svgr/core": "^4.0.3",
"@svgr/plugin-jsx": "^4.0.3",
"@svgr/plugin-svgo": "^4.0.3"
},
"devDependencies": {
"parcel-bundler": "^1.10.3"
Expand Down
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,24 @@ function _extends() {
var _ref =
/*#__PURE__*/
_react.default.createElement(\\"title\\", null, \\"Dismiss\\");
var _ref2 =
/*#__PURE__*/
_react.default.createElement(\\"desc\\", null, \\"Created with Sketch.\\");
var _ref3 =
/*#__PURE__*/
_react.default.createElement(\\"defs\\", null);
var _ref4 =
/*#__PURE__*/
_react.default.createElement(\\"g\\", {
id: \\"Blocks\\",
stroke: \\"none\\",
strokeWidth: 1,
stroke: \\"#063855\\",
strokeWidth: 2,
fill: \\"none\\",
fillRule: \\"evenodd\\",
strokeLinecap: \\"square\\"
}, _react.default.createElement(\\"g\\", {
id: \\"Dismiss\\",
stroke: \\"#063855\\",
strokeWidth: 2
}, _react.default.createElement(\\"path\\", {
d: \\"M51,37 L37,51\\",
id: \\"Shape\\"
}), _react.default.createElement(\\"path\\", {
d: \\"M51,51 L37,37\\",
id: \\"Shape\\"
})));
d: \\"M51 37L37 51M51 51L37 37\\"
}));
var SvgComponent = function SvgComponent(props) {
var SvgIcon = function SvgIcon(props) {
return _react.default.createElement(\\"svg\\", _extends({
width: \\"88px\\",
height: \\"88px\\",
viewBox: \\"0 0 88 88\\"
}, props), _ref, _ref2, _ref3, _ref4);
width: 88,
height: 88
}, props), _ref);
};
var _default = SvgComponent;
var _default = SvgIcon;
exports.default = _default;",
"map": undefined,
},
Expand Down
46 changes: 46 additions & 0 deletions packages/parcel-plugin-svgr/src/asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Asset } from 'parcel-bundler'
import { transformAsync, createConfigItem } from '@babel/core'
import svgo from '@svgr/plugin-svgo'
import jsx from '@svgr/plugin-jsx'
import convert from '@svgr/core'
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)],
}

class ReactSVGAsset extends Asset {
type = 'js'

async parse(contents) {
const code = await convert(
contents,
{},
{
caller: {
name: '@svgr/parcel',
defaultPlugins: [svgo, jsx],
},
filePath: this.name,
},
)

const { code: babelCode } = await transformAsync(code, babelOptions)

return babelCode
}

async generate() {
return [{ type: 'js', value: this.ast }]
}
}

module.exports = ReactSVGAsset
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Parcel requires that plugins be exported with `module.exports`.
// It won't be able to load the plugin if it is exported with "export default plugin" and then transpiled.
module.exports = function svgrParcelPlugin (bundler) {
module.exports = function svgrParcelPlugin(bundler) {
// Parcel requires that the asset be passed in as a module path.
bundler.addAssetType('svg', require.resolve('./asset'))
}
28 changes: 28 additions & 0 deletions packages/parcel-plugin-svgr/src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Bundler from 'parcel-bundler'
import path from 'path'
import plugin from '.'

const getCode = bundle =>
Array.from(bundle.assets)
.filter(asset => asset.id === 'icon.svg')
.map(asset => asset.generated)

describe('parcel plugin', () => {
it('should convert file', async () => {
const bundler = new Bundler(path.join(__dirname, '__fixtures__/icon.svg'), {
outDir: path.join(__dirname, '../../../__fixtures_build__/parcel'),
cache: false,
detailedReport: false,
hmr: false,
logLevel: 0,
target: 'node',
watch: false,
})

plugin(bundler)

const bundle = await bundler.bundle()
const code = getCode(bundle)
expect(code).toMatchSnapshot()
})
})
44 changes: 0 additions & 44 deletions packages/parcel/src/asset.js

This file was deleted.

37 changes: 0 additions & 37 deletions packages/parcel/src/index.test.js

This file was deleted.

13 changes: 2 additions & 11 deletions packages/rollup/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import convert from '@svgr/core'
import { createFilter } from 'rollup-pluginutils'
import { transform as babelTransform, createConfigItem } from '@babel/core'
import { transformAsync, createConfigItem } from '@babel/core'
import svgo from '@svgr/plugin-svgo'
import jsx from '@svgr/plugin-jsx'
import presetReact from '@babel/preset-react'
Expand Down Expand Up @@ -53,17 +53,8 @@ function svgrPlugin(options = {}) {
},
})

const pBabelTransform = async code =>
new Promise((resolve, reject) => {
babelTransform(code, babelOptions, (err, result) => {
if (err) reject(err)
else resolve(result.code)
})
})

if (babel) {
const code = await pBabelTransform(jsCode)

const { code } = await transformAsync(jsCode, babelOptions)
return { code, map: { mappings: '' } }
}

Expand Down
15 changes: 5 additions & 10 deletions packages/webpack/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getOptions } from 'loader-utils'
import { transform as babelTransform, createConfigItem } from '@babel/core'
import { transformAsync, createConfigItem } from '@babel/core'
import convert from '@svgr/core'
import svgo from '@svgr/plugin-svgo'
import jsx from '@svgr/plugin-jsx'
Expand Down Expand Up @@ -36,14 +36,6 @@ function svgrLoader(source) {
? `export default ${exportMatches[1]}`
: null

const pBabelTransform = async jsCode =>
new Promise((resolve, reject) => {
babelTransform(jsCode, babelOptions, (err, result) => {
if (err) reject(err)
else resolve(result.code)
})
})

const tranformSvg = svg =>
convert(svg, options, {
caller: {
Expand All @@ -53,7 +45,10 @@ function svgrLoader(source) {
},
filePath: this.resourcePath,
})
.then(jsCode => (babel ? pBabelTransform(jsCode) : jsCode))
.then(jsCode => {
if (!babel) return jsCode
return transformAsync(jsCode, babelOptions).then(({ code }) => code)
})
.then(result => callback(null, result))
.catch(err => callback(err))

Expand Down
Loading

0 comments on commit dc084a9

Please sign in to comment.