Skip to content

Commit

Permalink
fix: allow bundle to be used in web workers (#408)
Browse files Browse the repository at this point in the history
`globalObject` is `"window"` by default which is not available in web workers. Using `"self"` allows the bundle to work in both environments.

resolves ipfs/js-ipfs#2349

Co-authored-by: David Dias <[email protected]>
Co-authored-by: achingbrain <[email protected]>
  • Loading branch information
3 people authored Oct 26, 2020
1 parent 156a5c1 commit 3f72e50
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/config/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
],
Expand Down
1 change: 1 addition & 0 deletions src/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
7 changes: 7 additions & 0 deletions test/browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const loadFixture = require('../fixtures')
const expect = require('chai').expect
const globalThis = require('ipfs-utils/src/globalthis')

describe('browser', () => {
it('fixtures', () => {
Expand All @@ -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)
})
})
4 changes: 4 additions & 0 deletions test/fixtures/tests/context-access.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict'

// export something from the global scope
module.exports = Uint8Array

0 comments on commit 3f72e50

Please sign in to comment.