Skip to content

Commit

Permalink
feat: support file-loader v5
Browse files Browse the repository at this point in the history
Fixes #367
  • Loading branch information
gregberge committed Dec 18, 2019
1 parent 95df1f4 commit e898c71
Show file tree
Hide file tree
Showing 12 changed files with 2,907 additions and 2,857 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"codecov": "^3.5.0",
"conventional-github-releaser": "^3.1.3",
"eslint": "^6.0.1",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-jsx-a11y": "^6.2.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"@svgr/plugin-prettier": "^4.3.2",
"@svgr/plugin-svgo": "^4.3.1",
"camelcase": "^5.3.1",
"chalk": "^2.4.2",
"commander": "^2.20.0",
"chalk": "^3.0.0",
"commander": "^4.0.1",
"dashify": "^2.0.0",
"glob": "^7.1.4",
"output-file-sync": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"dependencies": {
"@svgr/plugin-jsx": "^4.3.3",
"camelcase": "^5.3.1",
"cosmiconfig": "^5.2.1"
"cosmiconfig": "^6.0.0"
}
}
14 changes: 10 additions & 4 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cosmiconfig from 'cosmiconfig'
import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig'

export const DEFAULT_CONFIG = {
h2xConfig: null,
Expand All @@ -25,6 +25,12 @@ const explorer = cosmiconfig('svgr', {
rcExtensions: true,
})

const explorerSync = cosmiconfigSync('svgr', {
sync: true,
cache: true,
rcExtensions: true,
})

export async function resolveConfig(searchFrom, configFile) {
if (configFile == null) {
const result = await explorer.search(searchFrom)
Expand All @@ -36,10 +42,10 @@ export async function resolveConfig(searchFrom, configFile) {

resolveConfig.sync = (searchFrom, configFile) => {
if (configFile == null) {
const result = explorer.searchSync(searchFrom)
const result = explorerSync.search(searchFrom)
return result ? result.config : null
}
const result = explorer.loadSync(configFile)
const result = explorerSync.load(configFile)
return result ? result.config : null
}

Expand All @@ -49,7 +55,7 @@ export async function resolveConfigFile(filePath) {
}

resolveConfigFile.sync = filePath => {
const result = explorer.searchSync(filePath)
const result = explorerSync.search(filePath)
return result ? result.filepath : null
}

Expand Down
8 changes: 4 additions & 4 deletions packages/parcel-plugin-svgr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"parcel-bundler": "^1.10.0"
},
"dependencies": {
"@babel/core": "^7.4.5",
"@babel/plugin-transform-react-constant-elements": "^7.0.0",
"@babel/preset-env": "^7.4.5",
"@babel/preset-react": "^7.0.0",
"@babel/core": "^7.7.5",
"@babel/plugin-transform-react-constant-elements": "^7.7.4",
"@babel/preset-env": "^7.7.6",
"@babel/preset-react": "^7.7.4",
"@svgr/core": "^4.3.3",
"@svgr/plugin-jsx": "^4.3.3",
"@svgr/plugin-svgo": "^4.3.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"prepublishOnly": "yarn run build"
},
"dependencies": {
"@babel/core": "^7.4.5",
"@babel/core": "^7.7.5",
"@svgr/babel-preset": "^4.3.3",
"@svgr/hast-util-to-babel-ast": "^4.3.2",
"svg-parser": "^2.0.0"
"svg-parser": "^2.0.2"
}
}
2 changes: 1 addition & 1 deletion packages/plugin-svgo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"prepublishOnly": "yarn run build"
},
"dependencies": {
"cosmiconfig": "^5.2.1",
"cosmiconfig": "^6.0.0",
"merge-deep": "^3.0.2",
"svgo": "^1.2.2"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-svgo/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-underscore-dangle */
import SVGO from 'svgo'
import cosmiconfig from 'cosmiconfig'
import { cosmiconfigSync } from 'cosmiconfig'
import mergeDeep from 'merge-deep'

const explorer = cosmiconfig('svgo', {
const explorer = cosmiconfigSync('svgo', {
searchPlaces: [
'package.json',
'.svgorc',
Expand Down Expand Up @@ -114,7 +114,7 @@ function getInfo(state) {
export default function svgoPlugin(code, config, state) {
if (!config.svgo) return code
const filePath = getFilePath(state)
const svgoRcConfig = config.runtimeConfig ? explorer.searchSync(filePath) : {}
const svgoRcConfig = config.runtimeConfig ? explorer.search(filePath) : {}
const svgo = createSvgo(config, svgoRcConfig)
const { data } = optimizeSync.call(svgo, code, getInfo(state))
return data
Expand Down
12 changes: 6 additions & 6 deletions packages/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
"prepublishOnly": "yarn run build"
},
"dependencies": {
"@babel/core": "^7.4.5",
"@babel/plugin-transform-react-constant-elements": "^7.0.0",
"@babel/preset-env": "^7.4.5",
"@babel/preset-react": "^7.0.0",
"@babel/core": "^7.7.5",
"@babel/plugin-transform-react-constant-elements": "^7.7.4",
"@babel/preset-env": "^7.7.6",
"@babel/preset-react": "^7.7.4",
"@svgr/core": "^4.3.3",
"@svgr/plugin-jsx": "^4.3.3",
"@svgr/plugin-svgo": "^4.3.1",
"rollup-pluginutils": "^2.8.1"
"rollup-pluginutils": "^2.8.2"
},
"devDependencies": {
"rollup": "^1.14.2",
"rollup-plugin-image": "^1.0.2",
"rollup-plugin-url": "^2.2.1"
"rollup-plugin-url": "^3.0.1"
}
}
4 changes: 2 additions & 2 deletions packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
},
"devDependencies": {
"babel-loader": "^8.0.6",
"memory-fs": "^0.4.1",
"url-loader": "^2.0.0",
"memory-fs": "^0.5.0",
"url-loader": "^3.0.0",
"webpack": "^4.33.0"
}
}
18 changes: 11 additions & 7 deletions packages/webpack/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ function svgrLoader(source) {
})
})

const exportMatches = source
.toString('utf-8')
.match(/^module.exports\s*=\s*(.*)/)
const previousExport = exportMatches
? `export default ${exportMatches[1]}`
: null
const previousExport = (() => {
if (source.toString('utf-8').startsWith('export ')) {
return source
}

const exportMatches = source
.toString('utf-8')
.match(/^module.exports\s*=\s*(.*)/)
return exportMatches ? `export default ${exportMatches[1]}` : null
})()

const tranformSvg = svg =>
convert(svg, options, {
Expand All @@ -52,7 +56,7 @@ function svgrLoader(source) {
.then(result => callback(null, result))
.catch(err => callback(err))

if (exportMatches) {
if (previousExport) {
readSvg().then(tranformSvg)
} else {
tranformSvg(source)
Expand Down
Loading

0 comments on commit e898c71

Please sign in to comment.