webpack-contrib / worker-loader

A webpack loader that registers a script as a Web Worker
MIT License
1.46k stars 273 forks source link

loader.option.publicPath do not work ? #323

Closed huyansheng3 closed 3 years ago

huyansheng3 commented 3 years ago

when i run the test case, .chunk.js use the loader.option.publicPath, but the .worker.js do not use the loader.option.publicPath

it('should work and respect the "output.publicPath" option default value', async () => {
    const nanoid = customAlphabet("1234567890abcdef", 10);
    const compiler = getCompiler(
      "./chunks/entry.js",
      {
        worker: "SharedWorker",
        filename: "[name].[contenthash].worker.js",
        chunkFilename: "[id].[contenthash].worker.js",
        publicPath: "/public-path-static123/",
      },
      {
        output: {
          path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`),
          filename: "[name].bundle.js",
          chunkFilename: "[name].chunk.js",
          publicPath: "/public-path-static456/",
        },
      }
    );
    const stats = await compile(compiler);
    const result = await getResultFromBrowser(stats);

    expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot(
      "module"
    );
    expect(result).toMatchSnapshot("result");
    expect(getWarnings(stats)).toMatchSnapshot("warnings");
    expect(getErrors(stats)).toMatchSnapshot("errors");
  });

image

image

http://localhost:5000/public-path-static456/worker.9ce1180cb43e7000b1a2.worker.js should be /public-path-static123/worker.9ce1180cb43e7000b1a2.worker.js ?

huyansheng3 commented 3 years ago

rewritePublicPath.js

 __webpack_public_path__ = '/';

import './rewritePublicPath.js'

// import UpgradeSharedWorker from './upgrade-notify.sharedworker.js';

https://github.com/webpack-contrib/worker-loader/issues/281 this can fix the problem, maybe is not a good solution.

alexander-akait commented 3 years ago

What is webpack version?

huyansheng3 commented 3 years ago

wepack 4.x or 5.x has the same problem .

I just modify the project test case; then see the /outputs content @alexander-akait

alexander-akait commented 3 years ago

you don't need this loader for webpack v5

alexander-akait commented 3 years ago

We describe it in our readme https://github.com/webpack-contrib/worker-loader#readme

huyansheng3 commented 3 years ago

oader.option.publicPath only add to the webpack output.publicPath, can only set the worker.js full public path

mmazzarolo commented 3 years ago

@alexander-akait just a note: AFAIK Webpack 5 implementation of the loading mechanism doesn't support setting publicPath specific only to web-workers. It will always use whatever is set in output.publicPath (which might not be the same path you need for the worker).