This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
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: Refactor & Enhance supporting vConsole
BREAKING CHANGE: Change the way to import library and replacement url hash key
- Loading branch information
Showing
36 changed files
with
625 additions
and
287 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
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 |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import path from 'path' | ||
|
||
import commonjs from '@rollup/plugin-commonjs'; | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import json from '@rollup/plugin-json' | ||
import resolve from '@rollup/plugin-node-resolve'; | ||
import resolve from '@rollup/plugin-node-resolve' | ||
import typescript from '@rollup/plugin-typescript' | ||
import visualizer from 'rollup-plugin-visualizer' | ||
|
||
import config from './package.json' | ||
|
||
export default { | ||
input: config.entry, | ||
output: { | ||
dir: config.deploy, | ||
format: 'umd', | ||
sourcemap: true, | ||
name: 'SourceReplacement' | ||
}, | ||
plugins: [ | ||
resolve({ | ||
browser: true, | ||
}), | ||
commonjs(), | ||
typescript({ | ||
declaration: true, | ||
declarationDir: config.deploy, | ||
exclude: 'src/**/*.test.ts', | ||
}), | ||
json(), | ||
visualizer({ | ||
filename: path.resolve(config.deploy, 'stat.html') | ||
}), | ||
], | ||
}; | ||
const sharedPlugins = [ | ||
resolve({ | ||
browser: true, | ||
}), | ||
commonjs(), | ||
typescript({ | ||
declaration: true, | ||
declarationDir: config.deploy, | ||
exclude: 'src/**/*.test.ts', | ||
}), | ||
json(), | ||
visualizer({ | ||
filename: path.resolve(config.deploy, 'stat.html'), | ||
}), | ||
] | ||
|
||
export default [ | ||
{ | ||
input: 'src/executors/code-blocker/index.ts', | ||
output: { | ||
dir: config.deploy, | ||
format: 'cjs', | ||
}, | ||
plugins: sharedPlugins | ||
}, | ||
{ | ||
input: 'src/executors/source-replacement/index.ts', | ||
output: { | ||
dir: config.deploy, | ||
format: 'umd', | ||
name: 'SourceReplacement', | ||
}, | ||
plugins: sharedPlugins, | ||
}, | ||
] |
81 changes: 81 additions & 0 deletions
81
src/core/preventPerformExistingScriptDuringInjection/index.test.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,81 @@ | ||
import { REPLACEMENT_SOURCE_KEY } from 'debugConstants' | ||
import preventPerformExistingScriptDuringInjection from '.' | ||
|
||
describe('preventPerformExistingScriptDuringInjection()', () => { | ||
const MOCK_URL = 'http://example.com' | ||
|
||
const promptSpy = jest.spyOn(global, 'prompt').mockReturnValue(MOCK_URL) | ||
|
||
Object.defineProperty(window, 'location', { | ||
value: { | ||
reload: jest.fn(), | ||
}, | ||
}) | ||
|
||
class VConsolePlugin { | ||
events: Record<string, any> = {} | ||
|
||
on = jest.fn().mockImplementation((key: string, handler: Function) => { | ||
const watcher = jest.fn() | ||
|
||
handler(watcher) | ||
|
||
this.events[key] = watcher.mock.calls[0][0] | ||
}) | ||
} | ||
|
||
class FakeVConsole { | ||
static VConsolePlugin = VConsolePlugin | ||
|
||
plugins: VConsolePlugin[] = [] | ||
|
||
addPlugin = jest.fn().mockImplementation(plugin => { | ||
this.plugins.push(plugin) | ||
}) | ||
} | ||
|
||
window['VConsole'] = FakeVConsole | ||
|
||
window['vConsole'] = new FakeVConsole() | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
sessionStorage.clear() | ||
}) | ||
|
||
it('should throw exception if app is marked from session that to be replaced', () => { | ||
sessionStorage.setItem(REPLACEMENT_SOURCE_KEY, MOCK_URL) | ||
|
||
expect(() => preventPerformExistingScriptDuringInjection()).toThrowError( | ||
new Error( | ||
'The under of code block stop performing causing by replacement process is running', | ||
), | ||
) | ||
}) | ||
|
||
it('should init vconsole plugin for set source from prompt to session', () => { | ||
preventPerformExistingScriptDuringInjection() | ||
|
||
const plugin = window['vConsole'].plugins[0] | ||
|
||
expect(window['vConsole'].addPlugin).toBeCalledTimes(1) | ||
expect(window['vConsole'].addPlugin).toBeCalledWith(plugin) | ||
expect(plugin).toBeInstanceOf(VConsolePlugin) | ||
|
||
expect(plugin.events.renderTab).toBe('<div>Click to Replacement button below</div>') | ||
|
||
expect(plugin.events.addTool).toHaveLength(1) | ||
|
||
const addToolEvent = plugin.events.addTool[0] | ||
|
||
expect(addToolEvent.name).toBe('Replacement') | ||
|
||
addToolEvent.onClick() | ||
|
||
expect(promptSpy).toBeCalledTimes(1) | ||
expect(promptSpy).toBeCalledWith('Input target replacement') | ||
|
||
expect(sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)).toBe(MOCK_URL) | ||
expect(location.reload).toBeCalledTimes(1) | ||
}) | ||
}) |
46 changes: 46 additions & 0 deletions
46
src/core/preventPerformExistingScriptDuringInjection/index.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,46 @@ | ||
import { REPLACEMENT_SOURCE_KEY } from 'debugConstants' | ||
|
||
import VConsole from 'vconsole' | ||
|
||
const PLUGIN_NAME = 'source_replacement' | ||
|
||
const TAB_NAME = 'Source Replacement' | ||
|
||
function preventPerformExistingScriptDuringInjection() { | ||
if (sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)) { | ||
throw new Error( | ||
'The under of code block stop performing causing by replacement process is running', | ||
) | ||
} else { | ||
const Console: typeof VConsole = window['VConsole'] | ||
|
||
const consoleInstance: VConsole = window['vConsole'] | ||
|
||
if (Console && consoleInstance) { | ||
const plugin = new Console.VConsolePlugin(PLUGIN_NAME, TAB_NAME) | ||
|
||
plugin.on('renderTab', callback => { | ||
callback('<div>Click to Replacement button below</div>') | ||
}) | ||
|
||
plugin.on('addTool', callback => { | ||
callback([ | ||
{ | ||
name: 'Replacement', | ||
onClick: () => { | ||
const targetSource = prompt('Input target replacement') | ||
|
||
sessionStorage.setItem(REPLACEMENT_SOURCE_KEY, targetSource!) | ||
|
||
location.reload() | ||
}, | ||
}, | ||
]) | ||
}) | ||
|
||
consoleInstance.addPlugin(plugin) | ||
} | ||
} | ||
} | ||
|
||
export default preventPerformExistingScriptDuringInjection |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.