“This is the 26th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Import-local Executes the flow
What it does: When there is a scaffolding command in node_modules in the current project, and a scaffolding command in the global Node environment, it takes precedence over the local version in node_modules.
// core\lerna\cli.js
Import / / in the local
const importLocal = require("import-local");
// If importLocal(__filename) is true, a line of log is output
// Check whether scaffolding exists in local 'node_modules'
// __filename is the current file address D:\lerna-main\core\ lerna-cli.js
if (importLocal(__filename)) {
// Use the local version of the log output
require("npmlog").info("cli"."using local version of lerna");
} else {
require(".")(process.argv.slice(2));
}
Copy the code
Look at the source code for import-local. It doesn’t look like there’s a lot of it
// node_modules\import-local\index.js
const path = require('path');
const resolveCwd = require('resolve-cwd');
const pkgDir = require('pkg-dir');
module.exports = filename= > {
// filename Specifies the global execution file of the scaffold
// Get the global directory where the scaffold is located, containing package.json
// If the current module is deeply nested, the directory containing package.json will be searched layer by layer
const globalDir = pkgDir.sync(path.dirname(filename));
// Compare globalDir and filename relative paths to get the final CLI.js
const relativePath = path.relative(globalDir, filename);
// Merge the globalDir and package.json paths and retrieve the package.json file contents
const pkg = require(path.join(globalDir, 'package.json'));
// Combine pkg.name with relativePath to get lerna/cli.js
// Then call resolveCwd to determine if the current project is local to the module return to the local module path
const localFile = resolveCwd.silent(path.join(pkg.name, relativePath));
// Return null if the module has loaded modules otherwise
returnlocalFile && path.relative(localFile, filename) ! = =' ' ? require(localFile) : null;
};
Copy the code
As you can see, the import-local module is a function that executes require(localFile) inside the function when the condition is met and then returns to execute it
require("npmlog").info("cli"."using local version of lerna");
Copy the code
Using local version of Lerna is printed first, because the scaffolding command implementation uses microtask queues, although require(localFile) is executed first. However, the real execution logic is stored in the microtask queue, and using local version of LERna is the last content of the macro task, so the log will be output when the macro task is executed, and then the actual business logic code in the microtask queue will be executed.
Design resume based on Lerna
You can add this chapter to your resume after you have fully mastered it
Note: I did not understand chapter 4-12 ~ 4-18, so I need to review it later, but it will not affect the progress of the main course
- Be familiar with
yargs
Scaffolding development framework - Familiar with more
package
Management toollerna
The application method and implementation principle of - Deep understanding of
node.js
Module path resolution process
How to develop a scaffold using Yargs
Take vuE-CLI as an example. The most basic command is vue create project –force
- bin:
package.json
Configured inbin
Property, which can be interpreted as the main command, i.evue
, local development time throughnpm link
Install locally. - Need to be in
bin
Point to the file that is added to the scaffold’s executable file#! /usr/bin/env node
Tell the operating system to query in environment variablesnode
, and through thenode
To execute the file. - command: command, as in the example
create
。 - param: parameter, as in the example
project
。 - option: arguments can also carry options, as in the example
--force
.
Then talk about the scaffold initialization process
- Call the constructor to generate a scaffold:
Yargs ()
- call
yargs
Commonly used methods to enhance the function of scaffoldingYargs.options
Yargs.group
. .
- Parse scaffold parameters
- using
yargs/helpers
To provide thehideBin
, the callYargs(hideBin(process.argv)).argv
Complete resolution Yargs.parse(argv,options)
- using
- Register scaffold command
Yargs.command(command,describe,builder,handler)
Yargs.command({command,describe,builder,handler})
How does LERNA work
Lerna is a multi-package project management tool based on Git + NPM
The use of the Lerna
- Lerna init
- Lerna create
- Lerna add
- Lerna link
- . .
Implementation principle of Lerna
- through
import-local
Call local firstlerna
Command. - through
Yargs
Generate scaffolding, register global properties first, then register commands, and finally passparse
Method parses the parameters. Lerna
The command is passed in for registrationbuilder
和handler
Two parameters,builder
Method used to register command specificoptions
,handler
The business logic used to process commands.Lerna
By configuringnpm
A locally dependent approach to local development, written in thepackage.json
Write to the dependency of:file:your-local-module-path
In thelerna publish
Will automatically replace the changed path with an online path.
Node. js module path resolution process
. . loading