Skip to content
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

Change return type of process from string to { code, map } #86

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ impl Options {
}
}

#[wasm_bindgen(getter_with_clone)]
pub struct CodeMapPair {
pub code: String,
pub map: String,
}

#[wasm_bindgen]
impl CodeMapPair {
#[wasm_bindgen(constructor)]
pub fn new(code: String, map: String) -> Self {
Self { code, map }
}
}

#[wasm_bindgen]
pub struct Preprocessor {
// TODO: reusing this between calls result in incorrect spans; there may
Expand Down Expand Up @@ -123,13 +137,13 @@ impl Preprocessor {
Self {}
}

pub fn process(&self, src: String, options: JsValue) -> Result<String, JsValue> {
pub fn process(&self, src: String, options: JsValue) -> Result<CodeMapPair, JsValue> {
let options = Options::new(options);
let preprocessor = CorePreprocessor::new();
let result = preprocessor.process(&src, options);

match result {
Ok(output) => Ok(output),
Ok(output) => Ok(CodeMapPair::new(output.code, output.map)),
Err(err) => Err(as_javascript_error(err, preprocessor.source_map()).into()),
}
}
Expand Down
49 changes: 31 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use base64::{engine::general_purpose, Engine as _};
use std::path::PathBuf;
use swc_common::comments::SingleThreadedComments;
use swc_common::source_map::SourceMapGenConfig;
use swc_common::{self, sync::Lrc, FileName, SourceMap, Mark};
use swc_common::{self, sync::Lrc, FileName, Mark, SourceMap};
use swc_core::common::GLOBALS;
use swc_ecma_ast::{
Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, Module, ModuleDecl,
ModuleExportName, ModuleItem,
Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, Module, ModuleDecl, ModuleExportName,
ModuleItem,
};
use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
Expand All @@ -21,9 +21,9 @@ use swc_ecma_visit::{as_folder, VisitMutWith, VisitWith};
use uuid::Uuid;

mod bindings;
mod locate;
mod snippets;
mod transform;
mod locate;

#[derive(Default)]
pub struct Options {
Expand All @@ -36,6 +36,11 @@ pub struct Preprocessor {
comments: SingleThreadedComments,
}

pub struct CodeMapPair {
pub code: String,
pub map: String,
}

struct SourceMapConfig;
impl SourceMapGenConfig for SourceMapConfig {
fn file_name_to_source(&self, f: &swc_common::FileName) -> String {
Expand All @@ -47,7 +52,6 @@ impl SourceMapGenConfig for SourceMapConfig {
}
}


impl Preprocessor {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -93,7 +97,7 @@ impl Preprocessor {
&self,
src: &str,
options: Options,
) -> Result<String, swc_ecma_parser::error::Error> {
) -> Result<CodeMapPair, swc_ecma_parser::error::Error> {
let target_specifier = "template";
let target_module = "@ember/template-compiler";
let filename = match options.filename {
Expand All @@ -116,7 +120,11 @@ impl Preprocessor {
GLOBALS.set(&Default::default(), || {
let mut parsed_module = parser.parse_module()?;

let id = private_ident!(format!("{}_{}", target_specifier, Uuid::new_v4().to_string().replace("-", "")));
let id = private_ident!(format!(
"{}_{}",
target_specifier,
Uuid::new_v4().to_string().replace("-", "")
));
let mut needs_import = false;
parsed_module.visit_mut_with(&mut as_folder(transform::TransformVisitor::new(
&id,
Expand All @@ -132,13 +140,16 @@ impl Preprocessor {

parsed_module.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));

Ok(self.print(&parsed_module, options.inline_source_map))
let codemap = self.print(&parsed_module, options.inline_source_map);

Ok(codemap)
})
}

fn print(&self, module: &Module, inline_source_map: bool) -> String {
fn print(&self, module: &Module, inline_source_map: bool) -> CodeMapPair {
let mut buf = vec![];
let mut srcmap = vec![];
let mut source_map_buffer = vec![];
let mut emitter = Emitter {
cfg: Default::default(),
cm: self.source_map.clone(),
Expand All @@ -152,27 +163,30 @@ impl Preprocessor {
};
emitter.emit_module(module).unwrap();

if inline_source_map {
let mut source_map_buffer = vec![];
self.source_map()
.build_source_map_with_config(&srcmap, None, SourceMapConfig {})
.to_writer(&mut source_map_buffer)
.unwrap();
self.source_map()
.build_source_map_with_config(&srcmap, None, SourceMapConfig {})
.to_writer(&mut source_map_buffer)
.unwrap();

if inline_source_map {
let mut comment = "//# sourceMappingURL=data:application/json;base64,"
.to_owned()
.into_bytes();
buf.append(&mut comment);

let mut encoded = general_purpose::URL_SAFE_NO_PAD
.encode(source_map_buffer)
.encode(source_map_buffer.clone())
.into_bytes();

buf.append(&mut encoded);
}

let s = String::from_utf8_lossy(&buf);
s.to_string()

CodeMapPair {
code: s.to_string(),
map: String::from_utf8(source_map_buffer.clone()).unwrap(),
}
}

pub fn source_map(&self) -> Lrc<SourceMap> {
Expand Down Expand Up @@ -209,7 +223,6 @@ fn insert_import(
#[cfg(test)]
mod test_helpers;


macro_rules! testcase {
($test_name:ident, $input:expr, $expected:expr) => {
#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
);

match result {
Ok(output) => println!("{}", output),
Ok(output) => println!("{}", output.code),
Err(err) => {
let handler =
Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(p.source_map()));
Expand Down
5 changes: 4 additions & 1 deletion src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ pub fn testcase(input: &str, expected: &str) -> Result<(), swc_ecma_parser::erro
let re = Regex::new(r"template_[0-9a-f]{32}").unwrap();
let p = Preprocessor::new();
let actual = p.process(input, Default::default())?;
let actual_santized = re.replace_all(&actual, "template_UUID");
let actual_santized = re.replace_all(&actual.code, "template_UUID");
let normalized_expected = normalize(expected);
if actual_santized != normalized_expected {
panic!(
"code differs from expected:\n{}",
format!("{}", Changeset::new(&actual_santized, &normalized_expected, "\n"))
);
}

assert!(!actual.map.is_empty(), "expected .map to not be empty");

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe(`parse`, function () {
p.process(
`const thing = "face";
<template>Hi`,
{ filename: 'path/to/my/component.gjs' }
{ filename: "path/to/my/component.gjs" }
);
}).to.throw(`Parse Error at path/to/my/component.gjs:2:15: 2:15`);
});
Expand Down
6 changes: 3 additions & 3 deletions test/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe(`process`, function () {
it("works for a basic example", function () {
let output = p.process("<template>Hi</template>");

expect(normalizeOutput(output)).to
expect(normalizeOutput(output.code)).to
.equalCode(`import { template as template_UUID } from "@ember/template-compiler";
export default template_UUID(\`Hi\`, {
eval () {
Expand All @@ -36,7 +36,7 @@ describe(`process`, function () {

let output = p.process(input);

expect(normalizeOutput(output)).to.equalCode(
expect(normalizeOutput(output.code)).to.equalCode(
`import { template as template_UUID } from "@ember/template-compiler";
class Foo extends Component {
greeting = 'Hello';
Expand Down Expand Up @@ -97,7 +97,7 @@ describe(`process`, function () {
it("Provides inline source maps if inline_source_map option is set to true", function () {
let output = p.process(`<template>Hi</template>`, { inline_source_map: true });

expect(output).to.match(
expect(output.code).to.match(
/sourceMappingURL=data:application\/json;base64,/
);
});
Expand Down
2 changes: 1 addition & 1 deletion test/require.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("cjs/require", function () {
it("can call process", function () {
let output = p.process("<template>Hi</template>");

expect(normalizeOutput(output)).to
expect(normalizeOutput(output.code)).to
.equalCode(`import { template as template_UUID } from "@ember/template-compiler";
export default template_UUID(\`Hi\`, {
eval () {
Expand Down
Loading