Provide a way to add the '.js' file extension to the end of module specifiers
Problem
In order to use es6 modules in the browser, you need a .js file extension. However output doesn't add it. In ts: `import { ModalBackground } from './ModalBackground';` In ES2015 output: `import { ModalBackground } from './ModalBackground';` Ideally I would like this to be output `import { ModalBackground } from './ModalBackground.js';` That way I can use the output in Chrome 51 [code block] Related to https://github.com/Microsoft/TypeScript/issues/13422
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Add .js Extension to ES6 Module Specifiers in TypeScript Output
TypeScript does not automatically append the .js file extension to module specifiers in ES6 output, which is required for proper module resolution in browsers. This behavior is due to the TypeScript compiler's handling of module paths, which does not include file extensions by default in the generated JavaScript.
Awaiting Verification
Be the first to verify this fix
- 1
Update TypeScript Configuration
Modify the TypeScript configuration file (tsconfig.json) to include a custom module resolution strategy that appends the .js extension to module specifiers.
bash[object Object] - 2
Create a Custom Build Script
Implement a build script that processes the output JavaScript files to append the .js extension to all module specifiers. This can be done using a simple Node.js script that reads the output files and modifies the import statements.
bash[object Object] - 3
Run the Build Process
Execute the TypeScript compiler followed by the custom build script to generate the output files with the correct .js extensions in module specifiers.
bash[object Object] - 4
Test the Output in Browser
Open the generated JavaScript files in a browser (e.g., Chrome 51) to verify that the module specifiers now include the .js extension and that the modules load correctly without errors.
bashN/A
Validation
Open the browser's developer tools and check the console for any module loading errors. Ensure that all modules are correctly loaded without 404 errors related to missing .js extensions.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep