Skip to content

Use simple import.meta.url for worker creation #23889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/lib/libpthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,7 @@ var LibraryPThread = {
worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}});
} else
#endif
// We need to generate the URL with import.meta.url as the base URL of the JS file
// instead of just using new URL(import.meta.url) because bundler's only recognize
// the first case in their bundling step. The latter ends up producing an invalid
// URL to import from the server (e.g., for webpack the file:// path).
// See https://github.com/webpack/webpack/issues/12638
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions }}});
worker = new Worker(import.meta.url, {{{ pthreadWorkerOptions }}});
#else // EXPORT_ES6
var pthreadMainJs = _scriptName;
#if expectToReceiveOnModule('mainScriptUrlOrBlob')
Expand Down
35 changes: 29 additions & 6 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5536,20 +5536,33 @@ def test_error_reporting(self):

@also_with_threads
@parameterized({
'': (False,),
'es6': (True,),
})
def test_webpack(self, es6):
if es6:
'': (False,False,False),
# 'blob': (False,True,True),
# 'url': (False,True,False),
'es6': (True,False,False),
# 'es6_url': (True,True,True),
# 'es6_blob': (True,True,False),
})
def test_webpack(self, es6, main_script_url_or_blob, use_blob):
if es6 and main_script_url_or_blob:
copytree(test_file('webpack_es6_with_%s' % ('blob' if use_blob else 'url')), '.')
self.emcc_args += ['-sEXPORT_ES6', '-pthread', '-sPTHREAD_POOL_SIZE=1']
outfile = 'dist/hello.mjs'
elif es6:
copytree(test_file('webpack_es6'), '.')
self.emcc_args += ['-sEXPORT_ES6', '-pthread', '-sPTHREAD_POOL_SIZE=1']
outfile = 'src/hello.mjs'
elif main_script_url_or_blob:
copytree(test_file('webpack_with_%s' % ('blob' if use_blob else 'url')), '.')
self.emcc_args += ['-pthread', '-sPTHREAD_POOL_SIZE=1']
outfile = 'dist/hello.js'
else:
copytree(test_file('webpack'), '.')
outfile = 'src/hello.js'
self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-o', outfile])
self.run_process(shared.get_npm_cmd('webpack') + ['--mode=development', '--no-devtool'])
shutil.copy('src/hello.wasm', 'dist/')
if not main_script_url_or_blob:
shutil.copy('src/hello.wasm', 'dist/')
self.run_browser('dist/index.html', '/report_result?exit:0')

@also_with_threads
Expand All @@ -5559,6 +5572,16 @@ def test_vite(self):
self.run_process(shared.get_npm_cmd('vite') + ['build'])
self.run_browser('dist/index.html', '/report_result?exit:0')

@parameterized({
'':('vite_with_blob',),
'url':('vite_with_blob_url',),
})
def test_vite_with_blob(self, package):
copytree(test_file(package), '.')
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-sPTHREAD_POOL_SIZE=1', '-pthread', '-sPROXY_TO_PTHREAD', '-o', 'public/hello.mjs'])
self.run_process(shared.get_npm_cmd('vite') + ['build'])
self.run_browser('dist/index.html', '/report_result?exit:0')

@also_with_threads
def test_rollup(self):
copytree(test_file('rollup'), '.')
Expand Down
6 changes: 6 additions & 0 deletions test/vite_with_blob/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<hr><div id='output'></div><hr>
<script type="module" src="./index.mjs"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions test/vite_with_blob/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fetch('hello.mjs').then((resp) => resp.blob().then((blob) => {
var params_blob = {
print: (function() {
var element = document.getElementById('output');
return function(text) {
console.log(text);
element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>';
};
})(),
locateFile: function(path, _prefix) {
return path;
},
canvas: document.getElementById('canvas'),
mainScriptUrlOrBlob: blob
};

params_blob.print('testing...');

import(/* @vite-ignore */ URL.createObjectURL(blob)).then((factory) =>
factory.default(params_blob).then((instance) => {
console.log('loaded by blob');
})
);
}));
3 changes: 3 additions & 0 deletions test/vite_with_blob/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
base: './'
}
6 changes: 6 additions & 0 deletions test/vite_with_blob_url/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<hr><div id='output'></div><hr>
<script type="module" src="./index.mjs"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions test/vite_with_blob_url/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var params_url = {
print: (function() {
var element = document.getElementById('output');
return function(text) {
console.log(text);
element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>';
};
})(),
locateFile: function(path, _prefix) {
console.log("path",_prefix,path);
return path;
},
canvas: document.getElementById('canvas'),
mainScriptUrlOrBlob: 'hello.mjs'
};

