Skip to content

Commit 3723a39

Browse files
committed
update
1 parent cda17bf commit 3723a39

File tree

5 files changed

+289
-0
lines changed

5 files changed

+289
-0
lines changed

mini-webpack/webpack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function getModuleInfo(file) {
2020
// 依赖收集
2121
const deps = {};
2222
traverse(ast, {
23+
// visitor函数
2324
ImportDeclaration({ node }) {
2425
const dirname = path.dirname(file);
2526
const abspath = "./" + path.join(dirname, node.source.value);

toolchain/babel/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// console.log 和 console.error 中插入代码的位置信息
2+
//一些参数的功能。
3+
const parser = require('@babel/parser')
4+
const traverse = require('@babel/traverse').default
5+
const generator = require('@babel/generator').default
6+
const types = require('@babel/types')
7+
8+
const sourceFileName = 'sourceFile.js'
9+
10+
const sourceCode = `
11+
console.log(1)
12+
function log() : number {
13+
console.debug('before');
14+
console.error(2);
15+
console.debug('after');
16+
}
17+
log();
18+
class Foo {
19+
bar(): void {
20+
console.log(3)
21+
}
22+
render() {
23+
return ''
24+
// return <div><div>
25+
}
26+
}
27+
`
28+
29+
const ast = parser.parse(sourceCode, {
30+
plugins: ['typescript','jsx']
31+
})
32+
33+
traverse(ast, {
34+
CallExpression(path) {
35+
const calleeStr = generator(path.node.callee).code
36+
if(['console.log','console.error'].includes(calleeStr)) {
37+
const {line,column} = path.node.loc.start
38+
path.node.arguments.unshift(types.stringLiteral(`${sourceFileName}(${line},${column}):`))
39+
40+
}
41+
}
42+
})
43+
44+
const {code ,map } = generator(ast, {
45+
sourceMaps: true,
46+
sourceFileName
47+
})
48+
console.log('code ',code)
49+
50+
console.log('map',map)

toolchain/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "toolchain",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"@babel/parser": "^7.14.3",
14+
"@babel/traverse": "^7.14.2",
15+
"@babel/types": "^7.14.2"
16+
}
17+
}

toolchain/yarn-error.log

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Arguments:
2+
/usr/local/bin/node /usr/local/bin/yarn add @babel/core @babel/template @babel/types @babel/generate @babel/traverse @babel/parser
3+
4+
PATH:
5+
/Users/xiaran/opt/anaconda3/bin:/Users/xiaran/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
6+
7+
Yarn version:
8+
1.22.4
9+
10+
Node version:
11+
14.7.0
12+
13+
Platform:
14+
darwin x64
15+
16+
Trace:
17+
Error: https://registry.npm.taobao.org/@babel%2fgenerate: Not found
18+
at Request.params.callback [as _callback] (/usr/local/lib/node_modules/yarn/lib/cli.js:66987:18)
19+
at Request.self.callback (/usr/local/lib/node_modules/yarn/lib/cli.js:140748:22)
20+
at Request.emit (events.js:314:20)
21+
at Request.<anonymous> (/usr/local/lib/node_modules/yarn/lib/cli.js:141720:10)
22+
at Request.emit (events.js:314:20)
23+
at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/yarn/lib/cli.js:141642:12)
24+
at Object.onceWrapper (events.js:420:28)
25+
at IncomingMessage.emit (events.js:326:22)
26+
at endReadableNT (_stream_readable.js:1244:12)
27+
at processTicksAndRejections (internal/process/task_queues.js:80:21)
28+
29+
npm manifest:
30+
{
31+
"name": "toolchain",
32+
"version": "1.0.0",
33+
"description": "",
34+
"main": "index.js",
35+
"scripts": {
36+
"test": "echo \"Error: no test specified\" && exit 1"
37+
},
38+
"keywords": [],
39+
"author": "",
40+
"license": "ISC"
41+
}
42+
43+
yarn manifest:
44+
No manifest
45+
46+
Lockfile:
47+
No lockfile

toolchain/yarn.lock

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@babel/code-frame@^7.12.13":
6+
version "7.12.13"
7+
resolved "https://registry.npm.taobao.org/@babel/code-frame/download/@babel/code-frame-7.12.13.tgz#dcfc826beef65e75c50e21d3837d7d95798dd658"
8+
integrity sha1-3PyCa+72XnXFDiHTg319lXmN1lg=
9+
dependencies:
10+
"@babel/highlight" "^7.12.13"
11+
12+
"@babel/generator@^7.14.2":
13+
version "7.14.3"
14+
resolved "https://registry.nlark.com/@babel/generator/download/@babel/generator-7.14.3.tgz#0c2652d91f7bddab7cccc6ba8157e4f40dcedb91"
15+
integrity sha1-DCZS2R973at8zMa6gVfk9A3O25E=
16+
dependencies:
17+
"@babel/types" "^7.14.2"
18+
jsesc "^2.5.1"
19+
source-map "^0.5.0"
20+
21+
"@babel/helper-function-name@^7.14.2":
22+
version "7.14.2"
23+
resolved "https://registry.nlark.com/@babel/helper-function-name/download/@babel/helper-function-name-7.14.2.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40babel%2Fhelper-function-name%2Fdownload%2F%40babel%2Fhelper-function-name-7.14.2.tgz#397688b590760b6ef7725b5f0860c82427ebaac2"
24+
integrity sha1-OXaItZB2C273cltfCGDIJCfrqsI=
25+
dependencies:
26+
"@babel/helper-get-function-arity" "^7.12.13"
27+
"@babel/template" "^7.12.13"
28+
"@babel/types" "^7.14.2"
29+
30+
"@babel/helper-get-function-arity@^7.12.13":
31+
version "7.12.13"
32+
resolved "https://registry.npm.taobao.org/@babel/helper-get-function-arity/download/@babel/helper-get-function-arity-7.12.13.tgz?cache=0&sync_timestamp=1612314686467&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40babel%2Fhelper-get-function-arity%2Fdownload%2F%40babel%2Fhelper-get-function-arity-7.12.13.tgz#bc63451d403a3b3082b97e1d8b3fe5bd4091e583"
33+
integrity sha1-vGNFHUA6OzCCuX4diz/lvUCR5YM=
34+
dependencies:
35+
"@babel/types" "^7.12.13"
36+
37+
"@babel/helper-split-export-declaration@^7.12.13":
38+
version "7.12.13"
39+
resolved "https://registry.npm.taobao.org/@babel/helper-split-export-declaration/download/@babel/helper-split-export-declaration-7.12.13.tgz?cache=0&sync_timestamp=1612314686094&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40babel%2Fhelper-split-export-declaration%2Fdownload%2F%40babel%2Fhelper-split-export-declaration-7.12.13.tgz#e9430be00baf3e88b0e13e6f9d4eaf2136372b05"
40+
integrity sha1-6UML4AuvPoiw4T5vnU6vITY3KwU=
41+
dependencies:
42+
"@babel/types" "^7.12.13"
43+
44+
"@babel/helper-validator-identifier@^7.14.0":
45+
version "7.14.0"
46+
resolved "https://registry.nlark.com/@babel/helper-validator-identifier/download/@babel/helper-validator-identifier-7.14.0.tgz?cache=0&sync_timestamp=1619727556616&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40babel%2Fhelper-validator-identifier%2Fdownload%2F%40babel%2Fhelper-validator-identifier-7.14.0.tgz#d26cad8a47c65286b15df1547319a5d0bcf27288"
47+
integrity sha1-0mytikfGUoaxXfFUcxml0Lzycog=
48+
49+
"@babel/highlight@^7.12.13":
50+
version "7.14.0"
51+
resolved "https://registry.nlark.com/@babel/highlight/download/@babel/highlight-7.14.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40babel%2Fhighlight%2Fdownload%2F%40babel%2Fhighlight-7.14.0.tgz#3197e375711ef6bf834e67d0daec88e4f46113cf"
52+
integrity sha1-MZfjdXEe9r+DTmfQ2uyI5PRhE88=
53+
dependencies:
54+
"@babel/helper-validator-identifier" "^7.14.0"
55+
chalk "^2.0.0"
56+
js-tokens "^4.0.0"
57+
58+
"@babel/parser@^7.12.13", "@babel/parser@^7.14.2", "@babel/parser@^7.14.3":
59+
version "7.14.3"
60+
resolved "https://registry.nlark.com/@babel/parser/download/@babel/parser-7.14.3.tgz?cache=0&sync_timestamp=1621284787674&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40babel%2Fparser%2Fdownload%2F%40babel%2Fparser-7.14.3.tgz#9b530eecb071fd0c93519df25c5ff9f14759f298"
61+
integrity sha1-m1MO7LBx/QyTUZ3yXF/58UdZ8pg=
62+
63+
"@babel/template@^7.12.13":
64+
version "7.12.13"
65+
resolved "https://registry.npm.taobao.org/@babel/template/download/@babel/template-7.12.13.tgz#530265be8a2589dbb37523844c5bcb55947fb327"
66+
integrity sha1-UwJlvooliduzdSOETFvLVZR/syc=
67+
dependencies:
68+
"@babel/code-frame" "^7.12.13"
69+
"@babel/parser" "^7.12.13"
70+
"@babel/types" "^7.12.13"
71+
72+
"@babel/traverse@^7.14.2":
73+
version "7.14.2"
74+
resolved "https://registry.nlark.com/@babel/traverse/download/@babel/traverse-7.14.2.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40babel%2Ftraverse%2Fdownload%2F%40babel%2Ftraverse-7.14.2.tgz#9201a8d912723a831c2679c7ebbf2fe1416d765b"
75+
integrity sha1-kgGo2RJyOoMcJnnH678v4UFtdls=
76+
dependencies:
77+
"@babel/code-frame" "^7.12.13"
78+
"@babel/generator" "^7.14.2"
79+
"@babel/helper-function-name" "^7.14.2"
80+
"@babel/helper-split-export-declaration" "^7.12.13"
81+
"@babel/parser" "^7.14.2"
82+
"@babel/types" "^7.14.2"
83+
debug "^4.1.0"
84+
globals "^11.1.0"
85+
86+
"@babel/types@^7.12.13", "@babel/types@^7.14.2":
87+
version "7.14.2"
88+
resolved "https://registry.nlark.com/@babel/types/download/@babel/types-7.14.2.tgz?cache=0&sync_timestamp=1620839476067&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40babel%2Ftypes%2Fdownload%2F%40babel%2Ftypes-7.14.2.tgz#4208ae003107ef8a057ea8333e56eb64d2f6a2c3"
89+
integrity sha1-QgiuADEH74oFfqgzPlbrZNL2osM=
90+
dependencies:
91+
"@babel/helper-validator-identifier" "^7.14.0"
92+
to-fast-properties "^2.0.0"
93+
94+
ansi-styles@^3.2.1:
95+
version "3.2.1"
96+
resolved "https://registry.nlark.com/ansi-styles/download/ansi-styles-3.2.1.tgz?cache=0&sync_timestamp=1618995547052&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fansi-styles%2Fdownload%2Fansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
97+
integrity sha1-QfuyAkPlCxK+DwS43tvwdSDOhB0=
98+
dependencies:
99+
color-convert "^1.9.0"
100+
101+
chalk@^2.0.0:
102+
version "2.4.2"
103+
resolved "https://registry.nlark.com/chalk/download/chalk-2.4.2.tgz?cache=0&sync_timestamp=1618995355917&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fchalk%2Fdownload%2Fchalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
104+
integrity sha1-zUJUFnelQzPPVBpJEIwUMrRMlCQ=
105+
dependencies:
106+
ansi-styles "^3.2.1"
107+
escape-string-regexp "^1.0.5"
108+
supports-color "^5.3.0"
109+
110+
color-convert@^1.9.0:
111+
version "1.9.3"
112+
resolved "https://registry.npm.taobao.org/color-convert/download/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
113+
integrity sha1-u3GFBpDh8TZWfeYp0tVHHe2kweg=
114+
dependencies:
115+
color-name "1.1.3"
116+
117+
118+
version "1.1.3"
119+
resolved "https://registry.npm.taobao.org/color-name/download/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
120+
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=
121+
122+
debug@^4.1.0:
123+
version "4.3.1"
124+
resolved "https://registry.npm.taobao.org/debug/download/debug-4.3.1.tgz?cache=0&sync_timestamp=1607566580543&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdebug%2Fdownload%2Fdebug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"
125+
integrity sha1-8NIpxQXgxtjEmsVT0bE9wYP2su4=
126+
dependencies:
127+
ms "2.1.2"
128+
129+
escape-string-regexp@^1.0.5:
130+
version "1.0.5"
131+
resolved "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz?cache=0&sync_timestamp=1618677243201&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fescape-string-regexp%2Fdownload%2Fescape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
132+
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
133+
134+
globals@^11.1.0:
135+
version "11.12.0"
136+
resolved "https://registry.npm.taobao.org/globals/download/globals-11.12.0.tgz?cache=0&sync_timestamp=1617957623227&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fglobals%2Fdownload%2Fglobals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
137+
integrity sha1-q4eVM4hooLq9hSV1gBjCp+uVxC4=
138+
139+
has-flag@^3.0.0:
140+
version "3.0.0"
141+
resolved "https://registry.npm.taobao.org/has-flag/download/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
142+
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0=
143+
144+
js-tokens@^4.0.0:
145+
version "4.0.0"
146+
resolved "https://registry.nlark.com/js-tokens/download/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
147+
integrity sha1-GSA/tZmR35jjoocFDUZHzerzJJk=
148+
149+
jsesc@^2.5.1:
150+
version "2.5.2"
151+
resolved "https://registry.npm.taobao.org/jsesc/download/jsesc-2.5.2.tgz?cache=0&sync_timestamp=1603891224688&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fjsesc%2Fdownload%2Fjsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
152+
integrity sha1-gFZNLkg9rPbo7yCWUKZ98/DCg6Q=
153+
154+
155+
version "2.1.2"
156+
resolved "https://registry.npm.taobao.org/ms/download/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
157+
integrity sha1-0J0fNXtEP0kzgqjrPM0YOHKuYAk=
158+
159+
source-map@^0.5.0:
160+
version "0.5.7"
161+
resolved "https://registry.npm.taobao.org/source-map/download/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
162+
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
163+
164+
supports-color@^5.3.0:
165+
version "5.5.0"
166+
resolved "https://registry.npm.taobao.org/supports-color/download/supports-color-5.5.0.tgz?cache=0&sync_timestamp=1618560959124&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fsupports-color%2Fdownload%2Fsupports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
167+
integrity sha1-4uaaRKyHcveKHsCzW2id9lMO/I8=
168+
dependencies:
169+
has-flag "^3.0.0"
170+
171+
to-fast-properties@^2.0.0:
172+
version "2.0.0"
173+
resolved "https://registry.nlark.com/to-fast-properties/download/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"
174+
integrity sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4=

0 commit comments

Comments
 (0)