Skip to content

Commit

Permalink
feat: raw import support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 17, 2021
1 parent fea46a5 commit 0af3032
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-replace": "^3.0.0",
"@rollup/pluginutils": "^4.1.1",
"chalk": "^4.1.1",
"consola": "^2.15.3",
"defu": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { Plugin } from 'rollup'
import { findStaticImports } from 'mlly'
import MagicString from 'magic-string'

export function CJSBridgePlugin (_opts?: any): Plugin {
export function cjsPlugin (_opts?: any): Plugin {
return {
name: 'cjs-bridge',
name: 'unbuild-cjs',
renderChunk (code, _chunk, opts) {
if (opts.format === 'es') {
return CJSToESM(code)
Expand Down
1 change: 1 addition & 0 deletions src/builder/utils/json.ts → src/builder/plugins/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function JSONPlugin (options: RollupJsonOptions): Plugin {
const plugin = rollupJSONPlugin(options)
return <Plugin>{
...plugin,
name: 'unbuild-json',
transform (code, id) {
const res = plugin.transform!.call(this, code, id)
if (res && typeof res !== 'string' && 'code' in res && res.code && res.code.startsWith(EXPORT_DEFAULT)) {
Expand Down
29 changes: 29 additions & 0 deletions src/builder/plugins/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createFilter } from '@rollup/pluginutils'
import type { FilterPattern } from '@rollup/pluginutils'
import type { Plugin } from 'rollup'

export interface RawLoaderOptions {
include?: FilterPattern
exclude?: FilterPattern
}

const defaults: RawLoaderOptions = {
include: [/\.(md|txt|css|htm|html)$/],
exclude: []
}

export function rawPlugin (opts: RawLoaderOptions = {}): Plugin {
opts = { ...opts, ...defaults }
const filter = createFilter(opts.include, opts.exclude)
return {
name: 'unbuild-raw',
transform (code, id) {
if (filter(id)) {
return {
code: `export default ${JSON.stringify(code)}`,
map: null
}
}
}
}
}
9 changes: 6 additions & 3 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { relative, resolve } from 'pathe'
import consola from 'consola'
import { getpkg } from '../utils'
import type { BuildContext } from '../types'
import { JSONPlugin } from './utils/json'
import { CJSBridgePlugin } from './utils/cjs-bridge'
import { JSONPlugin } from './plugins/json'
import { rawPlugin } from './plugins/raw'
import { cjsPlugin } from './plugins/cjs'

export async function rollupBuild (ctx: BuildContext) {
if (ctx.stub) {
Expand Down Expand Up @@ -144,7 +145,9 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {
// Preserve dynamic imports for CommonJS
{ renderDynamicImport () { return { left: 'import(', right: ')' } } },

ctx.cjsBridge && CJSBridgePlugin({})
ctx.cjsBridge && cjsPlugin({}),

rawPlugin()
].filter(Boolean)
} as RollupOptions
}
2 changes: 2 additions & 0 deletions test/fixture/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ console.log(require('os').arch())
console.log(require.resolve('rollup'))
import('os').then(os => console.log(os.arch()))

import('./test.html').then(console.log)

export const foo = 'bar'
1 change: 1 addition & 0 deletions test/fixture/src/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Hello world</p>

0 comments on commit 0af3032

Please sign in to comment.