-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { SanSourceFile, isTypedSanSourceFile } from '../models/san-source-file' | ||
import debugFactory from 'debug' | ||
|
||
const debug = debugFactory('san-ssr:remove-modules') | ||
|
||
/** | ||
* 删除模块引用 | ||
*/ | ||
export function removeModules (sanSourceFile: SanSourceFile, modules: RegExp[]) { | ||
if (!isTypedSanSourceFile(sanSourceFile)) { | ||
debug('TypedSanSourceFile is required') | ||
return | ||
} | ||
const importDeclarations = sanSourceFile.tsSourceFile.getImportDeclarations() | ||
debug('removing modules', importDeclarations) | ||
importDeclarations.forEach(i => { | ||
const specifierValue = i.getModuleSpecifierValue() | ||
if (modules.some(re => re.test(specifierValue))) { | ||
debug(`remove ${specifierValue}`) | ||
i.remove() | ||
} | ||
}) | ||
} |
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,12 @@ | ||
/// <reference path="./remove-modules.d.ts"/> | ||
import foo from 'foo' | ||
import bar from 'bar' | ||
import { Component } from 'san' | ||
|
||
export default class RemoveModulesComp extends Component { | ||
public static template = '<div>Remove foo</div>' | ||
mounted () { | ||
foo() | ||
bar() | ||
} | ||
} |
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,2 @@ | ||
declare module 'foo' | ||
declare module 'bar' |
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,26 @@ | ||
import { removeModules } from '../../../src/parsers/remove-modules' | ||
import { TypedSanSourceFile } from '../../../src/models/san-source-file' | ||
import { SanProject } from '../../../src/models/san-project' | ||
import { resolve } from 'path' | ||
|
||
const testRoot = resolve(__dirname, '../../') | ||
const stubRoot = resolve(testRoot, 'stub') | ||
|
||
const mockDebug = jest.fn() | ||
jest.mock('debug', () => () => (str: string) => mockDebug(str)) | ||
|
||
describe('removeModules', () => { | ||
it('should deal with TypedSanSourceFile only', () => { | ||
const proj = new SanProject() | ||
const sanSourceFile = proj.parseSanSourceFile(resolve(stubRoot, './a.comp.js')) | ||
removeModules(sanSourceFile, []) | ||
expect(mockDebug).toHaveBeenCalledWith('TypedSanSourceFile is required') | ||
}) | ||
|
||
it('should remove modules', () => { | ||
const proj = new SanProject(resolve(testRoot, '../tsconfig.json')) | ||
const sanSourceFile = proj.parseSanSourceFile(resolve(stubRoot, './remove-modules.comp.ts')) as TypedSanSourceFile | ||
removeModules(sanSourceFile, [/^foo/]) | ||
expect(sanSourceFile.tsSourceFile.getImportDeclaration('foo')).toBeUndefined() | ||
}) | ||
}) |