Skip to content

Commit

Permalink
fix: better support js viritual modules
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Aug 11, 2023
1 parent c6017c9 commit 297d053
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/transform/create-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export default ({ browser }: { browser: boolean }) => {
.join("|")}|\\.(?:${config.moduleFileExtensions.join("|")})))$`
);

concatMap.add(filename, code, map);

for (let dep of deps) {
if (typeof dep !== "string") {
if (dep.virtualPath) {
Expand All @@ -92,10 +94,22 @@ export default ({ browser }: { browser: boolean }) => {

for (const key in acceptedMatch.groups) {
if (acceptedMatch.groups[key] !== undefined) {
const [, nestedTransformerPath, nestedTransformerOpts] =
config.transform[parseInt(key.slice(1), 10)];
// eslint-disable-next-line @typescript-eslint/no-var-requires
const transformResult = require(config.transform[
parseInt(key.slice(1), 10)
][1]).process(depCode, dep.virtualPath, transformOptions);
const nestedTransformerModule = require(nestedTransformerPath);
const nestedTransformer =
nestedTransformerModule.createTransformer
? nestedTransformerModule.createTransformer(
nestedTransformerOpts
)
: nestedTransformerModule;
const transformResult = nestedTransformer.process
? nestedTransformer.process(depCode, dep.virtualPath, {
...transformOptions,
...nestedTransformerOpts,
})
: dep.code;

if (typeof transformResult === "object") {
depCode = transformResult.code;
Expand All @@ -109,21 +123,20 @@ export default ({ browser }: { browser: boolean }) => {
}
}

concatMap.add(null, `((module, exports) => {`);
concatMap.add(null, `\n;((module, exports) => {`);
concatMap.add(dep.virtualPath, depCode, depMap);
concatMap.add(null, `})({ exports: {} })`);
concatMap.add(null, `\n})({ exports: {} })`);
continue;
} else {
dep = dep.path;
}
}

if (acceptPathReg.test(dep)) {
concatMap.add(null, `require(${JSON.stringify(dep)})`);
concatMap.add(null, `\n;require(${JSON.stringify(dep)})`);
}
}

concatMap.add(filename, code, map);
code = concatMap.content.toString("utf-8");
map = concatMap.sourceMap;
}
Expand Down

0 comments on commit 297d053

Please sign in to comment.