webpack 5中的CSS Sourcemap,带有SASS-LOADER,POSTCSS-LOADER和MINICSSEXTRACTPLUGIN

发布于 2025-01-28 12:06:44 字数 1491 浏览 5 评论 0原文

似乎我遵循了所有指令来生成CSS源地图,但仍未在输出CSS中找到它们(我希望看到/ *#sourcemappingurl = base.css.map */之类的东西。在输出中)。这是我的webpack配置的相关位:

const ENV = process.env;
const scriptDir = path.join(__dirname, 'scripts');

const config = {
  devtool: ENV.WEBPACK_DEVTOOL || 'eval-source-map',

  mode: 'development',

  ...

  module: {
    rules: [
      ...
      {
        test: /\.(scss|sass|css)$/,
        use: [
          // Roll styles into a separate file in /styles folder
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              // There are a bunch of hardcoded paths within CSS that assume we have a URL structure of a Django app. This option makes sure those are not followed during build.
              url: false,
              sourceMap: true,
            },
          },
          {loader: 'postcss-loader', options: {sourceMap: true}},
          {loader: 'sass-loader', options: {sourceMap: true}},
        ],
      },
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({filename: '../styles/base.css'}),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(ENV.NODE_ENV),
    }),
  ],
};

if (ENV.NODE_ENV === 'production') {
  config.devtool = 'hidden-source-map';
  config.mode = 'production';
}

if (ENV.WEBPACK_PLUGIN) {
  const Plugin = require(ENV.WEBPACK_PLUGIN);
  config.plugins.push(new Plugin());
}

module.exports = config;

Seems like I've followed all the instructions to generate CSS source maps, but still not finding them in the output CSS (I expect to see something like /*# sourceMappingURL=base.css.map */ in the output). Here are the relevant bits of my Webpack config:

const ENV = process.env;
const scriptDir = path.join(__dirname, 'scripts');

const config = {
  devtool: ENV.WEBPACK_DEVTOOL || 'eval-source-map',

  mode: 'development',

  ...

  module: {
    rules: [
      ...
      {
        test: /\.(scss|sass|css)$/,
        use: [
          // Roll styles into a separate file in /styles folder
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              // There are a bunch of hardcoded paths within CSS that assume we have a URL structure of a Django app. This option makes sure those are not followed during build.
              url: false,
              sourceMap: true,
            },
          },
          {loader: 'postcss-loader', options: {sourceMap: true}},
          {loader: 'sass-loader', options: {sourceMap: true}},
        ],
      },
    ],
  },
  plugins: [
    new MiniCssExtractPlugin({filename: '../styles/base.css'}),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(ENV.NODE_ENV),
    }),
  ],
};

if (ENV.NODE_ENV === 'production') {
  config.devtool = 'hidden-source-map';
  config.mode = 'production';
}

if (ENV.WEBPACK_PLUGIN) {
  const Plugin = require(ENV.WEBPACK_PLUGIN);
  config.plugins.push(new Plugin());
}

module.exports = config;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文