Proper way to run an importScripts plugin #3302
Unanswered
CraigglesO
asked this question in
Troubleshooting
Replies: 1 comment 1 reply
-
Just to add to this discussion, It seems like I can simplify the plugin a little by doing THIS for my workers: import aWorker from './workers/a.worker.js'
....
const aWorker = new Worker(URL.createObjectURL(new Blob([`importScripts('${aWorker}')`], { type: 'text/javascript' })), { name: 'a-worker' }) and my worker plugin currently looks like this: module.exports = function (config, options) {
return {
name: 'worker-plugin',
resolve: {
input: ['.js'],
output: ['.js'],
},
async load ({ filePath }) {
if (filePath.includes('.worker.js')) {
const wjs = await fs.readFile(filePath, 'utf-8')
const js = 'export default \'https://greggman.github.io/doodles/test/ping-worker.js\''
return {
'.js': js
}
}
}
}
}
Then the only thing I have to worry about is pre building the worker with Webpack and sending out a string that is the location of the pre-made worker. basically, if you can offer advice on how to pre-build the worker I think I'll be set. I don't think it will be to hard to figure out during the build phase, but sending out the appropriate location for the actual worker file during dev is a bit tricky. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
// the js with the workers look like this:
I'm building an app where you add the app using a <script> tag in your html, therefore the workers won't build because they will be requested at a different origin than the source. So I am trying to build an importScripts method.
My questions are as follows:
Beta Was this translation helpful? Give feedback.
All reactions