typescript rollup.js live reload not updating

cyvaqqii  于 2023-05-19  发布在  TypeScript
关注(0)|答案(2)|浏览(142)

我正在使用Rollup.js构建一个react应用程序,并尝试使用live reload来实现更好的DX。
我安装了所有的软件包,第一次运行时一切看起来都很好。
当我尝试更新源代码时,bundle被重新构建&页面被刷新,但更改不会出现在编译后的JS中。如果我结束该过程并重新启动,则会出现更改。
rollup.config.ts随附如下:

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace';
import url from './renderer/rollupImageAssetsUrl';
import rollupUrl from '@rollup/plugin-url';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import dts from 'rollup-plugin-dts';
import svgr from '@svgr/rollup';
import type { RollupOptions } from 'rollup'
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';

const devConfig : RollupOptions = {
  input: './template/index.tsx',
  output: {
    file: './template/build/index.js',
    format: 'iife'
  },
  plugins: [
    replace({
      'process.env.NODE_ENV': JSON.stringify( 'production' ),
      CLASS_TYPE : process.env.CLASS_TYPE ? JSON.stringify(process.env.CLASS_TYPE) : JSON.stringify(`oocss`)
    }),
    resolve({
      browser: true,
      dedupe: ['react', 'react-dom'],
    }),
    typescript({
      tsconfig: './renderer/tsconfig.json',
      declaration: false,
      paths : {
        "component-library/style-library" : ["./../src/index.ts"]
      }
    }),
    commonjs(),
    url({
      fileName: 'images/[name][extname]',
      exclude: ['**/src/Icons/**/**.svg'],
      limit: 0,
      preserveImports: true
    }),
    rollupUrl({
      include: ['**/src/Icons/**/**.svg']
    }),
    svgr({
      icon: true,
      replaceAttrValues : {
        "#000" : "currentColor",
        "black" : "currentColor",
        "#000000" : "currentColor"
      },
      svgProps: {
        'pointerEvents' : 'none',
      }
    }),
    serve({
      contentBase: './template',
      host: '0.0.0.0',
      port: '3002',
    }),
    livereload({
      port: 3003,
      delay: 200
    }),
  ],
  watch: {
    buildDelay: 500,
    clearScreen: true
  }
}

export default devConfig;

我的Docker设置:

version: '3.9'
services:
  builder:
    container_name: style_library_package_builder_dev
    build:
      context: .
      dockerfile: docker_images/builder/Dockerfile
    image: style-library-package-builder:v0.2.0
    ports:
      - "3002:3002"
      - "3003:3003"
    environment:
      - "TZ=Asia/Tokyo"
      - "CHOKIDAR_USEPOLLING=true"
      - "WATCHPACK_POLLING=true"
    volumes:
      - ./app/.storybook:/var/app/builder/.storybook
      - ./app/renderer:/var/app/builder/renderer
      - ./app/utils:/var/app/builder/utils
      - ./app/template:/var/app/builder/template
      - ./app/package.json:/var/app/builder/package.json
      - ./app/rollup.config.ts:/var/app/builder/rollup.config.ts
      - ./app/rollup.config.dev.ts:/var/app/builder/rollup.config.dev.ts
      - ./app/tsconfig.json:/var/app/builder/tsconfig.json
      - ./app/webpack.config.style.ts:/var/app/builder/webpack.config.style.ts
      - ./app/yarn.lock:/var/app/builder/yarn.lock
      - ./app/.babelrc.json:/var/app/builder/.babelrc.json
      - ./component-library/style-library/builder/src:/var/app/builder/src
      - ./component-library/style-library/builder/assets:/var/app/builder/assets
      - ./component-library/style-library/builder/stories:/var/app/builder/stories
      - ./component-library/style-library/builder/config:/var/app/builder/config
      - ./component-library/style-library/lib:/var/app/lib
      - ./component-library/style-library/package.json:/var/app/package.json
      - ./component-library/package.json:/var/package.json
      - ./component-library/docs:/var/docs
      - /var/app/builder/node_modules
    tty: true
    privileged: true
    secrets:
      - host_ssh_key
secrets:
  host_ssh_key:
    file: ${KEY}

我在Docker容器中运行rollup.js(w/ image node:18-alpine3.15)。
你能想到这种行为的原因吗?任何帮助都很感激。
尝试了CHOKIDAR_USEPOLLING=truerollup.watch.chokidar.usePolling = true,但没有成功
编辑:我已经尝试编辑图像,似乎更新的图像反映在观看模式。但是当我更新Typescripts文件时,仍然没有运气发生:(
生成新的捆绑包,但不包括更改。@rollup/typescript插件在watch模式下会有问题吗?

3npbholx

3npbholx1#

在你的docker yml配置文件中,你有以下设置:
CHOKIDAR_USEPOLLING=true
您可以参考以下内容了解更多详情:Live reload React with Docker

taor4pac

taor4pac2#

经过一天的尝试,我终于发现在使用Rollup.JS的watch模式时,@rollup/plugin-typescript(v11.1.1)存在问题。
我已经修改了我的rollup.config.ts,并将我的Typescript转译器从@rollup/plugin-typescript改为@rollup/plugin-babel,一切都很完美。
附上我目前的设置,供大家来解决这个问题:

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace';
import url from './renderer/rollupImageAssetsUrl';
import rollupUrl from '@rollup/plugin-url';
import svgr from '@svgr/rollup';
import type { RollupOptions } from 'rollup'
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';
import html from '@rollup/plugin-html';
import { babel } from '@rollup/plugin-babel';

const devConfig : RollupOptions = {
  cache: false,
  input: './template/index.tsx',
  output: {
    file: './build/index.js',
    format: 'iife'
  },
  plugins: [
    replace({
      preventAssignment: true,
      'process.env.NODE_ENV': JSON.stringify( 'production' ),
      CLASS_TYPE : process.env.CLASS_TYPE ? JSON.stringify(process.env.CLASS_TYPE) : JSON.stringify(`oocss`)
    }),
    resolve({
      browser: true,
      dedupe: ['react', 'react-dom'],
    }),
    typescript({
      cacheDir: './build/cache',
      tsconfig: './renderer/tsconfig.json',
      declaration : true,
      outDir : './build/types',
      emitDeclarationOnly: true,
      noForceEmit: true,
      paths : {
        "component-library/style-library" : ["./../src/index.ts"]
      }
    }),
    babel({
      exclude: "**/node_modules/**",
      presets: ["@babel/preset-env","@babel/preset-react","@babel/preset-typescript"],
      extensions: [".js", ".jsx", ".ts", ".tsx"],
    }),
    commonjs(),
    url({
      fileName: 'images/[name][extname]',
      exclude: ['**/src/Icons/**/**.svg'],
      limit: 0,
      preserveImports: true
    }),
    rollupUrl({
      include: ['**/src/Icons/**/**.svg']
    }),
    svgr({
      icon: true,
      replaceAttrValues : {
        "#000" : "currentColor",
        "black" : "currentColor",
        "#000000" : "currentColor"
      },
      svgProps: {
        'pointerEvents' : 'none',
      }
    }),
    html(),
    serve({
      contentBase: './build',
      host: '0.0.0.0',
      port: '3002',
    }),
    livereload({
      port: 3003,
      delay: 500
    })
  ],
  watch: {
    chokidar: {
      usePolling: true
    },
    buildDelay: 500
  }
}

export default () => {
  return devConfig;
};

我仍然在这里留下了@rollup/plugin-typescript用于类型检查和路径别名。
稍后会在Github issue中报告这一点。

相关问题