•  


GitHub - jacksteamdev/rollup-plugin-bundle-imports: Bundle imports separately and use the result in your code.
Skip to content

jacksteamdev/rollup-plugin-bundle-imports

Repository files navigation

rollup-plugin-bundle-imports logo

rollup-plugin-bundle-imports

npm (scoped) GitHub last commit License TypeScript Declarations Included

Fiverr: We make Chrome extensions ko-fi


Bundle imports separately and use the result as a file path or a string of code . Tested to work recursively or as multiple plugin instances with different options.

If you are coming here from rollup-plugin-code-string , the API has become more robust, but the defaults will work the same!

Table of Contents

Getting Started

This is a Rollup plugin, so your project will need to be up and running with Rollup .

Installation

$ npm i rollup-plugin-bundle-imports -D

Usage

// rollup.config.js


import
 {
 bundleImports
 }
 from
 'rollup-plugin-bundle-imports'


export
 default
 {

  input
: 
'index.js'
,

  output
: 
{

    file
: 
'dist/bundle-esm.js'
,

    format
: 
'esm'
,

  }

  plugins
: 
[
bundleImports
(
)
]
,

}

Default is to import a module that ends in .code.js as a string.

import
 code
 from
 './script.code.js'

Bundle A Service Worker

Use options.importAs to bundle an imported module and emit it as an asset file. The imported value will be the file path to the asset.

// rollup.config.js


import
 {
 bundleImports
 }
 from
 'rollup-plugin-bundle-imports'


export
 default
 {

  input
: 
'register-service-worker.js'
,

  plugins
: 
[

    bundleImports
(
{

      include
: 
[
'**/my-sw.js'
]
,

      // Import as path to bundle

      importAs
: 
'path'
,

    }
)
,

  ]
,

}
// register-service-worker.js


import
 swPath
 from
 './my-sw.js'


navigator
.
serviceWorker
.
register
(
swPath
)

Bundle A Chrome Extension Content Script

Bundle a content script to a code string to inject from the background page of a Web or Chrome extension.

// rollup.config.js


import
 {
 bundleImports
 }
 from
 'rollup-plugin-bundle-imports'


export
 default
 {

  input
: 
'background.js'
,

  plugins
: 
[

    bundleImports
(
{

      include
: 
[
'**/content.js'
,
 '**/inject.js'
]
,

      // Import as code string to bundle

      importAs
: 
'code'
,

    }
)
,

  ]
,

}
// background.js


import
 code
 from
 './content.js'


// Inject the bundled code to the active tab

browser
.
tabs
.
executeScript
(
{
 code 
}
)

Recursive Usage

Inject the bundled code from the content script of the previous example into the page runtime through a script tag.

// content.js


import
 code
 from
 './inject.js'


const
 script
 =
 document
.
createElement
(
'script'
)

script
.
text
 =
 code


document
.
head
.
append
(
script
)

script
.
remove
(
)

Import As Both Code And Paths

If you want to import some files as code and others as file paths, just create two plugins with different settings!

Both plugin instances will work recursively with each other, so you can import a path into a code string, or import a code string into an asset and import the asset path into your entry bundle.

// rollup.config.js


import
 {
 bundleImports
 }
 from
 'rollup-plugin-bundle-imports'


export
 default
 {

  input
: 
'index.js'
,

  plugins
: 
[

    bundleImports
(
{

      include
: 
[
'**/my-sw.js'
]
,

      // Import as path to bundle

      importAs
: 
'path'
,

    }
)
,

    bundleImports
(
{

      include
: 
[
'**/content.js'
,
 '**/inject.js'
]
,

      // Import as code string to bundle

      importAs
: 
'code'
,

    }
)
,

  ]
,

}

Features

Works With TypeScript

TypeScript definitions are included, so no need to install an additional @types library!

Options API

[include]

Type: string[]

Glob patterns to for module names to include.

bundleImports
(
{

  // Include files that end in `.code.js`

  include
: 
[
'**/*.code.js'
]
,

}
)

[exclude]

Type: string[]

Glob patterns to for module names to include.

bundleImports
(
{

  // Exclude files that end in `.code.js`

  include
: 
[
'**/*.code.js'
]
,

  // Except for this one...

  exclude
: 
[
'src/not-me.code.js'
]
,

}
)

[importAs]

Type: "path" | "code"

Use "code" to bundle the module and import it as a string.

bundleImports
(
{

  importAs
: 
'path'
,

}
)

Use "path" to emit the module as a file and import the file path as a string. This works well for service workers , for example.

bundleImports
(
{

  importAs
: 
'path'
,

}
)

[options]

Type: string[]

rollup-plugin-bundle-imports bundles the module into an IIFE, and uses the plugins array defined in your Rollup input options by default.

If you need to use other plugins or plugin settings for bundled imports, this is the place. You can set any of the Rollup input options in options .

The properties options.file and options.output will be ignored.

Note that most libraries expect to be bundled into a UMD or IIFE, so using options.format to create an ES2015 module may cause unexpected results.

bundleImports
(
{

  options
: 
{

    // Bundle the module as an ESM2015 module

    format
: 
'esm'
,

    // Use a different set of plugins

    plugins
: 
[
resolve
(
)
,
 commonjs
(
)
]
,

  }
,

}
)

Default options

// rollup.config.js


import
 {
 rollup
 }
 from
 'rollup'

import
 bundle
 from
 'rollup-plugin-bundle-imports'


// These are the default options

const
 options
 =
 {

  include
: 
[
'**/*.code.js'
]
,

  importAs
: 
'code'
,

  // Rollup input options for the imported module

  options
: 
{

    plugins
: 
[
resolve
(
)
,
 commonjs
(
)
]
,

    output
: 
{

      format
: 
'iife'
,

      preferConst
: 
true
,

    }
,

  }
,

}


export
 default
 {

  input
: 
'index.js'
,

  output
: 
{

    file
: 
'dist/bundle-esm.js'
,

    format
: 
'esm'
,

  }

  plugins
: 
[
bundleImports
(
)
]
,

  // This is the same as above

  plugins
: 
[
bundleImports
(
options
)
]
,

}

Releases

No releases published

Packages

No packages published
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본