如何在npm包中通过filepath引用资产? (申请:webworker)

时间:2017-11-01 23:17:48

标签: javascript node.js webpack web-worker

我编写并发布了一个vanilla JS节点包,包含以下package.json:

{
  "name": "...",
  "description": "...",
  "version": "1.0.0",
  "types": "./index.d.ts",
  "main": "./dist/bundle.js",
...
}

我也在使用webpack,其中包含以下webpack.config.js

const path = require('path');
const WebpackShellPlugin = require('webpack-shell-plugin');

module.exports = {
    entry: './src/js/main.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js',
        library: '',
        libraryTarget: 'commonjs-module'
    },
    plugins: [
        new WebpackShellPlugin({onBuildStart:['rm -r dist', 'mkdir dist']})
    ]
};

最后,我正在以下列方式在我的库中使用worker-loader:

var MyWorker = require('worker-loader!./workers/worker.js');
...
var worker = new MyWorker();

当我运行webpack时,它会毫无问题地进行编译,并将bundle.jsc6...[hash code].worker.js放在dist/中。所以我就这样发布了包。

现在,当我尝试将库包含在一个带有import MyClass from 'packagename'的不同的typescript / babel / es6项目中时,一切运行正常,直到我们到达库中的行:

var worker = new MyWorker();

此时浏览器会发出404错误,无法找到localhost:8080/c6...[hash code].worker.js

有谁知道如何处理这个问题?我已确认文件c6...[hash code].worker.js位于node_modules/packagename/dist

0 个答案:

没有答案