Skip to content

Commit 9ef5727

Browse files
authored
Add first class Javascript/Typescript support to the Mill build tool #4127 (#4187)
This pr implements the examples for jslib/dependencies. #3927 Checklist: - [x] **example/jslib/dependencies** - [x] 1-npm-deps/ - [x] 2-unmanaged-packages/ - [x] 3-downloading-unmanaged-packages/ - [x] 4-repository-config/: examples of how to use alternate NPM mirrors or repositories
1 parent 3c246f7 commit 9ef5727

File tree

13 files changed

+168
-1
lines changed

13 files changed

+168
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
= Typescript Library Dependencies
2+
3+
include::partial$gtag-config.adoc[]
4+
5+
This page goes into more detail about configuring third party dependencies
6+
for `TypescriptModule`s.
7+
8+
== Adding Dependencies
9+
10+
include::partial$example/javascriptlib/dependencies/1-npm-deps.adoc[]
11+
12+
== Unmanaged Packages
13+
14+
include::partial$example/javascriptlib/dependencies/2-unmanaged-packages.adoc[]
15+
16+
== Downloading Unmanaged Packages
17+
18+
include::partial$example/javascriptlib/dependencies/3-downloading-unmanaged-packages.adoc[]
19+
20+
== Using Custom or Private Registries
21+
22+
include::partial$example/javascriptlib/dependencies/4-repository-config.adoc[]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package build
2+
import mill._, javascriptlib._
3+
4+
object foo extends TypeScriptModule {
5+
def npmDeps = Seq("[email protected]")
6+
def npmDevDeps = Seq("@types/[email protected]")
7+
}
8+
9+
// You can define `npmDeps` field to add dependencies to your module and
10+
// define `npmDevDeps` field to add dev dependencies to your module, which will be installed
11+
// via configured registry.
12+
13+
/** Usage
14+
> mill foo.run e b k a l m o p
15+
Sorted with lodash: [a,b,e,k,l,m,o,p]
16+
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {sortBy} from 'node_modules/lodash'
2+
3+
const args = process.argv.slice(2);
4+
console.log(`Sorted with lodash: [${sortBy(args).join(",")}]`);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package build
2+
import mill._, javascriptlib._
3+
4+
object foo extends TypeScriptModule {
5+
def unmanagedDeps = Seq.from(os.list(millSourcePath / "lib").map(PathRef(_)))
6+
}
7+
8+
// You can override `unmanagedDeps` to point to a
9+
// source distribution .tar.gz you place on the
10+
// filesystem, e.g. in the above snippet any files that happen to live in the
11+
// `lib/` folder.
12+
13+
// Note:
14+
// When installing from a .tgz file, Node.js and npm determine the package name
15+
// and how to import it from the package.json file inside the .tgz archive.
16+
17+
// For more info on `package.json`, see: https://docs.npmjs.com/cli/v10/configuring-npm/package-json?v=true&utm_source=chatgpt.com
18+
// For more info on creating unmanaged dependecies, see: https://docs.npmjs.com/cli/v10/commands/npm-pack
19+
20+
/** Usage
21+
> mill foo.run e b k a l m o p
22+
Sorted with lodash: [a,b,e,k,l,m,o,p]
23+
*/
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {sortBy} from 'node_modules/lodash'
2+
3+
const args = process.argv.slice(2);
4+
console.log(`Sorted with lodash: [${sortBy(args).join(",")}]`);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// You can also override `unmanagedDeps` to point it at wheels that you want to
2+
// download from arbitrary URLs.
3+
// `requests.get` comes from the https://github.com/com-lihaoyi/requests-scala[Requests-Scala]
4+
// library, one of Mill's xref:fundamentals/bundled-libraries.adoc[Bundled Libraries].
5+
//
6+
package build
7+
import mill._, javascriptlib._
8+
9+
object foo extends TypeScriptModule {
10+
def unmanagedDeps = Task {
11+
val name = "lodash-4.17.21.tgz"
12+
val url = "https://github.com/lodash/lodash/archive/refs/tags/4.17.21.tar.gz"
13+
os.write(Task.dest / name, requests.get.stream(url))
14+
Seq(PathRef(Task.dest / name))
15+
}
16+
}
17+
18+
/** Usage
19+
> mill foo.run e b k a l m o p
20+
Sorted with lodash: [a,b,e,k,l,m,o,p]
21+
*/
22+
23+
// Tasks like `unmanagedDeps` and `npmDeps` are cached, so your package is downloaded only
24+
// once and re-used indefinitely after that. This is usually not a problem, because usually URLs
25+
// follow the rule that https://www.w3.org/Provider/Style/URI[Cool URIs don't change], and so files
26+
// downloaded from the same URL will always contain the same contents.
27+
28+
// NOTE: An unmanaged depedencies downloaded via `requests.get` is still unmanaged: even though you
29+
// downloaded it from somewhere, `requests.get` does not know how to pull in third party
30+
// dependencies or de-duplicate different versions.
31+
32+
// In case you **do** want mill to take care of managing dependencies of a package,
33+
// you shouldn't get that package in `unmanagedDeps` (like we did in the
34+
// example above). Instead, you can declare the dependency as a regular `npmDeps` and `npmDevDeps`
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {sortBy} from 'node_modules/lodash'
2+
3+
const args = process.argv.slice(2);
4+
console.log(`Sorted with lodash: [${sortBy(args).join(",")}]`);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
registry=https://registry.npmmirror.com
2+
3+
# Use scoped private/alternative & set authentication
4+
# @private/package-name:registry=private.repo.com/:_authToken=your-auth-token
5+
6+
# Optional: on private/alternative registry
7+
# .../:_authToken=your-auth-token
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package build
2+
import mill._, javascriptlib._
3+
4+
object foo extends TypeScriptModule {
5+
def npmDeps = Seq("[email protected]")
6+
def npmDevDeps = Seq("@types/[email protected]")
7+
}
8+
9+
// Mill uses https://registry.npmjs.org to find and install dependencies.
10+
//
11+
// === Alternate/Private registries
12+
//
13+
// You can configure registries via a `.npmrc` file in project root.
14+
// For more information on npmrc see: https://docs.npmjs.com/cli/v9/configuring-npm/npmrc.
15+
// The example below uses the alternate registry https://registry.npmmirror.com
16+
17+
/** Usage
18+
19+
> mill foo.run e b k a l m o p
20+
Sorted with lodash: [a,b,e,k,l,m,o,p]
21+
....
22+
{
23+
version: '4.17.21',
24+
resolved: 'https://registry.npmmirror.com/lodash/-/lodash-4.17.21.tgz',
25+
integrity: ...,
26+
...
27+
}
28+
29+
> cd out/foo/npmInstall.dest && npm config get registry
30+
https://registry.npmmirror.com
31+
*/

0 commit comments

Comments
 (0)