Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
publicPath option
Browse files Browse the repository at this point in the history
Added the ability to use a different public path when loading workers as a way to work around cross-origin policy issues.
  • Loading branch information
jpage-godaddy committed Oct 16, 2017
1 parent a9ec386 commit 3a481c3
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
test/expected
.idea
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ self.postMessage({ foo: 'foo' })
self.addEventListener('message', (event) => console.log(event))
```


## Cross-origin policy workarounds

Note that cross-origin policies for these worker objects still apply, so if your webpack assets are not hosted in the
same origin as your application (including if you're loading them through the webpack dev server), they may be blocked
by your browser. There are two workarounds. Firstly, you can inline the worker as a blob with the `inline` parameter:

``` javascript
var MyWorker = require("worker?inline!./file.js");
```

Secondly, you may proxy the script request through your application by overriding the public path for worker files
via the `publicPath` option (make sure to URL-encode the path if specifying through a query string):

```javascript
var MyWorker = require("worker?publicPath=%2Fworkers%2F!./file.js");
```


### `Integrating with TypeScript`

To integrate with TypeScript, you will need to define a custom module for the exports of your worker
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const schema = require('./options.json');

const getWorker = (file, content, options) => {
const workerPublicPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;
const publicPath = options.publicPath ? JSON.stringify(options.publicPath) : '__webpack_public_path__';
const workerPublicPath = `${publicPath} + ${JSON.stringify(file)}`;
if (options.inline) {
const createInlineWorkerPath = JSON.stringify(`!!${path.join(__dirname, 'createInlineWorker.js')}`);
const fallbackWorkerPath = options.fallback === false ? 'null' : workerPublicPath;
Expand Down
3 changes: 3 additions & 0 deletions options.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
},
"fallback": {
"type": "boolean"
},
"publicPath": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/public-path-override/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const Worker = require('./worker.js');
1 change: 1 addition & 0 deletions test/fixtures/public-path-override/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// named worker test mark
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,26 @@ describe('worker-loader', () => {
})
);
});

it('should use the publicPath option as the base URL if specified', () =>
makeBundle('public-path-override', {
module: {
rules: [
{
test: /worker\.js$/,
loader: '../index.js',
options: {
publicPath: '/some/proxy/',
},
},
],
},
}).then((stats) => {
const assets = stats.compilation.assets;
const bundle = assets['bundle.js'];
const workerFile = Object.keys(assets).find(name => /worker\.js$/.test(name));

assert.notEqual(bundle._cachedSource.indexOf(`new Worker("/some/proxy/" + "${workerFile}")`), -1);
})
);
});

0 comments on commit 3a481c3

Please sign in to comment.