Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rollup plugin multi input #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"release-it": "^14.13.1",
"rimraf": "^3.0.2",
"rollup": "^2.70.1",
"rollup-plugin-multi-input": "^1.3.1",
"semver": "^7.3.5",
"typescript": "^4.6.2"
}
Expand Down
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import postcssSugarSS from "sugarss"
import { createFilter } from "@rollup/pluginutils"
import { getHash } from "asset-hash"

const MULTI_INPUT_PLUGIN_NAME = 'rollup-plugin-multi-input'

const scriptExtensions = /^\.(json|mjs|js|jsx|ts|tsx)$/

const styleParser = {
Expand All @@ -27,7 +29,7 @@ function getPostCssPlugins(keepName) {
postcssSmartAsset({
url: "copy",
useHash: true,
keepName
keepName,
})
]
}
Expand Down Expand Up @@ -61,7 +63,7 @@ export default function rebase(options = {}) {
exclude,
verbose = false,
keepName = false,
assetFolder = ""
assetFolder = "",
} = options

const filter = createFilter(include, exclude)
Expand All @@ -70,6 +72,7 @@ export default function rebase(options = {}) {
const files = {}

let root = null
let hasMultiInput = false

function rootRelative(file) {
// Last sequence is for Windows support
Expand All @@ -94,12 +97,20 @@ export default function rebase(options = {}) {
return Boolean(files[fileSource])
},

options({ plugins }) {
// Check whether we are using rollup-plugin-multi-input
hasMultiInput = !!plugins?.some(plugin => plugin != null && typeof plugin !== "boolean" && plugin.name === MULTI_INPUT_PLUGIN_NAME)
},

/* eslint-disable complexity, max-statements */
async resolveId(importee, importer) {
// Ignore root files which are typically script files. Delegate to other
// plugins or default behavior.
if (!importer) {
root = path.dirname(path.resolve(importee))
// If multiInput is detected, set root once for proper paths
if (!hasMultiInput || (hasMultiInput && !root)) {
root = path.dirname(path.resolve(importee))
}
return null
}

Expand Down
28 changes: 28 additions & 0 deletions test/multi-input/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Multi input support 1`] = `
"HDWIGpiP.md
SmVOQyTt.png
index.js
sub"
`;

exports[`Multi input support 2`] = `
"import index from './sub/folder/child.js';
export { default } from './sub/folder/child.js';
import './SmVOQyTt.png';
import './HDWIGpiP.md';
"
`;

exports[`Multi input support 3`] = `
"import image from '../../SmVOQyTt.png';
import content from '../../HDWIGpiP.md';

function index() {
return content + image
}

export { index as default };
"
`;
19 changes: 19 additions & 0 deletions test/multi-input/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { bundleWithMultiInput, clean, list, read } from "../util"
import { join, sep } from "path"

const root = __dirname

test("Multi input support", async () => {
await bundleWithMultiInput(root, ['src/**/!(*.test).js'], "output", {}, {
transformOutputPath: (output) => {
// Need to transform output as bundle path starts from root of project
return output.replace(`${join('test', 'multi-input', 'src')}${sep}`, '');
},
})

expect(await list(root, "output")).toMatchSnapshot()
expect(await read(root, "output/index.js")).toMatchSnapshot()
expect(await read(root, "output/sub/folder/child.js")).toMatchSnapshot()

await clean(root, "output")
})
Binary file added test/multi-input/src/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/multi-input/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import index from "./sub/folder/child"

export default index
6 changes: 6 additions & 0 deletions test/multi-input/src/sub/folder/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import image from "../../image.png"
import content from "./content.md"

export default function() {
return content + image
}
1 change: 1 addition & 0 deletions test/multi-input/src/sub/folder/content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello
18 changes: 18 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readdir } from "fs"

import fs from "fs-extra"
import { rollup } from "rollup"
import multiInput from "rollup-plugin-multi-input";

import rebasePlugin from "../src"

Expand All @@ -21,6 +22,23 @@ export async function bundle(root, input, output, pluginOptions = {}) {
})
}

export async function bundleWithMultiInput(root, input, output, pluginOptions = {}, multiInputOptions = {}) {
const plugins = [
multiInput(multiInputOptions),
rebasePlugin(pluginOptions)
];

const result = await rollup({
input: input.map(i => join(root, i)),
plugins,
})

await result.write({
format: "es",
dir: join(root, output),
})
}

export async function clean(root, files) {
const input = Array.isArray(files) ? files : [ files ]
const tasks = input.map((file) => fs.remove(join(root, file)))
Expand Down