Skip to content

Commit

Permalink
feat: replaces axios with fetch; adds terser webpack plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
msimmer committed Sep 20, 2022
1 parent ff2a158 commit 2f1922b
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 18 deletions.
3 changes: 3 additions & 0 deletions packages/b-ber-reader-react/.browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
last 2 versions
> 2%
not dead
1 change: 1 addition & 0 deletions packages/b-ber-reader-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"style-loader": "^0.20.2",
"sugarss": "^1.0.1",
"tar": "^6.1.11",
"terser-webpack-plugin": "^4.2.3",
"url-loader": "^1.0.1",
"webpack": "^4.20.2",
"webpack-bundle-analyzer": "^3.3.2",
Expand Down
3 changes: 1 addition & 2 deletions packages/b-ber-reader-react/src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ class App extends Component {
// Books with a different full-path specified in the container.xml will of
// course fail
try {
const resp = await Request.get(manifestURL)
const resp = await Request.getJson(manifestURL)
const { data } = resp

const { href: opfURL } = data.resources.find(
res => res.type === 'application/oebps-package+xml'
)
Expand Down
2 changes: 1 addition & 1 deletion packages/b-ber-reader-react/src/components/Footnote.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Footnote extends React.Component {
const { hash } = new window.URL(this.props.href)
const id = hash.slice(1)

const { data } = await Request.get(this.props.href)
const { data } = await Request.getText(this.props.href)
const parser = new window.DOMParser()
const doc = parser.parseFromString(data, 'text/html')
const elem = doc.getElementById(id)
Expand Down
6 changes: 4 additions & 2 deletions packages/b-ber-reader-react/src/components/Reader/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function createStateFromOPF(callback) {
let guideItems
let spineItems

data = await Request.get(opfURL)
data = await Request.getText(opfURL)
data = await XMLAdaptor.parseOPF(data)
data = await XMLAdaptor.parseNCX(data, opsURL)
;[guideItems, spineItems] = await Promise.all([
Expand Down Expand Up @@ -77,7 +77,9 @@ export async function loadSpineItem(spineItem, deferredCallback) {

try {
const { cache, opsURL } = this.state
const { data, request } = await Request.get(requestedSpineItem.absoluteURL)
const { data, request } = await Request.getText(
requestedSpineItem.absoluteURL
)

content = await XMLAdaptor.parseSpineItemResponse({
data,
Expand Down
20 changes: 16 additions & 4 deletions packages/b-ber-reader-react/src/helpers/Request.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import axios from 'axios'
import Url from './Url'

class Request {
static get(url) {
return axios.get(url)
static async get(requestUrl, type) {
const resp = await fetch(requestUrl)
const { ok, status, url } = resp

const data = await resp[type]()

return { data, request: { ok, status, url } }
}

static async getJson(requestUrl) {
return Request.get(requestUrl, 'json')
}

static async getText(requestUrl) {
return Request.get(requestUrl, 'text')
}

static getBooks(basePath = '') {
const url = `${Url.stripTrailingSlash(basePath)}/api/books.json`
return Request.get(url)
return Request.getJson(url)
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/b-ber-reader-react/src/helpers/XMLAdaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import generate from 'css-tree/lib/generator'

import Url from './Url'
import Request from './Request'
import Cache from './Cache'
// import Cache from './Cache'
import { BookMetadata, SpineItem, GuideItem } from '../models'
import { processingInstructions, isValidNode } from '../lib/process-nodes'
import DocumentProcessor from '../lib/DocumentProcessor'
Expand Down Expand Up @@ -69,7 +69,7 @@ class XMLAdaptor {
}

const { href } = item.attributes
Request.get(Url.resolveRelativeURL(opsURL, href)).then(({ data }) => {
Request.getText(Url.resolveRelativeURL(opsURL, href)).then(({ data }) => {
resolve({
...rootNode,
__ncx: JSON.parse(xmljs.xml2json(data)),
Expand Down Expand Up @@ -300,14 +300,14 @@ class XMLAdaptor {
}

static async parseSpineItemResponse(response) {
const { responseURL } = response.request
const { url: responseURL } = response.request

const {
hash,
opsURL,
paddingLeft,
columnGap,
cache: useLocalStorageCache,
// cache: useLocalStorageCache,
} = response

const htmlToReactParser = new HtmlToReactParser()
Expand Down Expand Up @@ -360,7 +360,7 @@ class XMLAdaptor {
// return
// }

Request.get(url).then(rsp => {
Request.getText(url).then(rsp => {
// if (useLocalStorageCache) {
// console.log('No CSS cache - setting cache for %s', url)
// Cache.set(url, rsp.data)
Expand Down
6 changes: 6 additions & 0 deletions packages/b-ber-reader-react/webpack/config.production.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable global-require */

const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const TerserPlugin = require('terser-webpack-plugin')

const common = require('./common')
const loaders = require('./loaders')
Expand Down Expand Up @@ -35,6 +36,11 @@ module.exports = {
},
},

optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},

plugins,

module: {
Expand Down
4 changes: 0 additions & 4 deletions packages/b-ber-reader-react/webpack/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ module.exports = [

// https://github.com/webpack/webpack/issues/4039
modules: 'commonjs',

targets: {
browsers: 'last 2 versions, > 2%',
},
},
],
'@babel/preset-react',
Expand Down

0 comments on commit 2f1922b

Please sign in to comment.