mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-09 15:08:12 -05:00
docs(examples): update example with webpack
This commit is contained in:
@@ -7,14 +7,5 @@ A sample Webpack build for the browser.
|
|||||||
|
|
||||||
```
|
```
|
||||||
$ npm i
|
$ npm i
|
||||||
$ npm run build-all
|
$ npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
There are two WebPack configuration:
|
|
||||||
|
|
||||||
- the minimal configuration, just bundling the application and its dependencies. The `app.js` file in the `dist` folder is the result of that build.
|
|
||||||
|
|
||||||
- a slimmer one, where:
|
|
||||||
- the JSON polyfill needed for IE6/IE7 support has been removed.
|
|
||||||
- the `debug` calls and import have been removed (the [debug](https://github.com/visionmedia/debug) library is included in the build by default).
|
|
||||||
- the source has been uglified (dropping IE8 support), and an associated SourceMap has been generated.
|
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<!-- <script src="dist/app.js"></script> -->
|
<script src="dist/bundle.js"></script>
|
||||||
<script src="dist/app.slim.js"></script>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
15
examples/webpack-build/index.js
Normal file
15
examples/webpack-build/index.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { io } from "socket.io-client";
|
||||||
|
|
||||||
|
const socket = io("http://localhost:3000");
|
||||||
|
|
||||||
|
socket.on("connect", () => {
|
||||||
|
console.log(`connect ${socket.id}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("connect_error", (err) => {
|
||||||
|
console.log(`connect_error due to ${err.message}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("disconnect", (reason) => {
|
||||||
|
console.log(`disconnect due to ${reason}`);
|
||||||
|
});
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
import io from 'socket.io-client';
|
|
||||||
|
|
||||||
const socket = io('http://localhost:3000');
|
|
||||||
|
|
||||||
console.log('init');
|
|
||||||
|
|
||||||
socket.on('connect', onConnect);
|
|
||||||
|
|
||||||
function onConnect(){
|
|
||||||
console.log('connect ' + socket.id);
|
|
||||||
}
|
|
||||||
@@ -2,20 +2,15 @@
|
|||||||
"name": "webpack-build",
|
"name": "webpack-build",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "A sample Webpack build",
|
"description": "A sample Webpack build",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "webpack --config ./support/webpack.config.js",
|
"build": "webpack"
|
||||||
"build-slim": "webpack --config ./support/webpack.config.slim.js",
|
|
||||||
"build-json-parser": "webpack --config ./support/webpack.config.json-parser.js",
|
|
||||||
"build-all": "npm run build && npm run build-slim && npm run build-json-parser"
|
|
||||||
},
|
},
|
||||||
"author": "Damien Arrachequesne",
|
"author": "Damien Arrachequesne",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"socket.io-client": "^2.0.2",
|
"socket.io-client": "^4.4.1",
|
||||||
"socket.io-json-parser": "^2.1.0"
|
"webpack": "^5.72.0",
|
||||||
},
|
"webpack-cli": "^4.9.2"
|
||||||
"devDependencies": {
|
|
||||||
"strip-loader": "^0.1.2",
|
|
||||||
"webpack": "^2.6.1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
module.exports = function () { return function () {}; };
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
|
|
||||||
module.exports = {
|
|
||||||
entry: './lib/index.js',
|
|
||||||
output: {
|
|
||||||
path: require('path').join(__dirname, '../dist'),
|
|
||||||
filename: 'app.js'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
|
|
||||||
var webpack = require('webpack');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
entry: './lib/index.js',
|
|
||||||
output: {
|
|
||||||
path: require('path').join(__dirname, '../dist'),
|
|
||||||
filename: 'app.json-parser.js'
|
|
||||||
},
|
|
||||||
// generate sourcemap
|
|
||||||
devtool: 'source-map',
|
|
||||||
plugins: [
|
|
||||||
// replace require('debug')() with an noop function
|
|
||||||
new webpack.NormalModuleReplacementPlugin(/debug/, process.cwd() + '/support/noop.js'),
|
|
||||||
// replace socket.io-parser with socket.io-json-parser
|
|
||||||
new webpack.NormalModuleReplacementPlugin(/socket\.io-parser/, 'socket.io-json-parser'),
|
|
||||||
// use uglifyJS (IE9+ support)
|
|
||||||
new webpack.optimize.UglifyJsPlugin({
|
|
||||||
compress: {
|
|
||||||
warnings: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
],
|
|
||||||
module: {
|
|
||||||
loaders: [
|
|
||||||
{
|
|
||||||
// strip `debug()` calls
|
|
||||||
test: /\.js$/,
|
|
||||||
loader: 'strip-loader?strip[]=debug'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
|
|
||||||
var webpack = require('webpack');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
entry: './lib/index.js',
|
|
||||||
output: {
|
|
||||||
path: require('path').join(__dirname, '../dist'),
|
|
||||||
filename: 'app.slim.js'
|
|
||||||
},
|
|
||||||
// generate sourcemap
|
|
||||||
devtool: 'source-map',
|
|
||||||
plugins: [
|
|
||||||
// replace require('debug')() with an noop function
|
|
||||||
new webpack.NormalModuleReplacementPlugin(/debug/, process.cwd() + '/support/noop.js'),
|
|
||||||
// use uglifyJS (IE9+ support)
|
|
||||||
new webpack.optimize.UglifyJsPlugin({
|
|
||||||
compress: {
|
|
||||||
warnings: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
],
|
|
||||||
module: {
|
|
||||||
loaders: [
|
|
||||||
{
|
|
||||||
// strip `debug()` calls
|
|
||||||
test: /\.js$/,
|
|
||||||
loader: 'strip-loader?strip[]=debug'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
7
examples/webpack-build/webpack.config.js
Normal file
7
examples/webpack-build/webpack.config.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export default {
|
||||||
|
entry: "./index.js",
|
||||||
|
mode: "production",
|
||||||
|
output: {
|
||||||
|
filename: "bundle.js",
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user