Use a babel plugin in order to strip /src/ from the import paths

This commit is contained in:
calixteman 2026-03-01 22:17:30 +01:00
parent 5279646985
commit 2eb145f3f1
No known key found for this signature in database
GPG Key ID: 0C5442631EE0691F
2 changed files with 22 additions and 8 deletions

View File

@ -292,6 +292,20 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
};
}
function babelPluginStripSrcPath() {
return {
name: "babel-plugin-strip-src-path",
visitor: {
"ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration":
function ({ node }) {
if (node.source?.value.includes("/src/")) {
node.source.value = node.source.value.replace("/src/", "/");
}
},
},
};
}
function preprocessPDFJSCode(ctx, content) {
return transformSync(content, {
configFile: false,
@ -299,4 +313,8 @@ function preprocessPDFJSCode(ctx, content) {
}).code;
}
export { babelPluginPDFJSPreprocessor, preprocessPDFJSCode };
export {
babelPluginPDFJSPreprocessor,
babelPluginStripSrcPath,
preprocessPDFJSCode,
};

View File

@ -15,6 +15,7 @@
import {
babelPluginPDFJSPreprocessor,
babelPluginStripSrcPath,
preprocessPDFJSCode,
} from "./external/builder/babel-plugin-pdfjs-preprocessor.mjs";
import { exec, execSync, spawn, spawnSync } from "child_process";
@ -1592,6 +1593,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
],
plugins: [
[babelPluginPDFJSPreprocessor, ctx],
[babelPluginStripSrcPath],
[
"add-header-comment",
{
@ -1604,13 +1606,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
sourceFileName: relativeSourcePath,
});
let code = result.code;
code = code.replaceAll(
/(\sfrom\s".*?)(?:\/src)(\/[^"]*"?;)$/gm,
(all, prefix, suffix) => prefix + suffix
);
file.contents = Buffer.from(code);
file.contents = Buffer.from(result.code);
// Attach the source map to the file for gulp-sourcemaps
if (result.map) {
file.sourceMap = result.map;