-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first class Javascript/Typescript support to the Mill build tool (#…
…4127) This pr implements the examples for jslib/module. #3927 Key Changes: - handle usage of resources files in bundled and non-bundled environments - handle custom-resources usage - Standard resource files in `@<module-name>/resources` or custom resources files in user defined custom resource path can be imported globally`@<module-name>/resources-directory` - allows for access to resources defined in dependent modules - allows for access to resources in bundled code and environment - allows for access to resources define in `/test` - updated build script to handle multiple resources directories Implements Task generatedSources - imported via `@generated/generated-file-name` Note: For `example/javascriptlib/module/3-override-tasks/`, the foo directory needs to exist, hence the file `foo/readme.md`, code sources are generated from its .mill file ### Checklist - [x] **example/jslib/module** - [x] common-config/ - [x] resources/ - [x] custom-tasks/ - [x] override-tasks/ - [x] compilation-execution-flags/ - [x] executable-config
- Loading branch information
1 parent
edc3b54
commit 168ceee
Showing
39 changed files
with
787 additions
and
181 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
|
||
= Building Javascript with Mill | ||
|
||
include::partial$gtag-config.adoc[] | ||
|
||
|
||
:language: Java | ||
:language-small: java | ||
:language: Typescript | ||
:language-small: typescript | ||
|
||
== Simple Typescript Module | ||
|
||
include::partial$example/javascriptlib/basic/1-simple.adoc[] | ||
|
||
== React Module | ||
|
||
include::partial$example/javascriptlib/basic/2-react.adoc[] | ||
|
||
== Custom Build Logic | ||
|
||
include::partial$example/javascriptlib/basic/3-custom-build-logic.adoc[] | ||
|
||
== Multi Module Project | ||
|
||
include::partial$example/javascriptlib/basic/4-multi-modules.adoc[] | ||
|
||
== Simple Client-Server | ||
|
||
include::partial$example/javascriptlib/basic/5-client-server-hello.adoc[] | ||
|
||
== Realistic Client-Server Example Project | ||
|
||
include::partial$example/javascriptlib/basic/6-client-server-realistic.adoc[] | ||
|
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,30 @@ | ||
= Typescript Module Configuration | ||
|
||
include::partial$gtag-config.adoc[] | ||
|
||
:language: Typescript | ||
:language-small: typescript | ||
|
||
== Common Configuration Overrides | ||
|
||
include::partial$example/javascriptlib/module/1-common-config.adoc[] | ||
|
||
== Custom Tasks | ||
|
||
include::partial$example/javascriptlib/module/2-custom-tasks.adoc[] | ||
|
||
== Overriding Tasks | ||
|
||
include::partial$example/javascriptlib/module/3-override-tasks.adoc[] | ||
|
||
== Compilation & Execution Flags | ||
|
||
include::partial$example/javascriptlib/module/4-compilation-execution-flags.adoc[] | ||
|
||
== Filesystem Resources | ||
|
||
include::partial$example/javascriptlib/module/5-resources.adoc[] | ||
|
||
== Bundling Configuration | ||
|
||
include::partial$example/javascriptlib/module/6-executable-config.adoc[] |
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,14 @@ | ||
= Testing Typescript Projects | ||
|
||
include::partial$gtag-config.adoc[] | ||
|
||
This page will discuss common topics around working with test suites using the Mill build tool | ||
|
||
== Defining Unit Test Suites | ||
|
||
include::partial$example/javascriptlib/testing/1-test-suite.adoc[] | ||
|
||
|
||
== Test Dependencies | ||
|
||
include::partial$example/javascriptlib/testing/2-test-deps.adoc[] |
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
35 changes: 14 additions & 21 deletions
35
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs/promises'; | ||
|
||
const Resources: string = process.env.RESOURCESDEST || "@foo/resources.dest" // `RESOURCES` is generated on bundle | ||
const LineCount = require.resolve(`${Resources}/line-count.txt`); | ||
|
||
export default class Foo { | ||
static getLineCount(resourcePath: string): string | null { | ||
try { | ||
const filePath = path.join(resourcePath, 'line-count.txt'); | ||
console.log('[Reading file:]', filePath); | ||
return fs.readFileSync(filePath, 'utf-8'); | ||
} catch (error) { | ||
console.error('Error reading file:', error); | ||
return null; | ||
} | ||
static async getLineCount(): Promise<string> { | ||
return await fs.readFile(LineCount, 'utf-8'); | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== "test") { | ||
let resourcePath = process.argv[2]; | ||
if (!resourcePath) resourcePath = process.env.RESOURCE_PATH; | ||
// no resource found, exit | ||
if (!resourcePath) { | ||
console.error('Error: No resource path provided.'); | ||
process.exit(1); | ||
} | ||
|
||
const lineCount = Foo.getLineCount(resourcePath); | ||
console.log('Line Count:', lineCount); | ||
(async () => { | ||
try { | ||
const lineCount = await Foo.getLineCount(); | ||
console.log('Line Count:', lineCount); | ||
} catch (err) { | ||
console.error('Error:', err); | ||
} | ||
})() | ||
} |
42 changes: 3 additions & 39 deletions
42
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,13 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import Foo from 'foo/foo'; | ||
|
||
// Mock the 'fs' module | ||
jest.mock('fs'); | ||
|
||
describe('Foo.getLineCount', () => { | ||
const mockResourcePath = path.join(__dirname, '../resources'); | ||
const mockFilePath = path.join(mockResourcePath, 'line-count.txt'); | ||
|
||
let consoleLogSpy: jest.SpyInstance; | ||
let consoleErrorSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
process.env.NODE_ENV = "test"; // Set NODE_ENV for all tests | ||
jest.clearAllMocks(); | ||
// Mock console.log and console.error | ||
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => { | ||
}); | ||
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleLogSpy.mockRestore(); | ||
consoleErrorSpy.mockRestore(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the content of the line-count.txt file', () => { | ||
const mockContent = '42'; | ||
jest.spyOn(fs, 'readFileSync').mockReturnValue(mockContent); | ||
|
||
const result = Foo.getLineCount(mockResourcePath); | ||
expect(result).toBe(mockContent); | ||
expect(fs.readFileSync).toHaveBeenCalledWith(mockFilePath, 'utf-8'); | ||
it('should return the content of the line-count.txt file', async () => { | ||
const expected: string = await Foo.getLineCount(); | ||
expect(expected).toBe("21"); | ||
}); | ||
|
||
it('should return null if the file cannot be read', () => { | ||
jest.spyOn(fs, 'readFileSync').mockImplementation(() => { | ||
throw new Error('File not found'); | ||
}); | ||
|
||
const result = Foo.getLineCount(mockResourcePath); | ||
expect(result).toBeNull(); | ||
expect(fs.readFileSync).toHaveBeenCalledWith(mockFilePath, 'utf-8'); | ||
}); | ||
}); |
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
File renamed without changes.
Oops, something went wrong.