Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 修复开启 styleLoader 和 cssLoader 的 sourceMap 后, js 文件会暴露 css 源码的问题 #11417

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/bundler-webpack/src/config/cssRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface IOpts {
}

export async function addCSSRules(opts: IOpts) {
const { config, userConfig } = opts;
const { config, userConfig, env } = opts;

const rulesConfig = [
{ name: 'css', test: /\.css(\?.*)?$/ },
Expand Down Expand Up @@ -98,6 +98,12 @@ export async function addCSSRules(opts: IOpts) {
};
}

// 开启了 styleLoader 和 css 的 sourceMap, 线上打包会暴露源码, 做一层防护
const cssLoaderOptionsFixed: { sourceMap?: boolean } = {};
if (env === Env.production && userConfig.styleLoader) {
cssLoaderOptionsFixed.sourceMap = false;
}

rule
.use('css-loader')
.loader(require.resolve('css-loader'))
Expand All @@ -108,13 +114,13 @@ export async function addCSSRules(opts: IOpts) {
filter: (url: string) => {
// Don't parse absolute URLs
// ref: https://github.com/webpack-contrib/css-loader#url
if (url.startsWith('/')) return false;
return true;
return !url.startsWith('/');
},
},
import: true,
modules: cssLoaderModulesConfig,
...userConfig.cssLoader,
...cssLoaderOptionsFixed,
});

rule
Expand Down
Loading