如何解决Webpack源映射:在Firefox中可用,在Chrome中不可用
我不明白为什么我可以在Firefox中看到我的所有源映射(从TypeScript到JavaScript的映射),但是在Chrome中却大多找不到它们。
这只是Firefox看到的内容的一部分(如果我打开所有文件夹,它就不能全部显示在一张屏幕截图中):
这是Chrome在同一网站上可以看到的全部内容(assets
文件夹只是图像,而不是代码):
我只是使用webpack devtool: 'source-map'
选项。我尝试使用SourceMapDevToolPlugin
只会使情况变得更糟,而不是变得更好。
我将在此处发布完整的webpack.config.js
,但它很大,我怀疑这是否所有问题。这是一个webpack.config.js
,它的根源是一个古老的Angular项目,对此进行了简化,但保留了用于处理.scss
文件之类的部件。
我必须承认,我绝不是Webpack的专家,我只是遵循现有模板并根据需要对其进行了修改。我毫不怀疑此配置可以大大简化和改进。
该项目的完整源代码可以在这里找到:https://github.com/kshetline/aw-clock/tree/gps
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const rxPaths = require('rxjs/_esm5/path-mapping');
const autoprefixer = require('autoprefixer');
const postcssUrl = require('postcss-url');
const postcssImports = require('postcss-import');
const entryPoints = ['inline','polyfills','sw-register','styles','vendor','main'];
const baseHref = '';
const deployUrl = '';
const projectRoot = process.cwd();
const maximumInlineSize = 10;
const NODE_ENV = process.env.NODE_ENV || 'production';
const postcssPlugins = function (loader) {
// noinspection JSValidateTypes
return [
postcssImports({
resolve: (url,context) => {
return new Promise((resolve,reject) => {
let hadTilde = false;
if (url && url.startsWith('~')) {
url = url.substr(1);
hadTilde = true;
}
loader.resolve(context,(hadTilde ? '' : './') + url,(err,result) => {
if (err) {
if (hadTilde) {
reject(err);
return;
}
loader.resolve(context,url,result) => {
if (err) {
reject(err);
}
else {
resolve(result);
}
});
}
else {
resolve(result);
}
});
});
},load: (filename) => {
return new Promise((resolve,reject) => {
// noinspection JSValidateTypes
loader.fs.readFile(filename,data) => {
if (err) {
reject(err);
return;
}
const content = data.toString();
resolve(content);
});
});
}
}),postcssUrl({
filter: ({ url }) => url.startsWith('~'),url: ({ url }) => {
const fullPath = path.join(projectRoot,'node_modules',url.substr(1));
return path.relative(loader.context,fullPath).replace(/\\/g,'/');
}
}),postcssUrl([
{
// Only convert root relative URLs,which CSS-Loader won't process into require().
filter: ({ url }) => url.startsWith('/') && !url.startsWith('//'),url: ({ url }) => {
if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
// If deployUrl is absolute or root relative,ignore baseHref & use deployUrl as is.
return `${deployUrl.replace(/\/$/,'')}${url}`;
}
else if (baseHref.match(/:\/\//)) {
// If baseHref contains a scheme,include it as is.
return baseHref.replace(/\/$/,'') +
`/${deployUrl}/${url}`.replace(/\/\/+/g,'/');
}
else {
// Join together base-href,deploy-url and the original URL.
// Also dedupe multiple slashes into single ones.
return `/${baseHref}/${deployUrl}/${url}`.replace(/\/\/+/g,'/');
}
}
},{
url: 'inline',// NOTE: maxSize is in KB
maxSize: maximumInlineSize,fallback: 'rebase',},{ url: 'rebase' },]),autoprefixer({ grid: true }),];
};
// noinspection JSUnusedGlobalSymbols
module.exports = {
mode: NODE_ENV,performance: { hints: false },resolve: {
extensions: [
'.ts','.js'
],symlinks: true,modules: [
'./src','./node_modules'
],alias: rxPaths(),mainFields: [
'browser','module','main'
]
},resolveLoader: {
modules: [
'./node_modules'
],alias: rxPaths()
},entry: {
main: [
'./src/main.ts'
],polyfills: [
'./src/polyfills.ts'
],styles: [
'./src/styles.scss'
]
},output: {
path: path.join(process.cwd(),'dist','public'),filename: '[name].bundle.js',chunkFilename: '[id].chunk.js',crossOriginLoading: false
},module: {
rules: [
{
test: /\.html$/,loader: 'raw-loader'
},{
test: /\.(eot|svg|cur)$/,loader: 'file-loader',options: {
name: '[name].[hash:20].[ext]',limit: 10000
}
},{
test: /\.(jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,loader: 'url-loader',{
exclude: [
path.join(process.cwd(),'src/styles.scss')
],test: /\.css$/,use: [
{
loader: 'raw-loader'
},{
loader: 'postcss-loader',options: {
ident: 'embedded',plugins: postcssPlugins,sourceMap: true
}
}
]
},test: /\.scss$|\.sass$/,sourceMap: true
}
},{
loader: 'sass-loader',options: {
sourceMap: true,precision: 8,includePaths: []
}
}
]
},test: /\.less$/,{
loader: 'less-loader',options: {
sourceMap: true
}
}
]
},test: /\.styl$/,{
loader: 'stylus-loader',paths: []
}
}
]
},{
include: [
path.join(process.cwd(),use: [
'style-loader',{
loader: 'raw-loader'
},{
loader: 'sass-loader'
}
]
},{
test: /\.ts|\.tsx$/,use: 'ts-loader',exclude: [/node_modules/,'/server/**/*']
}
]
},optimization: {
minimize: true,minimizer: [new TerserPlugin()],devtool: 'source-map',plugins: [
new CopyWebpackPlugin({
patterns: [
{
context: 'src',to: '',from: 'assets/**/*',globOptions: {
dot: true,ignore: [
'.gitkeep','**/.DS_Store','**/Thumbs.db'
],debug: 'warning'
}
},{
context: 'src',from: 'favicon.ico'
}
]
}),new ProgressPlugin({}),new CircularDependencyPlugin({
exclude: /([\\/])node_modules([\\/])/,failOnError: false,onDetected: false,cwd: projectRoot
}),new HtmlWebpackPlugin({
template: './src/index.html',filename: './index.html',hash: false,inject: 'head',compile: true,favicon: false,minify: false,cache: true,showErrors: true,chunks: 'all',excludeChunks: [],title: 'Webpack App',xhtml: true,chunksSortMode: function sort(left,right) {
// noinspection JSUnresolvedVariable
const leftIndex = entryPoints.indexOf((left.names && left.names[0]) || left.toString());
// noinspection JSUnresolvedVariable
const rightIndex = entryPoints.indexOf((right.names && right.names[0]) || right.toString());
return Math.sign(leftIndex - rightIndex);
}
}),function () {
this.plugin('done',stats => {
if (stats.compilation.errors && stats.compilation.errors.length > 0) {
if (stats.compilation.errors.length === 0)
console.error(stats.compilation.errors[0]);
else {
console.error(stats.compilation.errors.map(err =>
err && typeof err === 'object' && err.message ? err.message : '').join('\n'));
}
process.exit(1);
}
});
}
],node: {
global: true
},devServer: {
historyApiFallback: true
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。