Skip to content

Commit

Permalink
Extract helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
thesoftwarephilosopher committed Aug 14, 2024
1 parent 57cf4d4 commit ba0ec8f
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,7 @@ export class Compiler {
sourceMaps: 'inline',
module: { type: 'es6' },
plugin: (program) => {
if (browserFilePath) {
for (const imp of program.body) {
if (imp.type === 'ImportDeclaration') {
const dep = imp.source.value;
const version = (
this.packageJson.devDependencies[dep] ??
this.packageJson.dependencies[dep]
);
if (version) {
delete imp.source.raw;
imp.source.value = `https://cdn.jsdelivr.net/npm/${dep}@${version}/+esm`;
}
else {
const typeDep = '@types/' + dep.replace(/^@(.+?)\/(.+)/, '$1__$2');
if (this.packageJson.devDependencies[typeDep]) {
delete imp.source.raw;
imp.source.value = `https://cdn.jsdelivr.net/npm/${dep}/+esm`;
}
}
}
}
}
return program;
return this.#renameImports(program, browserFilePath);
},
jsc: {
parser: {
Expand Down Expand Up @@ -72,4 +50,30 @@ export class Compiler {
return result;
}

#renameImports(program: swc.Program, browserFilePath?: string): swc.Program {
if (browserFilePath) {
for (const imp of program.body) {
if (imp.type === 'ImportDeclaration') {
const dep = imp.source.value;
const version = (
this.packageJson.devDependencies[dep] ??
this.packageJson.dependencies[dep]
);
if (version) {
delete imp.source.raw;
imp.source.value = `https://cdn.jsdelivr.net/npm/${dep}@${version}/+esm`;
}
else {
const typeDep = '@types/' + dep.replace(/^@(.+?)\/(.+)/, '$1__$2');
if (this.packageJson.devDependencies[typeDep]) {
delete imp.source.raw;
imp.source.value = `https://cdn.jsdelivr.net/npm/${dep}/+esm`;
}
}
}
}
}
return program;
}

}

0 comments on commit ba0ec8f

Please sign in to comment.