diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ad464d..ba1f121 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,14 +3,16 @@ name: CI on: pull_request: push: - branches: [ main ] + branches: + - 'main' + - 'v[0-9]+' jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - run: npm ci --ignore-scripts + - run: npm install --ignore-scripts - run: echo "$PWD/node_modules/.bin" >> "$GITHUB_PATH" - run: deno test --no-run - run: deno lint @@ -19,5 +21,5 @@ jobs: - run: lib/cli.js --outDir nodejs/ - run: git diff --no-index lib/ nodejs/ - - run: deno test --allow-read=. + - run: deno test --allow-read=. src/ - run: fdt lib/**/*.test.js diff --git a/src/_transformations/specifiers.ts b/src/_transformations/specifiers.ts index 1bf693b..bea9ab2 100644 --- a/src/_transformations/specifiers.ts +++ b/src/_transformations/specifiers.ts @@ -1,26 +1,14 @@ +import { Node, type SourceFile } from "../deps.deno.ts"; import * as re from "../util/regexp.ts"; -export const name = /(?:@[\w.-]+\/)?[\w.-]+/; -const version = /[^/?]+/; +export const services = ["npm:", "https://esm.sh/", "https://cdn.skypack.dev/"]; +export const name = /^(?:@[\w.-]+\/)?[\w.-]+$/; +const version = /^@[^/?]+$/; const path = /\/[^?]*/; +const url = re.tag()`^${re.union(services)}(${name})${version}?(${path})?.*$`; -const patterns = [ - re.tag()`^npm:(${name})(?:@${version})?(${path})?`, - re.tag()`^https://esm\.sh/(${name})(?:@${version})?(${path})?`, - re.tag()`^https://cdn\.skypack\.dev/(${name})(?:@${version})?(${path})?`, - re.tag()`^https://deno\.land/std(?:@${version})?/node/([\w/]+)\.ts$`, - re.tag()`^https://nest\.land/std/node/${version}/([\w/]+)\.ts$`, -]; - -const transpileHttpsImport = (specifier: string) => { - for (const pattern of patterns) { - const match = pattern.exec(specifier); - if (match === null) continue; - const [, pkg, path = ""] = match; - return pkg + path; - } - return specifier; -}; +const transpileHttpsImport = (specifier: string) => + specifier.replace(url, "$1$2"); const transpileRelativeImport = (specifier: string) => specifier @@ -33,3 +21,17 @@ export const transpileSpecifier = (specifier: string) => { if (isRelative(specifier)) return transpileRelativeImport(specifier); return transpileHttpsImport(specifier); }; + +export function transpileSpecifiers(sourceFile: SourceFile) { + for (const statement of sourceFile.getStatements()) { + if ( + Node.isImportDeclaration(statement) || + Node.isExportDeclaration(statement) + ) { + const modSpecifierValue = statement.getModuleSpecifierValue(); + if (modSpecifierValue !== undefined) { + statement.setModuleSpecifier(transpileSpecifier(modSpecifierValue)); + } + } + } +} diff --git a/src/util/regexp.ts b/src/util/regexp.ts index 068694a..36667b9 100644 --- a/src/util/regexp.ts +++ b/src/util/regexp.ts @@ -3,7 +3,7 @@ export type RegExpSource = Pick; const _source = (arg: string | RegExpSource) => typeof arg === "string" ? arg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // escape string - : arg.source; + : arg.source.replace(/^\^|\$$/g, ""); export const tag = (flags = "") => (