-
-
Notifications
You must be signed in to change notification settings - Fork 121
start emitting declarations during the build - extracted from the JS files #417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -53,9 +53,8 @@ export default { | |||||||||
| // Ensure that .gjs files are properly integrated as Javascript | ||||||||||
| addon.gjs(), | ||||||||||
|
|
||||||||||
| // addons are allowed to contain imports of .css files, which we want rollup | ||||||||||
| // to leave alone and keep in the published output. | ||||||||||
| addon.keepAssets(['**/*.css']), | ||||||||||
| // Emit .d.ts declaration files | ||||||||||
| addon.declarations('declarations', 'tsc --declaration'), | ||||||||||
|
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Check if the file exists and examine it
find . -name "rollup.config.mjs" -type fRepository: adopted-ember-addons/ember-moment Length of output: 109 🏁 Script executed: # Read the rollup.config.mjs file to understand context
cat -n ember-moment/rollup.config.mjs 2>/dev/null || find . -name "rollup.config.mjs" -exec cat -n {} +Repository: adopted-ember-addons/ember-moment Length of output: 3043 🏁 Script executed: # Look for package.json to understand dependencies
find . -name "package.json" -type f | head -5Repository: adopted-ember-addons/ember-moment Length of output: 203 🏁 Script executed: # Check for tsconfig files
find . -name "tsconfig.json" -type fRepository: adopted-ember-addons/ember-moment Length of output: 105 🏁 Script executed: # Read the main tsconfig.json
cat -n tsconfig.json 2>/dev/null || cat -n ./ember-moment/tsconfig.json 2>/dev/null || find . -name "tsconfig.json" -exec cat -n {} +Repository: adopted-ember-addons/ember-moment Length of output: 897 🌐 Web query:
💡 Result: The TypeScript compiler options --emitDeclarationOnly, --declaration, and allowJs work together to generate declaration files (.d.ts) from JavaScript files without emitting JavaScript output. Key points: - emitDeclarationOnly: true (or --emitDeclarationOnly): Instructs the compiler to only emit .d.ts files, skipping .js files. Useful when using another transpiler for JS or providing types for consumers. - declaration: true (or --declaration): Enables generation of .d.ts files. Required when using emitDeclarationOnly. - allowJs: true (or --allowJs): Allows the compiler to process .js files as part of the program, enabling type checking and declaration generation from them. To generate .d.ts files from .js files: 1. Create a tsconfig.json: { "compilerOptions": { "allowJs": true, "declaration": true, "emitDeclarationOnly": true, "outDir": "dist" // Optional: directs output to a separate directory }, "include": ["src//.js"] } 2. Run tsc (uses tsconfig.json) or CLI: tsc src//.js --allowJs --declaration --emitDeclarationOnly --outDir dist. This setup processes .js files (thanks to allowJs), generates corresponding .d.ts files (via declaration + emitDeclarationOnly), and avoids overwriting original .js files (use outDir). Examples from docs: - CLI: tsc index.js --declaration --emitDeclarationOnly - For JS projects incrementally adopting TS. Note: Cannot combine emitDeclarationOnly with noEmit. For jsconfig.json projects, there may be compatibility issues with --emitDeclarationOnly. Official docs confirm this combination for creating .d.ts from .js files, especially with JSDoc for better types. Citations:
Add This build uses Rollup/Babel to emit JavaScript. When TypeScript generates declarations from JavaScript with Suggested change- addon.declarations('declarations', 'tsc --declaration'),
+ addon.declarations('declarations', 'tsc --declaration --emitDeclarationOnly'),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
| // Remove leftover build artifacts when starting a new build. | ||||||||||
| addon.clean(), | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "extends": "@ember/library-tsconfig", | ||
| "include": ["./src/**/*", "./unpublished-development-types/**/*"], | ||
| "compilerOptions": { | ||
| "allowJs": true, | ||
| "declarationDir": "declarations", | ||
|
|
||
| /** | ||
| https://www.typescriptlang.org/tsconfig#rootDir | ||
| "Default: The longest common path of all non-declaration input files." | ||
|
|
||
| Because we want our declarations' structure to match our rollup output, | ||
| we need this "rootDir" to match the "srcDir" in the rollup.config.mjs. | ||
|
|
||
| This way, we can have simpler `package.json#exports` that matches | ||
| imports to files on disk | ||
| */ | ||
| "rootDir": "./src", | ||
|
|
||
| "types": ["ember-source/types"] | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: adopted-ember-addons/ember-moment
Length of output: 203
🏁 Script executed:
Repository: adopted-ember-addons/ember-moment
Length of output: 1435
🏁 Script executed:
# Read the ember-moment/package.json file which is the one being reviewed cat -n ./ember-moment/package.jsonRepository: adopted-ember-addons/ember-moment
Length of output: 4987
🏁 Script executed:
# Check the tsconfig.json to understand declaration generation cat -n ./ember-moment/tsconfig.jsonRepository: adopted-ember-addons/ember-moment
Length of output: 897
🏁 Script executed:
Repository: adopted-ember-addons/ember-moment
Length of output: 132
🏁 Script executed:
Repository: adopted-ember-addons/ember-moment
Length of output: 3043
🏁 Script executed:
Repository: adopted-ember-addons/ember-moment
Length of output: 415
🏁 Script executed:
Repository: adopted-ember-addons/ember-moment
Length of output: 918
🌐 Web query:
TypeScript package.json exports longest prefix pattern matching module resolution💡 Result:
TypeScript's module resolution for package.json "exports" field uses longest prefix pattern matching, analogous to its "paths" mapping resolution. In TypeScript's module resolution (under moduleResolution: "node16", "nodenext", or "bundler"), when resolving an import specifier against a package's "exports" map, it selects the export key with the longest matching prefix for the subpath. This matches Node.js's PACKAGE_EXPORTS_RESOLVE algorithm, which iterates over exports keys (in package.json object order) and uses the first (longest-matching) subpath pattern or exact match. For subpath patterns (keys ending in ""), matching replaces "" with the corresponding suffix from the requested subpath. TypeScript prioritizes "exports" over legacy fields like "typesVersions", "types", and "main" when present. To enable: Use "moduleResolution": "node16"/"nodenext"/"bundler" and "resolvePackageJsonExports": true (default in those modes). Example package.json: { "exports": { ".": "./index.js", "./foo": "./foo.js", "./bar/": "./bar/.js" } } Import "pkg/bar/baz" matches "./bar/*" (longest prefix "./bar/" > "."). This prevents access to unexported subpaths, enforcing encapsulation.
Citations:
Add explicit
./_app_/*export to prevent false type advertisements.The wildcard
./*export pattern matches_app_/*paths (fromember-addon.app-js), but TypeScript's longest-prefix matching means if./_app_/*is defined, it takes precedence. Currently, the./*pattern advertisesdeclarations/_app_/**.d.tstypes that won't be generated—the build only creates declarations fromsrc/(via tsconfig.json'srootDir: "./src"), not from the_app_/build artifact. Add an explicit JS-only./_app_/*export before./*to carve those paths out cleanly and prevent false type resolution.📦 Suggested change
"exports": { ".": { "types": "./declarations/index.d.ts", "default": "./dist/index.js" }, "./addon-main.js": "./addon-main.cjs", + "./_app_/*": "./dist/_app_/*.js", "./*": { "types": "./declarations/*.d.ts", "default": "./dist/*.js" } },📝 Committable suggestion
🤖 Prompt for AI Agents