Skip to content

Commit

Permalink
feat: add parcel plugin (#235)
Browse files Browse the repository at this point in the history
Closes #215
  • Loading branch information
restlessbit authored and gregberge committed Nov 24, 2018
1 parent e3009cb commit 144dbe3
Show file tree
Hide file tree
Showing 12 changed files with 1,925 additions and 78 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',
}
2 changes: 2 additions & 0 deletions packages/parcel-plugin-svgr/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/
.*
25 changes: 25 additions & 0 deletions packages/parcel-plugin-svgr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# @svgr/parcel-plugin-svgr

Parcel plugin for SVGR.

```
npm install @svgr/parcel-plugin-svgr
```

## Usage

In your code:

```js
import Star from './star.svg'

const App = () => (
<div>
<Star />
</div>
)
```

## License

MIT
42 changes: 42 additions & 0 deletions packages/parcel-plugin-svgr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@svgr/parcel-plugin-svgr",
"description": "SVGR Parcel plugin.",
"version": "4.0.4",
"main": "lib/index.js",
"repository": "[email protected]:smooth-code/svgr.git",
"author": "Mario Pabon <[email protected]>",
"publishConfig": {
"access": "public"
},
"keywords": [
"svgr",
"svg",
"react",
"parcel",
"parcel-plugin"
],
"engines": {
"node": ">=8"
},
"license": "MIT",
"scripts": {
"prebuild": "rm -rf lib/",
"build": "babel --config-file ../../babel.config.js -d lib --ignore \"**/*.test.js\" src",
"prepublishOnly": "yarn run build"
},
"peerDependencies": {
"parcel-bundler": "^1.10.0"
},
"dependencies": {
"@babel/core": "^7.1.6",
"@babel/plugin-transform-react-constant-elements": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@svgr/core": "^4.0.3",
"@svgr/plugin-jsx": "^4.0.3",
"@svgr/plugin-svgo": "^4.0.3"
},
"devDependencies": {
"parcel-bundler": "^1.10.3"
}
}
13 changes: 13 additions & 0 deletions packages/parcel-plugin-svgr/src/__fixtures__/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions packages/parcel-plugin-svgr/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parcel plugin should convert file 1`] = `
Array [
Object {
"js": "\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require(\\"react\\"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
var _ref =
/*#__PURE__*/
_react.default.createElement(\\"g\\", {
stroke: \\"#063855\\",
strokeWidth: 2,
fill: \\"none\\",
fillRule: \\"evenodd\\",
strokeLinecap: \\"square\\"
}, _react.default.createElement(\\"path\\", {
d: \\"M51 37L37 51M51 51L37 37\\"
}));
var SvgIcon = function SvgIcon(props) {
return _react.default.createElement(\\"svg\\", _extends({
width: 88,
height: 88
}, props), _ref);
};
var _default = SvgIcon;
exports.default = _default;",
"map": undefined,
},
]
`;
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
6 changes: 6 additions & 0 deletions packages/parcel-plugin-svgr/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +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) {
// 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()
})
})
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

1 comment on commit 144dbe3

@vercel
Copy link

@vercel vercel bot commented on 144dbe3 Nov 24, 2018

Choose a reason for hiding this comment

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

Successfully aliased the URL https://svgr-eymazptvzc.now.sh to the following alias.

Please sign in to comment.