Skip to content

Commit

Permalink
feat: global js ,global css (umijs#42)
Browse files Browse the repository at this point in the history
* feat: global js ,global css

* fix: debug

* docs: todo
  • Loading branch information
ycjcl868 committed Feb 10, 2020
1 parent 2037453 commit dff14a4
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 3 deletions.
8 changes: 5 additions & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ umi-next:
https://github.com/umijs/umi-next/pull/27
- webpack-dev-server 功能查漏补缺 @ycjcl868
core:
- logger @ycjcl868 @done
https://github.com/umijs/umi-next/pull/26
- logger @ycjcl868 @done
- .env 环境变量加载、环境变量统一 Enum @ycjcl868 @done
https://github.com/umijs/umi-next/pull/26
- 插件里可感知命令行参数,通过 api.args @done @sorrycc
- logger 优化
比如插件层如何打日志?如何过滤日志?
Expand All @@ -24,7 +25,8 @@ umi-next:
- api.routes
- 添加 api.chainWebpackConfig,提供 chainWebpack 的配置 @done @sorrycc
- umi build ,hash 在 html 中未起效 @bug @done @ycjcl868
preset-built-in:
preset-built-in:
- 支持 global-css、global-js @ycjcl868 @done
dev & build:
- 支持 H5 的多页功能
dev:
Expand Down
13 changes: 13 additions & 0 deletions packages/preset-built-in/src/fixtures/global-files/.umirc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

export default {
history: {
type: 'memory',
options: {
initialEntries: ['/'],
},
},
mountElementId: '',
routes: [
{ path: '/', component: 'index' },
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.h1 {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.customUtil = (str) => str;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export default () => <h1 className="h1">{window.customUtil('hello Global')}</h1>
20 changes: 20 additions & 0 deletions packages/preset-built-in/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Service } from '@umijs/core';
import { join } from 'path';
import { render, cleanup } from '@testing-library/react';
import { rimraf } from '@umijs/utils';
import { readFileSync } from 'fs';

const fixtures = join(__dirname, 'fixtures');

afterEach(cleanup);

test('api.writeTmpFile error in register stage', async () => {
const cwd = join(fixtures, 'api-writeTmpFile');
const service = new Service({
Expand Down Expand Up @@ -32,3 +35,20 @@ test('api.writeTmpFile', async () => {
expect(readFileSync(tmpFile, 'utf-8')).toEqual('foo');
rimraf.sync(tmpFile);
});

test('global js', async () => {
const cwd = join(fixtures, 'global-files');
const service = new Service({
cwd,
presets: [require.resolve('./index.ts')],
});
await service.run({
name: 'g',
args: {
_: ['g', 'tmp'],
},
});
const reactNode = require(join(cwd, 'src', '.umi-test', 'umi.ts')).default;
const { container } = render(reactNode);
expect(container.textContent).toEqual('hello Global');
});
2 changes: 2 additions & 0 deletions packages/preset-built-in/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export default function() {

// misc
require.resolve('./plugins/routes'),
require.resolve('./plugins/features/global-js'),
require.resolve('./plugins/features/global-css'),

// generate files
require.resolve('./plugins/generateFiles/core/history'),
Expand Down
31 changes: 31 additions & 0 deletions packages/preset-built-in/src/plugins/features/global-css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { relative } from 'path';
import { IApi } from '@umijs/types';
import { createDebug } from '@umijs/utils';
import { getGlobalFile } from '../utils';

const debug = createDebug('umi:preset-build-in:global-css');

export default (api: IApi) => {
const {
paths,
utils: { winPath },
} = api;
const { absSrcPath = '', absTmpPath = '' } = paths;
const files = [
'global.css',
'global.less',
'global.scss',
'global.sass',
'global.styl',
'global.stylus',
];
const globalCSSFile = getGlobalFile({ absSrcPath, files });
debug('globalCSSFile', globalCSSFile);

api.addEntryCodeAhead(
() => `
${globalCSSFile
.map(file => `require('${winPath(relative(absTmpPath, file))}');`)
.join('')}`,
);
};
20 changes: 20 additions & 0 deletions packages/preset-built-in/src/plugins/features/global-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { relative } from 'path';
import { IApi } from '@umijs/types';
import { createDebug } from '@umijs/utils';
import { getGlobalFile } from '../utils';

const debug = createDebug('umi:preset-build-in:global-js');

export default (api: IApi) => {
const { paths } = api;
const { absSrcPath = '', absTmpPath = '' } = paths;
const files = ['global.tsx', 'global.ts', 'global.jsx', 'global.js'];
const globalJSFile = getGlobalFile({ absSrcPath, files });
debug('globalJSFile', globalJSFile);

api.addEntryImportsAhead(() =>
globalJSFile.map(file => ({
source: relative(absTmpPath, file),
})),
);
};
16 changes: 16 additions & 0 deletions packages/preset-built-in/src/plugins/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from 'fs';
import { join } from 'path';
import { winPath } from '@umijs/utils';
import { getGlobalFile } from './utils';

test('getGlobalFile', () => {
const existsSyncMock = jest
.spyOn(fs, 'existsSync')
.mockImplementation(res => true);
const files = getGlobalFile({
files: ['global.ts', 'global.tsx', 'global.js'],
absSrcPath: winPath(__dirname),
});
expect(files).toEqual([join(winPath(__dirname), 'global.ts')]);
existsSyncMock.mockRestore();
});
19 changes: 19 additions & 0 deletions packages/preset-built-in/src/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { existsSync } from 'fs';
import { join } from 'path';

type IGetGlobalFile = (opts: {
absSrcPath: string;
files: string[];
}) => string[];

/**
* get global file like (global.js, global.css)
* @param absSrcPath
* @param files default load global files
*/
export const getGlobalFile: IGetGlobalFile = ({ absSrcPath, files }) => {
return files
.map(file => join(absSrcPath || '', file))
.filter(file => existsSync(file))
.slice(0, 1);
};

0 comments on commit dff14a4

Please sign in to comment.