Информация об изменениях

Сообщение Re[3]: Custom Loader for Typescript от 23.01.2025 2:38

Изменено 23.01.2025 2:39 bnk

Re[3]: Custom Loader for Typescript
Здравствуйте, Sinclair, Вы писали:

bnk>>Читай — vite/webpack/parcel/rollup, вот это вот.


S>Хм. А кто-то из них помогает в описанной ситуации?


В принципе они все они поддерживают лоадеры, т.е. компилироваться все будет.
Я бы спросил как написать кастомный лоадер для webpack или vite для кастомного расширения файла у GPT, он базовые вещи должен на ура генерить. Я спросил ради интереса, вот: test-custom-vite-plugin
Для vite, потому как типа по умолчанию у меня, ну и быстрее прочего. Режим разработки (вотч) там есть, запускается vite без параметров.

src/index.ts
import message from './message.custom';

console.log(message);

src/message.custom
This is the content of the custom file!

vite-custom-loader.js
export default function customFileLoader() {
  return {
    name: 'vite-plugin-custom-file-loader',

    // Transform function to handle files with .custom extension
    transform(src, id) {
      // Check if the file has the .custom extension
      if (id.endsWith('.custom')) {
        // Process the content of the .custom file
        const processedCode = processCustomFile(src);

        // Return the processed code as a JavaScript module
        return {
          code: processedCode,
          map: null, // You can generate a sourcemap if required
        };
      }
      return null; // Return null if you do not want to handle the file
    },
  };
}

// A simple function to simulate processing .custom files
function processCustomFile(code) {
  // Example: Wrap the content in a default export
  return `export default ${JSON.stringify(code)};`;
}
Re[3]: Custom Loader for Typescript
Здравствуйте, Sinclair, Вы писали:

bnk>>Читай — vite/webpack/parcel/rollup, вот это вот.


S>Хм. А кто-то из них помогает в описанной ситуации?


В принципе они все они поддерживают лоадеры, т.е. компилироваться все будет.
Я поспрашивал как написать кастомный лоадер для vite для кастомного расширения файла у GPT, он базовые вещи должен на ура генерить, вот: test-custom-vite-plugin
Для vite, потому как типа по умолчанию у меня, ну и быстрее прочего. Режим разработки (вотч) там есть, запускается vite без параметров.

src/index.ts
import message from './message.custom';

console.log(message);

src/message.custom
This is the content of the custom file!

vite-custom-loader.js
export default function customFileLoader() {
  return {
    name: 'vite-plugin-custom-file-loader',

    // Transform function to handle files with .custom extension
    transform(src, id) {
      // Check if the file has the .custom extension
      if (id.endsWith('.custom')) {
        // Process the content of the .custom file
        const processedCode = processCustomFile(src);

        // Return the processed code as a JavaScript module
        return {
          code: processedCode,
          map: null, // You can generate a sourcemap if required
        };
      }
      return null; // Return null if you do not want to handle the file
    },
  };
}

// A simple function to simulate processing .custom files
function processCustomFile(code) {
  // Example: Wrap the content in a default export
  return `export default ${JSON.stringify(code)};`;
}