forked from rolldown/rolldown
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust/rolldown): support
inject
imports (rolldown#1933)
This PR adds the missing pieces for inject plugin. --------- Co-authored-by: Yunfei <i.heyunfei@gmail.com>
- Loading branch information
Showing
18 changed files
with
287 additions
and
14 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
38 changes: 38 additions & 0 deletions
38
crates/rolldown/tests/rolldown/function/inject/_config.json
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,38 @@ | ||
{ | ||
"config": { | ||
"inject": [ | ||
// import { Promise } from 'es6-promise' | ||
{ | ||
"type": "named", | ||
"imported": "Promise", | ||
"from": "./promise-shim" | ||
}, | ||
// import { Promise as P } from 'es6-promise' | ||
{ | ||
"type": "named", | ||
"imported": "Promise", | ||
"alias": "P", | ||
"from": "./promise-shim" | ||
}, | ||
// import $ from 'jquery' | ||
{ | ||
"type": "named", | ||
"imported": "default", | ||
"alias": "$", | ||
"from": "./jquery" | ||
}, | ||
// import * as fs from 'node:fs' | ||
{ | ||
"type": "namespace", | ||
"alias": "fs", | ||
"from": "./node-fs" | ||
}, | ||
// `Object.assign` | ||
{ | ||
"type": "named", | ||
"imported": "Object.assign", | ||
"from": "./object-assign-shim" | ||
} | ||
] | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
crates/rolldown/tests/rolldown/function/inject/artifacts.snap
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,30 @@ | ||
--- | ||
source: crates/rolldown_testing/src/integration_test.rs | ||
--- | ||
# Assets | ||
|
||
## main.mjs | ||
|
||
```js | ||
import { default as assert } from "node:assert"; | ||
//#region promise-shim.js | ||
const Promise = "promise-shim"; | ||
//#endregion | ||
//#region jquery.js | ||
var jquery_default = "jquery"; | ||
//#endregion | ||
//#region node-fs.js | ||
var node_fs_default = "node-fs"; | ||
//#endregion | ||
//#region main.js | ||
assert.strictEqual(Promise, "promise-shim"); | ||
assert.strictEqual(Promise, "promise-shim"); | ||
assert.strictEqual(jquery_default, "jquery"); | ||
assert.strictEqual(node_fs_default, "node-fs"); | ||
//#endregion | ||
``` |
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 @@ | ||
export default 'jquery' |
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 @@ | ||
import assert from 'node:assert' | ||
|
||
assert.strictEqual(Promise, 'promise-shim') | ||
assert.strictEqual(P, 'promise-shim') | ||
assert.strictEqual($, 'jquery') | ||
assert.strictEqual(fs.default, 'node-fs') | ||
// FIXME: oxc injects invalid statements `import { default as 'Object.assign' from 'object-assign-shim'` }`, so it fails. | ||
// assert.strictEqual(Object.assign, 'object-assign-shim') |
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 @@ | ||
export default 'node-fs' |
1 change: 1 addition & 0 deletions
1
crates/rolldown/tests/rolldown/function/inject/object-assign-shim.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 @@ | ||
export default 'object-assign-shim' |
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 @@ | ||
export const Promise = 'promise-shim' |
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
47 changes: 47 additions & 0 deletions
47
crates/rolldown_common/src/inner_bundler_options/types/inject_import.rs
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,47 @@ | ||
#[cfg(feature = "deserialize_bundler_options")] | ||
use schemars::JsonSchema; | ||
#[cfg(feature = "deserialize_bundler_options")] | ||
use serde::Deserialize; | ||
|
||
/// # Usage | ||
/// - `import { Promise } from 'es6-promise'` => `InjectImport::named("Promise", None,"es6-promise")` | ||
/// - `import { Promise as P } from 'es6-promise'` => `InjectImport::named("Promise", Some("P"), "es6-promise")` | ||
/// - `import $ from 'jquery'` => `InjectImport::named("default", Some("$"), "jquery")` | ||
/// - `import $ from 'jquery'` => `InjectImport::default("$", "jquery")` | ||
/// - `import * as fs from 'node:fs'` => `InjectImport::namespace("fs", "node:fs")` | ||
/// | ||
/// - `InjectImport::named("Object.assign", None, "es6-object-assign")` is a special form to inject shims to the following code: | ||
/// ```js | ||
/// console.log(Object.assign({ a: 1 }, { b: 2 })); | ||
/// ``` | ||
/// | ||
/// will be, after the injection, transformed to: | ||
/// | ||
/// ```js | ||
/// import object_assign from "es6-object-assign"; | ||
/// console.log(object_assign({ a: 1 }, { b: 2 })); | ||
///``` | ||
#[derive(Debug)] | ||
#[cfg_attr( | ||
feature = "deserialize_bundler_options", | ||
derive(Deserialize, JsonSchema), | ||
serde(rename_all = "camelCase", deny_unknown_fields, tag = "type") | ||
)] | ||
pub enum InjectImport { | ||
Named { imported: String, alias: Option<String>, from: String }, | ||
Namespace { alias: String, from: String }, | ||
} | ||
|
||
impl InjectImport { | ||
pub fn named(imported: String, alias: Option<String>, from: String) -> Self { | ||
Self::Named { imported, from, alias } | ||
} | ||
|
||
pub fn namespace(alias: String, from: String) -> Self { | ||
Self::Namespace { from, alias } | ||
} | ||
|
||
pub fn default(alias: String, from: String) -> Self { | ||
Self::Named { imported: "default".to_string(), alias: Some(alias), from } | ||
} | ||
} |
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
Oops, something went wrong.