Socket
Socket
Sign inDemoInstall

@surma/rollup-plugin-off-main-thread

Package Overview
Dependencies
17
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.0 to 2.2.0

137

index.js

@@ -14,2 +14,4 @@ /**

"use strict";
const { readFileSync } = require("fs");

@@ -19,3 +21,2 @@ const { join } = require("path");

const MagicString = require("magic-string");
const tippex = require("tippex");
const json5 = require("json5");

@@ -103,8 +104,4 @@

// Tippex is performing regex matching under the hood, but automatically ignores comments
// and string contents so it's more reliable on JS syntax.
tippex.match(
code,
workerRegexpForTransform,
(
for (const match of code.matchAll(workerRegexpForTransform)) {
let [
fullMatch,

@@ -115,22 +112,21 @@ partBeforeArgs,

workerFile,
) => {
// We need to get this before the `await`, otherwise `lastIndex`
// will be already overridden.
const workerParametersEndIndex = workerRegexpForTransform.lastIndex;
const matchIndex = workerParametersEndIndex - fullMatch.length;
const workerParametersStartIndex = matchIndex + partBeforeArgs.length;
] = match;
let workerIdPromise;
if (workerSource === "import.meta.url") {
// Turn the current file into a chunk
workerIdPromise = Promise.resolve(id);
} else {
// Otherwise it's a string literal either directly or in the `new URL(...)`.
if (directWorkerFile) {
const fullMatchWithOpts = `${fullMatch}, …)`;
const fullReplacement = `new Worker(new URL(${directWorkerFile}, import.meta.url), …)`;
const workerParametersEndIndex = match.index + fullMatch.length;
const matchIndex = match.index;
const workerParametersStartIndex = matchIndex + partBeforeArgs.length;
if (!longWarningAlreadyShown) {
this.warn(
`rollup-plugin-off-main-thread:
let workerIdPromise;
if (workerSource === "import.meta.url") {
// Turn the current file into a chunk
workerIdPromise = Promise.resolve(id);
} else {
// Otherwise it's a string literal either directly or in the `new URL(...)`.
if (directWorkerFile) {
const fullMatchWithOpts = `${fullMatch}, …)`;
const fullReplacement = `new Worker(new URL(${directWorkerFile}, import.meta.url), …)`;
if (!longWarningAlreadyShown) {
this.warn(
`rollup-plugin-off-main-thread:
\`${fullMatchWithOpts}\` suggests that the Worker should be relative to the document, not the script.

@@ -142,49 +138,46 @@ In the bundler, we don't know what the final document's URL will be, and instead assume it's a URL relative to the current module.

This will become a hard error in the future.`,
matchIndex
);
longWarningAlreadyShown = true;
} else {
this.warn(
`rollup-plugin-off-main-thread: Treating \`${fullMatchWithOpts}\` as \`${fullReplacement}\``,
matchIndex
);
}
workerFile = directWorkerFile;
}
// Cut off surrounding quotes.
workerFile = workerFile.slice(1, -1);
if (!/^\.{1,2}\//.test(workerFile)) {
matchIndex
);
longWarningAlreadyShown = true;
} else {
this.warn(
`Paths passed to the Worker constructor must be relative to the current file, i.e. start with ./ or ../ (just like dynamic import!). Ignoring "${workerFile}".`,
`rollup-plugin-off-main-thread: Treating \`${fullMatchWithOpts}\` as \`${fullReplacement}\``,
matchIndex
);
return;
}
workerFile = directWorkerFile;
}
workerIdPromise = this.resolve(workerFile, id).then(res => res.id);
// Cut off surrounding quotes.
workerFile = workerFile.slice(1, -1);
if (!/^\.{1,2}\//.test(workerFile)) {
this.warn(
`Paths passed to the Worker constructor must be relative to the current file, i.e. start with ./ or ../ (just like dynamic import!). Ignoring "${workerFile}".`,
matchIndex
);
continue;
}
// tippex.match accepts only sync callback, but we want to perform &
// wait for async job here, so we track those promises separately.
replacementPromises.push(
(async () => {
const resolvedWorkerFile = await workerIdPromise;
workerFiles.push(resolvedWorkerFile);
const chunkRefId = this.emitFile({
id: resolvedWorkerFile,
type: "chunk"
});
ms.overwrite(
workerParametersStartIndex,
workerParametersEndIndex,
`new URL(import.meta.ROLLUP_FILE_URL_${chunkRefId}, import.meta.url)`
);
})()
);
workerIdPromise = this.resolve(workerFile, id).then(res => res.id);
}
);
replacementPromises.push(
(async () => {
const resolvedWorkerFile = await workerIdPromise;
workerFiles.push(resolvedWorkerFile);
const chunkRefId = this.emitFile({
id: resolvedWorkerFile,
type: "chunk"
});
ms.overwrite(
workerParametersStartIndex,
workerParametersEndIndex,
`new URL(import.meta.ROLLUP_FILE_URL_${chunkRefId}, import.meta.url)`
);
})()
);
}
// No matches found.

@@ -259,3 +252,4 @@ if (!replacementPromises.length) {

tippex.match(code, workerRegexpForOutput, (fullMatch, optionsWithCommaStr, optionsStr) => {
for (const match of code.matchAll(workerRegexpForOutput)) {
let [fullMatch, optionsWithCommaStr, optionsStr] = match;
let options;

@@ -269,10 +263,10 @@ try {

console.warn("Couldn't match options object", fullMatch, ": ", e);
return;
continue;
}
if (!("type" in options)) {
// Nothing to do.
return;
continue;
}
delete options.type;
const replacementEnd = workerRegexpForOutput.lastIndex;
const replacementEnd = match.index + fullMatch.length;
const replacementStart = replacementEnd - optionsWithCommaStr.length;

@@ -286,3 +280,3 @@ optionsStr = json5.stringify(options);

);
});
}

@@ -304,5 +298,8 @@ // Mangle define() call

const newCode = ms.toString();
const hasCodeChanged = code !== newCode;
return {
code: ms.toString(),
map: ms.generateMap({ hires: true })
code: newCode,
// Avoid generating sourcemaps if possible as it can be a very expensive operation
map: hasCodeChanged ? ms.generateMap({ hires: true }) : null
};

@@ -309,0 +306,0 @@ }

@@ -0,0 +0,0 @@ /**

{
"name": "@surma/rollup-plugin-off-main-thread",
"version": "2.1.0",
"version": "2.2.0",
"description": "Use Rollup with workers and ES6 modules today.",

@@ -31,7 +31,6 @@ "main": "index.js",

"dependencies": {
"ejs": "^2.6.1",
"ejs": "^3.1.6",
"json5": "^2.2.0",
"magic-string": "^0.25.0",
"tippex": "^3.0.0"
"magic-string": "^0.25.0"
}
}

@@ -0,0 +0,0 @@ # rollup-plugin-off-main-thread

{
"extends": ["config:base"]
}

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ const MARKER = "my-special-import";

@@ -0,0 +0,0 @@ let worker;

{
"urlLoaderScheme": "fancy-custom-scheme"
}

@@ -0,0 +0,0 @@ module.exports = (config, outputOptions, omt) => {

{
"publicPath": "/base/tests/fixtures/public-path/build"
}

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -14,8 +14,2 @@ /**

/*
* This is commented out and should be ignored by the plugin.
* If it doesn't, Rollup and test will fail.
* new Worker("non-existing-worker", { type: "module" });
*/
// The type module should get removed for AMD format!

@@ -22,0 +16,0 @@ const w = new Worker("./worker.js", { type: "module" });

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc