Skip to content

Commit b9f6d98

Browse files
committed
feat: refactor and support lock core-js path
1 parent 70cfef4 commit b9f6d98

21 files changed

+2237
-97
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ node_modules
88

99
/swc_plugin_auto_css_modules.wasm
1010
/example/.swc
11+
/debug

Cargo.lock

+15-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
[package]
22
name = "swc_plugin_auto_css_modules"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
edition = "2021"
55
authors = ["fz6m"]
66
description = "Auto css modules plugin for swc"
77
license = "MIT"
8+
repository = "https://github.com/xn-sakina/swc-plugin-auto-css-modules.git"
89
publish = false
910

1011
[lib]
11-
crate-type = ["cdylib"]
12+
crate-type = ["cdylib", "rlib"]
1213

1314
[profile.release]
1415
lto = true
1516

1617
[dependencies]
17-
serde = "1"
18+
serde_json = "1.0.79"
19+
auto_css_modules = { version = "1.0.1", path = "./transform" }
1820
swc_core = { version = "0.48.29", features = [
1921
"ecma_plugin_transform",
20-
"ecma_utils",
2122
"ecma_visit",
2223
"ecma_ast",
23-
"common",
2424
] }

example/_do.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { transformSync } from '@swc/core'
2+
import type { ISwcPluginAutoCssModulesConfig } from '../'
3+
4+
export const transform = (
5+
code: string,
6+
opts: ISwcPluginAutoCssModulesConfig = {}
7+
) => {
8+
return transformSync(code, {
9+
module: {
10+
type: 'es6',
11+
ignoreDynamic: true,
12+
},
13+
jsc: {
14+
parser: {
15+
syntax: 'typescript',
16+
dynamicImport: true,
17+
tsx: true,
18+
},
19+
target: 'es5',
20+
experimental: {
21+
plugins: [
22+
[
23+
require.resolve(
24+
'../target/wasm32-wasi/release/swc_plugin_auto_css_modules.wasm'
25+
),
26+
opts,
27+
],
28+
],
29+
},
30+
minify: {
31+
mangle: false,
32+
compress: false,
33+
},
34+
},
35+
minify: false,
36+
filename: 'index.ts',
37+
sourceMaps: false,
38+
})
39+
}

example/corejs.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import assert from 'assert'
2+
import { transform } from './_do'
3+
4+
const result = transform(
5+
`
6+
import a from 'core-js';
7+
import b from 'core-js/cc';
8+
import c from 'core-js/cc/dd';
9+
10+
import 'core-js';
11+
import 'core-js/cc';
12+
import 'core-js/cc/dd';
13+
14+
import d from '../core-js';
15+
import e from './core-js';
16+
import { f } from 'foo';
17+
import * as g from 'foo';
18+
19+
a, b, c, d, e, f, g;
20+
`,
21+
{
22+
lock_core_js_pkg_path: '/core-js/absolute-path',
23+
// dirname(
24+
// require.resolve('core-js/package.json')
25+
// )
26+
}
27+
)
28+
29+
assert(
30+
result.code.trim() ===
31+
`
32+
import a from "core-js";
33+
import b from "/core-js/absolute-path/cc";
34+
import c from "/core-js/absolute-path/cc/dd";
35+
import "core-js";
36+
import "/core-js/absolute-path/cc";
37+
import "/core-js/absolute-path/cc/dd";
38+
import d from "../core-js";
39+
import e from "./core-js";
40+
import { f } from "foo";
41+
import * as g from "foo";
42+
a, b, c, d, e, f, g;
43+
`.trim()
44+
)

example/index.ts

+3-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { transformSync } from '@swc/core'
21
import assert from 'assert'
2+
import { transform } from './_do'
33

4-
const result = transformSync(
5-
`
4+
const result = transform(`
65
import a from 'foo.less';
76
import b from 'foo.scss';
87
import c from 'foo.sass';
@@ -11,32 +10,7 @@ import e from './foo.styl';
1110
import { f } from 'foo';
1211
import * as g from 'foo';
1312
a, b, c, d, e, f, g;
14-
`,
15-
{
16-
module: {
17-
type: 'es6',
18-
ignoreDynamic: true,
19-
},
20-
jsc: {
21-
parser: {
22-
syntax: 'typescript',
23-
dynamicImport: true,
24-
tsx: true,
25-
},
26-
target: 'es5',
27-
experimental: {
28-
plugins: [[require.resolve('../swc_plugin_auto_css_modules.wasm'), {}]],
29-
},
30-
minify: {
31-
mangle: false,
32-
compress: false,
33-
},
34-
},
35-
minify: false,
36-
filename: 'index.ts',
37-
sourceMaps: false,
38-
}
39-
)
13+
`)
4014

