From 0af30328571e4d9864211e52fa0bb2f4542571f3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 17 Nov 2021 20:32:39 +0100 Subject: [PATCH] feat: raw import support --- package.json | 1 + .../{utils/cjs-bridge.ts => plugins/cjs.ts} | 4 +-- src/builder/{utils => plugins}/json.ts | 1 + src/builder/plugins/raw.ts | 29 +++++++++++++++++++ src/builder/rollup.ts | 9 ++++-- test/fixture/src/index.ts | 2 ++ test/fixture/src/test.html | 1 + 7 files changed, 42 insertions(+), 5 deletions(-) rename src/builder/{utils/cjs-bridge.ts => plugins/cjs.ts} (93%) rename src/builder/{utils => plugins}/json.ts (96%) create mode 100644 src/builder/plugins/raw.ts create mode 100644 test/fixture/src/test.html diff --git a/package.json b/package.json index 043fe5b9..0fe1f32f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/builder/utils/cjs-bridge.ts b/src/builder/plugins/cjs.ts similarity index 93% rename from src/builder/utils/cjs-bridge.ts rename to src/builder/plugins/cjs.ts index a3e99416..abb79f08 100644 --- a/src/builder/utils/cjs-bridge.ts +++ b/src/builder/plugins/cjs.ts @@ -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) diff --git a/src/builder/utils/json.ts b/src/builder/plugins/json.ts similarity index 96% rename from src/builder/utils/json.ts rename to src/builder/plugins/json.ts index 24c8f037..023fca12 100644 --- a/src/builder/utils/json.ts +++ b/src/builder/plugins/json.ts @@ -8,6 +8,7 @@ export function JSONPlugin (options: RollupJsonOptions): Plugin { const plugin = rollupJSONPlugin(options) return { ...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)) { diff --git a/src/builder/plugins/raw.ts b/src/builder/plugins/raw.ts new file mode 100644 index 00000000..5afefd6c --- /dev/null +++ b/src/builder/plugins/raw.ts @@ -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 + } + } + } + } +} diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 4429e60a..51585ebf 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -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) { @@ -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 } diff --git a/test/fixture/src/index.ts b/test/fixture/src/index.ts index 39ea209f..0c014d05 100644 --- a/test/fixture/src/index.ts +++ b/test/fixture/src/index.ts @@ -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' diff --git a/test/fixture/src/test.html b/test/fixture/src/test.html new file mode 100644 index 00000000..a04fa3c4 --- /dev/null +++ b/test/fixture/src/test.html @@ -0,0 +1 @@ +

Hello world