Skip to content

Commit 122c9e0

Browse files
authored
Merge pull request #178 from arvinxx/beta
支持 contentScript 添加依赖
2 parents dde4db2 + 4b64554 commit 122c9e0

13 files changed

+222
-75
lines changed

.github/workflows/release.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ on:
33
push:
44
branches:
55
- master
6+
- beta
67

78
jobs:
89
release:
@@ -24,7 +25,8 @@ jobs:
2425
- name: Lint
2526
run: yarn lint && yarn tsc
2627

27-
- run: yarn test
28+
- name: Test
29+
run: yarn build && yarn test
2830

2931
- name: release
3032
run: yarn release

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
run: yarn lint && yarn tsc
2222

2323
- name: test
24-
run: yarn test:coverage
24+
run: yarn build && yarn test:coverage
2525

2626
- name: Generate coverage
2727
run: bash <(curl -s https://codecov.io/bash)

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
# [0.6.0-beta.1](https://github.com/arvinxx/umi-plugin-extensions/compare/v0.5.0...v0.6.0-beta.1) (2021-04-25)
4+
5+
6+
### ✨ Features
7+
8+
* 添加 dependency 注入支持 ([bef3ff6](https://github.com/arvinxx/umi-plugin-extensions/commit/bef3ff6))
9+
310
# [0.5.0](https://github.com/arvinxx/umi-plugin-extensions/compare/v0.4.1...v0.5.0) (2021-04-16)
411

512

example/.umirc.ts

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export default defineConfig({
2626
persistent: true,
2727
},
2828
contentScripts: [
29+
{
30+
matches: ['http://*/*', 'https://*/*'],
31+
entries: ['jquery', 'require.js'],
32+
runAt: 'document_end',
33+
},
2934
{
3035
matches: ['https://github.com/*'],
3136
entries: ['@/contentScripts/github'],

example/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2-
"dependencies": {},
2+
"dependencies": {
3+
"jquery": "^3.6.0",
4+
"require.js": "^1.0.0"
5+
},
6+
"devDependencies": {
7+
"@types/jquery": "^3.5.5"
8+
},
39
"version": "1.0.0"
410
}

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "umi-plugin-extensions",
3-
"version": "0.5.0",
3+
"version": "0.6.0-beta.1",
44
"main": "lib/index.js",
55
"description": "a umi plugin to build chrome extensions",
66
"authors": {
@@ -91,7 +91,7 @@
9191
"cross-env": "^7.0.3",
9292
"eslint": "^7.7.0",
9393
"eslint-import-resolver-alias": "^1.1.2",
94-
"eslint-import-resolver-typescript": "^2.2.1",
94+
"eslint-import-resolver-typescript": "^2.4.0",
9595
"express": "^4.15.3",
9696
"father-build": "^1.19.1",
9797
"gh-pages": "^3.1.0",
@@ -112,6 +112,6 @@
112112
"typedoc-plugin-external-module-map": "^1.2.1",
113113
"typedoc-plugin-markdown": "^3.6.0",
114114
"typescript": "^4.2.3",
115-
"umi": "^3.4.8"
115+
"umi": "^3.4.11"
116116
}
117117
}

src/functions/contentScripts.test.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'path';
22
import { Service } from 'umi';
3-
import { render } from '@testing-library/react';
3+
import { readFileSync } from 'fs';
44

55
const fixtures = join(__dirname, '../fixtures');
66

@@ -18,8 +18,6 @@ test('normal tmp', async () => {
1818
},
1919
});
2020

21-
// eslint-disable-next-line global-require,import/no-dynamic-require
22-
const reactNode = require(join(cwd, '.umi-test', 'umi.ts')).default;
23-
const { container } = render(reactNode);
24-
expect(container.textContent).toEqual('');
21+
const html = readFileSync(join(cwd, 'dist', 'index.html'), 'utf-8');
22+
expect(html).toContain('root');
2523
});

src/functions/contentScripts.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { join } from 'path';
22
import fse from 'fs-extra';
33

44
import type { IApi } from 'umi';
5-
import { updateContentScripts } from '../utils';
5+
import { getDependencyPath, updateContentScripts } from '../utils';
66

77
/**
88
* 将 contentScripts 添加到打包对象中
99
* 并在输出结果中添加 contentScripts 脚本
1010
* @param api
1111
*/
1212
export default (api: IApi) => {
13-
const { paths } = api.service;
13+
const { paths, cwd } = api.service;
1414

1515
// 修正 webpack 关于 contentScripts 的配置
1616
api.chainWebpack((config) => {
@@ -23,10 +23,10 @@ export default (api: IApi) => {
2323

2424
// 将 contentScripts 作为一个入口插入打包对象中
2525
contentScripts.forEach((item, index) => {
26-
const { entries } = item;
27-
2826
const entry = `contentScript_${index}`;
2927

28+
const entries = item.entries.map((e) => getDependencyPath(e, cwd));
29+
3030
config.entry(entry).merge(entries);
3131
});
3232

src/index.test.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'path';
22
import { Service } from 'umi';
3-
import { render } from '@testing-library/react';
3+
import { readFileSync } from 'fs';
44

55
const fixtures = join(__dirname, './fixtures');
66

@@ -18,8 +18,6 @@ test('normal tmp', async () => {
1818
},
1919
});
2020

21-
// eslint-disable-next-line global-require,import/no-dynamic-require
22-
const reactNode = require(join(cwd, '.umi-test', 'umi.ts')).default;
23-
const { container } = render(reactNode);
24-
expect(container.textContent).toEqual('');
21+
const html = readFileSync(join(cwd, 'dist', 'index.html'), 'utf-8');
22+
expect(html).toContain('root');
2523
});

src/utils/dependency.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { join } from 'path';
2+
3+
export const getDependencyPath = (entry: string, cwd?: string) => {
4+
return entry.startsWith('@/') || entry.startsWith('.')
5+
? entry
6+
: join(cwd || process.cwd(), 'node_modules', entry);
7+
};

src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './csp';
22
export * from './env';
33
export * from './route';
44
export * from './manifest';
5+
export * from './dependency';

test/utils/dependency.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getDependencyPath } from '../../src/utils';
2+
3+
describe('getDependencyPath', () => {
4+
it('base', () => {
5+
expect(getDependencyPath('jquery')).toEqual(
6+
process.cwd() + '/node_modules/jquery',
7+
);
8+
});
9+
it('with cwd', () => {
10+
expect(getDependencyPath('jquery', '/hhh')).toEqual(
11+
'/hhh/node_modules/jquery',
12+
);
13+
});
14+
it('not modules', () => {
15+
expect(getDependencyPath('@/jquery')).toEqual('@/jquery');
16+
expect(getDependencyPath('./jquery')).toEqual('./jquery');
17+
});
18+
});

0 commit comments

Comments
 (0)