forked from umijs/umi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: global js ,global css (umijs#42)
* feat: global js ,global css * fix: debug * docs: todo
- Loading branch information
Showing
11 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/preset-built-in/src/fixtures/global-files/.umirc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
], | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/preset-built-in/src/fixtures/global-files/src/global.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.h1 { | ||
color: red; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/preset-built-in/src/fixtures/global-files/src/global.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
window.customUtil = (str) => str; |
3 changes: 3 additions & 0 deletions
3
packages/preset-built-in/src/fixtures/global-files/src/pages/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/preset-built-in/src/plugins/features/global-css.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
packages/preset-built-in/src/plugins/features/global-js.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
})), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |