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

Commit

Permalink
feat: add publicPath support (options.publicPath) (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpage-godaddy authored and michael-ciniawsky committed Oct 19, 2017
1 parent a9ec386 commit 96c6144
Show file tree
Hide file tree
Showing 7 changed files with 64 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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ worker.addEventListener("message", function (event) {})
|**`name`**|`{String}`|`[hash].worker.js`|Set a custom name for the output script|
|**`inline`**|`{Boolean}`|`false`|Inline the worker as a BLOB|
|**`fallback`**|`{Boolean}`|`false`|Require a fallback for non-worker supporting environments|
|**`publicPath`**|`{String}`|`null`|Override the path from which worker scripts are downloaded|

### `name`

Expand Down Expand Up @@ -130,6 +131,18 @@ self.postMessage({ foo: 'foo' })
self.addEventListener('message', (event) => console.log(event))
```

### `publicPath`

Overrides the path from which worker scripts are downloaded. If not specified, the same public path used for other
webpack assets is used.

```js
{
loader: 'worker-loader'
options: { publicPath: '/scripts/workers/' }
}
```

### `Integrating with ES2015 Modules`

> ℹ️ You can even use ES2015 Modules if you have the [`babel-loader`](https://github.com/babel/babel-loader) configured.
Expand Down Expand Up @@ -187,6 +200,27 @@ worker.onmessage = (event) => {};
worker.addEventListener("message", (event) => {});
```

### Cross-origin policy workarounds

Web Workers are restricted by a same-origin policy, so if your webpack assets are not being served from the same origin
as your application, their download may be blocked by your browser. This scenario can commonly occur if you are hosting
your assets under a CDN domain. Even downloads from the webpack dev server could be blocked.

There are two workarounds. Firstly, you can inline the worker as a blob instead of downloading it as an external
script via the [`inline`](#inline) parameter:

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

Secondly, you may override the base download URL for your worker script via the [`publicPath`](#publicpath) option.

```javascript
// This will cause the worker to be downloaded from `/workers/file.js`
var MyWorker = require("worker?publicPath=%2Fworkers%2F!./file.js");
```


<h2 align="center">Maintainers</h2>

<table>
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 96c6144

Please sign in to comment.