@surma/rollup-plugin-off-main-thread
Advanced tools
Comparing version 1.1.1 to 1.2.0
33
index.js
@@ -33,5 +33,7 @@ /** | ||
// A function that determines whether the loader code should be prepended to a | ||
// certain chunk. Should return true if the load is suppsoed to be prepended. | ||
// certain chunk. Should return true if the load is supposed to be prepended. | ||
prependLoader: (chunk, workerFiles) => | ||
chunk.isEntry || workerFiles.includes(chunk.facadeModuleId) | ||
chunk.isEntry || workerFiles.includes(chunk.facadeModuleId), | ||
// The scheme used when importing workers as a URL. | ||
urlLoaderScheme: "omt" | ||
}; | ||
@@ -46,2 +48,3 @@ | ||
const suffix = `) + "${opts.marker}_end"`; | ||
const urlLoaderPrefix = opts.urlLoaderScheme + ":"; | ||
@@ -56,2 +59,21 @@ let workerFiles; | ||
async resolveId(id, importer) { | ||
if (!id.startsWith(urlLoaderPrefix)) return; | ||
const path = id.slice(urlLoaderPrefix.length); | ||
const newId = (await this.resolve(path, importer)).id; | ||
if (!newId) throw Error(`Cannot find module '${path}'`); | ||
return urlLoaderPrefix + newId; | ||
}, | ||
load(id) { | ||
if (!id.startsWith(urlLoaderPrefix)) return; | ||
const realId = id.slice(urlLoaderPrefix.length); | ||
const chunkRef = this.emitFile({ id: realId, type: "chunk" }); | ||
return `export default import.meta.ROLLUP_FILE_URL_${chunkRef};`; | ||
}, | ||
async transform(code, id) { | ||
@@ -87,3 +109,6 @@ // Copy the regexp as they are stateful and this hook is async. | ||
workerFiles.push(resolvedWorkerFile); | ||
const chunkRefId = this.emitChunk(resolvedWorkerFile); | ||
const chunkRefId = this.emitFile({ | ||
id: resolvedWorkerFile, | ||
type: "chunk" | ||
}); | ||
@@ -96,3 +121,3 @@ const workerFileStartIndex = match.index + "new Worker(".length; | ||
workerFileEndIndex, | ||
`import.meta.ROLLUP_CHUNK_URL_${chunkRefId}` | ||
`import.meta.ROLLUP_FILE_URL_${chunkRefId}` | ||
); | ||
@@ -99,0 +124,0 @@ } |
{ | ||
"name": "@surma/rollup-plugin-off-main-thread", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Use Rollup with workers and ES6 modules today.", | ||
@@ -23,3 +23,3 @@ "main": "index.js", | ||
"prettier": "1.18.2", | ||
"rollup": "1.16.2" | ||
"rollup": "1.31.1" | ||
}, | ||
@@ -26,0 +26,0 @@ "repository": { |
@@ -17,2 +17,6 @@ # rollup-plugin-off-main-thread | ||
I set up [a gist] to show a full setup with OMT. | ||
### Config | ||
```js | ||
@@ -33,4 +37,26 @@ // rollup.config.js | ||
I set up [a gist] to show a full setup with OMT. | ||
### Auto bundling | ||
In your project's code: | ||
```js | ||
const worker = new Worker("./worker.js"); | ||
``` | ||
This will just work. | ||
### Importing workers as URLs | ||
If your worker constructor doesn't match `workerRegexp` (see options below), you might find it easier to import the worker as a URL. In your project's code: | ||
```js | ||
import workerURL from "omt:./worker.js"; | ||
import paintWorkletURL from "omt:./paint-worklet.js"; | ||
const worker = new Worker(workerURL, { name: "main-worker" }); | ||
CSS.paintWorklet.addModule(paintWorkletURL); | ||
``` | ||
`./worker.js` and `./paint-worklet.js` will be added to the output as chunks. | ||
## Options | ||
@@ -50,2 +76,3 @@ | ||
- `prependLoader`: A function that determines whether the loader code should be prepended to a certain chunk. Should return true if the load is suppsoed to be prepended. | ||
- `urlLoaderScheme`: Scheme to use when importing workers as URLs. If `undefined`, OMT will use `"omt"`. | ||
@@ -52,0 +79,0 @@ [when workers]: https://dassur.ma/things/when-workers |
@@ -31,3 +31,5 @@ /** | ||
"./tests/fixtures/amd-function-name/entry.js", | ||
"./tests/fixtures/single-default/entry.js" | ||
"./tests/fixtures/single-default/entry.js", | ||
"./tests/fixtures/import-worker-url/entry.js", | ||
"./tests/fixtures/import-worker-url-custom-scheme/entry.js" | ||
].forEach(async input => { | ||
@@ -34,0 +36,0 @@ const pathName = path.dirname(input); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67093
65
1024
82