Skip to content

Commit

Permalink
✨ feat: 添加 dependency 注入支持
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Apr 25, 2021
1 parent b20e51b commit bef3ff6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions example/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export default defineConfig({
persistent: true,
},
contentScripts: [
{
matches: ['http://*/*', 'https://*/*'],
entries: ['jquery', 'require.js'],
runAt: 'document_end',
},
{
matches: ['https://github.com/*'],
entries: ['@/contentScripts/github'],
Expand Down
8 changes: 7 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"dependencies": {},
"dependencies": {
"jquery": "^3.6.0",
"require.js": "^1.0.0"
},
"devDependencies": {
"@types/jquery": "^3.5.5"
},
"version": "1.0.0"
}
8 changes: 4 additions & 4 deletions src/functions/contentScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { join } from 'path';
import fse from 'fs-extra';

import type { IApi } from 'umi';
import { updateContentScripts } from '../utils';
import { getDependencyPath, updateContentScripts } from '../utils';

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

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

// 将 contentScripts 作为一个入口插入打包对象中
contentScripts.forEach((item, index) => {
const { entries } = item;

const entry = `contentScript_${index}`;

const entries = item.entries.map((e) => getDependencyPath(e, cwd));

config.entry(entry).merge(entries);
});

Expand Down
7 changes: 7 additions & 0 deletions src/utils/dependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { join } from 'path';

export const getDependencyPath = (entry: string, cwd?: string) => {
return entry.startsWith('@/') || entry.startsWith('.')
? entry
: join(cwd || process.cwd(), 'node_modules', entry);
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './csp';
export * from './env';
export * from './route';
export * from './manifest';
export * from './dependency';
18 changes: 18 additions & 0 deletions test/utils/dependency.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getDependencyPath } from '../../src/utils';

describe('getDependencyPath', () => {
it('base', () => {
expect(getDependencyPath('jquery')).toEqual(
process.cwd() + '/node_modules/jquery',
);
});
it('with cwd', () => {
expect(getDependencyPath('jquery', '/hhh')).toEqual(
'/hhh/node_modules/jquery',
);
});
it('not modules', () => {
expect(getDependencyPath('@/jquery')).toEqual('@/jquery');
expect(getDependencyPath('./jquery')).toEqual('./jquery');
});
});

0 comments on commit bef3ff6

Please sign in to comment.