-
-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parser options for preserving require and import (#7939)
- Loading branch information
Showing
31 changed files
with
712 additions
and
4 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
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
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
8 changes: 8 additions & 0 deletions
8
packages/rspack-test-tools/tests/configCases/parsing/import-dynamic/index.js
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,8 @@ | ||
const dir = process.env.name | ||
|
||
import('./other.js') | ||
|
||
import('./' + dir + '/other.js') | ||
import(dir) | ||
|
||
|
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/parsing/import-dynamic/other.js
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 @@ | ||
module.exports = 'other'; |
22 changes: 22 additions & 0 deletions
22
packages/rspack-test-tools/tests/configCases/parsing/import-dynamic/rspack.config.js
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,22 @@ | ||
// TODO: move to webpack-test after merged into webpack | ||
|
||
/** @type {import("../../../../types").Configuration} */ | ||
module.exports = { | ||
entry: { | ||
bundle0: "./index.js", | ||
test: "./test.js" | ||
}, | ||
module: { | ||
parser: { | ||
javascript: { | ||
importDynamic: false | ||
} | ||
} | ||
}, | ||
output: { | ||
filename: "[name].js" | ||
}, | ||
node: { | ||
__dirname: false | ||
} | ||
}; |
6 changes: 6 additions & 0 deletions
6
packages/rspack-test-tools/tests/configCases/parsing/import-dynamic/test.config.js
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,6 @@ | ||
module.exports = { | ||
findBundle: function (i, options) { | ||
return ["test.js"]; | ||
} | ||
}; | ||
|
9 changes: 9 additions & 0 deletions
9
packages/rspack-test-tools/tests/configCases/parsing/import-dynamic/test.js
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,9 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
it("import() a dynamical expression should preserve as-is when `importDynamic` is disabled", () => { | ||
const code = fs.readFileSync(path.join(__dirname, "./bundle0.js"), 'utf-8'); | ||
expect(code).not.toContain("import('./other.js')"); | ||
expect(code).toContain("import('./' + dir + '/other.js')"); | ||
expect(code).toContain("import(dir)"); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/rspack-test-tools/tests/configCases/parsing/require-as-expression/index.js
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,9 @@ | ||
require('./other.js') | ||
|
||
const resolve1 = require.resolve | ||
resolve1('./other.js') | ||
|
||
const lazyFn = (module, requireFn) => {} | ||
lazyFn('./other.js', require) | ||
|
||
|
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/parsing/require-as-expression/other.js
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 @@ | ||
module.exports = 'other'; |
20 changes: 20 additions & 0 deletions
20
packages/rspack-test-tools/tests/configCases/parsing/require-as-expression/rspack.config.js
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,20 @@ | ||
/** @type {import("../../../../types").Configuration} */ | ||
module.exports = { | ||
entry: { | ||
bundle0: "./index.js", | ||
test: "./test.js" | ||
}, | ||
module: { | ||
parser: { | ||
javascript: { | ||
requireAsExpression: false | ||
} | ||
} | ||
}, | ||
output: { | ||
filename: "[name].js" | ||
}, | ||
node: { | ||
__dirname: false | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/rspack-test-tools/tests/configCases/parsing/require-as-expression/test.config.js
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,7 @@ | ||
// TODO: move to webpack-test after merged into webpack | ||
|
||
module.exports = { | ||
findBundle: function (i, options) { | ||
return ["test.js"]; | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/rspack-test-tools/tests/configCases/parsing/require-as-expression/test.js
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,9 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
it("use require as a expression should preserve as-is when `requireAsExpression` is disabled", () => { | ||
const code = fs.readFileSync(path.join(__dirname, "./bundle0.js"), 'utf-8'); | ||
expect(code).not.toContain("require('./other.js')"); | ||
expect(code).toContain("resolve1('./other.js')"); | ||
expect(code).toContain(`lazyFn('./other.js', require)`); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/rspack-test-tools/tests/configCases/parsing/require-dynamic/index.js
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,11 @@ | ||
const dir = process.env.name | ||
|
||
const require1 = require(dir) | ||
|
||
const require2 = require('./other.js') | ||
|
||
const require3 = require('./foo/' + dir + '.js') | ||
|
||
const require4 = require(a + './foo/' + dir + '.js') | ||
|
||
const require5 = require(dir ? './foo/' + dir + '.js' : './foo/nested' + dir + 'js') |
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/parsing/require-dynamic/other.js
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 @@ | ||
module.exports = 'other'; |
24 changes: 24 additions & 0 deletions
24
packages/rspack-test-tools/tests/configCases/parsing/require-dynamic/rspack.config.js
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,24 @@ | ||
// TODO: move to webpack-test after merged into webpack | ||
|
||
/** @type {import("../../../../types").Configuration} */ | ||
module.exports = { | ||
entry: { | ||
bundle0: "./index.js", | ||
test: "./test.js" | ||
}, | ||
module: { | ||
parser: { | ||
javascript: { | ||
requireDynamic: false, | ||
// To preserve `require(...)`, we need to use `requireAsExpression: false` alongside. | ||
requireAsExpression: false | ||
} | ||
} | ||
}, | ||
output: { | ||
filename: "[name].js" | ||
}, | ||
node: { | ||
__dirname: false | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/rspack-test-tools/tests/configCases/parsing/require-dynamic/test.config.js
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,5 @@ | ||
module.exports = { | ||
findBundle: function (i, options) { | ||
return ["test.js"]; | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/rspack-test-tools/tests/configCases/parsing/require-dynamic/test.js
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,11 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
it("require a dynamical expression should preserve as-is when `requireDynamic` is disabled", () => { | ||
const code = fs.readFileSync(path.join(__dirname, "./bundle0.js"), 'utf-8'); | ||
expect(code).toContain("require(dir)"); | ||
expect(code).not.toContain("require('./other.js')"); | ||
expect(code).toContain("require('./foo/' + dir + '.js')"); | ||
expect(code).toContain("require(a + './foo/' + dir + '.js')"); | ||
expect(code).toContain("require(dir ? './foo/' + dir + '.js' : './foo/nested' + dir + 'js')"); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/rspack-test-tools/tests/configCases/parsing/require-resolve/index.js
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,19 @@ | ||
const dir = process.env.DIR_READ_FROM_RUNTIME | ||
|
||
const resolve1 = require.resolve(dir) | ||
|
||
const resolve2 = require.resolve('./other.js') | ||
|
||
const resolve3 = require.resolve('./foo/' + dir + '.js') | ||
|
||
const resolve4 = require.resolve(process.env.RANDOM ? './foo/' + dir + '.js' : './bar/' + dir + 'js') | ||
|
||
|
||
// Can't handle, `require` will turn into expression | ||
// const resolve5 = require.resolve | ||
// resolve5('./other.js') | ||
|
||
// Can't handle, `require` will turn into `undefined` | ||
// const __require = require | ||
// const resolve6 = __require.resolve('./other.js') | ||
|
25 changes: 25 additions & 0 deletions
25
packages/rspack-test-tools/tests/configCases/parsing/require-resolve/rspack.config.js
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,25 @@ | ||
// TODO: move to webpack-test after merged into webpack | ||
|
||
/** @type {import("../../../../types").Configuration} */ | ||
module.exports = { | ||
entry: { | ||
bundle0: "./index.js", | ||
test: "./test.js" | ||
}, | ||
module: { | ||
parser: { | ||
javascript: { | ||
requireResolve: false, | ||
// To preserve `require.resolve`, we need to use `requireResolve: false` to preserve | ||
// the `resolve` method and `requireAsExpression: false` to preserve `require`. | ||
requireAsExpression: false | ||
} | ||
} | ||
}, | ||
output: { | ||
filename: "[name].js" | ||
}, | ||
node: { | ||
__dirname: false | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/rspack-test-tools/tests/configCases/parsing/require-resolve/test.config.js
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,5 @@ | ||
module.exports = { | ||
findBundle: function (i, options) { | ||
return ["test.js"]; | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/rspack-test-tools/tests/configCases/parsing/require-resolve/test.js
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,10 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
it("`require.resolve` should preserve as-is when `requireResolve` is disabled", () => { | ||
const code = fs.readFileSync(path.join(__dirname, "./bundle0.js"), 'utf-8'); | ||
expect(code).toContain("require.resolve(dir)"); | ||
expect(code).toContain("require.resolve('./other.js')"); | ||
expect(code).toContain("require.resolve('./foo/' + dir + '.js')"); | ||
expect(code).toContain("require.resolve(process.env.RANDOM ? './foo/' + dir + '.js' : './bar/' + dir + 'js')"); | ||
}); |
Oops, something went wrong.
5416306
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.
📝 Benchmark detail: Open
5416306
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.
📝 Ran ecosystem CI: Open