学习 create-react-app
文章目录
版本说明:2.0.4
首先看文档中怎样使用:
这里的两条命令是等价的,以npm init ...为例来看命令是怎么用的
上面的意思就是说npm init <initializer>会调用已知的npm package create-<initializer>,并且会执行这个create-<initializer>中package.json定义的bin;
所以这里调用的是create-react-app这个包中的bin字段;
这里的bin的作用依然看官方文档,意思就是如果全局安装,会在环境变量中加入myapp,并与cli.js连接;如果是项目内安装,那么放在node_modules/.bin/中,(这里node_modules/.bin/是放置所有可执行文件的地方), 本地调用依然会执行cli.js文件。
所以再看./index.js文件, 这里不贴图, 占地方, 有兴趣自己看 create-react-app/index.js。其实这个文件只是做node的版本控制,保证不低于8.0;在文件最后重要的来了
来看一下createReactApp文件,这个6,有900+行,依然放链接啦 createReactApp.js,
以下是部分源码
let projectName;
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<project-directory>')
.usage(`${chalk.green('<project-directory>')} [options]`)
.action(name => {
projectName = name;
})
.option('--verbose', 'print additional logs')
.option('--info', 'print environment debug info')
.option(
'--scripts-version <alternative-package>',
'use a non-standard version of react-scripts'
)
.option('--use-npm')
.option('--use-pnp')
.option('--typescript')
.allowUnknownOption()
.on('--help', () => {
console.log(` Only ${chalk.green('<project-directory>')} is required.`);
console.log();
console.log(
` A custom ${chalk.cyan('--scripts-version')} can be one of:`
);
console.log(` - a specific npm version: ${chalk.green('0.8.2')}`);
console.log(` - a specific npm tag: ${chalk.green('@next')}`);
console.log(
` - a custom fork published on npm: ${chalk.green(
'my-react-scripts'
)}`
);
console.log(
` - a local path relative to the current working directory: ${chalk.green(
'file:../my-react-scripts'
)}`
);
console.log(
` - a .tgz archive: ${chalk.green(
'https://mysite.com/my-react-scripts-0.8.2.tgz'
)}`
);
console.log(
` - a .tar.gz archive: ${chalk.green(
'https://mysite.com/my-react-scripts-0.8.2.tar.gz'
)}`
);
console.log(
` It is not needed unless you specifically want to use a fork.`
);
console.log();
console.log(
` If you have any problems, do not hesitate to file an issue:`
);
console.log(
` ${chalk.cyan(
'https://github.com/facebook/create-react-app/issues/new'
)}`
);
console.log();
})
.parse(process.argv);
这里使用到的是commander.js这个神器,是一个帮助快速开发Nodejs命令行工具的package。
commander.js常见的用法就是和package.json中的bin搭配发布自己的npm包
通读下来createReactApp.js就是一个帮助搭建项目文件夹,写package.json一些语句的作用。
其中比较重要的是安装包的过程, 从下面代码能得知默认为我们安装了react,react-dom,react-scripts(使用ts,还要安装几个依赖包)
// function run()
const packageToInstall = getInstallPackage(version, originalDirectory);
const allDependencies = ['react', 'react-dom', packageToInstall];
if (useTypescript) {
allDependencies.push(
'@types/react',
'@types/react-dom',
'@types/jest',
'typescript'
);
}
/* 下面这个函数就是来获取react-scripts应该安装的版本号 */
function getInstallPackage(version, originalDirectory) {
let packageToInstall = 'react-scripts';
const validSemver = semver.valid(version);
if (validSemver) {
packageToInstall += `@${validSemver}`;
} else if (version) {
if (version[0] === '@' && version.indexOf('/') === -1) {
packageToInstall += version;
} else if (version.match(/^file:/)) {
packageToInstall = `file:${path.resolve(
originalDirectory,
version.match(/^file:(.*)?$/)[1]
)}`;
} else {
// for tar.gz or alternative paths
packageToInstall = version;
}
}
return packageToInstall;
}
未完待续...
不对的请赐教,请轻喷,鞠躬

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论