-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The goal of the pr is to allow existing Ts projects run with mill, it takes into considerations the fluidity of Ts project structure and removes the reliance on `ts-paths` as the only means to access `module` dependencies. This pr refactors `TscModule ` formerly (`TypeScriptModule `), it aims to introduce the features: - [x] `package` RootModule with TscModule - [x] relative paths - [x] esm support; use swc-core for compiling Key changes: - `compile` task has been modified, these modifications: - change how sources, mod-sources, resources and generated sources are grouped, allowing for the use of relative paths - these changes also taking into consideration `RootModule` use, the top level src feature now works along side `TscModule` - Due to this restructuring, implementation of `PublishModule` has been significantly altered as `TscModule` compilations output now prepares source files internal and external (source files generated via a task that would end up in the `out/`) in a manner that nodes `npm` publishing tool will understand and work with. - The restructuring also consequentially fixes the need for `TscModule` to violate the new `writing to path during execution phase` restrictions. As the previous implementation would require files (mostly test config files and build scripts) to be created within or moved to `compile.dest` during execution phases. - It also provides minimal support of esm. Additional examples - [x] `module/7-root-module` for RootModule base pr for #4425 @lihaoyi --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3f0e0e3
commit abdee90
Showing
24 changed files
with
890 additions
and
592 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/javascriptlib/basic/3-custom-build-logic/foo/src/foo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/javascriptlib/basic/3-custom-build-logic/foo/test/src/foo/foo.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
example/javascriptlib/basic/6-client-server-realistic/server/src/server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ if (process.env.NODE_ENV !== "test") { | |
console.error('Error:', err); | ||
} | ||
})(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// You can use ``object `package` extends RootModule`` to use a `Module` | ||
// as the root module of the file: | ||
|
||
package build | ||
|
||
import mill._, javascriptlib._ | ||
|
||
object `package` extends RootModule with TypeScriptModule { | ||
def moduleName = "foo" | ||
def npmDeps = Seq("[email protected]") | ||
object test extends TypeScriptTests with TestModule.Jest | ||
} | ||
|
||
// Since our ``object `package` extends RootModule``, its files live in a | ||
// top-level `src/` folder. | ||
|
||
// Mill will ordinarily use the name of the singleton object, as | ||
// the default value for the `moduleName` task. | ||
|
||
// For exmaple: | ||
|
||
// The `moduleName` for the singleton `bar` defined as | ||
// `object bar extends TypeScriptModule` would be `bar`, | ||
// with the expected source directory in `bar/src`. | ||
|
||
// For this example, since we use the `RootModule` we would need to manually define our | ||
// `moduleName`. | ||
// | ||
// The `moduleName` is used in the generated `tsconfig` files `compilerOptions['paths']` | ||
// as the modules `src/` path mapping and to define the default `mainFileName`, which is | ||
// the modules entry file. | ||
// The generated mapping for this example would be `"foo/*": [...]`. and its expected main file | ||
// woudld be `foo.ts` located in top-level `src/` | ||
|
||
/** Usage | ||
> mill run James Bond prof | ||
Hello James Bond Professor | ||
|
||
> mill test | ||
PASS .../foo.test.ts | ||
... | ||
Test Suites:...1 passed, 1 total... | ||
Tests:...3 passed, 3 total... | ||
... | ||
|
||
> mill show bundle | ||
Build succeeded! | ||
|
||
> node out/bundle.dest/bundle.js James Bond prof | ||
Hello James Bond Professor | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {Map} from 'immutable'; | ||
|
||
interface User { | ||
firstName: string | ||
lastName: string | ||
role: string | ||
} | ||
|
||
export const defaultRoles: Map<string, string> = Map({prof: "Professor"}); | ||
|
||
/** | ||
* Generate a user object based on command-line arguments | ||
* @param args Command-line arguments | ||
* @returns User object | ||
*/ | ||
export function generateUser(args: string[]): User { | ||
return { | ||
firstName: args[0] || "unknown", | ||
lastName: args[1] || "unknown", | ||
role: defaultRoles.get(args[2], ""), | ||
}; | ||
} | ||
|
||
// Main CLI logic | ||
if (process.env.NODE_ENV !== "test") { | ||
const args = process.argv.slice(2); // Skip 'node' and script name | ||
const user = generateUser(args); | ||
|
||
console.log(defaultRoles.toObject()); | ||
console.log(args[2]); | ||
console.log(defaultRoles.get(args[2])); | ||
console.log("Hello " + user.firstName + " " + user.lastName + " " + user.role); | ||
} |
64 changes: 64 additions & 0 deletions
64
example/javascriptlib/module/7-root-module/test/src/foo.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {generateUser, defaultRoles} from "foo/foo"; | ||
import {Map} from 'immutable'; | ||
|
||
// Define the type roles object | ||
type RoleKeys = "admin" | "user"; | ||
type Roles = { | ||
[key in RoleKeys]: string; | ||
}; | ||
|
||
// Mock `defaultRoles` as a global variable for testing | ||
const mockDefaultRoles = Map<string, string>({ | ||
admin: "Administrator", | ||
user: "User", | ||
}); | ||
|
||
describe("generateUser function", () => { | ||
beforeAll(() => { | ||
process.env.NODE_ENV = "test"; // Set NODE_ENV for all tests | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should generate a user with all specified fields", () => { | ||
// Override the `defaultRoles` map for testing | ||
(defaultRoles as any).get = mockDefaultRoles.get.bind(mockDefaultRoles); | ||
|
||
const args = ["John", "Doe", "admin"]; | ||
const user = generateUser(args); | ||
|
||
expect(user).toEqual({ | ||
firstName: "John", | ||
lastName: "Doe", | ||
role: "Administrator", | ||
}); | ||
}); | ||
|
||
test("should default lastName and role when they are not provided", () => { | ||
(defaultRoles as any).get = mockDefaultRoles.get.bind(mockDefaultRoles); | ||
|
||
const args = ["Jane"]; | ||
const user = generateUser(args); | ||
|
||
expect(user).toEqual({ | ||
firstName: "Jane", | ||
lastName: "unknown", | ||
role: "", | ||
}); | ||
}); | ||
|
||
test("should default all fields when args is empty", () => { | ||
(defaultRoles as any).get = mockDefaultRoles.get.bind(mockDefaultRoles); | ||
|
||
const args: string[] = []; | ||
const user = generateUser(args); | ||
|
||
expect(user).toEqual({ | ||
firstName: "unknown", | ||
lastName: "unknown", | ||
role: "", | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
example/javascriptlib/testing/1-test-suite/bar/test/src/bar/calculator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/javascriptlib/testing/1-test-suite/baz/test/src/baz/calculator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/javascriptlib/testing/3-integration-suite-cypress/server/src/server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
example/javascriptlib/testing/3-integration-suite-playwright/server/src/server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.