Skip to content

Commit 2fe397e

Browse files
committed
Webpack example and test
1 parent 6a95761 commit 2fe397e

File tree

11 files changed

+110
-8
lines changed

11 files changed

+110
-8
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ doc/
44
examples/static/watson-speech.js
55
examples/static/bower_components/
66
examples/node_modules/
7+
examples/static/webpack-bundle.js

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ doc/
99
*-auth.json
1010
.env
1111
dist/*.min.js
12+
examples/static/webpack-bundle.js

examples/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "1.0.0",
44
"description": "Examples showing the IBM Watson Speech Javascript SDK in action.",
55
"scripts": {
6-
"start": "node server.js"
6+
"start": "npm run build-webpack && npm run server",
7+
"server": "node server.js",
8+
"build-webpack": "webpack"
79
},
810
"dependencies": {
911
"defaults": "^1.0.3",
@@ -16,5 +18,8 @@
1618
"watson-speech": "*",
1719
"whatwg-fetch": "^1.0.0"
1820
},
19-
"license": "Apache-2.0"
21+
"license": "Apache-2.0",
22+
"devDependencies": {
23+
"webpack": "^2.2.1"
24+
}
2025
}

examples/server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ app.use(express.static(__dirname + '/static'));
2727

2828
// set up express-browserify to serve bundles for examples
2929
var isDev = app.get('env') === 'development';
30-
app.get('/bundle.js', expressBrowserify('static/browserify-app.js', {
30+
app.get('/browserify-bundle.js', expressBrowserify('static/browserify-app.js', {
3131
watch: isDev,
3232
debug: isDev
3333
}));
@@ -36,6 +36,7 @@ app.get('/audio-video-deprecated/bundle.js', expressBrowserify('static/audio-vid
3636
debug: isDev
3737
}));
3838

39+
// webpack bundle is built by running `npm run build-webpack` or `npm start`
3940

4041
// token endpoints
4142
// **Warning**: these endpoints should be guarded with additional authentication & authorization for production use

examples/static/browserify.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>Watson Speech to Text client example</title>
5+
<title>Watson Speech to Text Browserify example</title>
66
</head>
77
<body>
88

@@ -14,10 +14,10 @@ <h2>Output:</h2>
1414
<div id="output">--</div>
1515
</section>
1616

17-
<script src="bundle.js"></script>
17+
<script src="browserify-bundle.js"></script>
1818

1919
<h2>Code for this demo:</h2>
20-
<p>Note: this code is compiled into <a href="bundle.js"><code>bundle.js</code></a> by <code>express-browserify</code>.
20+
<p>Note: this code is compiled into <a href="browserify-bundle.js"><code>browserify-bundle.js</code></a> by <code>express-browserify</code>.
2121
This requires Node.js.</p>
2222

2323
<pre><code><embed type="text/plain" src="browserify-app.js" width="100%" height="600"></embed></code></pre>

examples/static/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ <h3>Microphone Input</h3>
1717
<li><a href="microphone-streaming-model.html">Transcribe from Microphone, Streaming with chosen model</a></li>
1818
<li><a href="microphone-streaming-object-to-console.html">Transcribe from Microphone, send JSON to console (includes text and metadata; v0.22+ format)</a></li>
1919
<li><a href="browserify.html">Example bundled with browserify</a>*</li>
20+
<li><a href="webpack.html">Example bundled with Webpack</a>*</li>
2021
<li><strike><a href="microphone-streaming-object-extracted-to-console.html">Deprecated: Transcribe from Microphone, send JSON to console with results extracted (pre-v0.22 format)</a></strike></li>
2122
</ul>
2223
<h3>File Input</h3>
@@ -33,7 +34,7 @@ <h3>File Input</h3>
3334
<li><strike><a href="audio-video-deprecated/">Deprecated: Transcribe from HTML5 &lt;audio&gt; or &lt;video&gt; element</a></strike>*</li>
3435
</ul>
3536

36-
<p>* Examples that depend on browserify work with the example Node.js server, but not the example Python server.</p>
37+
<p>* Examples that depend on browserify or webpack require Node.js and will not work with the example Python server.</p>
3738

3839
<h2>Text to Speech</h2>
3940
<ul>

examples/static/webpack-app.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Webpack bundling example
2+
3+
'use strict';
4+
5+
// global window.fetch pollyfill for IE/Edge & Older Chrome/FireFox
6+
require('whatwg-fetch');
7+
8+
// keep the bundle slim by only requiring the necessary modules
9+
var recognizeMicrophone = require('watson-speech/speech-to-text/recognize-microphone');
10+
11+
document.querySelector('#button').onclick = function() {
12+
13+
fetch('/api/speech-to-text/token')
14+
.then(function(response) {
15+
return response.text();
16+
}).then(function(token) {
17+
18+
var stream = recognizeMicrophone({
19+
token: token,
20+
continuous: false, // false = automatically stop transcription the first time a pause is detected
21+
outputElement: '#output' // CSS selector or DOM Element
22+
});
23+
24+
stream.on('error', function(err) {
25+
console.log(err);
26+
});
27+
28+
}).catch(function(error) {
29+
console.log(error);
30+
});
31+
};

examples/static/webpack.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Watson Speech to Text Webpack example</title>
6+
</head>
7+
<body>
8+
9+
<section>
10+
<h2>Transcribe from Microphone</h2>
11+
<button id="button">Start Microphone Transcription</button>
12+
13+
<h2>Output:</h2>
14+
<div id="output">--</div>
15+
</section>
16+
17+
<script src="webpack-bundle.js"></script>
18+
19+
<h2>Code for this demo:</h2>
20+
<p>Note: this code is compiled into <a href="webpack-bundle.js"><code>webpack-bundle.js</code></a> by running either
21+
<code>npm run build-webpack</code> or <code>npm start</code>.
22+
This requires Node.js.</p>
23+
24+
<pre><code><embed type="text/plain" src="webpack-app.js" width="100%" height="600"></embed></code></pre>
25+
26+
</body>
27+
</html>

examples/webpack.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
entry: './static/webpack-app.js',
5+
output: {
6+
filename: './static/webpack-bundle.js'
7+
}
8+
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
"karma-express-http-server": "0.0.1",
3434
"karma-firefox-launcher": "^1.0.0",
3535
"karma-mocha": "^1.3.0",
36+
"memory-fs": "^0.4.1",
3637
"mocha": "^3.2.0",
3738
"serve-static": "^1.11.1",
3839
"sinon": "^1.17.7",
3940
"uglify-js": "^2.7.5",
4041
"watchify": "^3.8.0",
41-
"watson-developer-cloud": "^2.14.5"
42+
"watson-developer-cloud": "^2.14.5",
43+
"webpack": "^2.2.1"
4244
},
4345
"dependencies": {
4446
"clone": "^2.1.0",

0 commit comments

Comments
 (0)