Comments (6)
I realized I can use import 'mock-socket/dist/mock-socket'
instead.
Is it impossible to import es6 .js
files from a npm installed package, with webpack?
Because although it works this way, it seems better to require the original file. Webpack gives me this warning:
WARNING in ./~/mock-socket/dist/mock-socket.js
Critical dependencies:
1:113-120 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
@ ./~/mock-socket/dist/mock-socket.js 1:113-120
from mock-socket.
Thanks for the issue @saulshanabrook. You can import the individual JS files you will just need to have a es6 to es5 transpiler (such as babel) sit right before the webpack step. You can see how I do it in broccoli:
First I run babel over all of my files:
Line 36 in a625630
Then I send them to browserify:
Line 47 in a625630
I have never done this using webpack but there should be tutorials online about setting up babel + webpack
from mock-socket.
Thanks for the response @thoov. The thing is, I have babel set up and it works right on all my local files. I have webpack set up to use babel for all .js files.
from mock-socket.
Can you paste in the "bundler" file so I can see how you are preparing the files for babel?
from mock-socket.
webpack.config.js
:
var path = require('path')
var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var loadersByExtension = require('./config/loadersByExtension')
var booleanFromEnv = require('./config/booleanFromEnv')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var AutoprefixerCore = require('autoprefixer-core')
var plugins = [
new webpack.PrefetchPlugin('react'),
new HtmlWebpackPlugin({
inject: true,
template: 'app/index.html'
}),
new webpack.PrefetchPlugin('react/lib/ReactComponentBrowserEnvironment'),
new ExtractTextPlugin('[name].css', {
allChunks: true
}),
new webpack.NoErrorsPlugin()
]
if (booleanFromEnv('WEBPACK_MINIMIZE_DEV', false)) {
plugins.push(
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false
}
}),
new webpack.optimize.DedupePlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})
)
}
if (booleanFromEnv('WEBPACK_HOT_COMPONENTS', false)) {
plugins.push(
new webpack.HotModuleReplacementPlugin()
)
}
module.exports = {
entry: booleanFromEnv('WEBPACK_HOT_COMPONENTS', false) ? [
'webpack-dev-server/client?http://0.0.0.0:3000', // WebpackDevServer host and port
'webpack/hot/dev-server',
'./app/mainApp'
] : './app/mainApp',
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
sourceMapFilename: '[name].map',
pathinfo: process.env.WEBPACK_PATH_INFO
},
module: {
loaders: loadersByExtension({
'jsx': booleanFromEnv('WEBPACK_HOT_COMPONENTS', false) ? ['react-hot', 'babel-loader?stage=0'] : 'babel-loader?stage=0',
'js': {
loader: 'babel-loader?stage=0',
include: path.join(__dirname, 'app')
},
'json': 'json-loader',
'json5': 'json5-loader',
'txt': 'raw-loader',
'png|jpg|jpeg|gif|svg': 'url-loader?limit=10000',
'woff|woff2': 'url-loader?limit=100000',
'ttf|eot': 'file-loader',
'wav|mp3': 'file-loader',
'html': 'html-loader',
'md|markdown': ['html-loader', 'markdown-loader'],
'css': ExtractTextPlugin.extract('style-loader', 'css-loader?module&importLoaders=1!postcss-loader'),
'less': ExtractTextPlugin.extract('style-loader', 'css-loader!less-loader')
}),
preloaders: loadersByExtension({
'js': 'source-map-loader'
})
},
postcss: [AutoprefixerCore],
devtool: process.env.WEBPACK_DEVTOOL,
debug: booleanFromEnv('WEBPACK_DEBUG', false),
resolve: {
root: path.join(__dirname, 'app'),
extensions: ['', '.web.js', '.js', '.jsx']
},
plugins: plugins,
devServer: {
port: 8080,
host: '0.0.0.0',
inline: true
}
}
config/loadersByExtensions.js
:
function extsToRegExp (exts) {
return new RegExp('\\.(' + exts.map(function (ext) {
return ext.replace(/\./g, '\\.')
}).join('|') + ')(\\?.*)?$')
}
module.exports = function loadersByExtension (obj) {
var loaders = []
Object.keys(obj).forEach(function (key) {
var exts = key.split('|')
var value = obj[key]
var entry = {
extensions: exts,
test: extsToRegExp(exts)
}
if (Array.isArray(value)) {
entry.loaders = value
} else if (typeof value === 'string') {
entry.loader = value
} else {
Object.keys(value).forEach(function (valueKey) {
entry[valueKey] = value[valueKey]
})
}
loaders.push(entry)
})
return loaders
}
config/booleanFromEnv.js
:
module.exports = function booleanFromEnv (key, defaultValue) {
return JSON.parse(process.env[key] || defaultValue)
}
package.json
:
{
"name": "react-starter",
"version": "0.0.0",
"description": "",
"main": "app/app.js",
"scripts": {
"lint": "standard",
"test-docker": "env WDIO_DOCKER=true wdio wdio.conf.js",
"test-saucelabs": "env WDIO_SAUCELABS=true wdio wdio.conf.js",
"dev-server": "env WEBPACK_DEV_SERVER=true WEBPACK_DEVTOOL=eval-source-map WEBPACK_DEBUG=true webpack-dev-server --colors --progress --hot",
"build": "env WEBPACK_DEV_MINIMIZE=true webpack --progress --profile --colors"
},
"license": "MIT",
"dependencies": {
"async": "^1.4.0",
"autoprefixer-core": "^5.2.1",
"babel-core": "^5.8.5",
"babel-eslint": "^4.0.0",
"babel-loader": "^5.3.2",
"baobab-react": "^0.1.1",
"bootstrap": "^3.3.5",
"bootswatch": "^3.3.5",
"chai": "^3.2.0",
"chai-as-promised": "^5.1.0",
"css-loader": "^0.15.5",
"extract-text-webpack-plugin": "^0.8.2",
"file-loader": "^0.8.4",
"html-loader": "^0.3.0",
"html-webpack-plugin": "^1.6.0",
"items-store": "^0.7.0",
"json-loader": "^0.5.2",
"less": "^2.5.1",
"less-loader": "^2.2.0",
"markdown-loader": "^0.1.3",
"mocha": "^2.2.5",
"mock-socket": "^0.5.4",
"postcss-loader": "^0.5.1",
"query-string": "^2.4.0",
"react": "^0.13.3",
"react-hot-loader": "^1.2.8",
"source-map-loader": "^0.1.5",
"standard": "^4.5.4",
"style-loader": "^0.12.3",
"superagent": "^1.2.0",
"url-loader": "^0.5.6",
"webdriverio": "^3.1.0",
"webpack": "^1.10.5",
"webpack-dev-server": "^1.10.1"
},
"standard": {
"parser": "babel-eslint",
"globals": [
"WebSocket",
"describe",
"it",
"location"
]
}
}
from mock-socket.
import { SocketIO, Server } from 'mock-socket';
Now works
from mock-socket.
Related Issues (20)
- Security vulnerability in url-parse dependency HOT 2
- Is this repo still maintained? HOT 5
- Using QueryParams in ws url doesn't work HOT 1
- How to mock Apollo client to test graphql subscription HOT 1
- Add `connectionDelay` option to Server to delay opening connection
- Mock ws inside web worker HOT 3
- Authorization Bypass Through User-Controlled Key in NPM url-parse prior to 1.5.8. HOT 2
- Delay execution of dispatched events HOT 5
- Enable and disable MockSocket at runtime HOT 2
- mock-socket configuration issue ? HOT 1
- Playwright example HOT 3
- [Typescript] Client.addEventListener and Server.addEventlistener has wrong typing HOT 1
- send should throw an error if readyState is CONNECTING HOT 1
- Bug in ws client mock - wrong close code when server not found HOT 1
- Export `CloseEvent` HOT 1
- Spec doesn't actually say that `send()` should throw when the socket is `CLOSING` or `CLOSED` HOT 2
- Calling `close()` in the `CONNECTING` state should not result in open callback being called
- Do you need to import the WebSocket in this example? HOT 4
- The socket-server stops after 1 message is emitted HOT 4
- Apparently, test.cb() has been deprecated by ava HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mock-socket.