Skip to content

Commit

Permalink
feat: add transform command and expose process config from api
Browse files Browse the repository at this point in the history
  • Loading branch information
joneff committed Sep 9, 2022
1 parent e32c87d commit 1eff9eb
Show file tree
Hide file tree
Showing 42 changed files with 775 additions and 325 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const output = {
filename: '[name:toLower].css' // specify output file name; defaults to '[name].css'
};
const options = {
implementation: 'sass-embedded', // defaults to 'node-sass',
compiler: 'sass-embedded', // defaults to 'node-sass',
api: 'modern', // defaults to 'legacy'
sassOptions: {
minify: true // defaults to false
Expand Down Expand Up @@ -109,9 +109,9 @@ module.exports = {
// * caching
'sass-build:recommended'
],
files: [
build: [
{
// use packages/default as the folder in which all lookup is based. default is the process.cwd()
// use packages/default as the folder in which all lookup is based. default is process.cwd()
cwd: 'packages/default',
// path to source file, relative to cwd
file: 'scss/all.scss',
Expand All @@ -123,7 +123,7 @@ module.exports = {
file: 'scss/all.scss',
outFile: 'dist/all.css',
// use different sass implementation
implementation: 'sass-embedded',
compiler: 'sass-embedded',
// use @use instead of @import syntax
api: 'modern'
}
Expand Down
4 changes: 2 additions & 2 deletions __tests__/sass-build/sass-build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const FIXTURES_PATH = path.resolve( __dirname, '../__fixtures__' );
fs.rmSync( `${FIXTURES_PATH}/dist`, { recursive: true, force: true } );
});

const opts : Partial<CliOptions> = {
const opts : Partial<CliBuildOptions> = {
api: 'legacy'
};

Expand All @@ -33,7 +33,7 @@ const FIXTURES_PATH = path.resolve( __dirname, '../__fixtures__' );
describe( 'sassBuild', () => {

test('sassBuild compiles', () => {
sassBuild( file, outFile, <CliOptions> opts );
sassBuild( file, outFile, <CliBuildOptions> opts );
assert.equal( fileExists( outFile ), true );
});

Expand Down
10 changes: 5 additions & 5 deletions __tests__/sass-compile/sass-compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const FIXTURES_PATH = path.resolve( __dirname, '../__fixtures__' );
logger.level = 'error';
});

const opts : Partial<CliOptions> = {
const opts : Partial<CliBuildOptions> = {
api: 'legacy'
};

Expand All @@ -25,7 +25,7 @@ const FIXTURES_PATH = path.resolve( __dirname, '../__fixtures__' );
describe( 'sassCompile', () => {

test('sassCompile compiles', () => {
const fileContent = sassCompile( file, <CliOptions> opts );
const fileContent = sassCompile( file, <CliBuildOptions> opts );
assert.notEqual( fileContent, '' );
});

Expand All @@ -35,13 +35,13 @@ const FIXTURES_PATH = path.resolve( __dirname, '../__fixtures__' );

test('minify: true', () => {
const sassOptions : Partial<SassOptions> = { minify: true };
const fileContent = sassCompile( file, <CliOptions> { ...opts, sassOptions } );
const fileContent = sassCompile( file, <CliBuildOptions> { ...opts, sassOptions } );

assert.equal( fileContent, 'body{color:red}\n' );
});
test('minify: false', () => {
const sassOptions : Partial<SassOptions> = { minify: false };
const fileContent = sassCompile( file, <CliOptions> { ...opts, sassOptions } );
const fileContent = sassCompile( file, <CliBuildOptions> { ...opts, sassOptions } );

assert.equal( fileContent, 'body {\n color: red;\n}\n' );
});
Expand All @@ -52,7 +52,7 @@ const FIXTURES_PATH = path.resolve( __dirname, '../__fixtures__' );

test('correct path', () => {
const sassOptions : Partial<SassOptions> = { loadPaths: [ FIXTURES_PATH ] };
const fileContent = sassCompile( nestedFile, <CliOptions> { ...opts, sassOptions } );
const fileContent = sassCompile( nestedFile, <CliBuildOptions> { ...opts, sassOptions } );

assert.notEqual( fileContent, '' );
});
Expand Down
22 changes: 11 additions & 11 deletions __tests__/sass-compiler/wrap-compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe( 'sass-compiler', () => {
const NODE_SASS = 'node-sass';
const DART_SASS = 'dart-sass';
const SASS_EMBEDDED = 'sass-embedded';
const EMPTY_OPTS = <CliOptions>{};
const EMPTY_OPTS = <CliBuildOptions>{};

test('Default wrapper compiler is node-sass', () => {
const compiler = wrapCompiler( EMPTY_OPTS );
Expand All @@ -20,59 +20,59 @@ describe( 'sass-compiler', () => {
});

test(`Setting compiler name to 'node-sass' works correctly`, () => {
const compiler = wrapCompiler( { implementation: 'node-sass' } as CliOptions );
const compiler = wrapCompiler( { compiler: 'node-sass' } as CliBuildOptions );
const compilerName = compiler.info.split('\t')[0];

assert.equal(compilerName, NODE_SASS);
});

test(`Setting compiler name to 'sass' works correctly`, () => {
const compiler = wrapCompiler( { implementation: 'sass' } as CliOptions );
const compiler = wrapCompiler( { compiler: 'sass' } as CliBuildOptions );
const compilerName = compiler.info.split('\t')[0];

assert.equal(compilerName, DART_SASS);
});

test(`Setting compiler name to 'sass-embedded' works correctly`, () => {
const compiler = wrapCompiler( { implementation: 'sass-embedded' } as CliOptions );
const compiler = wrapCompiler( { compiler: 'sass-embedded' } as CliBuildOptions );
const compilerName = compiler.info.split('\t')[0];

assert.equal(compilerName, SASS_EMBEDDED);
});

test(`Setting compiler name to require('node-sass') works correctly`, () => {
const compiler = wrapCompiler( { implementation: require('node-sass') } as CliOptions );
const compiler = wrapCompiler( { compiler: require('node-sass') } as CliBuildOptions );
const compilerName = compiler.info.split('\t')[0];

assert.equal(compilerName, NODE_SASS);
});

test(`Setting compiler name to require('sass') works correctly`, () => {
const compiler = wrapCompiler( { implementation: require('sass') } as CliOptions );
const compiler = wrapCompiler( { compiler: require('sass') } as CliBuildOptions );
const compilerName = compiler.info.split('\t')[0];

assert.equal(compilerName, DART_SASS);
});

test(`Setting compiler name to require('sass-embedded') works correctly`, () => {
const compiler = wrapCompiler( { implementation: require('sass-embedded') } as CliOptions );
const compiler = wrapCompiler( { compiler: require('sass-embedded') } as CliBuildOptions );
const compilerName = compiler.info.split('\t')[0];

assert.equal(compilerName, SASS_EMBEDDED);
});

test('Setting invalid compiler throws an error', () => {
assert.throws( () => {
wrapCompiler( { implementation: '' } as CliOptions );
wrapCompiler( { compiler: '' } as CliBuildOptions );
});
assert.throws( () => {
wrapCompiler( { implementation: 'glob' } as CliOptions );
wrapCompiler( { compiler: 'glob' } as CliBuildOptions );
});
assert.throws( () => {
wrapCompiler( { implementation: 'no-such-compiler' } as CliOptions );
wrapCompiler( { compiler: 'no-such-compiler' } as CliBuildOptions );
});
assert.throws( () => {
wrapCompiler( { implementation: require('glob') } as CliOptions );
wrapCompiler( { compiler: require('glob') } as CliBuildOptions );
});
});

Expand Down
39 changes: 23 additions & 16 deletions lib/sass-build-recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ const cwd = process.cwd();
const { sassCacheImporter } = require('../dist/importers/cache-importer');
const { sassPackageImporter } = require('../dist/importers/package-importer');

/** @type {CliOptions} */
/** @type {ConfigOptions} */
const config = {
cwd: cwd,
implementation: 'node-sass',
api: 'legacy',
sassOptions: {
minify: false,
functions: [],
importers: [
sassCacheImporter(),
sassPackageImporter({ cwd: cwd })
]
},
postcss: [
calc({ precision: 10 }),
autoprefixer()
]
defaults: {
transform: {
cwd: cwd
},
build: {
cwd: cwd,
compiler: 'node-sass',
api: 'legacy',
sassOptions: {
minify: false,
functions: [],
importers: [
sassCacheImporter(),
sassPackageImporter({ cwd: cwd })
]
},
postcss: [
calc({ precision: 10 }),
autoprefixer()
]
}
}
};

module.exports = config;
45 changes: 40 additions & 5 deletions 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 @@ -35,6 +35,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@joneff/baka": "^2.0.0",
"glob": "^8.0.3",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
Expand Down
6 changes: 3 additions & 3 deletions src/build/build-files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { wrapImplementation } from '../compiler';
import { wrapCompiler } from '../compiler';

export function sassBuildFiles( file: string | string[], output: OutputOptions = <OutputOptions>{}, options?: CliOptions ) : void {
const compiler = wrapImplementation( options );
export function sassBuildFiles( file: string | string[], output: OutputOptions = <OutputOptions>{}, options?: CliBuildOptions ) : void {
const compiler = wrapCompiler( options );

compiler.buildFiles( file, output, options?.sassOptions );
}
6 changes: 3 additions & 3 deletions src/build/build-string.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { wrapImplementation } from '../compiler';
import { wrapCompiler } from '../compiler';

export function sassBuildString( source: string, outFile: string, options?: CliOptions ) : void {
const compiler = wrapImplementation( options );
export function sassBuildString( source: string, outFile: string, options?: CliBuildOptions ) : void {
const compiler = wrapCompiler( options );

compiler.buildString( source, outFile, options?.sassOptions );
}
6 changes: 3 additions & 3 deletions src/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { wrapImplementation } from '../compiler';
import { wrapCompiler } from '../compiler';

export function sassBuild( file: string, outFile: string, options?: CliOptions ) : void {
const compiler = wrapImplementation( options );
export function sassBuild( file: string, outFile: string, options?: CliBuildOptions ) : void {
const compiler = wrapCompiler( options );

compiler.build( file, outFile, options?.sassOptions );
}
4 changes: 2 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argsParser from 'yargs-parser';

import * as commands from './commands';
import { processConfig } from './process-config';
import { processConfigFile } from '../config';
import {
exit,
logger
Expand Down Expand Up @@ -61,7 +61,7 @@ export function cli() {
logger.silly('cli', 'No config passed.');
}

processConfig( config );
processConfigFile( config );
exitSuccess();
}

Expand Down
Loading

0 comments on commit 1eff9eb

Please sign in to comment.