-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(css): support css asset in bundleless mode and esm/cjs (#582)
- Loading branch information
Showing
13 changed files
with
211 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
.counter-title { | ||
width: 100px; | ||
height: 100px; | ||
background: no-repeat url('./assets/logo.svg'); | ||
background-size: cover; | ||
} | ||
|
||
.counter-text { | ||
font-size: 50px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { type Rspack, rspack } from '@rsbuild/core'; | ||
import { RSLIB_CSS_ENTRY_FLAG } from './cssConfig'; | ||
import { | ||
ABSOLUTE_PUBLIC_PATH, | ||
AUTO_PUBLIC_PATH, | ||
SINGLE_DOT_PATH_SEGMENT, | ||
} from './libCssExtractLoader'; | ||
import { getUndoPath } from './utils'; | ||
|
||
const pluginName = 'LIB_CSS_EXTRACT_PLUGIN'; | ||
|
||
type Options = Record<string, unknown>; | ||
|
||
class LibCssExtractPlugin implements Rspack.RspackPluginInstance { | ||
readonly name: string = pluginName; | ||
options: Options; | ||
constructor(options?: Options) { | ||
this.options = options ?? {}; | ||
} | ||
|
||
apply(compiler: Rspack.Compiler): void { | ||
// 1. mark and remove the normal css asset | ||
// 2. preserve CSS Modules asset | ||
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { | ||
compilation.hooks.chunkAsset.tap(pluginName, (_chunk, filename) => { | ||
const asset = compilation.getAsset(filename); | ||
if (!asset) { | ||
return; | ||
} | ||
const needRemove = Boolean(asset.name.match(RSLIB_CSS_ENTRY_FLAG)); | ||
if (needRemove) { | ||
compilation.deleteAsset(filename); | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* The following code is modified based on | ||
* https://github.com/webpack-contrib/mini-css-extract-plugin/blob/3effaa0319bad5cc1bf0ae760553bf7abcbc35a4/src/index.js#L1597 | ||
* | ||
* replace publicPath placeholders of miniCssExtractLoader | ||
*/ | ||
compiler.hooks.make.tap(pluginName, (compilation) => { | ||
compilation.hooks.processAssets.tap(pluginName, (assets) => { | ||
const chunkAsset = Object.keys(assets).filter((name) => | ||
/\.css/.test(name), | ||
); | ||
for (const name of chunkAsset) { | ||
compilation.updateAsset(name, (old) => { | ||
const oldSource = old.source().toString(); | ||
const replaceSource = new rspack.sources.ReplaceSource(old); | ||
|
||
function replace(searchValue: string, replaceValue: string) { | ||
let start = oldSource.indexOf(searchValue); | ||
while (start !== -1) { | ||
replaceSource.replace( | ||
start, | ||
start + searchValue.length - 1, | ||
replaceValue, | ||
); | ||
start = oldSource.indexOf(searchValue, start + 1); | ||
} | ||
} | ||
|
||
replace(ABSOLUTE_PUBLIC_PATH, ''); | ||
replace(SINGLE_DOT_PATH_SEGMENT, '.'); | ||
const undoPath = getUndoPath( | ||
name, | ||
compilation.outputOptions.path!, | ||
false, | ||
); | ||
replace(AUTO_PUBLIC_PATH, undoPath); | ||
|
||
return replaceSource; | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
export { LibCssExtractPlugin }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* This function is copied from | ||
* https://github.com/webpack-contrib/mini-css-extract-plugin/blob/3effaa0319bad5cc1bf0ae760553bf7abcbc35a4/src/utils.js#L169 | ||
* linted by biome | ||
*/ | ||
function getUndoPath( | ||
filename: string, | ||
outputPathArg: string, | ||
enforceRelative: boolean, | ||
): string { | ||
let depth = -1; | ||
let append = ''; | ||
|
||
let outputPath = outputPathArg.replace(/[\\/]$/, ''); | ||
|
||
for (const part of filename.split(/[/\\]+/)) { | ||
if (part === '..') { | ||
if (depth > -1) { | ||
depth--; | ||
} else { | ||
const i = outputPath.lastIndexOf('/'); | ||
const j = outputPath.lastIndexOf('\\'); | ||
const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j); | ||
|
||
if (pos < 0) { | ||
return `${outputPath}/`; | ||
} | ||
|
||
append = `${outputPath.slice(pos + 1)}/${append}`; | ||
|
||
outputPath = outputPath.slice(0, pos); | ||
} | ||
} else if (part !== '.') { | ||
depth++; | ||
} | ||
} | ||
|
||
return depth > 0 | ||
? `${'../'.repeat(depth)}${append}` | ||
: enforceRelative | ||
? `./${append}` | ||
: append; | ||
} | ||
|
||
export { getUndoPath }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.