diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 710a0b5c..7fe8c3e6 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -972,7 +972,7 @@ impl<'a> Context<'a> { " function passStringToWasm(arg) {{ {} - const buf = cachedEncoder.encode(arg); + const buf = cachedTextEncoder.encode(arg); const ptr = wasm.__wbindgen_malloc(buf.length); getUint8Memory().set(buf, ptr); return [ptr, buf.length]; @@ -1065,58 +1065,32 @@ impl<'a> Context<'a> { if !self.exposed_globals.insert("text_encoder") { return; } - if self.config.nodejs_experimental_modules { - self.imports - .push_str("import { TextEncoder } from 'util';\n"); - } else if self.config.nodejs { - self.global( - " - const TextEncoder = require('util').TextEncoder; - ", - ); - } else if !(self.config.browser || self.config.no_modules) { - self.global( - " - const TextEncoder = typeof self === 'object' && self.TextEncoder - ? self.TextEncoder - : require('util').TextEncoder; - ", - ); - } - self.global( - " - let cachedEncoder = new TextEncoder('utf-8'); - ", - ); + self.expose_text_processor("TextEncoder"); } fn expose_text_decoder(&mut self) { if !self.exposed_globals.insert("text_decoder") { return; } + self.expose_text_processor("TextDecoder"); + } + + fn expose_text_processor(&mut self, s: &str) { if self.config.nodejs_experimental_modules { - self.imports - .push_str("import { TextDecoder } from 'util';\n"); + self.imports.push_str(&format!("import {{ {} }} from 'util';\n", s)); + self.global(&format!("let cached{0} = new {0}('utf-8');", s)); } else if self.config.nodejs { - self.global( - " - const TextDecoder = require('util').TextDecoder; - ", - ); + self.global(&format!("const {0} = require('util').{0};", s)); + self.global(&format!("let cached{0} = new {0}('utf-8');", s)); } else if !(self.config.browser || self.config.no_modules) { self.global( - " - const TextDecoder = typeof self === 'object' && self.TextDecoder - ? self.TextDecoder - : require('util').TextDecoder; - ", + &format!(" + const l{0} = typeof {0} === 'undefined' ? \ + require('util').{0} : {0};\ + ", s) ); + self.global(&format!("let cached{0} = new l{0}('utf-8');", s)); } - self.global( - " - let cachedDecoder = new TextDecoder('utf-8'); - ", - ); } fn expose_get_string_from_wasm(&mut self) { @@ -1148,7 +1122,7 @@ impl<'a> Context<'a> { self.global(&format!( " function getStringFromWasm(ptr, len) {{ - return cachedDecoder.decode(getUint8Memory().{}(ptr, ptr + len)); + return cachedTextDecoder.decode(getUint8Memory().{}(ptr, ptr + len)); }} ", method diff --git a/examples/add/package.json b/examples/add/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/add/package.json +++ b/examples/add/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/add/webpack.config.js b/examples/add/webpack.config.js index a5caf17f..53396f30 100644 --- a/examples/add/webpack.config.js +++ b/examples/add/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -8,7 +9,13 @@ module.exports = { filename: 'index.js', }, plugins: [ - new HtmlWebpackPlugin() + new HtmlWebpackPlugin(), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] + }) ], mode: 'development' }; diff --git a/examples/canvas/package.json b/examples/canvas/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/canvas/package.json +++ b/examples/canvas/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/canvas/src/lib.rs b/examples/canvas/src/lib.rs old mode 100755 new mode 100644 diff --git a/examples/canvas/webpack.config.js b/examples/canvas/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/canvas/webpack.config.js +++ b/examples/canvas/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/char/package.json b/examples/char/package.json index a0ead77a..806119cd 100644 --- a/examples/char/package.json +++ b/examples/char/package.json @@ -1,11 +1,13 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", - "webpack": "^4.8.3", + "webpack": "^4.11.1", "webpack-cli": "^3.1.1", - "webpack-dev-server": "^3.1.4" + "webpack-dev-server": "^3.1.0" } } diff --git a/examples/char/webpack.config.js b/examples/char/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/char/webpack.config.js +++ b/examples/char/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/closures/package.json b/examples/closures/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/closures/package.json +++ b/examples/closures/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/closures/src/lib.rs b/examples/closures/src/lib.rs old mode 100755 new mode 100644 diff --git a/examples/closures/webpack.config.js b/examples/closures/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/closures/webpack.config.js +++ b/examples/closures/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/console_log/package.json b/examples/console_log/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/console_log/package.json +++ b/examples/console_log/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/console_log/webpack.config.js b/examples/console_log/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/console_log/webpack.config.js +++ b/examples/console_log/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/dom/package.json b/examples/dom/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/dom/package.json +++ b/examples/dom/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/dom/webpack.config.js b/examples/dom/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/dom/webpack.config.js +++ b/examples/dom/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/duck-typed-interfaces/package.json b/examples/duck-typed-interfaces/package.json index 0657d4ba..806119cd 100644 --- a/examples/duck-typed-interfaces/package.json +++ b/examples/duck-typed-interfaces/package.json @@ -1,8 +1,11 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", + "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", "webpack-dev-server": "^3.1.0" diff --git a/examples/duck-typed-interfaces/src/lib.rs b/examples/duck-typed-interfaces/src/lib.rs old mode 100755 new mode 100644 diff --git a/examples/duck-typed-interfaces/webpack.config.js b/examples/duck-typed-interfaces/webpack.config.js index dce27149..53396f30 100644 --- a/examples/duck-typed-interfaces/webpack.config.js +++ b/examples/duck-typed-interfaces/webpack.config.js @@ -1,4 +1,6 @@ const path = require('path'); +const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -6,5 +8,14 @@ module.exports = { path: path.resolve(__dirname, 'dist'), filename: 'index.js', }, + plugins: [ + new HtmlWebpackPlugin(), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] + }) + ], mode: 'development' }; diff --git a/examples/fetch/package.json b/examples/fetch/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/fetch/package.json +++ b/examples/fetch/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/fetch/webpack.config.js b/examples/fetch/webpack.config.js index a5caf17f..53396f30 100644 --- a/examples/fetch/webpack.config.js +++ b/examples/fetch/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -8,7 +9,13 @@ module.exports = { filename: 'index.js', }, plugins: [ - new HtmlWebpackPlugin() + new HtmlWebpackPlugin(), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] + }) ], mode: 'development' }; diff --git a/examples/guide-supported-types-examples/package.json b/examples/guide-supported-types-examples/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/guide-supported-types-examples/package.json +++ b/examples/guide-supported-types-examples/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/guide-supported-types-examples/src/lib.rs b/examples/guide-supported-types-examples/src/lib.rs old mode 100755 new mode 100644 diff --git a/examples/guide-supported-types-examples/webpack.config.js b/examples/guide-supported-types-examples/webpack.config.js index a5caf17f..53396f30 100644 --- a/examples/guide-supported-types-examples/webpack.config.js +++ b/examples/guide-supported-types-examples/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -8,7 +9,13 @@ module.exports = { filename: 'index.js', }, plugins: [ - new HtmlWebpackPlugin() + new HtmlWebpackPlugin(), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] + }) ], mode: 'development' }; diff --git a/examples/hello_world/package.json b/examples/hello_world/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/hello_world/package.json +++ b/examples/hello_world/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/hello_world/webpack.config.js b/examples/hello_world/webpack.config.js index a5caf17f..53396f30 100644 --- a/examples/hello_world/webpack.config.js +++ b/examples/hello_world/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -8,7 +9,13 @@ module.exports = { filename: 'index.js', }, plugins: [ - new HtmlWebpackPlugin() + new HtmlWebpackPlugin(), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] + }) ], mode: 'development' }; diff --git a/examples/import_js/package.json b/examples/import_js/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/import_js/package.json +++ b/examples/import_js/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/import_js/webpack.config.js b/examples/import_js/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/import_js/webpack.config.js +++ b/examples/import_js/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/julia_set/package.json b/examples/julia_set/package.json index e3017d2c..806119cd 100644 --- a/examples/julia_set/package.json +++ b/examples/julia_set/package.json @@ -1,10 +1,12 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", - "webpack": "^4.0.1", + "webpack": "^4.11.1", "webpack-cli": "^3.1.1", "webpack-dev-server": "^3.1.0" } diff --git a/examples/julia_set/webpack.config.js b/examples/julia_set/webpack.config.js index 0d917993..f15dc6b5 100644 --- a/examples/julia_set/webpack.config.js +++ b/examples/julia_set/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,10 +11,13 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], - mode: 'development', - devServer: { - open: true - } + mode: 'development' }; diff --git a/examples/paint/package.json b/examples/paint/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/paint/package.json +++ b/examples/paint/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/paint/src/lib.rs b/examples/paint/src/lib.rs old mode 100755 new mode 100644 diff --git a/examples/paint/webpack.config.js b/examples/paint/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/paint/webpack.config.js +++ b/examples/paint/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/performance/package.json b/examples/performance/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/performance/package.json +++ b/examples/performance/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/performance/webpack.config.js b/examples/performance/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/performance/webpack.config.js +++ b/examples/performance/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/wasm-in-wasm/package.json b/examples/wasm-in-wasm/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/wasm-in-wasm/package.json +++ b/examples/wasm-in-wasm/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/wasm-in-wasm/webpack.config.js b/examples/wasm-in-wasm/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/wasm-in-wasm/webpack.config.js +++ b/examples/wasm-in-wasm/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/wasm2js/package.json b/examples/wasm2js/package.json index 5e7c8aa1..806119cd 100644 --- a/examples/wasm2js/package.json +++ b/examples/wasm2js/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/wasm2js/webpack.config.js b/examples/wasm2js/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/wasm2js/webpack.config.js +++ b/examples/wasm2js/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/webaudio/package.json b/examples/webaudio/package.json index b8bc0352..806119cd 100644 --- a/examples/webaudio/package.json +++ b/examples/webaudio/package.json @@ -1,10 +1,12 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", - "webpack": "^4.16.5", + "webpack": "^4.11.1", "webpack-cli": "^3.1.1", "webpack-dev-server": "^3.1.0" } diff --git a/examples/webaudio/webpack.config.js b/examples/webaudio/webpack.config.js index 453a1a59..f15dc6b5 100644 --- a/examples/webaudio/webpack.config.js +++ b/examples/webaudio/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/examples/webgl/.gitignore b/examples/webgl/.gitignore old mode 100755 new mode 100644 diff --git a/examples/webgl/Cargo.toml b/examples/webgl/Cargo.toml old mode 100755 new mode 100644 diff --git a/examples/webgl/README.md b/examples/webgl/README.md old mode 100755 new mode 100644 diff --git a/examples/webgl/index.html b/examples/webgl/index.html old mode 100755 new mode 100644 diff --git a/examples/webgl/index.js b/examples/webgl/index.js old mode 100755 new mode 100644 diff --git a/examples/webgl/package.json b/examples/webgl/package.json old mode 100755 new mode 100644 index 5e7c8aa1..806119cd --- a/examples/webgl/package.json +++ b/examples/webgl/package.json @@ -1,8 +1,10 @@ { "scripts": { + "build": "webpack", "serve": "webpack-dev-server" }, "devDependencies": { + "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", "webpack": "^4.11.1", "webpack-cli": "^3.1.1", diff --git a/examples/webgl/src/lib.rs b/examples/webgl/src/lib.rs old mode 100755 new mode 100644 diff --git a/examples/webgl/webpack.config.js b/examples/webgl/webpack.config.js old mode 100755 new mode 100644 index 453a1a59..f15dc6b5 --- a/examples/webgl/webpack.config.js +++ b/examples/webgl/webpack.config.js @@ -1,5 +1,6 @@ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); module.exports = { entry: './index.js', @@ -10,6 +11,12 @@ module.exports = { plugins: [ new HtmlWebpackPlugin({ template: "index.html" + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] }) ], mode: 'development' diff --git a/guide/lib.rs b/guide/lib.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/guide/src/SUMMARY.md b/guide/src/SUMMARY.md index 9abb834b..4ac12569 100644 --- a/guide/src/SUMMARY.md +++ b/guide/src/SUMMARY.md @@ -38,6 +38,7 @@ - [Command Line Interface](./reference/cli.md) - [Optimizing for Size](./reference/optimize-size.md) - [Supported Rust Targets](./reference/rust-targets.md) + - [Supported Browsers](./reference/browser-support.md) - [Supported Types](./reference/types.md) - [Imported JavaScript Types](./reference/types/imported-js-types.md) - [Exported Rust Types](./reference/types/exported-rust-types.md) diff --git a/guide/src/reference/browser-support.md b/guide/src/reference/browser-support.md new file mode 100644 index 00000000..e16e782e --- /dev/null +++ b/guide/src/reference/browser-support.md @@ -0,0 +1,61 @@ +# Supported Browsers + +The output of `wasm-bindgn` includes a JS file, and as a result it's good to +know what browsers that file is expected to be used in! By default the output +uses ES modules which isn't implemented in all browsers today, but when using a +bundler (like Webpack) you should be able to produce output suitable for all +browsers. + +Firefox, Chrome, Safari, and Edge browsers are all supported by +`wasm-bindgen`. If you find a problem in one of these browsers please [report +it] as we'd like to fix the bug! If you find a bug in another browser we would +also like to be aware of it! + +## Caveats + +* **IE 11** - `wasm-bindgen` by default requires support for + `WebAssembly`, but no version of IE currently supports `WebAssembly`. You can + support IE by [compiling wasm files to JS using `wasm2js`][w2js] (you can [see + an example of doing this too](../examples/wasm2js.html)). Note + that at this time no bundler will do this by default, but we'd love to + document plugins which do this if you are aware of one! + +* **Edge** - the `TextEncoder` and `TextDecoder` APIs are not currently + available in Edge which `wasm-bindgen` uses to encode/decode strings between + JS and Rust. You can polyfill this with at least one of two strategies: + + 1. If using a bundler, you can likely configure the bundler to polyfill these + types by default. For example if you're using Webpack you can use the + [`ProvidePlugin` interface][wpp] like so after also adding + [`text-encoding`] to your `package.json` + + ```js + const webpack = require('webpack'); + + module.exports = { + plugins: [ + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] + }) + ] + + // ... other configuration options + }; + ``` + + 2. If you're not using a bundler you can also include support manually by + adding a `