webpack - 带有 Webpack 4 的 Chrome 扩展生成错误的输出

标签 webpack google-chrome-extension

我的 webpack 配置..

module.exports = {
    entry: {
        background: ['babel-polyfill', resolve('src', 'background.js')],
    },
    output: {
        path: resolve('dist'),
        filename: '[name].js',
        publicPath: './',
    },
    plugins: [
        new webpack.optimize.ModuleConcatenationPlugin(),
    ],
    resolve: {extensions: ['.js', '.jsx']},
    module: {
        rules: [{
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            include: ['src'],
            use: {
                loader: 'babel-loader',
                query: {cacheDirectory: true},
            },
        }],
    },
};

我的背景文件...
console.log('Background ready!'); // Exepect to print this!!
chrome.runtime.onInstalled.addListener(() => {
  chrome.storage.sync.set({ color: '#3aa757' }, () => {
    console.log('The color is green.');
  });

  chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
    chrome.declarativeContent.onPageChanged.addRules([{
      conditions: [new chrome.declarativeContent.PageStateMatcher({
        pageUrl: { hostEquals: 'developer.chrome.com' },
      }),
      ],
      actions: [new chrome.declarativeContent.ShowPageAction()], 
    }]);
  });
});

我的 list ..
{
  "name": "..",
  "version": "1.0",
  "description": "..",
  "manifest_version": 2,
  "permissions": [
    "activeTab",
    "declarativeContent",
    "storage"
  ],
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}

问题...

当我加载我的扩展时..什么都没有执行...

enter image description here

这是我由 Webpack 生成的 background.js ......

enter image description here

最佳答案

我发现对我来说,webpack 4 在一个单独的包中创建了它的运行时。所以我改变了

optimization : {
  runtimeChunk : true,
},

在 webpack 配置中,要:
optimization : {
  runtimeChunk : false,
},

这导致 webpack 运行时成为输出包的一部分并由 chrome 执行。

关于webpack - 带有 Webpack 4 的 Chrome 扩展生成错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49709385/

相关文章:

javascript - Webpack 没有使用 react-toolbox 加载 SCSS

javascript - 没有 U2F Chrome 扩展的 U2F 支持

webpack - 工兵 webpack 警告 : 'mode' option has not been set

google-chrome-extension - Chrome 扩展程序 - 设置页面上的扩展程序图像

javascript - 如何更改地址栏的url

google-chrome - 在后台打开标签并从中获取数据 - Chrome 扩展

javascript - Chrome 扩展程序 : sendResponse returning empty object

javascript - 当文件名通过 props 传递时,React 组件无法动态 require() 图像文件

node.js - gulp 未在 Docker ubuntu 上运行

angular - 有什么特别的吗?类型 'Node' 无法转换为类型 'ChildNode'