From 3f72e5074145a8f2ec03143db4230514af664f95 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 26 Oct 2020 14:38:14 +0000 Subject: [PATCH] fix: allow bundle to be used in web workers (#408) `globalObject` is `"window"` by default which is not available in web workers. Using `"self"` allows the bundle to work in both environments. resolves https://github.com/ipfs/js-ipfs/issues/2349 Co-authored-by: David Dias Co-authored-by: achingbrain --- src/config/karma.conf.js | 3 --- src/config/webpack.config.js | 1 + test/browser.spec.js | 7 +++++++ test/fixtures/tests/context-access.js | 4 ++++ 4 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/tests/context-access.js diff --git a/src/config/karma.conf.js b/src/config/karma.conf.js index ada58d6dd..f65697460 100644 --- a/src/config/karma.conf.js +++ b/src/config/karma.conf.js @@ -21,9 +21,6 @@ const env = { // Webpack overrides for karma const karmaWebpackConfig = merge.strategy({ plugins: 'replace' })(webpackConfig(), { entry: '', - output: { - libraryTarget: 'var' - }, plugins: [ new webpack.DefinePlugin(env) ], diff --git a/src/config/webpack.config.js b/src/config/webpack.config.js index e24002b78..c3836ad58 100644 --- a/src/config/webpack.config.js +++ b/src/config/webpack.config.js @@ -29,6 +29,7 @@ const base = (env, argv) => { sourceMapFilename: filename + '.map', library: getLibraryName(pkg.name), libraryTarget: 'umd', + globalObject: 'self', // Use `self` as `window` doesn't not exist within a Service/Web Worker context devtoolModuleFilenameTemplate: info => 'file:' + encodeURI(info.absoluteResourcePath) }, module: { diff --git a/test/browser.spec.js b/test/browser.spec.js index 7ab2cc8ae..d8b77c6e5 100644 --- a/test/browser.spec.js +++ b/test/browser.spec.js @@ -3,6 +3,7 @@ const loadFixture = require('../fixtures') const expect = require('chai').expect +const globalThis = require('ipfs-utils/src/globalthis') describe('browser', () => { it('fixtures', () => { @@ -14,4 +15,10 @@ describe('browser', () => { expect(() => loadFixture('/test/fixtures/asdalkdjaskldjatest.txt')) .to.throw() }) + + it('can access context object', () => { + const context = require('./fixtures/tests/context-access') + + expect(context).to.equal(globalThis.Uint8Array) + }) }) diff --git a/test/fixtures/tests/context-access.js b/test/fixtures/tests/context-access.js new file mode 100644 index 000000000..eb037b54a --- /dev/null +++ b/test/fixtures/tests/context-access.js @@ -0,0 +1,4 @@ +'use strict' + +// export something from the global scope +module.exports = Uint8Array