Skip to content

Commit

Permalink
typescript path plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprudhomme committed Jan 31, 2025
1 parent b0ec0db commit 7d0afbb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 22 deletions.
16 changes: 0 additions & 16 deletions packages/atomic/.storybook/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ const config: StorybookConfig = {
plugins: [
nxViteTsPaths(),
resolveStorybookUtilsImports(),
resolveSrcImports(),
forceInlineCssImports(),
configType === 'PRODUCTION' && isCDN && externalizeDependencies(),
],
Expand All @@ -89,21 +88,6 @@ const resolveStorybookUtilsImports: PluginImpl = () => {
};
};

const resolveSrcImports: PluginImpl = () => {
return {
name: 'resolve-storybook-utils-imports',
async resolveId(source: string, importer, options) {
if (source.startsWith('@/src')) {
return this.resolve(
source.replace('@/src', path.resolve(__dirname, '../src')),
importer,
options
);
}
},
};
};

const forceInlineCssImports: PluginImpl = () => {
return {
name: 'force-inline-css-imports',
Expand Down
1 change: 1 addition & 0 deletions packages/atomic/dev/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
<atomic-search-box></atomic-search-box>
</atomic-layout-section>
<atomic-layout-section section="facets">
<atomic-boom></atomic-boom>
<atomic-facet-manager>
<atomic-automatic-facet-generator desired-count="3"></atomic-automatic-facet-generator>
<atomic-category-facet field="geographicalhierarchy" label="World Atlas" with-search>
Expand Down
3 changes: 2 additions & 1 deletion packages/atomic/scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createProgram,
flattenDiagnosticMessageText,
} from 'typescript';
import pathTransformer from './path-transform.mjs';
import svgTransformer from './svg-transform.mjs';

const args = argv.slice(2);
Expand Down Expand Up @@ -38,7 +39,7 @@ function emit(program) {
const writeFile = undefined;
const emitOnlyDtsFiles = false;
const customTransformers = {
before: [svgTransformer],
before: [svgTransformer, pathTransformer],
};

return program.emit(
Expand Down
50 changes: 50 additions & 0 deletions packages/atomic/scripts/path-transform.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {readFileSync} from 'fs';
import {basename, dirname, join, resolve, relative} from 'path';
import {
NodeFlags,
isImportDeclaration,
visitEachChild,
visitNode,
isStringLiteral,
} from 'typescript';

/**
* Custom SVG transformer to handle .svg imports.
*/
export default function pathTransformer(context) {
const {factory} = context;

function visit(node, sourceFile) {
if (isImportDeclaration(node) && isStringLiteral(node.moduleSpecifier)) {
const importPath = node.moduleSpecifier.text;

if (importPath.startsWith('@/')) {
const relativePath = getRelativeImportPath(
sourceFile.fileName,
importPath
);

console.log(relativePath);

return factory.updateImportDeclaration(
node,
node.modifiers,
node.importClause,
factory.createStringLiteral(relativePath),
node.assertClause
);
}
}
return visitEachChild(node, (child) => visit(child, sourceFile), context);
}

return (sourceFile) =>
visitNode(sourceFile, (node) => visit(node, sourceFile));
}

function getRelativeImportPath(sourceFilePath, importPath) {
const basePath = resolve(process.cwd());
const absoluteImportPath = resolve(basePath, importPath.replace('@/', ''));
const relativePath = relative(dirname(sourceFilePath), absoluteImportPath);
return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
}
5 changes: 4 additions & 1 deletion packages/atomic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
"no-nullable-attribute-binding": "warning"
}
}
]
],
"paths": {
"@/*": ["./*"]
}
},
"exclude": ["node_modules", "src/external-builds"],
"include": ["src"]
Expand Down
5 changes: 1 addition & 4 deletions packages/atomic/tsconfig.storybook.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
"emitDecoratorMetadata": true,
"composite": true,
"jsxFactory": "h",
"declaration": true,
"paths": {
"@/*": ["./*"]
}
"declaration": true
},
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"],
"include": [
Expand Down

0 comments on commit 7d0afbb

Please sign in to comment.