logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Start
      Introduction
      Quick Start
      Upgrading
      Glossary
      Tech Stack
      Core Concept
      Page Entry
      Build Engine
      Web Server
      Basic Features
      Routes
      Routing
      Config Routes
      Data Solution
      Data Fetching
      Data Writing
      Data Caching
      Rendering
      Server-Side Rendering
      Streaming SSR
      Rendering Cache
      Static Site Generation
      Render Preprocessing
      Styling
      Styling
      Use CSS Modules
      Using CSS-in-JS
      Using Tailwind CSS
      HTML Template
      Import Static Assets
      Import JSON Files
      Import SVG Assets
      Import Wasm Assets
      Debug
      Data Mocking
      Network Proxy
      Using Rsdoctor
      Using Storybook
      Testing
      Playwright
      Vitest
      Jest
      Cypress
      Path Alias
      Environment Variables
      Output Files
      Deploy Application
      Advanced Features
      Using Rspack
      Using BFF
      Basic Usage
      Runtime Framework
      Extend BFF Server
      Extend Request SDK
      File Upload
      Cross-Project Invocation
      Optimize Page Performance
      Code Splitting
      Inline Static Assets
      Bundle Size Optimization
      React Compiler
      Improve Build Performance
      Browser Compatibility
      Low-Level Tools
      Source Code Build Mode
      Server Monitor
      Monitors
      Logs Events
      Metrics Events
      Internationalization
      Basic Concepts
      Quick Start
      Configuration
      Locale Detection
      Resource Loading
      Routing Integration
      API Reference
      Advanced Usage
      Best Practices
      Custom Web Server
      Topic Detail
      Module Federation
      Introduction
      Getting Started
      Application-Level Modules
      Server-Side Rendering
      Deployment
      Integrating Internationalization
      FAQ
      Dependencies FAQ
      CLI FAQ
      Build FAQ
      HMR FAQ
      Deprecated
      📝 Edit this page
      Previous pageImport Static AssetsNext pageImport SVG Assets

      #Import JSON Files

      Modern.js supports import JSON files in code by default. You can use Rsbuild plugins to support importing YAML and Toml files and converting them to JSON format.

      #JSON file

      You can import JSON files directly in JavaScript files.

      #Example

      example.json
      {
        "name": "foo",
        "items": [1, 2]
      }
      index.js
      import example from './example.json';
      
      console.log(example.name); // 'foo';
      console.log(example.items); // [1, 2];

      #Named Import

      Modern.js does not support importing JSON files via named import yet:

      import { name } from './example.json';

      #YAML file

      YAML is a data serialization language commonly used for writing configuration files.

      You can configure the YAML plugin in modern.config.ts to support importing .yaml or .yml files, they will be automatically converted to JSON format.

      import { defineConfig } from '@modern-js/app-tools';
      import { pluginYaml } from '@rsbuild/plugin-yaml';
      
      export default defineConfig({
        plugins: [pluginYaml()],
      });

      #Example

      example.yaml
      ---
      hello: world
      foo:
        bar: baz
      import example from './example.yaml';
      
      console.log(example.hello); // 'world';
      console.log(example.foo); // { bar: 'baz' };

      #Add type declaration

      When you import a YAML file in your TypeScript code, please create a src/global.d.ts file in your project and add the corresponding type declaration:

      src/global.d.ts
      declare module '*.yaml' {
        const content: Record<string, any>;
        export default content;
      }
      
      declare module '*.yml' {
        const content: Record<string, any>;
        export default content;
      }

      #Toml file

      Toml is a semantically explicit, easy-to-read configuration file format.

      You can configure the TOML plugin in modern.config.ts to support importing .toml files, it will be automatically converted to JSON format.

      import { defineConfig } from '@modern-js/app-tools';
      import { pluginToml } from '@rsbuild/plugin-toml';
      
      export default defineConfig({
        plugins: [pluginToml()],
      });

      #Example

      example.toml
      hello = "world"
      
      [foo]
      bar = "baz"
      import example from './example.toml';
      
      console.log(example.hello); // 'world';
      console.log(example.foo); // { bar: 'baz' };

      #Add type declaration

      When you import Toml files in TypeScript code, please create a src/global.d.ts file in your project and add the corresponding type declarations:

      src/global.d.ts
      declare module '*.toml' {
        const content: Record<string, any>;
        export default content;
      }