4115
assert(
4216
result.code.trim() ===

example/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
"name": "example",
33
"private": true,
44
"scripts": {
5-
"test": "echo \"Error: no test specified\" && exit 1"
5+
"test": "pnpm test:corejs && pnpm test:index",
6+
"test:corejs": "tsx ./corejs.ts",
7+
"test:index": "tsx ./index.ts"
68
},
79
"devDependencies": {
810
"@swc/core": "^1.3.24",
911
"@types/node": "^18.11.17",
1012
"@xn-sakina/mental": "^2.0.0",
1113
"tsx": "^3.12.1",
12-
"typescript": "^4.9.4"
14+
"typescript": "^4.9.4",
15+
"core-js": "^3.27.1"
1316
}
1417
}

index.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface ISwcPluginAutoCssModulesConfig {
2+
/**
3+
* lock core-js package root path
4+
* @default undefined
5+
* @example dirname(require.resolve('core-js/package.json'))
6+
*/
7+
lock_core_js_pkg_path?: string
8+
9+
/**
10+
* auto add style file suffix
11+
* @default '?modules'
12+
* @example '?module-mark'
13+
*/
14+
style_file_suffix?: string
15+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"type": "git",
1010
"url": "https://github.com/xn-sakina/swc-plugin-auto-css-modules"
1111
},
12+
"types": "index.d.ts",
1213
"keywords": [
1314
"swc-plugin",
1415
"auto-css-modules"
@@ -18,7 +19,7 @@
1819
"build": "cargo build-wasi --release",
1920
"move": "cp ./target/wasm32-wasi/release/swc_plugin_auto_css_modules.wasm .",
2021
"test:cargo": "cargo test",
21-
"test:node": "cd ./example && pnpm tsx ./index.ts",
22+
"test:node": "cd ./example && pnpm test",
2223
"test": "pnpm test:cargo && pnpm test:node",
2324
"prepublishOnly": "pnpm build && pnpm move && pnpm test",
2425
"push": "npm publish --access public --registry https://registry.npmjs.com/"

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.rs

+11-53
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,17 @@
1+
#![allow(clippy::not_unsafe_ptr_arg_deref)]
12
use swc_core::{
2-
ecma::{
3-
ast::{ImportDecl, ImportSpecifier, Program},
4-
transforms::testing::test,
5-
visit::{as_folder, FoldWith, VisitMut, VisitMutWith},
6-
},
3+
ecma::{ast::Program, visit::FoldWith},
74
plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
85
};
96

10-
pub struct TransformVisitor;
11-
12-
const CSS_EXTS: [&str; 5] = [".css", ".less", ".scss", ".sass", ".styl"];
13-
14-
impl VisitMut for TransformVisitor {
15-
fn visit_mut_import_decl(&mut self, n: &mut ImportDecl) {
16-
n.visit_mut_children_with(self);
17-
18-
if n.specifiers.len() == 1 {
19-
if let ImportSpecifier::Default(_default_specifier) = &n.specifiers[0] {
20-
let source = n.src.value.to_string();
21-
for ext in CSS_EXTS {
22-
if source.ends_with(ext) {
23-
n.src = Box::new(format!("{}?modules", source).into());
24-
break;
25-
}
26-
}
27-
}
28-
}
29-
}
30-
}
31-
327
#[plugin_transform]
33-
pub fn auto_css_modules(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
34-
program.fold_with(&mut as_folder(TransformVisitor))
35-
}
8+
pub fn auto_css_modules(program: Program, data: TransformPluginProgramMetadata) -> Program {
9+
let config = serde_json::from_str(
10+
&data
11+
.get_transform_plugin_config()
12+
.expect("failed to get plugin config for auto_css_modules"),
13+
)
14+
.expect("invalid packages");
3615

37-
test!(
38-
Default::default(),
39-
|_| as_folder(TransformVisitor),
40-
boo,
41-
r#"
42-
import a from 'foo.less';
43-
import b from 'foo.scss';
44-
import c from 'foo.sass';
45-
import d from '../foo.css';
46-
import e from './foo.styl';
47-
import { f } from 'foo';
48-
import * as g from 'foo';
49-
"#,
50-
r#"
51-
import a from "foo.less?modules";
52-
import b from "foo.scss?modules";
53-
import c from "foo.sass?modules";
54-
import d from "../foo.css?modules";
55-
import e from "./foo.styl?modules";
56-
import { f } from 'foo';
57-
import * as g from 'foo';
58-
"#
59-
);
16+
program.fold_with(&mut auto_css_modules::auto_css_modules(config))
17+
}

0 commit comments

Comments
 (0)