Skip to content

Commit

Permalink
Merge pull request #130 from philschatz/fix-webpack
Browse files Browse the repository at this point in the history
Fix webpack
  • Loading branch information
philschatz committed Jan 18, 2017
2 parents 1f91932 + c983ab0 commit 241a7d5
Show file tree
Hide file tree
Showing 29 changed files with 675 additions and 267 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Expand Up @@ -9,3 +9,11 @@
/dist/octokat.js.map

/coverage/

# Generated by compiling the .coffee files
src/grammar/object-matcher.js
src/grammar/object-matcher.js.map
src/grammar/preview-headers.js
src/grammar/preview-headers.js.map
src/grammar/url-validator.js
src/grammar/url-validator.js.map
705 changes: 576 additions & 129 deletions dist/octokat.js

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions package.json
Expand Up @@ -9,11 +9,12 @@
"pretest": "npm run-script build",
"test": "npm run-script test-node && npm run-script test-browser",
"pretest-node": "babel --source-maps true --source-root ./index.js ./test/ -d ./test-transpiled/",
"test-node": "VCR_MODE=cache istanbul cover _mocha -- --reporter spec ./test-transpiled/**/node**.js",
"pretest-browser": "webpack --config webpack.config-browsertests.js",
"test-node": "VCR_MODE=cache istanbul cover --report json _mocha -- --reporter spec ./test-transpiled/**/node**.js",
"pretest-browser": "webpack --config ./webpack.config-browsertests.js",
"test-browser": "./script/run-test-browser.sh",
"test-browser-only": "mocha-phantomjs --reporter spec --timeout 20000 http://localhost:9876/test/index.html",
"dist": "webpack --config ./webpack.config-node.js -v --progress && webpack --config ./webpack.config-browser.js -v --progress",
"test-browser:only": "mocha-phantomjs --reporter spec --timeout 20000 http://localhost:9876/test/index.html",
"predist": "coffee --map --compile ./src/grammar/*.coffee",
"dist": "babel --source-maps true --out-dir ./dist/node/ ./src/ && webpack --config ./webpack.config-browser.js -v --progress",
"update": "bower update",
"bump": "grunt release",
"blanket": {
Expand Down Expand Up @@ -42,6 +43,7 @@
"eslint-config-standard": "^6.2.0",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^2.0.1",
"glob": "^7.1.1",
"grunt": "~1.0.1",
"grunt-bump": "~0.8.0",
"http-server": "^0.9.0",
Expand Down Expand Up @@ -76,6 +78,11 @@
"directories": {
"test": "test"
},
"browser": {
"./dist/node/adapters/promise-node.js": "./dist/node/adapters/promise-browser.js",
"./dist/node/adapters/xhr-node.js": "./dist/node/adapters/xhr-browser.js",
"./dist/node/adapters/base64-node.js": "./dist/node/adapters/base64-browser.js"
},
"tonicExampleFilename": "examples/tonic-example.js",
"license": "MIT"
}
17 changes: 17 additions & 0 deletions script/coverage-helper.js
@@ -0,0 +1,17 @@
var fs = require('fs')
var glob = require('glob')

glob('./dist/node/**/*.map', removeSourcesContent)
glob('./test-transpiled/**/*.map', removeSourcesContent)

function removeSourcesContent(err, files) {
if (files.length === 0) {
console.error('Could not find any sourcemap files to tweak for remap-istanbul')
process.exit(1)
}
files.forEach(function(file) {
var contents = JSON.parse(fs.readFileSync(file))
delete contents.sourcesContent
fs.writeFileSync(file, JSON.stringify(contents))
})
}
2 changes: 1 addition & 1 deletion script/run-test-browser-inner.sh
@@ -1,6 +1,6 @@
#!/bin/sh

npm run test-browser-only
npm run-script test-browser:only

# So that parallelshell exits on success, send a non-zero exit status
if [ $? -eq 0 ]
Expand Down
11 changes: 9 additions & 2 deletions script/send-coverage.sh
@@ -1,13 +1,20 @@
#!/bin/bash

# Istanbul does not preserve sourcemaps when generating code coverage so
# this uses remap-istanbul. But remap-istanbul likes to use the sourcesContent
# field in the sourcemap files which causes them to not point to the correct
# source. So coverage-helper.js removes that field in all sourcemap files.

