Skip to content

Commit

Permalink
Merge pull request #877 from umijs/dev-for-raw
Browse files Browse the repository at this point in the history
feat: Dev for raw
  • Loading branch information
ctts authored Jan 18, 2024
2 parents d468cd7 + 9324ed8 commit 35543ac
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl Compiler {
Arc::new(plugins::copy::CopyPlugin {}),
Arc::new(plugins::import::ImportPlugin {}),
// file types
Arc::new(plugins::raw::RawPlugin {}),
Arc::new(plugins::css::CSSPlugin {}),
Arc::new(plugins::context_module::ContextModulePlugin {}),
Arc::new(plugins::javascript::JavaScriptPlugin {}),
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod md;
pub mod meta;
pub mod minifish;
pub mod node_polyfill;
pub mod raw;
pub mod runtime;
pub mod svg;
pub mod toml;
Expand Down
28 changes: 28 additions & 0 deletions crates/mako/src/plugins/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::sync::Arc;

use mako_core::anyhow::Result;

use crate::compiler::Context;
use crate::load::{read_content, Content};
use crate::plugin::{Plugin, PluginLoadParam};

pub struct RawPlugin {}

impl Plugin for RawPlugin {
fn name(&self) -> &str {
"raw"
}

fn load(&self, param: &PluginLoadParam, _context: &Arc<Context>) -> Result<Option<Content>> {
if param.task.request.query.iter().any(|item| item.0 == "raw") {
let file_content = read_content(param.task.request.path.as_str())?;
let json_string = serde_json::to_string(&file_content)?;

return Ok(Some(Content::Js(format!(
"module.exports = {}",
json_string
))));
}
Ok(None)
}
}
17 changes: 17 additions & 0 deletions e2e/fixtures/raw/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require("assert");
const { parseBuildResult } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

const content = files["index.js"];

assert(
content.includes(
'module.exports = "const Hello = \\"Hello\\";\\nconst World = `World`;\\n"'
),
"support convert js"
);

assert(
content.includes("h1 {\\n background-size: 20px 20px;\\n}\\n"),
"support convert css"
);
1 change: 1 addition & 0 deletions e2e/fixtures/raw/mako.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions e2e/fixtures/raw/src/foo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
background-size: 20px 20px;
}
2 changes: 2 additions & 0 deletions e2e/fixtures/raw/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const Hello = "Hello";
const World = `World`;
5 changes: 5 additions & 0 deletions e2e/fixtures/raw/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from "./foo.css?raw";
console.log(css);

import js from "./foo.ts?raw";
console.log(js);

0 comments on commit 35543ac

Please sign in to comment.