IT한 것

rollup.js 패키징시 파일 제외하는 방법

lovian 2023. 2. 22. 11:59

node.js 모듈 번들링을 rollup.js을 이용해봤다.

간단하게 적용해보고 마음에 드는 편이라, CI에 적용을 하기 위해 패키징까지 시도한 상황

 

목표

  • 패키지 디렉터리 생성
  • 각종 js파일을 bundle.js로 묶기 (rollup의 기본 기능)
  • 필요한 리소스 파일을 패키지 디렉터리에 복사

아래가 내 rollup.config.js

export default [
    {
        input: 'src/index.ts',
        output: {
            file: 'build/bin/seport.js',
            format: 'es',
            sourcemap: true
        },
        plugins: [            
            del({
                targets: './build'
            }),
            nodeResolve({
                preferBuiltins: true
            }),
            typescript(),            
            json(),
            copy({
                targets: [
                    {src: './predist/*', dest: './build'},
                ]
            })            
        ]
    }
]

 

ts 파일을 잘 묶어서 build 디렉터리에 넣고, predist 디렉터리의 모든 요소를 build 디렉터리에 복사한다.

 

처음 만들때는 딱 좋았는데, 개발 하면서보니
predist에 logs 디렉터리의 로그 파일이 복사되는 것을 확인했다.

 

흔하디 흔한 exclude 속성이 있을것 이라고 생각했는데 없음.

 

내가 사용하는 copy 플러그인은 (https://github.com/vladshcherbin/rollup-plugin-copy)

exclude 기능이 없길레 포기하려던 차에 눈에 밟히는 문구

ll other options are passed to packages, used inside:

 

느낌적인 느낌으로 fs-extra copy를 살펴보니 filter가 있네

  • src <String> Note that if src is a directory it will copy everything inside of this directory, not the entire directory itself (see issue #537).
  • dest <String> Note that if src is a file, dest cannot be a directory (see issue #323).
  • options <Object>
    • overwrite <boolean>: overwrite existing file or directory, default is true. Note that the copy operation will silently fail if you set this to false and the destination exists. Use the errorOnExist option to change this behavior.
    • errorOnExist <boolean>: when overwrite is false and the destination exists, throw an error. Default is false.
    • dereference <boolean>: dereference symlinks, default is false.
    • preserveTimestamps <boolean>: When true, will set last modification and access times to the ones of the original source files. When false, timestamp behavior is OS-dependent. Default is false.
    • filter <Function>: Function to filter copied files. Return true to include, false to exclude. Can also return a Promise that resolves to true or false (or pass in an async function).
  • callback <Function>

 

“logs/“ 로 시작되는 파일을 모두 복사하지 않게 간단한 필터를 추가(마지막 /가 없으면 logs 디렉터리 자체가 만들어지지 않음)

export default [
    {
        input: 'src/index.ts',
        output: {
            file: 'build/bin/seport.js',
            format: 'es',
            sourcemap: true
        },
        plugins: [            
            del({
                targets: './build'
            }),
            nodeResolve({
                preferBuiltins: true
            }),
            typescript(),
            json(),            
            copy({
                targets: [
                    {src: './predist/*', dest: './build'},
                ], filter: (src, dest) => {
                    if (src.indexOf('predist/logs/') == 0) return false;
                        /* prevent to copy logs files */
                        return true;
                    }
            })
        ]
    }
]

 

결론

rollup-plugin-copy 은 내부에서 fs-extra copy를 이용하고 있으므로, fs-extra copy의 사용가이드를 참고하면 더 다양한 응용이 가능함.