Skip to content

Commit 04beedf

Browse files
committed
Specify which files are included when publishing
1 parent c0e28b7 commit 04beedf

File tree

1 file changed

+266
-28
lines changed

1 file changed

+266
-28
lines changed

writing-npm-libraries-using-typescript/Lesson.ipynb

Lines changed: 266 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
},
120120
{
121121
"cell_type": "code",
122-
"execution_count": 6,
122+
"execution_count": 49,
123123
"metadata": {},
124124
"outputs": [
125125
{
@@ -132,33 +132,22 @@
132132
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0G\\ |---------------------------------------------------------------------------|\n",
133133
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0G| |---------------------------------------------------------------------------|\n",
134134
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0GloadRequestedDeps -> get | |##########---------------------------------------|\n",
135-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0GloadRequestedDeps -> addN \\ |##########---------------------------------------|\n",
136-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0GloadRequestedDeps -> afte - |##########---------------------------------------|\n",
137-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0GloadRequestedDeps - |###################------------------------------|\n",
138-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0Gextract -> gunzTarPerm | |##################################---------------|\n",
139-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0Gextract -> gunzTarPerm | |##################################---------------|\n",
140-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0Gextract -> gunzTarPerm / |##################################---------------|\n",
141-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0Gextract -> gunzTarPerm \\ |##################################---------------|\n",
142-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0Gextract -> gunzTarPerm / |##################################---------------|\n",
143-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0Gextract -> gunzTarPerm \\ |##################################---------------|\n",
144-
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0Gextract -> gunzTarPerm / |##################################---------------|\n",
145-
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[?25l\u001b[0GrunTopLevelLifecycles / |###############################################--|\n",
146-
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[Kmy-amazing-package@0.0.1 /home/jupyter/notebooks/my-amazing-package\n",
147-
"`-- \u001b[40m\u001b[33mtypescript@3.5.3\u001b[0m\u001b[0m \n",
148-
"\n",
149-
"\u001b[?25l\u001b[0GrunTopLevelLifecycles / |###############################################--|\n",
135+
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0GloadRequestedDeps -> addN | |##########---------------------------------------|\n",
136+
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0GloadRequestedDeps -> afte \\ |##########---------------------------------------|\n",
137+
"\u001b[?25h\u001b[1A\u001b[?25l\u001b[0GloadRequestedDeps \\ |###################------------------------------|\n",
138+
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[?25l\u001b[0GrunTopLevelLifecycles - |###############################################--|\n",
150139
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[37m\u001b[40mnpm\u001b[0m \u001b[0m\u001b[30m\u001b[43mWARN\u001b[0m\u001b[35m\u001b[0m my-amazing-package@0.0.1 No description\n",
151-
"\u001b[0m\u001b[?25l\u001b[0GrunTopLevelLifecycles - |###############################################--|\n",
152-
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[37m\u001b[40mnpm\u001b[0m \u001b[0m\u001b[30m\u001b[43mWARN\u001b[0m\u001b[35m\u001b[0m my-amazing-package@0.0.1 No repository field.\n",
153140
"\u001b[0m\u001b[?25l\u001b[0GrunTopLevelLifecycles \\ |###############################################--|\n",
154-
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[37m\u001b[40mnpm\u001b[0m \u001b[0m\u001b[30m\u001b[43mWARN\u001b[0m\u001b[35m\u001b[0m my-amazing-package@0.0.1 No license field.\n",
141+
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[37m\u001b[40mnpm\u001b[0m \u001b[0m\u001b[30m\u001b[43mWARN\u001b[0m\u001b[35m\u001b[0m my-amazing-package@0.0.1 No repository field.\n",
155142
"\u001b[0m\u001b[?25l\u001b[0GrunTopLevelLifecycles | |###############################################--|\n",
143+
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[37m\u001b[40mnpm\u001b[0m \u001b[0m\u001b[30m\u001b[43mWARN\u001b[0m\u001b[35m\u001b[0m my-amazing-package@0.0.1 No license field.\n",
144+
"\u001b[0m\u001b[?25l\u001b[0GrunTopLevelLifecycles / |###############################################--|\n",
156145
"\u001b[?25h\u001b[?25h\u001b[1A\u001b[0G\u001b[K\u001b[?25h\u001b[0G\u001b[K"
157146
]
158147
}
159148
],
160149
"source": [
161-
"npm install --save typescript"
150+
"npm install --save-dev typescript"
162151
]
163152
},
164153
{
@@ -170,7 +159,7 @@
170159
},
171160
{
172161
"cell_type": "code",
173-
"execution_count": 7,
162+
"execution_count": 50,
174163
"metadata": {},
175164
"outputs": [
176165
{
@@ -182,6 +171,12 @@
182171
" \"version\": \"0.0.1\",\n",
183172
" \"dependencies\": {\n",
184173
" \"typescript\": \"^3.5.3\"\n",
174+
" },\n",
175+
" \"scripts\": {\n",
176+
" \"tsc\": \"tsc\"\n",
177+
" },\n",
178+
" \"devDependencies\": {\n",
179+
" \"typescript\": \"^3.5.3\"\n",
185180
" }\n",
186181
"}\n"
187182
]
@@ -236,35 +231,35 @@
236231
"Create the file `.gitignore` with the following content:\n",
237232
"\n",
238233
"```\n",
239-
"node_modules\n",
234+
"node_modules/\n",
240235
"npm-debug.log\n",
241236
"```\n"
242237
]
243238
},
244239
{
245240
"cell_type": "code",
246-
"execution_count": 9,
241+
"execution_count": 35,
247242
"metadata": {},
248243
"outputs": [],
249244
"source": [
250245
"# Write file\n",
251246
"cat > .gitignore << 'EndOfFile'\n",
252-
"node_modules\n",
253-
"npm-debug.log\n",
247+
"/node_modules/\n",
248+
"/npm-debug.log\n",
254249
"EndOfFile"
255250
]
256251
},
257252
{
258253
"cell_type": "code",
259-
"execution_count": 10,
254+
"execution_count": 36,
260255
"metadata": {},
261256
"outputs": [
262257
{
263258
"name": "stdout",
264259
"output_type": "stream",
265260
"text": [
266-
"node_modules\n",
267-
"npm-debug.log\n"
261+
"/node_modules/\n",
262+
"/npm-debug.log\n"
268263
]
269264
}
270265
],
@@ -1028,6 +1023,249 @@
10281023
"cat lib/index.js"
10291024
]
10301025
},
1026+
{
1027+
"cell_type": "markdown",
1028+
"metadata": {},
1029+
"source": [
1030+
"Let's see what the state of our git tree is:"
1031+
]
1032+
},
1033+
{
1034+
"cell_type": "code",
1035+
"execution_count": 33,
1036+
"metadata": {},
1037+
"outputs": [
1038+
{
1039+
"name": "stdout",
1040+
"output_type": "stream",
1041+
"text": [
1042+
"On branch master\n",
1043+
"Untracked files:\n",
1044+
" (use \"git add <file>...\" to include in what will be committed)\n",
1045+
"\n",
1046+
"\t\u001b[31mlib/\u001b[m\n",
1047+
"\t\u001b[31msrc/\u001b[m\n",
1048+
"\n",
1049+
"nothing added to commit but untracked files present (use \"git add\" to track)\n"
1050+
]
1051+
}
1052+
],
1053+
"source": [
1054+
"git status"
1055+
]
1056+
},
1057+
{
1058+
"cell_type": "markdown",
1059+
"metadata": {},
1060+
"source": [
1061+
"You'll see that both `src/` and `lib/` are listed there as untracked files\n",
1062+
"that we could potentially check-in to our repository.\n",
1063+
"But remember earlier that we said that it's preferable to make sure\n",
1064+
"that compiler output is not checked-in to a repository.\n",
1065+
"\n",
1066+
"So let's add `/lib/` to `.gitignore`:"
1067+
]
1068+
},
1069+
{
1070+
"cell_type": "code",
1071+
"execution_count": 40,
1072+
"metadata": {},
1073+
"outputs": [],
1074+
"source": [
1075+
"echo /lib/ >> .gitignore"
1076+
]
1077+
},
1078+
{
1079+
"cell_type": "code",
1080+
"execution_count": 41,
1081+
"metadata": {},
1082+
"outputs": [
1083+
{
1084+
"name": "stdout",
1085+
"output_type": "stream",
1086+
"text": [
1087+
"/node_modules/\n",
1088+
"/npm-debug.log\n",
1089+
"/lib/\n"
1090+
]
1091+
}
1092+
],
1093+
"source": [
1094+
"cat .gitignore"
1095+
]
1096+
},
1097+
{
1098+
"cell_type": "code",
1099+
"execution_count": 44,
1100+
"metadata": {},
1101+
"outputs": [
1102+
{
1103+
"name": "stdout",
1104+
"output_type": "stream",
1105+
"text": [
1106+
"On branch master\n",
1107+
"Changes not staged for commit:\n",
1108+
" (use \"git add <file>...\" to update what will be committed)\n",
1109+
" (use \"git checkout -- <file>...\" to discard changes in working directory)\n",
1110+
"\n",
1111+
"\t\u001b[31mmodified: .gitignore\u001b[m\n",
1112+
"\n",
1113+
"Untracked files:\n",
1114+
" (use \"git add <file>...\" to include in what will be committed)\n",
1115+
"\n",
1116+
"\t\u001b[31msrc/\u001b[m\n",
1117+
"\n",
1118+
"no changes added to commit (use \"git add\" and/or \"git commit -a\")\n"
1119+
]
1120+
}
1121+
],
1122+
"source": [
1123+
"git status"
1124+
]
1125+
},
1126+
{
1127+
"cell_type": "markdown",
1128+
"metadata": {},
1129+
"source": [
1130+
"And then let's commit our changes to git."
1131+
]
1132+
},
1133+
{
1134+
"cell_type": "code",
1135+
"execution_count": 46,
1136+
"metadata": {},
1137+
"outputs": [
1138+
{
1139+
"name": "stdout",
1140+
"output_type": "stream",
1141+
"text": [
1142+
"[master bdf3a8a] Add some typescript code\n",
1143+
" 3 files changed, 9 insertions(+), 2 deletions(-)\n",
1144+
" create mode 100644 src/.ipynb_checkpoints/index-checkpoint.ts\n",
1145+
" create mode 100644 src/index.ts\n"
1146+
]
1147+
}
1148+
],
1149+
"source": [
1150+
"git add .; git commit -m \"Add some typescript code\""
1151+
]
1152+
},
1153+
{
1154+
"cell_type": "code",
1155+
"execution_count": 47,
1156+
"metadata": {},
1157+
"outputs": [
1158+
{
1159+
"name": "stdout",
1160+
"output_type": "stream",
1161+
"text": [
1162+
"On branch master\n",
1163+
"nothing to commit, working tree clean\n"
1164+
]
1165+
}
1166+
],
1167+
"source": [
1168+
"git status"
1169+
]
1170+
},
1171+
{
1172+
"cell_type": "markdown",
1173+
"metadata": {},
1174+
"source": [
1175+
"## Publishing our NPM package\n",
1176+
"\n",
1177+
"We're almost ready to publish our package to NPM, there are just a couple of things remaining:\n",
1178+
"\n",
1179+
"* We need to specify which files get included in our package\n",
1180+
"* We need to specify our `main` module.\n",
1181+
"\n",
1182+
"### Specifying which files are included when publishing\n",
1183+
"\n",
1184+
"By default, running `npm publish` will package up every file that's included in your repository\n",
1185+
"([except for a few special-cases](https://docs.npmjs.com/files/package.json#files)).\n",
1186+
"This is undesireable as this means that a lot of useless / uneeded files will often be included,\n",
1187+
"needlessly increasing the size of your published package,\n",
1188+
"and taking up space in `node_modules` for any project that uses your package as a dependency.\n",
1189+
"\n",
1190+
"It's in your user's best interest that you keep this as small as possible.\n",
1191+
"\n",
1192+
"<img src=\"https://turnoff.us/image/en/npm-install.png\" width=\"400\"/>\n",
1193+
"\n",
1194+
"*Sidenote: There's a great project called [packagephobia](https://packagephobia.now.sh/) that\n",
1195+
"will calculate the effect that installing an npm package will have on your disk-space.*\n",
1196+
"\n",
1197+
"There are two primary ways of telling NPM which files to include:\n",
1198+
"\n",
1199+
"1. blacklisting files using `.npmignore`\n",
1200+
"2. whitelisting files using the `\"files\"` property in `package.json`\n",
1201+
"\n",
1202+
"We're going to use the second option,\n",
1203+
"as with a whitelist,\n",
1204+
"we're much less likely to accidentally include new files that are useless.\n",
1205+
"(for example, docs or files & images that are part of a static site for our project).\n",
1206+
"\n",
1207+
"We only need to explicitly include the files from `lib/`,\n",
1208+
"as standard files like `package.json` and `README` or `README.md` are always included\n",
1209+
"regardless of settings.\n",
1210+
"\n",
1211+
"So let's add the following to our `package.json`:\n",
1212+
"\n",
1213+
"```json\n",
1214+
"\"files\": [\n",
1215+
" \"lib/**/*\"\n",
1216+
"]\n",
1217+
"```"
1218+
]
1219+
},
1220+
{
1221+
"cell_type": "code",
1222+
"execution_count": 51,
1223+
"metadata": {},
1224+
"outputs": [
1225+
{
1226+
"name": "stdout",
1227+
"output_type": "stream",
1228+
"text": [
1229+
"json: updated \"package.json\" in-place\n"
1230+
]
1231+
}
1232+
],
1233+
"source": [
1234+
"json -I -f package.json -e 'this.files=[\"lib/**/*\"]'"
1235+
]
1236+
},
1237+
{
1238+
"cell_type": "code",
1239+
"execution_count": 52,
1240+
"metadata": {},
1241+
"outputs": [
1242+
{
1243+
"name": "stdout",
1244+
"output_type": "stream",
1245+
"text": [
1246+
"{\n",
1247+
" \"name\": \"my-amazing-package\",\n",
1248+
" \"version\": \"0.0.1\",\n",
1249+
" \"dependencies\": {\n",
1250+
" \"typescript\": \"^3.5.3\"\n",
1251+
" },\n",
1252+
" \"scripts\": {\n",
1253+
" \"tsc\": \"tsc\"\n",
1254+
" },\n",
1255+
" \"devDependencies\": {\n",
1256+
" \"typescript\": \"^3.5.3\"\n",
1257+
" },\n",
1258+
" \"files\": [\n",
1259+
" \"lib/**/*\"\n",
1260+
" ]\n",
1261+
"}\n"
1262+
]
1263+
}
1264+
],
1265+
"source": [
1266+
"cat package.json"
1267+
]
1268+
},
10311269
{
10321270
"cell_type": "code",
10331271
"execution_count": null,

0 commit comments

Comments
 (0)