-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: support env and improve structure
- Loading branch information
Showing
37 changed files
with
705 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
dist | ||
lib | ||
runtime |
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
node_modules | ||
*.log* | ||
dist | ||
lib | ||
runtime |
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
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,25 +1,25 @@ | ||
function getProxy (name: string, overrides: any = {}): any { | ||
function createMock (name: string, overrides: any = {}): any { | ||
const fn = function () { } | ||
fn.prototype.name = name | ||
const props: any = {} | ||
return new Proxy(fn, { | ||
get (_target, prop) { | ||
if (prop === 'caller') { return null } | ||
if (prop === '__createMock__') { return getProxy } | ||
if (prop === '__createMock__') { return createMock } | ||
if (prop in overrides) { return overrides[prop] } | ||
// @ts-ignore | ||
return (props[prop] = props[prop] || getProxy(`${name}.${prop.toString()}`)) | ||
return (props[prop] = props[prop] || createMock(`${name}.${prop.toString()}`)) | ||
}, | ||
apply (_target, _this, _args) { | ||
return getProxy(`${name}()`) | ||
return createMock(`${name}()`) | ||
}, | ||
construct (_target, _args, _newT) { | ||
return getProxy(`[${name}]`) as Object | ||
return createMock(`[${name}]`) as Object | ||
}, | ||
enumerate (_target) { | ||
return [] | ||
} | ||
}) | ||
} | ||
|
||
export default getProxy('mock') | ||
export default createMock('mock') |
File renamed without changes.
File renamed without changes.
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
4 changes: 2 additions & 2 deletions
4
src/node/http/response.ts → src.runtime/node/http/response.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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
src/node/stream/readable.ts → src.runtime/node/stream/readable.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
2 changes: 1 addition & 1 deletion
2
src/node/stream/writable.ts → src.runtime/node/stream/writable.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
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,5 @@ | ||
import mock from '../mock/proxy' | ||
|
||
export default mock.__createMock__('consola', { | ||
...console | ||
}) |
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// TODO |
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
// https://github.com/defunctzombie/node-process/ | ||
|
||
global.process = global.process || {} | ||
Object.assign(global.process, require('node-process')) | ||
|
||
// TODO: apply only non-existing keys | ||
Object.assign(global.process, require('node-process/browser.js')) | ||
|
||
export default global.process |
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 @@ | ||
export type Callback<E = Error | null | undefined> = (error?: E) => void | ||
export type HeadersObject = { [key: string]: string | string[] | undefined } | ||
export type BufferEncoding = any // TODO |
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,23 @@ | ||
import type { HeadersObject } from './types' | ||
|
||
export function rawHeaders (headers: HeadersObject) { | ||
const rawHeaders = [] | ||
for (const key in headers) { | ||
if (Array.isArray(headers[key])) { | ||
for (const h of headers[key] as any) { | ||
rawHeaders.push(key, h) | ||
} | ||
} else { | ||
rawHeaders.push(key, headers[key]) | ||
} | ||
} | ||
return rawHeaders | ||
} | ||
|
||
export function mergeFns (...functions: Function[]) { | ||
return function (...args: any[]) { | ||
for (const fn of functions) { | ||
fn(...args) | ||
} | ||
} | ||
} |
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 type { Preset, Environment } from './types' | ||
|
||
export function env (...presets: Preset[]) { | ||
const options: Environment = { | ||
alias: {}, | ||
inject: {} | ||
} | ||
|
||
for (const preset of presets) { | ||
if (preset.alias) { | ||
Object.assign(options.alias, preset.alias) | ||
} | ||
if (preset.inject) { | ||
Object.assign(options.alias, preset.inject) | ||
} | ||
} | ||
|
||
return options | ||
} |
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,4 @@ | ||
export * from './utils' | ||
export * from './presets' | ||
export * from './env' | ||
export * from './types' |
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 @@ | ||
export { default as nodeless } from './nodeless' | ||
export { default as vue2 } from './vue2' | ||
export { default as vue3 } from './vue3' |
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,28 @@ | ||
import { r, NodeBuiltinModules, mockAll } from '../utils' | ||
import type { Preset } from '../types' | ||
|
||
export default { | ||
alias: { | ||
...mockAll(NodeBuiltinModules), | ||
|
||
// Custom | ||
http: r('node/http'), | ||
process: r('shims/process'), | ||
|
||
// Browserify | ||
buffer: require.resolve('buffer/index.js'), | ||
util: require.resolve('util/util.js'), | ||
events: require.resolve('events/events.js'), | ||
inherits: require.resolve('inherits/inherits_browser.js'), | ||
|
||
// npm | ||
etag: r('mock/noop'), | ||
'mime-db': r('npm/mime-db'), | ||
mime: r('npm/mime') | ||
}, | ||
|
||
inject: { | ||
process: r('shims/process'), | ||
Buffer: ['buffer', 'Buffer'] | ||
} | ||
} as Preset |
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,15 @@ | ||
import { mockAll } from '../utils' | ||
import type { Preset } from '../types' | ||
|
||
export default { | ||
alias: { | ||
...mockAll([ | ||
'encoding', | ||
'he', | ||
'resolve', | ||
'source-map', | ||
'lodash.template', | ||
'serialize-javascript' | ||
]) | ||
} | ||
} as Preset |
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 @@ | ||
import { mockAll } from '../utils' | ||
import type { Preset } from '../types' | ||
|
||
export default { | ||
alias: { | ||
...mockAll([ | ||
'@babel/parser', | ||
'@vue/compiler-core', | ||
'@vue/compiler-dom', | ||
'@vue/compiler-ssr' | ||
]) | ||
} | ||
} as Preset |
Empty 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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export type Callback<E = Error | null | undefined> = (error?: E) => void | ||
export type HeadersObject = { [key: string]: string | string[] | undefined } | ||
export type BufferEncoding = any // TODO | ||
export interface Environment { | ||
alias: { [key: string]: string } | ||
inject: { [key: string]: string | string[] } | ||
} | ||
|
||
export interface Preset { | ||
alias?: { [key: string]: string } | ||
inject?: { [key: string]: string | string[] } | ||
} |
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 |
---|---|---|
@@ -1,23 +1,18 @@ | ||
import type { HeadersObject } from 'src/types' | ||
import { Module } from 'module' | ||
import { resolve, join } from 'upath' | ||
|
||
export function rawHeaders (headers: HeadersObject) { | ||
const rawHeaders = [] | ||
for (const key in headers) { | ||
if (Array.isArray(headers[key])) { | ||
for (const h of headers[key] as any) { | ||
rawHeaders.push(key, h) | ||
} | ||
} else { | ||
rawHeaders.push(key, headers[key]) | ||
} | ||
} | ||
return rawHeaders | ||
export const RUNTIME_DIR = resolve(__dirname, '../runtime') | ||
|
||
export const NodeBuiltinModules = Module.builtinModules | ||
|
||
export function mapArrToVal (val: any, arr: any[]) { | ||
return arr.reduce((p, c) => ({ ...p, [c]: val }), {}) | ||
} | ||
|
||
export function r (id: string) { | ||
return require.resolve(join(RUNTIME_DIR, id)) | ||
} | ||
|
||
export function mergeFns (...functions: Function[]) { | ||
return function (...args: any[]) { | ||
for (const fn of functions) { | ||
fn(...args) | ||
} | ||
} | ||
export function mockAll (packages: string[]) { | ||
return mapArrToVal(r('mock/proxy'), packages) | ||
} |
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
Oops, something went wrong.