Skip to content

Commit

Permalink
feat: support customize publicPathPlacehoder in css files
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucifier129 committed Jan 8, 2024
1 parent 13962c4 commit 5503fd4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
13 changes: 11 additions & 2 deletions controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ export default class Controller {
return publicPath + clientAssetPath
}

/**
* 用于替换 css 中的 publicPath 的占位符
*
* 默认为:@public_path
*/
publicPathPlaceholder = '@public_path'

/**
* 是否在 preload 里禁用 publicPath
* 默认为 false,只对 CRS 生效
Expand Down Expand Up @@ -362,8 +369,10 @@ export default class Controller {

const publicPathInCss = this.disablePublicPathForPreload ? localPublicPath : context.publicPath ?? localPublicPath

// 替换 css 中的 @public_path 未真实的 publicPath
content = content.replace(/@public_path\//g, publicPathInCss + '/')
const regexp = new RegExp(this.publicPathPlaceholder + '/', 'g')

// 替换 css 中的 public_path 为真实的 publicPath
content = content.replace(regexp, publicPathInCss + '/')

context.preload[name] = content
}).catch(error => {
Expand Down
7 changes: 7 additions & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ controller.handlers 是在初始化时,从 controller 的实例里收集的以

是否在 preload 里禁用 publicPath,默认为 false,只对 CRS 生效。如果为 true,会直接使用 node.js 服务端的静态资源路径

### controller.publicPathPlaceholder -> string

如果 preload 里的 css 文件里的图片等资源需要使用 publicPath,但是又不想在 preload 里写死 publicPath,可以使用该字段,

默认为 `@public_path`,在 preload 里写 `@public_path`,会在运行时被替换为 controller.context.publicPath


## Controller API

### controller.fetch(url=string, options=object)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-imvc",
"version": "2.10.14",
"version": "2.10.15",
"description": "An Isomorphic MVC Framework",
"main": "./index",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions project/src/preload/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class extends Controller {
preload = {
style: style
}
publicPathPlaceholder = '#public_path'
}


Expand Down
2 changes: 1 addition & 1 deletion project/src/preload/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ body {
.logo {
width: 267px;
height: 189px;
background-image: url(@public_path/img/react.png);
background-image: url(#public_path/img/react.png);
}

0 comments on commit 5503fd4

Please sign in to comment.