rollup-plugin-scss
Advanced tools
Comparing version 3.0.0 to 4.0.0
'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -9,5 +8,7 @@ var rollupPluginutils = require('rollup-pluginutils'); | ||
const filter = rollupPluginutils.createFilter(options.include || ['/**/*.css', '/**/*.scss', '/**/*.sass'], options.exclude); | ||
let dest = typeof options.output === 'string' ? options.output : null; | ||
const insertStyleFnName = '___$insertStylesToHeader'; | ||
const styles = {}; | ||
const fileName = options.fileName || | ||
(options.output === 'string' ? options.output : undefined); | ||
const name = options.name || 'output.css'; | ||
const prefix = options.prefix ? options.prefix + '\n' : ''; | ||
@@ -24,3 +25,3 @@ let includePaths = options.includePaths || ['node_modules/']; | ||
data: prefix + scss, | ||
outFile: dest, | ||
outFile: fileName || name, | ||
includePaths, | ||
@@ -94,3 +95,3 @@ importer: (url, prev, done) => { | ||
from: undefined, | ||
to: dest, | ||
to: fileName || name, | ||
map: map ? { prev: map, inline: false } : null | ||
@@ -183,10 +184,2 @@ })); | ||
} | ||
if (typeof dest !== 'string') { | ||
// Guess destination filename | ||
dest = opts.file || 'bundle.js'; | ||
if (dest.endsWith('.js')) { | ||
dest = dest.slice(0, -3); | ||
} | ||
dest = dest + '.css'; | ||
} | ||
const compiled = await compileToCSS(scss); | ||
@@ -205,14 +198,8 @@ if (typeof compiled !== 'object' || typeof compiled.css !== 'string') { | ||
} | ||
// Ensure that dest parent folders exist (create the missing ones) | ||
ensureParentDirsSync(path.dirname(dest)); | ||
// Emit styles to file | ||
fs.writeFile(dest, compiled.css, err => { | ||
if (options.verbose !== false) { | ||
if (err) { | ||
console.error(red(err.toString())); | ||
} | ||
else if (compiled.css) { | ||
console.log(green(dest || '?'), getSize(compiled.css.length)); | ||
} | ||
} | ||
this.emitFile({ | ||
type: 'asset', | ||
source: compiled.css, | ||
name, | ||
fileName | ||
}); | ||
@@ -224,6 +211,7 @@ if (options.sourceMap && compiled.map) { | ||
} | ||
fs.writeFile(dest + '.map', sourcemap, err => { | ||
if (options.verbose !== false && err) { | ||
console.error(red(err.toString())); | ||
} | ||
this.emitFile({ | ||
type: 'asset', | ||
source: sourcemap, | ||
name: name && name + '.map', | ||
fileName: fileName && fileName + '.map' | ||
}); | ||
@@ -275,24 +263,3 @@ } | ||
} | ||
function getSize(bytes) { | ||
return bytes < 10000 | ||
? bytes.toFixed(0) + ' B' | ||
: bytes < 1024000 | ||
? (bytes / 1024).toPrecision(3) + ' kB' | ||
: (bytes / 1024 / 1024).toPrecision(4) + ' MB'; | ||
} | ||
function ensureParentDirsSync(dir) { | ||
if (fs.existsSync(dir)) { | ||
return; | ||
} | ||
try { | ||
fs.mkdirSync(dir); | ||
} | ||
catch (err) { | ||
if (err.code === 'ENOENT') { | ||
ensureParentDirsSync(path.dirname(dir)); | ||
ensureParentDirsSync(dir); | ||
} | ||
} | ||
} | ||
module.exports = scss; |
@@ -7,5 +7,10 @@ /// <reference types="node" /> | ||
failOnError?: boolean; | ||
/** Literal asset filename, bypasses any hashes in the filename */ | ||
fileName?: string; | ||
include?: Parameters<CreateFilter>[0]; | ||
includePaths?: string[]; | ||
insert?: boolean; | ||
/** Asset name, defaults to output.css, Rollup may add a hash to this! Check out RollupConfig.output.assetFileNames */ | ||
name?: string; | ||
/** @deprecated Use `fileName` instead, currently still available for backwards compatibility */ | ||
output?: string | false | ((css: string, styles: Styles) => void); | ||
@@ -18,4 +23,5 @@ prefix?: string; | ||
watch?: string | string[]; | ||
outputStyle?: string; | ||
} | ||
declare type CSS = string | { | ||
type CSS = string | { | ||
css: string; | ||
@@ -22,0 +28,0 @@ map: string; |
@@ -1,2 +0,1 @@ | ||
import { writeFile, existsSync, mkdirSync } from 'fs'; | ||
import { dirname } from 'path'; | ||
@@ -7,5 +6,7 @@ import { createFilter } from 'rollup-pluginutils'; | ||
const filter = createFilter(options.include || ['/**/*.css', '/**/*.scss', '/**/*.sass'], options.exclude); | ||
let dest = typeof options.output === 'string' ? options.output : null; | ||
const insertStyleFnName = '___$insertStylesToHeader'; | ||
const styles = {}; | ||
const fileName = options.fileName || | ||
(options.output === 'string' ? options.output : undefined); | ||
const name = options.name || 'output.css'; | ||
const prefix = options.prefix ? options.prefix + '\n' : ''; | ||
@@ -22,3 +23,3 @@ let includePaths = options.includePaths || ['node_modules/']; | ||
data: prefix + scss, | ||
outFile: dest, | ||
outFile: fileName || name, | ||
includePaths, | ||
@@ -92,3 +93,3 @@ importer: (url, prev, done) => { | ||
from: undefined, | ||
to: dest, | ||
to: fileName || name, | ||
map: map ? { prev: map, inline: false } : null | ||
@@ -181,10 +182,2 @@ })); | ||
} | ||
if (typeof dest !== 'string') { | ||
// Guess destination filename | ||
dest = opts.file || 'bundle.js'; | ||
if (dest.endsWith('.js')) { | ||
dest = dest.slice(0, -3); | ||
} | ||
dest = dest + '.css'; | ||
} | ||
const compiled = await compileToCSS(scss); | ||
@@ -203,14 +196,8 @@ if (typeof compiled !== 'object' || typeof compiled.css !== 'string') { | ||
} | ||
// Ensure that dest parent folders exist (create the missing ones) | ||
ensureParentDirsSync(dirname(dest)); | ||
// Emit styles to file | ||
writeFile(dest, compiled.css, err => { | ||
if (options.verbose !== false) { | ||
if (err) { | ||
console.error(red(err.toString())); | ||
} | ||
else if (compiled.css) { | ||
console.log(green(dest || '?'), getSize(compiled.css.length)); | ||
} | ||
} | ||
this.emitFile({ | ||
type: 'asset', | ||
source: compiled.css, | ||
name, | ||
fileName | ||
}); | ||
@@ -222,6 +209,7 @@ if (options.sourceMap && compiled.map) { | ||
} | ||
writeFile(dest + '.map', sourcemap, err => { | ||
if (options.verbose !== false && err) { | ||
console.error(red(err.toString())); | ||
} | ||
this.emitFile({ | ||
type: 'asset', | ||
source: sourcemap, | ||
name: name && name + '.map', | ||
fileName: fileName && fileName + '.map' | ||
}); | ||
@@ -273,24 +261,3 @@ } | ||
} | ||
function getSize(bytes) { | ||
return bytes < 10000 | ||
? bytes.toFixed(0) + ' B' | ||
: bytes < 1024000 | ||
? (bytes / 1024).toPrecision(3) + ' kB' | ||
: (bytes / 1024 / 1024).toPrecision(4) + ' MB'; | ||
} | ||
function ensureParentDirsSync(dir) { | ||
if (existsSync(dir)) { | ||
return; | ||
} | ||
try { | ||
mkdirSync(dir); | ||
} | ||
catch (err) { | ||
if (err.code === 'ENOENT') { | ||
ensureParentDirsSync(dirname(dir)); | ||
ensureParentDirsSync(dir); | ||
} | ||
} | ||
} | ||
export default scss; | ||
export { scss as default }; |
{ | ||
"name": "rollup-plugin-scss", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Rollup multiple .scss, .sass and .css imports", | ||
@@ -14,7 +14,7 @@ "main": "index.cjs.js", | ||
"test:node-sass": "cd test/node-sass && rm -f output.* && rollup -c && cmp output.js ../expected.js && cmp output.css expected.css && cd ../..", | ||
"test:sass": "cd test/sass && rm -f output.* && rollup -c && cmp output.js ../expected.js && cmp output.css expected.css && cd ../..", | ||
"test:sass": "cd test/sass && rm -f output.* && rollup -c && cmp nested/output.js ../expected.js && cmp nested/output.css expected.css && cd ../..", | ||
"test:insert": "cd test/insert && rm -f output.* && rollup -c && cmp output.js expected.js && cd ../..", | ||
"test:postcss": "cd test/postcss && rm -f output.* && rollup -c && cmp output.js ../expected.js && cmp output.css expected.css && cd ../..", | ||
"test:processor": "cd test/processor && rm -f output.* && rollup -c && cmp output.js ../expected.js && cmp output.css expected.css && cd ../..", | ||
"test:sourcemap": "cd test/sourcemap && rm -f output.* && rollup -c && cmp output.js ../expected.js && cmp output.css expected.css && cmp output.css.map expected.css.map && cd ../..", | ||
"test:sourcemap": "cd test/sourcemap && rm -f output.* && rollup -c && cmp output.js ../expected.js && cmp nested/output.css expected.css && cmp nested/output.css.map expected.css.map && cd ../..", | ||
"test": "npm run test:node-sass && npm run test:sass && npm run test:processor && npm run test:postcss && npm run test:sourcemap && npm run test:insert", | ||
@@ -53,3 +53,3 @@ "testw": "cd test/node-sass && rm -f output.* && rollup -cw; cd ..", | ||
"autoprefixer": "^9.8.6", | ||
"node-sass": "5", | ||
"node-sass": "^8", | ||
"postcss": "^7.0.32", | ||
@@ -56,0 +56,0 @@ "prettier": "2", |
@@ -22,7 +22,7 @@ # Rollup multiple .scss, .sass and .css imports | ||
``` | ||
# v4 uses the rollup asset system | ||
npm install --save-dev rollup-plugin-scss@3 sass | ||
# v3 needs sass installed seperately (or node-sass) | ||
npm install --save-dev rollup-plugin-scss@3 sass | ||
# v2 has node-sass included (with option to use sass) | ||
npm install --save-dev rollup-plugin-scss@2 | ||
``` | ||
@@ -42,3 +42,5 @@ | ||
file: 'output.js', | ||
format: 'esm' | ||
format: 'esm', | ||
// Removes the hash from the asset filename | ||
assetFileNames: '[name][extname]' | ||
}, | ||
@@ -49,2 +51,22 @@ plugins: [ | ||
} | ||
// OR | ||
export default { | ||
input: 'input.js', | ||
output: { file: 'output.js', format: 'esm' }, | ||
plugins: [ | ||
scss({ fileName: 'bundle.css' }) // will output compiled styles to "bundle.css" | ||
] | ||
} | ||
// OR | ||
export default { | ||
input: 'input.js', | ||
output: { file: 'output.js', format: 'esm' }, | ||
plugins: [ | ||
scss() // will output compiled styles to "assets/output-123hash.css" | ||
] | ||
} | ||
``` | ||
@@ -65,8 +87,7 @@ | ||
scss({ | ||
// Choose *one* of these possible "output:..." options | ||
// Default behaviour is to write all styles to the bundle destination where .js is replaced by .css | ||
output: true, | ||
// Defaults to output.css, Rollup may add a hash to this! | ||
name: 'output.css', | ||
// Filename to write all styles to | ||
output: 'bundle.css', | ||
// Literal asset filename, bypasses the automated filenaming transformations | ||
fileName: 'output.css', | ||
@@ -73,0 +94,0 @@ // Callback that will be called ongenerate with two arguments: |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
210
2
33319
6
554