How to load modules that don't exist on file system? #6927
Answered
by
kdy1
brundonsmith
asked this question in
Q&A
-
I'm attempting to use SWC directly as a Rust crate and build a tool on top of it My tool generates its own JS files in memory, and I'd like to then pass them to SWC for transpiling and bundling before writing the final bundle to disk It seems like what I'd want to do is create fn load_with_handler(&self, handler: &Handler, name: &FileName) -> Result<ModuleData, Error> {
tracing::debug!("JsLoader.load({})", name);
let helpers = Helpers::new(false);
if let FileName::Custom(id) = name {
// Handle built-in modules
if id.starts_with("node:") {
let fm = self
.compiler
.cm
.new_source_file(name.clone(), "".to_string());
return Ok(ModuleData {
fm,
module: Module {
span: DUMMY_SP,
body: Default::default(),
shebang: Default::default(),
},
helpers: Default::default(),
});
// Handle disabled modules, eg when `browser` has a field
// set to `false`
} else {
// TODO: When we know the calling context is ESM
// TODO: switch to `export default {}`.
let fm = self
.compiler
.cm
.new_source_file(name.clone(), "module.exports = {}".to_string());
let module = parse_file_as_module(
&fm,
Syntax::Es(Default::default()),
Default::default(),
None,
&mut vec![],
)
.unwrap();
return Ok(ModuleData {
fm,
module,
helpers: Default::default(),
});
}
} Is this:
|
Beta Was this translation helpful? Give feedback.
Answered by
kdy1
Feb 10, 2023
Replies: 1 comment 2 replies
-
You mean you are using |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
brundonsmith
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You mean you are using
swc_bundler
? It's not actively maintained in favor of turbopack