-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48184cf
commit d407343
Showing
9 changed files
with
180 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.cjs |
91 changes: 91 additions & 0 deletions
91
modules/dev-tools/ts-plugins/ts-transform-append-extension/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* TypeScript transform to append file extension to import statements in the compiled JS files | ||
* Usage with ts-patch: | ||
{ | ||
"plugins": [ | ||
{ | ||
"transform": "ocular-dev-tools/ts-transform-append-extension", | ||
"extensions": [".js"], | ||
"after": true | ||
} | ||
] | ||
} | ||
* Adapted from https://github.com/murolem/ts-transformer-append-js-extension to support custom extensions | ||
*/ | ||
import * as path from 'path'; | ||
import type {Program, TransformationContext, SourceFile, Node} from 'typescript'; | ||
import type {TransformerExtras, PluginConfig} from 'ts-patch'; | ||
|
||
type AppendExtensionPluginConfig = PluginConfig & { | ||
/** List of file extensions, for example: | ||
* '.js': applies to paths without an extension, e.g. `import {add} from './math'` => `import {add} from './math.js'` | ||
* '.lib.cjs': applies to paths ending with .lib, e.g. `import fft from './fft.lib` => `import fft from './fft.lib.cjs'` | ||
* @default [".js"] | ||
*/ | ||
extensions?: string[]; | ||
}; | ||
|
||
export default function ( | ||
program: Program, | ||
pluginConfig: AppendExtensionPluginConfig, | ||
{ts}: TransformerExtras | ||
) { | ||
// only append .js when module specifier has no extension or user-provided extensions | ||
const {extensions = ['.js']} = pluginConfig; | ||
const extMappings = new Map<string, string>(); | ||
for (const ext of extensions) { | ||
const addition = path.extname(ext) || ext; | ||
const base = path.basename(ext, addition); | ||
extMappings.set(base, addition); | ||
} | ||
|
||
function shouldMutateModuleSpecifier(node: Node): string | false { | ||
if (!ts.isImportDeclaration(node) && !ts.isExportDeclaration(node)) return false; | ||
if (node.moduleSpecifier === undefined) return false; | ||
// only when module specifier is valid | ||
if (!ts.isStringLiteral(node.moduleSpecifier)) return false; | ||
// only when path is relative | ||
if (!node.moduleSpecifier.text.startsWith('./') && !node.moduleSpecifier.text.startsWith('../')) | ||
return false; | ||
// only when module specifier has accepted extension | ||
const ext = path.extname(node.moduleSpecifier.text); | ||
if (!extMappings.has(ext)) return false; | ||
return node.moduleSpecifier.text + extMappings.get(ext); | ||
} | ||
|
||
return (ctx: TransformationContext) => { | ||
const {factory} = ctx; | ||
|
||
return (sourceFile: SourceFile) => { | ||
function visit(node: Node): Node { | ||
const newImportSource = shouldMutateModuleSpecifier(node); | ||
if (newImportSource) { | ||
if (ts.isImportDeclaration(node)) { | ||
const newModuleSpecifier = factory.createStringLiteral(newImportSource); | ||
node = factory.updateImportDeclaration( | ||
node, | ||
node.modifiers, | ||
node.importClause, | ||
newModuleSpecifier, | ||
node.assertClause | ||
); | ||
} else if (ts.isExportDeclaration(node)) { | ||
const newModuleSpecifier = factory.createStringLiteral(newImportSource); | ||
node = factory.updateExportDeclaration( | ||
node, | ||
node.modifiers, | ||
node.isTypeOnly, | ||
node.exportClause, | ||
newModuleSpecifier, | ||
node.assertClause | ||
); | ||
} | ||
} | ||
|
||
return ts.visitEachChild(node, visit, ctx); | ||
} | ||
|
||
return ts.visitNode(sourceFile, visit); | ||
}; | ||
}; | ||
} |
73 changes: 73 additions & 0 deletions
73
modules/dev-tools/ts-plugins/ts-transform-version-inline/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* TypeScript transform to inject the current version of the package as string. | ||
* Usage with ts-patch: | ||
{ | ||
"plugins": [ | ||
{ | ||
"transform": "ocular-dev-tools/ts-transform-version-inline", | ||
"identifier": "PACKAGE_VERSION" | ||
} | ||
] | ||
} | ||
*/ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import type {Program, TransformationContext, SourceFile, Node} from 'typescript'; | ||
import type {TransformerExtras, PluginConfig} from 'ts-patch'; | ||
|
||
type VersionInlinePluginConfig = PluginConfig & { | ||
/** Identifier name to replace in code. | ||
* @default "__VERSION__" | ||
*/ | ||
identifier?: string; | ||
}; | ||
|
||
export default function ( | ||
program: Program, | ||
pluginConfig: VersionInlinePluginConfig, | ||
{ts}: TransformerExtras | ||
) { | ||
const {identifier = '__VERSION__'} = pluginConfig; | ||
|
||
return (ctx: TransformationContext) => { | ||
const {factory} = ctx; | ||
|
||
return (sourceFile: SourceFile) => { | ||
let packageVersion: string | null; | ||
|
||
function visit(node: Node): Node { | ||
if (ts.isIdentifier(node) && node.getText() === identifier) { | ||
if (packageVersion === undefined) { | ||
packageVersion = getPackageVersion(sourceFile.fileName); | ||
} | ||
if (packageVersion) { | ||
return factory.createStringLiteral(packageVersion); | ||
} | ||
} | ||
return ts.visitEachChild(node, visit, ctx); | ||
} | ||
return ts.visitNode(sourceFile, visit); | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
* Retrieve the version string from the closest package.json | ||
*/ | ||
function getPackageVersion(fileName: string): string | null { | ||
let currentDir = fileName; | ||
while (currentDir !== '/') { | ||
try { | ||
currentDir = path.dirname(currentDir); | ||
const packageJson = path.join(currentDir, 'package.json'); | ||
const stat = fs.statSync(packageJson); | ||
if (stat.isFile()) { | ||
const content = fs.readFileSync(packageJson, 'utf8'); | ||
return JSON.parse(content).version as string; | ||
} | ||
} catch { | ||
// file does not exist, try going up | ||
} | ||
} | ||
return null; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "esnext", | ||
"allowJs": true, | ||
"module": "commonjs", | ||
"allowJs": false, | ||
"checkJs": false, | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"noEmit": true, | ||
"baseUrl": ".", | ||
"skipLibCheck": true, | ||
"strict": true | ||
}, | ||
"include":[ | ||
"src/**/*" | ||
"src/**/*", | ||
"ts-plugins/**/*" | ||
] | ||
} |