style-resources-loader
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -11,2 +11,4 @@ /// <reference types="node" /> | ||
export declare type StyleResourcesInjector = (source: string | Buffer, resources: StyleResources) => string; | ||
export declare type StyleResourcesInternalInjectors = Record<'prepend' | 'append', StyleResourcesInjector>; | ||
export declare type StyleResourcesOriginalInjector = StyleResourcesInjector | keyof StyleResourcesInternalInjectors; | ||
export interface StyleResourcesLoaderOptions { | ||
@@ -18,2 +20,7 @@ patterns: string | string[]; | ||
} | ||
export declare type StyleResourcesLoaderOriginalOptions = Partial<StyleResourcesLoaderOptions>; | ||
export declare type StyleResourcesLoaderOriginalOptions = Partial<{ | ||
patterns: string | string[]; | ||
injector: StyleResourcesOriginalInjector; | ||
globOptions: glob.IOptions; | ||
resolveUrl: boolean; | ||
}>; |
@@ -5,4 +5,9 @@ "use strict"; | ||
const _1 = require("./"); | ||
const internalInjectors = { | ||
prepend: (source, resources) => resources.map(({ content }) => content).join('') + source, | ||
append: (source, resources) => source + resources.map(({ content }) => content).join(''), | ||
}; | ||
const getNormalizedInjector = (injector) => typeof injector === 'function' ? injector : internalInjectors[injector]; | ||
function getNormalizedOptions() { | ||
const defaultInjector = (source, resources) => resources.map(({ content }) => content).join('') + source; | ||
const defaultInjector = 'prepend'; | ||
const defaultGlobOptions = {}; | ||
@@ -15,4 +20,4 @@ const defaultResolveUrl = true; | ||
} | ||
if (typeof injector !== 'function') { | ||
throw new TypeError('[style-resources-loader] Expected options.injector to be a function. ' | ||
if (typeof injector !== 'function' && !Object.keys(internalInjectors).includes(injector)) { | ||
throw new TypeError('[style-resources-loader] Expected options.injector to be a function or `prepend`, `append`. ' | ||
+ `Instead received ${typeof injector}.`); | ||
@@ -30,3 +35,3 @@ } | ||
patterns, | ||
injector, | ||
injector: getNormalizedInjector(injector), | ||
globOptions, | ||
@@ -33,0 +38,0 @@ resolveUrl, |
{ | ||
"name": "style-resources-loader", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "CSS preprocessor resources loader for webpack", | ||
@@ -59,3 +59,2 @@ "engines": { | ||
], | ||
"mapCoverage": true, | ||
"coveragePathIgnorePatterns": [ | ||
@@ -72,18 +71,18 @@ "node_modules", | ||
"devDependencies": { | ||
"@types/glob": "^5.0.33", | ||
"@types/jest": "^21.1.8", | ||
"@types/loader-utils": "^1.1.0", | ||
"@types/node": "^8.0.54", | ||
"@types/webpack": "^3.8.1", | ||
"@types/webpack-merge": "^4.1.1", | ||
"@types/glob": "^5.0.35", | ||
"@types/jest": "^22.1.3", | ||
"@types/loader-utils": "^1.1.2", | ||
"@types/node": "^9.4.6", | ||
"@types/webpack": "^3.8.8", | ||
"@types/webpack-merge": "^4.1.2", | ||
"coveralls": "^3.0.0", | ||
"cross-env": "^5.0.5", | ||
"jest": "^21.2.1", | ||
"cross-env": "^5.1.3", | ||
"jest": "^22.4.2", | ||
"raw-loader": "^0.5.1", | ||
"ts-jest": "^21.2.4", | ||
"tslint": "^5.7.0", | ||
"tslint-config-airbnb": "^5.4.2", | ||
"typescript": "^2.6.2", | ||
"webpack": "^3.10.0", | ||
"webpack-merge": "^4.1.1" | ||
"ts-jest": "^22.4.0", | ||
"tslint": "^5.9.1", | ||
"tslint-config-airbnb": "^5.7.0", | ||
"typescript": "^2.7.2", | ||
"webpack": "^3.11.0", | ||
"webpack-merge": "^4.1.2" | ||
}, | ||
@@ -90,0 +89,0 @@ "peerDependencies": { |
@@ -72,3 +72,3 @@ [![npm][npm]][npm-url] | ||
patterns: path.resolve(__dirname, 'path/to/less/variables/*.less'), | ||
injector: (source, resources) => source + resources.map(({ content }) => content).join('') | ||
injector: 'append' | ||
} | ||
@@ -89,3 +89,3 @@ }] | ||
rules: [{ | ||
test: /\.scss$/, | ||
test: /\.styl$/, | ||
use: ['style-loader', 'css-loader', 'stylus-loader', { | ||
@@ -117,3 +117,3 @@ loader: 'style-resources-loader', | ||
|**[`patterns`](#patterns)**|`{String \| String[]}`|`/`|Path to the resources you would like to inject| | ||
|**[`injector`](#injector)**|`{Function}`|`(source, resources) => resources.map(({ content }) => content).join('') + source`|Controls the resources injection precisely| | ||
|**[`injector`](#injector)**|`{Function \| 'prepend' \| 'append'}`|`prepend`|Controls the resources injection precisely| | ||
|**[`globOptions`](#globoptions)**|`{Object}`|`{}`|An options that can be passed to `glob(...)`| | ||
@@ -142,7 +142,7 @@ |**[`resolveUrl`](#resolveurl)**|`{Boolean}`|`true`|Enable/Disable `@import` url to be resolved| | ||
An optional function which controls the resources injection precisely. | ||
An optional function which controls the resources injection precisely. It also supports `prepend` and `append` for convenience. | ||
It defaults to `(source, resources) => resources.map(({ content }) => content).join('') + source`, which means the loader prepends all resources to source file. | ||
It defaults to `prepend` (equivalent to `(source, resources) => resources.map(({ content }) => content).join('') + source` internally), which means the loader prepends all resources to source file. | ||
It receives two parameters: | ||
An injector function receives two parameters: | ||
@@ -149,0 +149,0 @@ |Name|Type|Default|Description| |
@@ -14,2 +14,6 @@ import * as glob from 'glob'; | ||
export type StyleResourcesInternalInjectors = Record<'prepend' | 'append', StyleResourcesInjector>; | ||
export type StyleResourcesOriginalInjector = StyleResourcesInjector | keyof StyleResourcesInternalInjectors; | ||
export interface StyleResourcesLoaderOptions { | ||
@@ -22,2 +26,7 @@ patterns: string | string[]; | ||
export type StyleResourcesLoaderOriginalOptions = Partial<StyleResourcesLoaderOptions>; | ||
export type StyleResourcesLoaderOriginalOptions = Partial<{ | ||
patterns: string | string[]; | ||
injector: StyleResourcesOriginalInjector; | ||
globOptions: glob.IOptions; | ||
resolveUrl: boolean; | ||
}>; |
import { loader } from 'webpack'; | ||
import { getOptions } from 'loader-utils'; | ||
import { StyleResourcesInjector, StyleResourcesLoaderOptions, StyleResourcesLoaderOriginalOptions } from '../'; | ||
import { | ||
StyleResourcesLoaderOptions, | ||
StyleResourcesLoaderOriginalOptions, | ||
StyleResourcesInjector, | ||
StyleResourcesInternalInjectors, | ||
StyleResourcesOriginalInjector, | ||
} from '../'; | ||
import { isString } from './'; | ||
const internalInjectors: StyleResourcesInternalInjectors = { | ||
prepend: (source, resources) => resources.map(({ content }) => content).join('') + source, | ||
append: (source, resources) => source + resources.map(({ content }) => content).join(''), | ||
}; | ||
const getNormalizedInjector = (injector: StyleResourcesOriginalInjector): StyleResourcesInjector => | ||
typeof injector === 'function' ? injector : internalInjectors[injector]; | ||
export function getNormalizedOptions(this: loader.LoaderContext): StyleResourcesLoaderOptions { | ||
const defaultInjector: StyleResourcesInjector = (source, resources) => | ||
resources.map(({ content }) => content).join('') + source; | ||
const defaultInjector = 'prepend'; | ||
@@ -29,5 +42,5 @@ const defaultGlobOptions = {}; | ||
if (typeof injector !== 'function') { | ||
if (typeof injector !== 'function' && !Object.keys(internalInjectors).includes(injector)) { | ||
throw new TypeError( | ||
'[style-resources-loader] Expected options.injector to be a function. ' | ||
'[style-resources-loader] Expected options.injector to be a function or `prepend`, `append`. ' | ||
+ `Instead received ${typeof injector}.`, | ||
@@ -53,3 +66,3 @@ ); | ||
patterns, | ||
injector, | ||
injector: getNormalizedInjector(injector), | ||
globOptions, | ||
@@ -56,0 +69,0 @@ resolveUrl, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35106
49
397