-
-
Notifications
You must be signed in to change notification settings - Fork 59
fix: treat module
as normal variable in ESM
#1260
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
Open
fi3ework
wants to merge
1
commit into
main
Choose a base branch
from
m3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
28 changes: 28 additions & 0 deletions
28
tests/integration/parser-javascript/api-plugin/index.test.ts
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,28 @@ | ||
import path from 'node:path'; | ||
import { expect, test } from '@rstest/core'; | ||
import { buildAndGetResults } from 'test-helper'; | ||
|
||
test("API plugin's api should be skipped in parser", async () => { | ||
const fixturePath = path.resolve(__dirname); | ||
const { entries } = await buildAndGetResults({ | ||
fixturePath, | ||
}); | ||
|
||
expect(entries.esm).toMatchInlineSnapshot(` | ||
"const a = require.cache; | ||
const b = require.extensions; | ||
const c = require.config; | ||
const d = require.version; | ||
const e = require.include; | ||
const f = require.onError; | ||
export { a, b, c, d, e, f }; | ||
" | ||
`); | ||
|
||
expect(entries.cjs).toContain('const a = require.cache;'); | ||
expect(entries.cjs).toContain('const b = require.extensions;'); | ||
expect(entries.cjs).toContain('const c = require.config;'); | ||
expect(entries.cjs).toContain('const d = require.version;'); | ||
expect(entries.cjs).toContain('const e = require.include;'); | ||
expect(entries.cjs).toContain('const f = require.onError;'); | ||
}); |
2 changes: 1 addition & 1 deletion
2
...ntegration/format/api-plugin/package.json → ...parser-javascript/api-plugin/package.json
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
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
tests/integration/parser-javascript/api-plugin/src/cjsFile.cjs
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,7 @@ | ||
const value = () => 42; | ||
|
||
// Make the export immutable | ||
Object.defineProperty(module, 'exports', { | ||
enumerable: true, | ||
get: value, | ||
}); |
File renamed without changes.
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,57 @@ | ||
import path from 'node:path'; | ||
import { expect, test } from '@rstest/core'; | ||
import { buildAndGetResults, queryContent } from 'test-helper'; | ||
|
||
test('`module` variable should be preserved as-is by `javascript.commonjs.exports = "false"`', async () => { | ||
const fixturePath = path.resolve(__dirname); | ||
const { contents } = await buildAndGetResults({ | ||
fixturePath, | ||
}); | ||
|
||
const esm1 = queryContent(contents.esm, 'm1.mjs', { basename: true }); | ||
const esm2 = queryContent(contents.esm, 'm2.mjs', { basename: true }); | ||
const cjs1 = queryContent(contents.cjs, 'm1.js', { basename: true }); | ||
const cjs2 = queryContent(contents.cjs, 'm2.js', { basename: true }); | ||
|
||
expect( | ||
( | ||
await Promise.all([ | ||
import(esm1.path), | ||
import(esm2.path), | ||
import(cjs1.path), | ||
import(cjs2.path), | ||
]) | ||
).map((m) => m.value), | ||
).toEqual([42, 42, 42, 42]); | ||
|
||
const checksM1 = [ | ||
'if (module.children) module.children = module.children.filter((item)=>item.filename !== path);', | ||
'module.exports = original', | ||
'if (module.exports && module.exports.test) return module.exports.test()', | ||
]; | ||
|
||
const checksEsm2 = [ | ||
'if (node_module.children) node_module.children = node_module.children.filter((item)=>item.filename !== path);', | ||
'node_module.exports = original', | ||
'if (node_module.exports && node_module.exports.test) return node_module.exports.test()', | ||
]; | ||
|
||
const checksCjs2 = [ | ||
'if (external_node_module_default().children) external_node_module_default().children = external_node_module_default().children.filter((item)=>item.filename !== path);', | ||
'external_node_module_default().exports = original', | ||
'if (external_node_module_default().exports && external_node_module_default().exports.test) return external_node_module_default().exports.test()', | ||
]; | ||
|
||
for (const check of checksM1) { | ||
expect(esm1.content).toContain(check); | ||
expect(cjs1.content).toContain(check); | ||
} | ||
|
||
for (const check of checksEsm2) { | ||
expect(esm2.content).toContain(check); | ||
} | ||
|
||
for (const check of checksCjs2) { | ||
expect(cjs2.content).toContain(check); | ||
} | ||
}); |
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,5 @@ | ||
{ | ||
"name": "parser-javascript-module-test", | ||
"version": "1.0.0", | ||
"private": true | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/integration/parser-javascript/module/rslib.config.ts
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,25 @@ | ||
import { defineConfig } from '@rslib/core'; | ||
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; | ||
|
||
export default defineConfig({ | ||
lib: [ | ||
// ESM | ||
generateBundleEsmConfig({ | ||
source: { | ||
entry: { | ||
m1: './src/module1.js', | ||
m2: './src/module2.js', | ||
}, | ||
}, | ||
}), | ||
// CJS | ||
generateBundleCjsConfig({ | ||
source: { | ||
entry: { | ||
m1: './src/module1.js', | ||
m2: './src/module2.js', | ||
}, | ||
}, | ||
}), | ||
], | ||
}); |
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,7 @@ | ||
const value = () => 42; | ||
|
||
// Make the export immutable | ||
Object.defineProperty(module, 'exports', { | ||
enumerable: true, | ||
get: value, | ||
}); |
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,36 @@ | ||
import value from './cjsFile.cjs'; | ||
export { value }; | ||
|
||
export function foo() { | ||
console.log(module); | ||
if (module.children) { | ||
module.children = module.children.filter((item) => item.filename !== path); | ||
} | ||
} | ||
|
||
export function bar() { | ||
const original = module.exports; | ||
|
||
try { | ||
module.exports = { | ||
...module.exports, | ||
test: () => 'ok', | ||
}; | ||
|
||
return module.exports.test(); | ||
} finally { | ||
module.exports = original; | ||
} | ||
} | ||
|
||
export function baz() { | ||
if (typeof module === 'undefined') { | ||
return undefined; | ||
} | ||
|
||
if (module.exports && module.exports.test) { | ||
return module.exports.test(); | ||
} | ||
|
||
return undefined; | ||
} |
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,37 @@ | ||
import module from 'node:module'; | ||
import value from './cjsFile.cjs'; | ||
export { value }; | ||
|
||
export function foo() { | ||
fi3ework marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log(module); | ||
if (module.children) { | ||
module.children = module.children.filter((item) => item.filename !== path); | ||
} | ||
} | ||
|
||
export function bar() { | ||
const original = module.exports; | ||
|
||
try { | ||
module.exports = { | ||
...module.exports, | ||
test: () => 'ok', | ||
}; | ||
|
||
return module.exports.test(); | ||
} finally { | ||
module.exports = original; | ||
} | ||
} | ||
|
||
export function baz() { | ||
if (typeof module === 'undefined') { | ||
return undefined; | ||
} | ||
|
||
if (module.exports && module.exports.test) { | ||
return module.exports.test(); | ||
} | ||
|
||
return undefined; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.