Skip to content

Commit 2eb145f

Browse files
committed
Use a babel plugin in order to strip /src/ from the import paths
1 parent 5279646 commit 2eb145f

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

external/builder/babel-plugin-pdfjs-preprocessor.mjs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,29 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
292292
};
293293
}
294294

295+
function babelPluginStripSrcPath() {
296+
return {
297+
name: "babel-plugin-strip-src-path",
298+
visitor: {
299+
"ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration":
300+
function ({ node }) {
301+
if (node.source?.value.includes("/src/")) {
302+
node.source.value = node.source.value.replace("/src/", "/");
303+
}
304+
},
305+
},
306+
};
307+
}
308+
295309
function preprocessPDFJSCode(ctx, content) {
296310
return transformSync(content, {
297311
configFile: false,
298312
plugins: [[babelPluginPDFJSPreprocessor, ctx]],
299313
}).code;
300314
}
301315

302-
export { babelPluginPDFJSPreprocessor, preprocessPDFJSCode };
316+
export {
317+
babelPluginPDFJSPreprocessor,
318+
babelPluginStripSrcPath,
319+
preprocessPDFJSCode,
320+
};

gulpfile.mjs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import {
1717
babelPluginPDFJSPreprocessor,
18+
babelPluginStripSrcPath,
1819
preprocessPDFJSCode,
1920
} from "./external/builder/babel-plugin-pdfjs-preprocessor.mjs";
2021
import { exec, execSync, spawn, spawnSync } from "child_process";
@@ -1592,6 +1593,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
15921593
],
15931594
plugins: [
15941595
[babelPluginPDFJSPreprocessor, ctx],
1596+
[babelPluginStripSrcPath],
15951597
[
15961598
"add-header-comment",
15971599
{
@@ -1604,13 +1606,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
16041606
sourceFileName: relativeSourcePath,
16051607
});
16061608

1607-
let code = result.code;
1608-
code = code.replaceAll(
1609-
/(\sfrom\s".*?)(?:\/src)(\/[^"]*"?;)$/gm,
1610-
(all, prefix, suffix) => prefix + suffix
1611-
);
1612-
1613-
file.contents = Buffer.from(code);
1609+
file.contents = Buffer.from(result.code);
16141610
// Attach the source map to the file for gulp-sourcemaps
16151611
if (result.map) {
16161612
file.sourceMap = result.map;

0 commit comments

Comments
 (0)