def compile_js()

in understanding_rl_vision/svelte3/compiling.py [0:0]


def compile_js(svelte_path, js_path=None, *, js_name=None, js_lint=None):
    """Compile Svelte to JavaScript.

    Arguments:
        svelte_path: path to input Svelte file
        js_path:     path to output JavaScript file
                     defaults to svelte_path with a new .js suffix
        js_name:     name of JavaScript global variable
                     defaults to _default_js_name
        js_lint:     whether to use eslint
                     defaults to True
    """
    if js_path is None:
        js_path = replace_file_extension(svelte_path, ".js")
    if js_name is None:
        js_name = _default_js_name
    if js_lint is None:
        js_lint = True

    eslint_config_fd, eslint_config_path = tempfile.mkstemp(
        suffix=".config.json", prefix="eslint_", dir=_temp_config_dir, text=True
    )
    eslint_config_path = os.path.abspath(eslint_config_path)
    rollup_config_fd, rollup_config_path = tempfile.mkstemp(
        suffix=".config.js", prefix="rollup_", dir=_temp_config_dir, text=True
    )
    rollup_config_path = os.path.abspath(rollup_config_path)

    svelte_dir = os.path.dirname(svelte_path) or os.curdir
    svelte_relpath = os.path.relpath(svelte_path, start=svelte_dir)
    js_relpath = os.path.relpath(js_path, start=svelte_dir)

    with open(eslint_config_fd, "w") as eslint_config_file:
        json.dump(
            {
                "env": {"browser": True, "es6": True},
                "extends": "eslint:recommended",
                "globals": {"Atomics": "readonly", "SharedArrayBuffer": "readonly"},
                "parserOptions": {"ecmaVersion": 2018, "sourceType": "module"},
                "plugins": ["svelte3"],
                "overrides": [{"files": ["*.svelte"], "processor": "svelte3/svelte3"}],
                "rules": {},
            },
            eslint_config_file,
        )

    with open(rollup_config_fd, "w") as rollup_config_file:
        rollup_config_file.write(
            """import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
import { eslint } from 'rollup-plugin-eslint';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import path from 'path';

export default {
  input: '"""
            + svelte_relpath
            + """',
  output: {
    file: '"""
            + js_relpath
            + """',
    format: 'iife',
    name: '"""
            + js_name
            + """'
  },
  plugins: [
    eslint({
      include: ['**'],
      """
            + ("" if js_lint else "exclude: ['**'],")
            + """
      configFile: '"""
            + eslint_config_path
            + """'
    }),
    svelte({
      include: ['"""
            + svelte_relpath
            + """', '**/*.svelte']
    }),
    resolve({
      customResolveOptions: {
        paths: process.env.NODE_PATH.split( /[;:]/ )
      }
    }),
    commonjs(),
    babel({
      include: ['**', path.resolve(process.env.NODE_PATH, 'svelte/**')],
      extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.svelte'],
      babelrc: false,
      cwd: process.env.NODE_PATH,
      presets: [['@babel/preset-env', {useBuiltIns: 'usage', corejs: 3}]]
    })
  ]
}
"""
        )

    with use_cwd(os.path.dirname(os.path.realpath(__file__)) or os.curdir):
        try:
            npm_root = shell_command("npm root --quiet")
        except CompileError as exn:
            raise CompileError(
                "Unable to find npm root.\nHave you installed Node.js?"
            ) from exn
        try:
            shell_command("npm ls")
        except CompileError as exn:
            shell_command("npm install")

    with use_cwd(svelte_dir):
        env = os.environ.copy()
        env["PATH"] = os.path.join(npm_root, ".bin") + ":" + env["PATH"]
        env["NODE_PATH"] = npm_root
        command_output = shell_command("rollup -c " + rollup_config_path, env=env)

    return {
        "js_path": js_path,
        "js_name": js_name,
        "command_output": command_output,
    }