@surma/rollup-plugin-off-main-thread
Advanced tools
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 @@ /** |
@@ -0,0 +0,0 @@ /** |
{ | ||
"amdFunctionName": "foo" | ||
} |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ const MARKER = "my-special-import"; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ let worker; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
{ | ||
"urlLoaderScheme": "fancy-custom-scheme" | ||
} |
@@ -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 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ module.exports = (config, outputOptions, omt) => { |
@@ -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 @@ /** |
{ | ||
"publicPath": "/base/tests/fixtures/public-path/build" | ||
} |
@@ -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 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /** |
@@ -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 @@ /** |
@@ -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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
92625
3
1542
2
+ Addedansi-styles@4.3.0(transitive)
+ Addedasync@3.2.5(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.112.0.1(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedejs@3.1.10(transitive)
+ Addedfilelist@1.0.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedjake@10.9.1(transitive)
+ Addedminimatch@3.1.25.1.6(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removedtippex@^3.0.0
- Removedejs@2.7.4(transitive)
- Removedtippex@3.0.0(transitive)
Updatedejs@^3.1.6