Skip to content

Commit

Permalink
feat: support import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Sep 20, 2022
1 parent ac75210 commit 1d3d200
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"build:debug": "napi build --platform",
"prepublishOnly": "napi prepublish -t npm",
"test": "vitest run",
"test:watch": "vitest",
"version": "napi version"
},
"devDependencies": {
Expand Down
19 changes: 16 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use swc_common::{
FileName, SourceMap,
};

use swc_ecma_ast::{ExportAll, NamedExport};
use swc_ecma_ast::{ExportAll, ImportDecl, NamedExport};
use swc_ecma_visit::{as_folder, Fold};
use swc_ecmascript::{transforms::pass::noop, visit::VisitMut};

Expand All @@ -19,7 +19,7 @@ impl VisitMut for Visitor {
fn visit_mut_export_all(&mut self, node: &mut ExportAll) {
let mut path = node.src.value.to_string();

if path.starts_with("./") || path.starts_with("../") {
if is_local_import(&path) {
path.push_str(&self.extension);
node.src.value = path.into()
}
Expand All @@ -28,11 +28,24 @@ impl VisitMut for Visitor {
fn visit_mut_named_export(&mut self, node: &mut NamedExport) {
let mut path = node.src.as_ref().unwrap().value.to_string();

if path.starts_with("./") || path.starts_with("../") {
if is_local_import(&path) {
path.push_str(&self.extension);
node.src.as_mut().unwrap().value = path.into()
}
}

fn visit_mut_import_decl(&mut self, node: &mut ImportDecl) {
let mut path = node.src.value.to_string();

if is_local_import(&path) {
path.push_str(&self.extension);
node.src.value = path.into()
}
}
}

fn is_local_import(path: &str) -> bool {
return path.starts_with("./") || path.starts_with("../");
}

fn visitor(extension: String) -> impl Fold {
Expand Down
116 changes: 88 additions & 28 deletions test/rollup-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,107 @@ import { localImport } from "../plugin";

const input = "input.js";
const output = { file: "output.js" };
const source = `
// ExportAllDeclaration
export * from "./local-file";
export * from "../file-from-parent-directory";
export * from 'some-dependency';
// ExportNamedDeclaration
export { a } from "./local-file";
export { b } from "../file-from-parent-directory";
export { c } from 'some-dependency';
`.trim();

beforeAll(() => {
writeFileSync(input, source, "utf-8");
});

afterAll(() => {
rmSync(input);
rmSync(output.file);
});

test("rollup", async () => {
const build = await rollup({
input,
external: () => true,
plugins: [localImport(".js")],
});

const bundle = await build.write({ output });
test("ExportAllDeclaration", async () => {
const output = await run(`
export * from "./local-file";
export * from "../file-from-parent-directory";
export * from 'some-dependency';
`);

expect(bundle.output).toHaveLength(1);
expect(bundle.output[0].code.trim()).toMatchInlineSnapshot(`
expect(output).toMatchInlineSnapshot(`
"export * from './local-file.js';
export { a } from './local-file.js';
export * from '../file-from-parent-directory.js';
export { b } from '../file-from-parent-directory.js';
export * from 'some-dependency';
export { c } from 'some-dependency';"
"
`);
});

test("ExportNamedDeclaration", async () => {
const output = await run(`
export { a } from "./local-file";
export { b } from "../file-from-parent-directory";
export { c } from 'some-dependency';
`);

expect(output).toMatchInlineSnapshot(`
"export { a } from './local-file.js';
export { b } from '../file-from-parent-directory.js';
export { c } from 'some-dependency';
"
`);
});

test("ImportDeclaration, default", async () => {
const output = await run(`
import a from "./local-file";
import b from "../file-from-parent-directory";
import c from 'some-dependency';
console.log(a,b,c);
`);

expect(output).toMatchInlineSnapshot(`
"import a from './local-file.js';
import b from '../file-from-parent-directory.js';
import c from 'some-dependency';
console.log(a, b, c);
"
`);
});

test("ImportDeclaration, named", async () => {
const output = await run(`
import { a } from "./local-file";
import { b } from "../file-from-parent-directory";
import { c } from 'some-dependency';
console.log(a,b,c);
`);

expect(output).toMatchInlineSnapshot(`
"import { a } from './local-file.js';
import { b } from '../file-from-parent-directory.js';
import { c } from 'some-dependency';
console.log(a, b, c);
"
`);
});

test("ImportDeclaration, side-effect", async () => {
const output = await run(`
import "./local-file";
import "../file-from-parent-directory";
import 'some-dependency';
`);

expect(output).toMatchInlineSnapshot(`
"import './local-file.js';
import '../file-from-parent-directory.js';
import 'some-dependency';
"
`);
});

test("plugin has name", () => {
expect(localImport(".js")).toHaveProperty("name", "local-import");
});

async function run(source) {
writeFileSync(input, source.trim(), "utf-8");

const build = await rollup({
input,
external: () => true,
plugins: [localImport(".js")],
});

const bundle = await build.write({ output });

return bundle.output[0].code;
}

0 comments on commit 1d3d200

Please sign in to comment.