params_url.print('testing...');

import(/* @vite-ignore */ './hello.mjs').then((factory) =>
factory.default(params_url).then((instance) => {
console.log('loaded by url');
})
);
6 changes: 6 additions & 0 deletions test/vite_with_blob_url/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
base: './',
build:{
rollupOptions:{external:['./hello.mjs']}
}
}
6 changes: 6 additions & 0 deletions test/webpack_es6_with_blob/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<hr><div id='output'></div><hr>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions test/webpack_es6_with_blob/src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fetch('hello.mjs').then((resp) => resp.blob().then((blob) => {
var params_blob = {
print: (function() {
var element = document.getElementById('output');
return function(text) {
console.log(text);
element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>';
};
})(),
locateFile: function(path, _prefix) {
return path;
},
canvas: document.getElementById('canvas'),
mainScriptUrlOrBlob: blob
};

params_blob.print('testing...');
var url = URL.createObjectURL(blob);
import(/* webpackIgnore: true */ url).then((factory) => {
factory.default(params_blob).then((instance) => {
console.log('loaded by blob');
});
});
}));
3 changes: 3 additions & 0 deletions test/webpack_es6_with_blob/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: "./src/index.mjs",
}
6 changes: 6 additions & 0 deletions test/webpack_es6_with_url/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<hr><div id='output'></div><hr>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions test/webpack_es6_with_url/src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var params_url = {
print: (function() {
var element = document.getElementById('output');
return function(text) {
console.log(text);
element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>';
};
})(),
locateFile: function(path, prefix) {
return path;
},
canvas: document.getElementById('canvas'),
mainScriptUrlOrBlob: 'hello.mjs'
};

params_url.print('testing...');
import(/* webpackIgnore: true */ 'hello.mjs').then((factory) => {
factory.default(params_url).then((instance) => {
console.log('loaded by url');
});
});
3 changes: 3 additions & 0 deletions test/webpack_es6_with_url/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: "./src/index.mjs",
}
6 changes: 6 additions & 0 deletions test/webpack_with_blob/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<hr><div id='output'></div><hr>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions test/webpack_with_blob/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
fetch('hello.js').then((resp) => resp.blob().then((blob) => {
var params_blob = {
print: (function() {
var element = document.getElementById('output');
return function(text) {
console.log(text);
element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>';
};
})(),
locateFile: function(path, _prefix) {
return path;
},
canvas: document.getElementById('canvas'),
mainScriptUrlOrBlob: blob
};

params_blob.print("testing..");

var script = document.createElement("script");
script.src = URL.createObjectURL(blob);
script.onload = () => {
Module(params_blob).then((instance) => { console.log("loaded") })
};
document.body.appendChild(script);
}))
3 changes: 3 additions & 0 deletions test/webpack_with_blob/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: "./src/index.js",
}
6 changes: 6 additions & 0 deletions test/webpack_with_url/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<hr><div id='output'></div><hr>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions test/webpack_with_url/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var params_url = {
print: (function() {
var element = document.getElementById('output');
return function(text) {
console.log(text);
element.innerHTML += text.replace('\n', '<br>', 'g') + '<br>';
};
})(),
locateFile: function(path, _prefix) {
return path;
},
canvas: document.getElementById('canvas'),
mainScriptUrlOrBlob: 'hello.js'
};

params_url.print("testing..");

var script = document.createElement("script");
script.src = params_url['mainScriptUrlOrBlob'];
script.onload = () => {
Module(params_url).then((instance) => { console.log("loaded") })
};
document.body.appendChild(script);
3 changes: 3 additions & 0 deletions test/webpack_with_url/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
entry: "./src/index.js",
}