-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
192 additions
and
177 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
2 changes: 1 addition & 1 deletion
2
examples/react-component-bundle-false/src/components/CounterButton/index.module.scss
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,3 @@ | ||
.button { | ||
background: yellow; | ||
background: url('../../assets/logo.svg') no-repeat; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
examples/react-component-bundle/src/components/CounterButton/index.module.scss
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,3 @@ | ||
.button { | ||
background: yellow; | ||
background: url('../../assets/logo.svg') no-repeat; | ||
} |
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
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,74 @@ | ||
import { type Rspack, rspack } from '@rsbuild/core'; | ||
import { getUndoPath } from '../css/utils'; | ||
|
||
type Options = { | ||
} | ||
/** | ||
* these codes is written according to | ||
* https://github.com/web-infra-dev/rspack/blob/61f0cd2b4e313445a9d3329ca71240e99edfb352/crates/rspack_plugin_asset/src/lib.rs#L531 | ||
*/ | ||
const pattern: RegExp = /__webpack_require__\.p\s\+\s["'](.+)["']/g; | ||
function extractAssetFilenames (content: string): string[] { | ||
console.log([...content.matchAll(pattern)]) | ||
return [...content.matchAll(pattern)].map(i => { | ||
return i?.[1] | ||
}).filter(Boolean) as string[]; | ||
} | ||
|
||
const RSLIB_NAMESPACE_OBJECT = `__rslib_asset__`; | ||
|
||
const esmSingleFileTemplate = (url: string) => `import ${RSLIB_NAMESPACE_OBJECT} from '${url}'; | ||
export default ${RSLIB_NAMESPACE_OBJECT};`; | ||
|
||
const cjsSingleFileTemplate = (url: string) => `module.exports = require('${url}');`; | ||
|
||
const pluginName = 'LIB_ASSET_EXTRACT_PLUGIN'; | ||
|
||
class LibAssetExtractPlugin implements Rspack.RspackPluginInstance { | ||
readonly name: string = pluginName; | ||
options: Options; | ||
constructor(options?: Options) { | ||
this.options = options ?? {}; | ||
} | ||
|
||
apply(compiler: Rspack.Compiler): void { | ||
compiler.hooks.make.tap(pluginName, (compilation) => { | ||
compilation.hooks.processAssets.tap(pluginName, (assets) => { | ||
const chunkAsset = Object.keys(assets).filter((name) => | ||
/js/.test(name), | ||
); | ||
for (const name of chunkAsset) { | ||
const isEsmFormat = compilation.options.output.module; | ||
const undoPath = getUndoPath( | ||
name, | ||
compilation.outputOptions.path!, | ||
true, | ||
); | ||
compilation.updateAsset(name, (old) => { | ||
const oldSource = old.source().toString(); | ||
const assetFilenames = extractAssetFilenames(oldSource); | ||
|
||
if (assetFilenames.length === 1) { | ||
const assetFilename = assetFilenames[0]; | ||
let newSource: string = ''; | ||
const url = `${undoPath}${assetFilename}`; | ||
|
||
if(isEsmFormat) { | ||
newSource = esmSingleFileTemplate(url); | ||
} else { | ||
newSource = cjsSingleFileTemplate(url); | ||
} | ||
return new rspack.sources.RawSource(newSource); | ||
} else { | ||
const newSource = new rspack.sources.ReplaceSource(old); | ||
assetFilenames.forEach(() => { | ||
}) | ||
return newSource; | ||
} | ||
|
||
}); | ||
} | ||
}) | ||
})} | ||
} | ||
export { LibAssetExtractPlugin}; |
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,30 @@ | ||
/** | ||
* Used to match `RegExp` | ||
* [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns). | ||
*/ | ||
const reRegExpChar: RegExp = /[\\^$.*+?()[\]{}|]/g, | ||
reHasRegExpChar: RegExp = RegExp(reRegExpChar.source); | ||
|
||
/** | ||
* Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+", | ||
* "?", "(", ")", "[", "]", "{", "}", and "|" in `string`. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 3.0.0 | ||
* @category String | ||
* @param {string} [string=''] The string to escape. | ||
* @returns {string} Returns the escaped string. | ||
* @example | ||
* | ||
* _.escapeRegExp('[lodash](https://lodash.com/)'); | ||
* // => '\[lodash\]\(https://lodash\.com/\)' | ||
*/ | ||
function escapeRegExp(string: string): string { | ||
return (string && reHasRegExpChar.test(string)) | ||
? string.replace(reRegExpChar, '\\$&') | ||
: string; | ||
} | ||
|
||
|
||
export {escapeRegExp} |
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
Oops, something went wrong.