From 3c306ac61b063d9b1dd372ea8078e2527caf5afc Mon Sep 17 00:00:00 2001 From: zougt Date: Fri, 11 Mar 2022 18:45:33 +0800 Subject: [PATCH] feat(): add customLinkHref --- README.md | 2 ++ package.json | 2 +- src/apply/arbitraryModeApply.js | 4 +--- src/apply/presetModeApply.js | 10 +++++++--- src/hot-loader/index.js | 4 +--- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 28b94c8..ffbfc1e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # @zougt/theme-css-extract-webpack-plugin 这个 webpack 插件与[@zougt/some-loader-utils](https://github.com/GitOfZGT/some-loader-utils)结合轻松实现在线动态主题,使用文档直接查看[@zougt/some-loader-utils](https://github.com/GitOfZGT/some-loader-utils) + ## Options | Name | Type | Default | Description | @@ -13,6 +14,7 @@ | **[`defaultScopeName`](#defaultScopeName)** | `{String}` | multipleScopeVars[0].scopeName | 默认使用主题名称 | | **[`themeLinkTagId`](#themeLinkTagId)** | `{String}` | `theme-link-tag` | 在 html 中使用主题 css 文件的 link 标签的 id | | **[`themeLinkTagAppend`](#themeLinkTagAppend)** | `{Boolean}` | `false` | 是否在其他 css 之后插入主题 css 文件的 link 标签 | +| **[`customLinkHref`](#customLinkHref)** | `{Function}` | `null` | 预设主题模式,抽取 css 后,自定义默认添加到 html 的 link 的 href | ### `multipleScopeVars` diff --git a/package.json b/package.json index fd57e10..d95d95b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zougt/theme-css-extract-webpack-plugin", - "version": "1.3.2", + "version": "1.4.0", "description": "extracts theme CSS into separate files", "license": "MIT", "repository": "GitOfZGT/theme-css-extract-webpack-plugin", diff --git a/src/apply/arbitraryModeApply.js b/src/apply/arbitraryModeApply.js index 76fbf19..7d23e7d 100644 --- a/src/apply/arbitraryModeApply.js +++ b/src/apply/arbitraryModeApply.js @@ -106,9 +106,7 @@ export function arbitraryModeApply(compiler) { ) { this.cacheThemeStyleContent = styleContent; const packRoot = require - .resolve(pack.name, { - paths: [process.cwd()], - }) + .resolve(pack.name) .replace(/[\\/]cjs\.js$/, '') .replace(/\\/g, '/'); // no return diff --git a/src/apply/presetModeApply.js b/src/apply/presetModeApply.js index 83d9845..56c4651 100644 --- a/src/apply/presetModeApply.js +++ b/src/apply/presetModeApply.js @@ -11,13 +11,17 @@ function getThemeExtractLinkTag({ publicPath, userOptions }) { (typeof userOptions.customThemeCssFileName === 'function' ? userOptions.customThemeCssFileName(userOptions.defaultScopeName) : '') || userOptions.defaultScopeName; + let href = `/${publicPath || ''}/${ + userOptions.outputDir || '' + }/${filename}.css`.replace(/\/+(?=\/)/g, ''); + if (typeof userOptions.customLinkHref === 'function') { + href = userOptions.customLinkHref(href); + } return { tagName: 'link', voidTag: true, attributes: { - href: userOptions.customLinkHref(`/${publicPath || ''}/${ - userOptions.outputDir || '' - }/${filename}.css`.replace(/\/+(?=\/)/g, '')), + href, rel: 'stylesheet', id: userOptions.themeLinkTagId, }, diff --git a/src/hot-loader/index.js b/src/hot-loader/index.js index 64db736..72743e0 100644 --- a/src/hot-loader/index.js +++ b/src/hot-loader/index.js @@ -7,9 +7,7 @@ import pack from '../../package.json'; function HotUpdateLoader(content) { const callback = this.async(); const packRoot = require - .resolve(pack.name, { - paths: [this.context || process.cwd()], - }) + .resolve(pack.name) .replace(/[\\/]cjs\.js$/, '') .replace(/\\/g, '/'); const param = getPluginParams();