Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
CompilationOptimizeChunkModules, CompilationParams, CompilerCompilation, CompilerFinishMake,
ConcatenatedModule, ConcatenatedModuleExportsDefinitions, DependencyId, ExportsType,
LibraryOptions, ModuleGraph, ModuleIdentifier, Plugin, PrefetchExportsInfoMode, RuntimeSpec,
RuntimeVariable, UsedNameItem,
RuntimeVariable, UsedNameItem, export_name,
rspack_sources::{ConcatSource, RawStringSource, SourceExt},
to_identifier,
};
Expand Down Expand Up @@ -397,7 +397,10 @@
})
.map(|(local, exported)| {
if let Some(exported) = exported {
format!("{local} as {exported}")
// Use export_name to properly quote identifiers that aren't valid JavaScript identifiers
let quoted_exported = export_name(exported)
.unwrap_or_else(|_| panic!("Failed to generate valid export name for: {}", exported));

Check failure on line 402 in crates/rspack_plugin_library/src/modern_module_library_plugin.rs

View workflow job for this annotation

GitHub Actions / Run Rust Tests / Rust check

variables can be used directly in the `format!` string
format!("{local} as {quoted_exported}")
} else {
local.clone()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"!top": "exclamation",
"regular": "normal",
"a.b": "dot",
"a-b": "dash"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs'
import path from 'path'

it('should properly quote export names that are not valid identifiers', async () => {
const json = fs.readFileSync(path.resolve(import.meta.dirname, './json.mjs'), 'utf-8');

// The export statement should quote special identifiers
expect(json).toContain('"!top"');
expect(json).toContain('"a.b"');
expect(json).toContain('"a-b"');

// Should not contain unquoted special chars which would be syntax errors
expect(json).not.toMatch(/\bas !top\b/);
expect(json).not.toMatch(/\bas a\.b\b/);

const jsonModule = await import(/*webpackIgnore: true*/'./json.mjs');
expect(jsonModule["!top"]).toBe("exclamation");
expect(jsonModule["regular"]).toBe("normal");
expect(jsonModule["a.b"]).toBe("dot");
expect(jsonModule["a-b"]).toBe("dash");
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
entry: {
main: {import: './index.js', filename: 'bundle.mjs'},
json: {import: './data.json', filename: 'json.mjs'},
},
output: {
module: true,
library: {
type: 'modern-module',
},
},
module: {
parser: {
javascript: {
importMeta: false
}
}
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
experiments: {
outputModule: true,
},
}
Loading