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

Nested import handling #29

Merged
merged 7 commits into from
Feb 3, 2021
Merged
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"scripts": {
"build": "rollup -c",
"dev": "rollup -cw",
"test:simple": "cd test/simple && rm -f output.* && rollup -c && cmp output.js ../expected.js && cmp output.css expected.css && cd ../..",
"test": "npm run test:simple",
"test:nested": "cd test/nested && rm -rf output && rollup -c && cmp output/bundle.js expected/bundle.js && cmp output/bundle.css expected/bundle.css && cd ../..",
"test:simple": "cd test/simple && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
"test": "npm run test:simple && npm run test:nested",
"lint": "prettier rollup.config.js src/**",
"prepare": "npm run build",
"prepublish": "npm run build"
Expand Down
66 changes: 50 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import { createFilter } from '@rollup/pluginutils'

var arraysEqual = function(a, b) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind moving helper functions to the bottom of the file?

Also, I prefer

Suggested change
var arraysEqual = function(a, b) {
function arraysEqual(a, b) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be ok if I created a util.js (or similar) file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not, I will move to the bottom of the file as suggested

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bottom seems fine :-)

if (a.length !== b.length) return false

for (let i = a.length; i--;) {
if (a[i] !== b[i]) return false
}

return true
}

export default function css(options = {}) {
const filter = createFilter(options.include || ['**/*.css'], options.exclude)
const styles = {}
const order = []
let dest = options.output
let changes = 0
let hasChanged = false
let prevIds = []

// Get all CSS modules in the order that they were imported
const getCSSModules = (id, getModuleInfo, modules = new Set()) => {
if (modules.has(id)) {
return new Set()
}

if (filter(id)) modules.add(id)

// Recursively retrieve all of imported CSS modules
getModuleInfo(id).importedIds.forEach(importId => {
modules = new Set([].concat(Array.from(modules), Array.from(getCSSModules(importId, getModuleInfo, modules))))
});

return modules
};

return {
name: 'css',
buildStart() {
hasChanged = false
},
transform(code, id) {
if (!filter(id)) {
return
Expand All @@ -22,32 +51,37 @@ export default function css(options = {}) {
}
}

// Track the order that each stylesheet is imported.
if (!order.includes(id)) {
order.push(id)
}

// Keep track of every stylesheet
// Check if it changed since last render
// NOTE: If we are in transform block, we can assume styles[id] !== code, right?
thgh marked this conversation as resolved.
Show resolved Hide resolved
if (styles[id] !== code && (styles[id] || code)) {
styles[id] = code
changes++
hasChanged = true
}

return ''
},
generateBundle(opts, bundle) {
// No stylesheet needed
if (!changes || options.output === false) {
return
const ids = []
thgh marked this conversation as resolved.
Show resolved Hide resolved

// Determine import order of files
for (const file in bundle) {
const root = bundle[file].facadeModuleId
const modules = getCSSModules(root, this.getModuleInfo)
ids.push(...Array.from(modules))
thgh marked this conversation as resolved.
Show resolved Hide resolved
}
changes = 0

// Combine all stylesheets, respecting import order
// If the files are imported in the same order and there are no changes
// or options.output is false, there is no work to be done
if (arraysEqual(prevIds, ids) && !hasChanged || options.output === false) return
prevIds = ids

let css = ''
for (let x = 0; x < order.length; x++) {
const id = order[x]
css += styles[id] || ''

// Combine all stylesheets, respecting import order
for (const index in ids) {
let id = ids[index]
thgh marked this conversation as resolved.
Show resolved Hide resolved
css += styles[id] + '\n' || ''
}

// Emit styles through callback
Expand Down
2 changes: 2 additions & 0 deletions test/nested/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Test Case
This is to test that nested imports are bundled in the proper order
3 changes: 3 additions & 0 deletions test/nested/css/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: blue;
}
3 changes: 3 additions & 0 deletions test/nested/css/g.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: green;
}
3 changes: 3 additions & 0 deletions test/nested/css/r.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: red;
}
9 changes: 9 additions & 0 deletions test/nested/expected/bundle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
background-color: blue;
}
body {
background-color: red;
}
body {
background-color: green;
}
3 changes: 3 additions & 0 deletions test/nested/expected/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log('This should happen first');

console.log('This should happen second');
5 changes: 5 additions & 0 deletions test/nested/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import './js/b.js';
import './css/r.css';
import './css/g.css';

console.log('This should happen second');
3 changes: 3 additions & 0 deletions test/nested/js/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import '../css/b.css';

console.log('This should happen first');
12 changes: 12 additions & 0 deletions test/nested/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import css from '../../src/index.js'

export default {
input: 'input.js',
output: {
file: 'output/bundle.js',
format: 'esm'
},
plugins: [
css({ output: 'bundle.css' })
]
}
File renamed without changes.
2 changes: 1 addition & 1 deletion test/input.css → test/simple/input.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.rollup {
color: green;
user-select: none;
}
}
File renamed without changes.
1 change: 0 additions & 1 deletion test/simple/output.js

This file was deleted.

6 changes: 3 additions & 3 deletions test/simple/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import css from '../../src/index.js'

export default {
input: '../input.js',
input: 'input.js',
output: {
file: 'output.js',
file: 'output/output.js',
format: 'esm'
},
plugins: [
css()
css({ output: 'output.css' })
]
}