MCPcopy Create free account
hub / github.com/QwikDev/qwik / transform

Function transform

packages/qwik/src/optimizer/src/plugins/plugin.ts:673–814  ·  view source on GitHub ↗
(
    ctx: Rollup.PluginContext,
    code: string,
    id: string,
    transformOpts = {} as Parameters<Extract<Plugin['transform'], Function>>[2]
  )

Source from the content-addressed store, hash-verified

671
672 let transformCount = 0;
673 const transform = async function (
674 ctx: Rollup.PluginContext,
675 code: string,
676 id: string,
677 transformOpts = {} as Parameters<Extract<Plugin['transform'], Function>>[2]
678 ): Promise<Rollup.TransformResult> {
679 if (id.startsWith('\0')) {
680 return;
681 }
682 const count = transformCount++;
683 const isServer = getIsServer(transformOpts);
684 const currentOutputs = isServer ? serverTransformedOutputs : clientTransformedOutputs;
685 if (currentOutputs.has(id)) {
686 // This is a QRL segment, and we don't need to process it any further
687 return;
688 }
689
690 const optimizer = getOptimizer();
691 const path = getPath();
692
693 const { pathId } = parseId(id);
694 const parsedPathId = path.parse(pathId);
695 const dir = parsedPathId.dir;
696 const base = parsedPathId.base;
697 const ext = parsedPathId.ext.toLowerCase();
698 if (ext in TRANSFORM_EXTS || TRANSFORM_REGEX.test(pathId)) {
699 /** Strip client|server code from qwik server|client, but not in lib/test */
700 const strip = opts.target === 'client' || opts.target === 'ssr';
701 debug(
702 `transform(${count})`,
703 `Transforming ${id} (for: ${isServer ? 'server' : 'client'}${strip ? ', strip' : ''})`
704 );
705
706 const mode =
707 opts.target === 'lib' ? 'lib' : opts.buildMode === 'development' ? 'dev' : 'prod';
708
709 if (mode !== 'lib') {
710 // this messes a bit with the source map, but it's ok for if statements
711 code = code.replaceAll(/__EXPERIMENTAL__\.(\w+)/g, (_, feature) => {
712 if (opts.experimental?.[feature as ExperimentalFeatures]) {
713 return 'true';
714 }
715 return 'false';
716 });
717 }
718
719 let filePath = base;
720 if (opts.srcDir) {
721 filePath = path.relative(opts.srcDir, pathId);
722 }
723 filePath = normalizePath(filePath);
724 const srcDir = opts.srcDir ? opts.srcDir : normalizePath(dir);
725 const entryStrategy: EntryStrategy = opts.entryStrategy;
726 let devPath: string | undefined;
727 if (devServer) {
728 devPath = devServer.moduleGraph.getModuleById(pathId)?.url;
729 }
730 const transformOpts: TransformModulesOptions = {

Callers

nothing calls this directly

Calls 15

parseIdFunction · 0.90
getIsServerFunction · 0.85
getOptimizerFunction · 0.85
getPathFunction · 0.85
debugFunction · 0.85
isAdditionalFileFunction · 0.85
diagnosticsCallbackFunction · 0.85
transformModulesSyncMethod · 0.80
lintMethod · 0.80
normalizePathFunction · 0.70
hasMethod · 0.65
parseMethod · 0.65

Tested by

no test coverage detected