Skip to content

Commit 5f7f9c0

Browse files
committed
feat: handle csp, extract css
1 parent f5819e4 commit 5f7f9c0

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.5.2
2+
3+
* Added `dist/csp/*` to support strict CSP with extracted CSS file.
4+
15
## 6.5.1
26

37
* Fixed types for mouse events.

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Drop `<script>` inside your HTML file and access the component via `window.VueEC
227227
```html
228228
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
229229
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
230-
<script src="https://cdn.jsdelivr.net/npm/[email protected].1"></script>
230+
<script src="https://cdn.jsdelivr.net/npm/[email protected].2"></script>
231231
```
232232
<!-- vue3Scripts:end -->
233233

@@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
247247
```html
248248
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
249249
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
250-
<script src="https://cdn.jsdelivr.net/npm/[email protected].1"></script>
250+
<script src="https://cdn.jsdelivr.net/npm/[email protected].2"></script>
251251
```
252252
<!-- vue2Scripts:end -->
253253

@@ -453,6 +453,10 @@ Vue-ECharts support the following events:
453453

454454
See supported events [here →](https://echarts.apache.org/en/api.html#events)
455455

456+
## CSP: `style-src` or `style-src-elem`
457+
458+
If you are applying a CSP to prevent inline `<style>` injection, you need to use files from `dist/csp` directory and include `dist/csp/style.css` into your app manually.
459+
456460
## Migration to v6
457461

458462
> 💡 Please make sure to read the [migration guide](https://echarts.apache.org/en/tutorial.html#ECharts%205%20Upgrade%20Guide) for ECharts 5 as well.

README.zh-Hans.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ import "echarts";
225225
```html
226226
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
227227
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
228-
<script src="https://cdn.jsdelivr.net/npm/[email protected].1"></script>
228+
<script src="https://cdn.jsdelivr.net/npm/[email protected].2"></script>
229229
```
230230
<!-- vue3Scripts:end -->
231231

@@ -245,7 +245,7 @@ app.component('v-chart', VueECharts)
245245
```html
246246
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
247247
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
248-
<script src="https://cdn.jsdelivr.net/npm/[email protected].1"></script>
248+
<script src="https://cdn.jsdelivr.net/npm/[email protected].2"></script>
249249
```
250250
<!-- vue2Scripts:end -->
251251

@@ -451,6 +451,10 @@ import { THEME_KEY } from 'vue-echarts'
451451
452452
静态方法请直接通过 [`echarts` 本身](https://echarts.apache.org/zh/api.html#echarts)进行调用。
453453
454+
## CSP: `style-src``style-src-elem`
455+
456+
如果你正在应用 CSP 来防止内联 `<style>` 注入,则需要使用 `dist/csp` 目录中的文件,并手动引入 `dist/csp/style.css`
457+
454458
## 迁移到 v6
455459
456460
> 💡 请确保同时查阅 ECharts 5 的[升级指南](https://echarts.apache.org/zh/tutorial.html#ECharts%205%20%E5%8D%87%E7%BA%A7%E6%8C%87%E5%8D%97)。

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-echarts",
3-
"version": "6.5.1",
3+
"version": "6.5.2",
44
"description": "Vue.js component for Apache ECharts.",
55
"author": "GU Yiling <[email protected]>",
66
"scripts": {

rollup.config.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@ import resolve from "@rollup/plugin-node-resolve";
44
import styles from "rollup-plugin-styles";
55
import { injectVueDemi } from "./scripts/rollup";
66

7+
/**
8+
* Convert Rollup option to a style extracted/injected version
9+
* @param {import('rollup').RollupOptions} option
10+
* @param {boolean} extract
11+
* @returns {import('rollup').RollupOptions}
12+
*/
13+
function handleStyle(option, extract) {
14+
// inject styles plugin
15+
const result = { ...option };
16+
const { plugins, output } = result;
17+
result.plugins = (plugins || []).concat(
18+
extract ? styles({ mode: ["extract", "style.css"] }) : styles()
19+
);
20+
21+
// modify output file names
22+
if (extract && output) {
23+
result.output = (Array.isArray(output) ? output : [output]).map(output => {
24+
return {
25+
...output,
26+
file: output.file.replace(/^dist\//, "dist/csp/"),
27+
assetFileNames: "[name][extname]"
28+
};
29+
});
30+
}
31+
32+
return result;
33+
}
34+
735
/** @type {import('rollup').RollupOptions[]} */
836
const options = [
937
{
@@ -15,8 +43,7 @@ const options = [
1543
outputPath: (path, kind) =>
1644
kind === "declaration" ? "dist/index.d.ts" : path
1745
}
18-
}),
19-
styles()
46+
})
2047
],
2148
external: ["vue-demi", "echarts/core", "resize-detector"],
2249
output: {
@@ -27,7 +54,7 @@ const options = [
2754
},
2855
{
2956
input: "src/index.ts",
30-
plugins: [typescript(), styles()],
57+
plugins: [typescript()],
3158
external: ["vue-demi", "echarts/core", "resize-detector"],
3259
output: [
3360
{
@@ -65,7 +92,7 @@ const options = [
6592
},
6693
{
6794
input: "src/global.ts",
68-
plugins: [resolve(), typescript(), styles()],
95+
plugins: [resolve(), typescript()],
6996
external: ["vue-demi", "echarts", "echarts/core"],
7097
output: [
7198
{
@@ -105,4 +132,7 @@ const options = [
105132
}
106133
];
107134

108-
export default options;
135+
export default [
136+
...options.map(option => handleStyle(option, false)),
137+
...options.map(option => handleStyle(option, true))
138+
];

0 commit comments

Comments
 (0)