-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: module-replace-plugin not work for new created module
- Loading branch information
Showing
50 changed files
with
843 additions
and
197 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
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
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,24 @@ | ||
import { WatchEvent } from "@shuvi/utils/lib/fileWatcher"; | ||
import { BaseComponent } from "./Base"; | ||
export interface Props { | ||
name: string; | ||
lookupFiles: string[]; | ||
fallbackFile: string; | ||
} | ||
interface State { | ||
file: string; | ||
} | ||
export default class FileSelector extends BaseComponent<Props, State> { | ||
private _watcherHandle; | ||
private _knownFiles; | ||
constructor(props: Props); | ||
_onFilesChange({ removals, changes }: WatchEvent): void; | ||
_destoryWatcher(): void; | ||
_createWatcher(): void; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
componentDidUpdate(prevProps: Props): void; | ||
shouldComponentUpdate(nextProps: Props, nextState: State): boolean; | ||
render(): JSX.Element; | ||
} | ||
export {}; |
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,96 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_1 = __importDefault(require("react")); | ||
const react_fs_1 = require("@shuvi/react-fs"); | ||
const fileWatcher_1 = require("@shuvi/utils/lib/fileWatcher"); | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const utils_1 = require("../utils"); | ||
const Base_1 = require("./Base"); | ||
function findFirstExistedFile(files) { | ||
for (let index = 0; index < files.length; index++) { | ||
const file = files[index]; | ||
if (fs_extra_1.default.existsSync(file)) { | ||
return file; | ||
} | ||
} | ||
return null; | ||
} | ||
class FileSelector extends Base_1.BaseComponent { | ||
constructor(props) { | ||
super(props); | ||
this._knownFiles = new Map(); | ||
const file = findFirstExistedFile(props.lookupFiles); | ||
let selectedFile; | ||
if (file) { | ||
selectedFile = file; | ||
this._knownFiles.set(file, true); | ||
} | ||
else { | ||
selectedFile = props.fallbackFile; | ||
} | ||
this.state = { | ||
file: selectedFile | ||
}; | ||
this._onFilesChange = this._onFilesChange.bind(this); | ||
} | ||
_onFilesChange({ removals, changes }) { | ||
for (let index = 0; index < changes.length; index++) { | ||
const existed = changes[index]; | ||
this._knownFiles.set(existed, true); | ||
} | ||
for (let index = 0; index < removals.length; index++) { | ||
const removed = removals[index]; | ||
this._knownFiles.delete(removed); | ||
} | ||
const { lookupFiles, fallbackFile } = this.props; | ||
let selectedFile = fallbackFile; | ||
for (let index = 0; index < lookupFiles.length; index++) { | ||
const file = lookupFiles[index]; | ||
if (this._knownFiles.has(file)) { | ||
selectedFile = file; | ||
break; | ||
} | ||
} | ||
if (selectedFile !== this.state.file) { | ||
this.setState({ | ||
file: selectedFile | ||
}); | ||
} | ||
} | ||
_destoryWatcher() { | ||
if (this._watcherHandle) { | ||
this._watcherHandle(); | ||
} | ||
} | ||
_createWatcher() { | ||
this._destoryWatcher(); | ||
if (this.props.lookupFiles.length) { | ||
this._watcherHandle = fileWatcher_1.watch({ files: this.props.lookupFiles }, this._onFilesChange); | ||
} | ||
} | ||
componentDidMount() { | ||
this._createWatcher(); | ||
} | ||
componentWillUnmount() { | ||
this._destoryWatcher(); | ||
} | ||
componentDidUpdate(prevProps) { | ||
if (!utils_1.arrayEqual(prevProps.lookupFiles, this.props.lookupFiles)) { | ||
this._createWatcher(); | ||
} | ||
} | ||
shouldComponentUpdate(nextProps, nextState) { | ||
return (this.state !== nextState || | ||
this.props.fallbackFile !== nextProps.fallbackFile || | ||
!utils_1.arrayEqual(this.props.lookupFiles, nextProps.lookupFiles)); | ||
} | ||
render() { | ||
const { name } = this.props; | ||
const { file } = this.state; | ||
return (react_1.default.createElement(react_fs_1.File, { name: name, content: `module.exports = require("${file}");` })); | ||
} | ||
} | ||
exports.default = FileSelector; |
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,14 @@ | ||
import { TemplateData } from "@shuvi/types/core"; | ||
import { BaseComponent } from "./Base"; | ||
export interface Props { | ||
name: string; | ||
templateFile?: string; | ||
template?: string; | ||
data?: TemplateData; | ||
} | ||
export default class FileTemplate extends BaseComponent<Props> { | ||
private _compileTemplate; | ||
private _readFile; | ||
private _renderTemplate; | ||
render(): JSX.Element | null; | ||
} |
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,35 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_1 = __importDefault(require("react")); | ||
const react_fs_1 = require("@shuvi/react-fs"); | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const handlebars_1 = __importDefault(require("handlebars")); | ||
const utils_1 = require("../utils"); | ||
const Base_1 = require("./Base"); | ||
class FileTemplate extends Base_1.BaseComponent { | ||
constructor() { | ||
super(...arguments); | ||
this._compileTemplate = utils_1.memoizeOne((template) => handlebars_1.default.compile(template)); | ||
this._readFile = utils_1.memoizeOne((path) => fs_extra_1.default.readFileSync(path, "utf8")); | ||
} | ||
_renderTemplate(template) { | ||
const templateFn = this._compileTemplate(template); | ||
const content = templateFn(this.props.data || {}); | ||
return react_1.default.createElement(react_fs_1.File, { name: this.props.name, content: content }); | ||
} | ||
render() { | ||
const { templateFile, template } = this.props; | ||
if (template) { | ||
return this._renderTemplate(template); | ||
} | ||
if (templateFile) { | ||
const tmplContent = this._readFile(templateFile); | ||
return this._renderTemplate(tmplContent); | ||
} | ||
return null; | ||
} | ||
} | ||
exports.default = FileTemplate; |
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 @@ | ||
import { FileNode, File } from "./files/FileNode"; | ||
export declare class ModelApp { | ||
bootstrapModule: string; | ||
appModuleFallback: string; | ||
appModuleLookups: string[]; | ||
documentModuleFallback: string; | ||
documentModuleLookups: string[]; | ||
routesContent: string; | ||
extraFiles: FileNode[]; | ||
addFile(file: File, dir?: string): void; | ||
removeFile(path: string): void; | ||
} |
Oops, something went wrong.