nodeVer=$(node --version)
semver=( ${nodeVer//./ } )

if [[ "${semver[0]}" == "v6" ]]; then

echo "==> Reporting coverage to codecov"
$(npm bin)/remap-istanbul --exclude 'index.js' --input ./coverage/coverage.json --output ./coverage/coverage-mapped-to-source.json --type json
$(npm bin)/codecov --file ./coverage/coverage-mapped-to-source.json
rm ./coverage/coverage.json # Just in case so codecov does not find it
node ./script/coverage-helper.js
$(npm bin)/remap-istanbul --exclude 'index.js' --input ./coverage/coverage-final.json --output ./coverage/coverage-mapped-to-source.json --type json
$(npm bin)/codecov --file=./coverage/coverage-mapped-to-source.json

else
echo "==> Skipping coverage reporting because remap-istanbul only works on node v6+"
Expand Down
7 changes: 7 additions & 0 deletions src/adapters/README.md
@@ -0,0 +1,7 @@
This directory contains files whose implementations differ between a browser and node.

For example, browsers have `btoa()`, `XMLHttpRequest`, and `Promise`
while node has `Buffer.toString('base64')`, `require('xmlhttprequest')`, and `require('es6-promise')`.

Webpack uses the `browser` directive in `package.json` to override the `*-node.js`
modules so they are not included in the build.
1 change: 1 addition & 0 deletions src/adapters/base64-browser.js
@@ -0,0 +1 @@
module.exports = btoa
4 changes: 4 additions & 0 deletions src/adapters/base64-node.js
@@ -0,0 +1,4 @@
module.exports = function base64encode (str) {
let buffer = new global['Buffer'](str, 'binary')
return buffer.toString('base64')
}
9 changes: 9 additions & 0 deletions src/adapters/base64.js
@@ -0,0 +1,9 @@
if (typeof btoa !== 'undefined') {
// For browsers use the native btoa
module.exports = require('./base64-browser')
} else if (typeof process !== 'undefined') {
// For node use HTTP adapter
module.exports = require('./base64-node')
} else {
throw new Error('Could not find base64 encode function')
}
1 change: 1 addition & 0 deletions src/adapters/promise-browser.js
@@ -0,0 +1 @@
module.exports = Promise
1 change: 1 addition & 0 deletions src/adapters/promise-node.js
@@ -0,0 +1 @@
module.exports = require('es6-promise').Promise
6 changes: 6 additions & 0 deletions src/adapters/promise.js
@@ -0,0 +1,6 @@
// Use native promises if Harmony is on
let Promise = this.Promise || require('./promise-node')
let newPromise = fn => new Promise(fn)
let allPromises = promises => Promise.all(promises)

module.exports = { newPromise, allPromises }
1 change: 1 addition & 0 deletions src/adapters/xhr-browser.js
@@ -0,0 +1 @@
module.exports = XMLHttpRequest
1 change: 1 addition & 0 deletions src/adapters/xhr-node.js
@@ -0,0 +1 @@
module.exports = require('xmlhttprequest').XMLHttpRequest
12 changes: 12 additions & 0 deletions src/adapters/xhr.js
@@ -0,0 +1,12 @@
let XHR
if (typeof XMLHttpRequest !== 'undefined') {
// For browsers use XHR adapter
XHR = require('./xhr-browser')
} else if (typeof process !== 'undefined') {
// For node use HTTP adapter
XHR = require('./xhr-node')
} else {
throw new Error('Could not find XMLHttpRequest')
}

module.exports = XHR
2 changes: 1 addition & 1 deletion src/base.js
Expand Up @@ -13,7 +13,7 @@ const applyHypermedia = require('./helpers/hypermedia')

// Checks if a response is a Buffer or not
const isBuffer = (data) => {
if (typeof global !== 'undefined') {
if (typeof global['Buffer'] !== 'undefined') {
return global['Buffer'].isBuffer(data)
} else {
// If `global` is not defined then we are not running inside Node so
Expand Down
1 change: 0 additions & 1 deletion src/hacks/xmlhttprequest-filler-browser.js

This file was deleted.

1 change: 0 additions & 1 deletion src/hacks/xmlhttprequest-filler-node.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/hacks/xmlhttprequest-filler.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/helpers/base64.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/authorization.js
@@ -1,4 +1,4 @@
const base64encode = require('../helpers/base64')
const base64encode = require('../adapters/base64')

module.exports = new class Authorization {
requestMiddlewareAsync (input, cb) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/promise/library-first.js
Expand Up @@ -4,7 +4,7 @@ if (!newPromise || !allPromises) {
({newPromise, allPromises} = require('../../helpers/promise-find-native'))
}
if ((typeof window === 'undefined' || window === null) && !newPromise) {
({newPromise, allPromises} = require('../../helpers/promise-node'))
({newPromise, allPromises} = require('../../adapters/promise'))
}

if ((typeof window !== 'undefined' && window !== null) && !newPromise) {
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/promise/native-first.js
Expand Up @@ -3,7 +3,7 @@ if (!newPromise || !allPromises) {
({newPromise, allPromises} = require('../../helpers/promise-find-library'))
}
if ((typeof window === 'undefined' || window === null) && !newPromise) {
({newPromise, allPromises} = require('../../helpers/promise-node'))
({newPromise, allPromises} = require('../../adapters/promise'))
}

if ((typeof window !== 'undefined' && window !== null) && !newPromise) {
Expand All @@ -18,5 +18,7 @@ if ((typeof window !== 'undefined' && window !== null) && !newPromise) {
}

module.exports = new class PreferNativeOverLibraryPromises {
promiseCreator = {newPromise, allPromises}
constructor() {
this.promiseCreator = {newPromise, allPromises}
}
}
9 changes: 1 addition & 8 deletions src/requester.js
Expand Up @@ -10,14 +10,7 @@ const { filter, map, waterfall } = require('./plus')
let ajax = function (options, cb) {
// Use the browser XMLHttpRequest if it exists. If not, then this is NodeJS
// Pull this in for every request so sepia.js has a chance to override `window.XMLHTTPRequest`
let XMLHttpRequest = null
if (typeof window !== 'undefined' && window !== null) {
({ XMLHttpRequest } = window)
} else {
let req = require;
({ XMLHttpRequest } = req('xmlhttprequest'))
}

const XMLHttpRequest = require('./adapters/xhr')
let xhr = new XMLHttpRequest()
xhr.dataType = options.dataType
__guardFunc__(xhr.overrideMimeType, f => f(options.mimeType))
Expand Down
14 changes: 2 additions & 12 deletions webpack.config-browser.js
@@ -1,11 +1,9 @@
var path = require('path')

module.exports = {
cache: true,
devtool: 'source-map',
entry: {
octokat: [
'./src/octokat.js'
'./dist/node/octokat.js'
]
},
output: {
Expand All @@ -17,19 +15,11 @@ module.exports = {
filename: '[name].js'
},
module: {
noParse: [
/promise-filler/
],
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
{ test: /\.coffee$/, loader: 'coffee-loader' }
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' }
]
},
resolve: {
alias: {
xmlhttprequest: path.join(__dirname, 'src/hacks/xmlhttprequest-filler-browser.js'),
'es6-promise': path.join(__dirname, 'src/hacks/promise-filler-browser.js')
},
extensions: ['', '.js', '.coffee']
}
}
9 changes: 0 additions & 9 deletions webpack.config-browsertests.js
@@ -1,5 +1,3 @@
var path = require('path')

module.exports = {
cache: true,
devtool: 'source-map',
Expand All @@ -17,18 +15,11 @@ module.exports = {
filename: 'octokat-browsertests.js'
},
module: {
noParse: [
/promise-filler/
],
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' }
]
},
resolve: {
alias: {
xmlhttprequest: path.join(__dirname, 'src/hacks/xmlhttprequest-filler-browser.js'),
'es6-promise': path.join(__dirname, 'src/hacks/promise-filler-browser.js')
},
extensions: ['', '.js']
}
}
38 changes: 0 additions & 38 deletions webpack.config-node.js

This file was deleted.

31 changes: 0 additions & 31 deletions webpack.config.js

This file was deleted.

0 comments on commit 241a7d5

Please sign in to comment.