我有一个 chrome 扩展,其中发布了不稳定、稳定和夜间版本,我需要能够动态设置扩展资源的公共路径。我一直在尝试以多种方式添加它,通过将 js 条目直接添加到 webpack.config.js 中,从我的 ts 条目中导入,直接放入 ts 文件中。
我只想将 __webpack_public_path__ 全局更改为我不能指望在构建之间始终保持相同的东西。
我试过这样:
__webpack_public_path__ = chrome.extension.getURL("");
var __webpack_public_path__ = chrome.extension.getURL("");
let __webpack_public_path__ = chrome.extension.getURL("");
window.__webpack_public_path__ = chrome.extension.getURL("");
有谁知道自 Webpack 2.x 以来这是否发生了变化?
更新!
这是我的 webpack.config.development.js
var path = require('path');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var WebpackNotifierPlugin = require('webpack-notifier');
var RawLoader = require('raw-loader');
var CssLoader = require('css-loader');
var TextLoader = require('text-loader');
var VendorChunkPlugin = require('webpack-vendor-chunk-plugin');
var webpack = require('webpack');
var ngtools = require('@ngtools/webpack');
var exec = require('child_process').exec;
var webpack_devtool = "source-map";
var webpack_name = "development";
var webpack_build_title = "Webpack Development Build";
var webpack_app_dir = "app";
var webpack_build_dir = "build";
var webpack_public_path = ""
module.exports = [
{
name: webpack_name,
devtool: webpack_devtool,
context: path.join(__dirname, webpack_app_dir),
entry: {
"webpack-setups": './webpack-setups.js',
"fa": "font-awesome-webpack2!./font-awesome.config.js",
"content_script": './hp.my.content_script.com_console.ts',
"content_script2": './hp.my.content_script2.ts',
"popup": './popup.ts',
},
output: {
path: path.join(__dirname, webpack_build_dir),
filename: '[name].bundle.js',
publicPath: webpack_public_path
},
resolve: {
modules: [
path.resolve(__dirname, "app"),
"node_modules",
"modded_node_modules"
],
extensions: [".webpack.js", ".web.js", ".js", ".ts"]
},
plugins: [
function () {
this.plugin('watch-run', function (watching, callback) {
console.log('Watch triggered! Begin compile at ' + new Date());
callback();
});
this.plugin('done', function () {
console.log('Finished compile at ' + new Date());
});
},
new WebpackNotifierPlugin({ title: webpack_build_title })
],
module: {
rules: [
{
test: /\.ts$/,
use: ["ts-loader"]
},
{ test: /\.css$/, use: ["style", "css"] },
{ test: /\.jpe?g$|\.gif$|\.png$/, use: "url" },
{ test: /\.html/, use: 'file?name=[path][name].[ext]' },
{ test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&minetype=application/font-woff" },
{ test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
]
}
}
];
webpack-setups.js 的内容
__webpack_public_path__ = chrome.extension.getURL("");
package.json 片段:
"webpack": "^2.1.0-beta.25",
"webpack-manifest-plugin": "^1.0.1",
"webpack-notifier": "^1.4.1",
"webpack-shell-plugin": "^0.4.3",
"webpack-vendor-chunk-plugin": "^1.0.0",