-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: use require to load esm #5366
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
| const obj = { foo: 'bar' }; | ||
|
|
||
| describe('cjs written in esm', () => { | ||
| it('should work', () => { | ||
| expect(obj, 'to equal', { foo: 'bar' }); | ||
| }); | ||
| }); | ||
|
|
||
| export const foo = 'bar'; |
This file contains hidden or 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,9 @@ | ||
| const obj = { foo: 'bar' }; | ||
|
|
||
| describe('cjs written in esm', () => { | ||
| it('should work', () => { | ||
| expect(obj, 'to equal', { foo: 'bar' }); | ||
| }); | ||
| }); | ||
|
|
||
| module.exports.foo = 'bar'; |
This file contains hidden or 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,9 @@ | ||
| const obj: unknown = { foo: 'bar' }; | ||
|
|
||
| describe('cts written in esm', () => { | ||
| it('should work', () => { | ||
| expect(obj, 'to equal', { foo: 'bar' }); | ||
| }); | ||
| }); | ||
|
|
||
| export const foo = 'bar'; |
This file contains hidden or 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,9 @@ | ||
| const obj = { foo: 'bar' }; | ||
|
|
||
| describe('cts written in esm', () => { | ||
| it('should work', () => { | ||
| expect(obj, 'to equal', { foo: 'bar' }); | ||
| }); | ||
| }); | ||
|
|
||
| module.exports.foo = 'bar'; |
This file contains hidden or 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,12 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
|
|
||
| const original = require.extensions['.js']; | ||
| require.extensions['.js'] = function (module, filename) { | ||
| if (!filename.includes('compiler-cjs')) { | ||
| return original(module, filename); | ||
| } | ||
| const content = fs.readFileSync(filename + '.compiled', 'utf8'); | ||
| return module._compile(content, filename); | ||
| }; |
This file contains hidden or 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,8 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
|
|
||
| require.extensions['.ts'] = function (module, filename) { | ||
| const content = fs.readFileSync(filename + '.compiled', 'utf8'); | ||
| return module._compile(content, filename); | ||
| }; |
This file contains hidden or 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 @@ | ||
| 'use strict'; | ||
|
|
||
| var exec = require('node:child_process').exec; | ||
| var path = require('node:path'); | ||
|
|
||
| describe('support CJS require.extension compilers with esm syntax', function () { | ||
| it('should support .js extension', function (done) { | ||
| exec( | ||
| '"' + | ||
| process.execPath + | ||
| '" "' + | ||
| path.join('bin', 'mocha') + | ||
| '" -R json --require test/compiler-fixtures/js.fixture "test/compiler-cjs/*.js"', | ||
| {cwd: path.join(__dirname, '..', '..')}, | ||
| function (error, stdout) { | ||
| if (error && !stdout) { | ||
| return done(error); | ||
| } | ||
| var results = JSON.parse(stdout); | ||
| expect(results, 'to have property', 'tests'); | ||
| var titles = []; | ||
| for (var index = 0; index < results.tests.length; index += 1) { | ||
| expect(results.tests[index], 'to have property', 'fullTitle'); | ||
| titles.push(results.tests[index].fullTitle); | ||
| } | ||
| expect( | ||
| titles, | ||
| 'to contain', | ||
| 'cjs written in esm should work', | ||
| ).and('to have length', 1); | ||
| done(); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| it('should support .ts extension', function (done) { | ||
| exec( | ||
| '"' + | ||
| process.execPath + | ||
| '" "' + | ||
| path.join('bin', 'mocha') + | ||
| '" -R json --require test/compiler-fixtures/ts.fixture "test/compiler-cjs/*.ts"', | ||
| {cwd: path.join(__dirname, '..', '..')}, | ||
| function (error, stdout) { | ||
| if (error && !stdout) { | ||
| return done(error); | ||
| } | ||
| var results = JSON.parse(stdout); | ||
| expect(results, 'to have property', 'tests'); | ||
| var titles = []; | ||
| for (var index = 0; index < results.tests.length; index += 1) { | ||
| expect(results.tests[index], 'to have property', 'fullTitle'); | ||
| titles.push(results.tests[index].fullTitle); | ||
| } | ||
| expect( | ||
| titles, | ||
| 'to contain', | ||
| 'cts written in esm should work', | ||
| ).and('to have length', 1); | ||
| done(); | ||
| } | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.