Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test.watch": "jest --watch"
},
"dependencies": {
"postcss": "6.0.20"
"postcss": "^7.0.16"
},
"devDependencies": {
"@types/jest": "^22.2.0",
Expand Down
10 changes: 9 additions & 1 deletion src/declarations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ProcessOptions } from 'postcss';

export interface PluginOptions {
injectGlobalPaths?: string[];
plugins?: Array<any>;
Expand All @@ -8,11 +10,17 @@ export interface PluginTransformResults {
id?: string;
}

export interface RendererOptions {
export interface RendererOptions extends ProcessOptions {
data: string;
plugins: Array<any>;
}

export interface RendererCtx {
env?: string;
file?: string;
options?: ProcessOptions;
}

export interface PluginCtx {
config: {
rootDir?: string;
Expand Down
30 changes: 21 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import { loadDiagnostic } from './diagnostics';
import * as d from './declarations';
import * as util from './util';


export function postcss(opts: d.PluginOptions = {}) {

export function postcss(
opts: ((ctx: d.RendererCtx) => d.PluginOptions) | d.PluginOptions = {}
) {
return {
name: 'postcss',
transform(sourceText: string, fileName: string, context: d.PluginCtx) {
if (typeof opts === 'function') {
opts = opts({
env: process.env.NODE_ENV,
file: fileName
});
}

if (!opts.hasOwnProperty('plugins') || opts.plugins.length < 1) {
return null;
}
Expand All @@ -29,7 +36,6 @@ export function postcss(opts: d.PluginOptions = {}) {
}

return new Promise<d.PluginTransformResults>(resolve => {

postCss(renderOpts.plugins)
.process(renderOpts.data, {
from: fileName
Expand All @@ -52,7 +58,9 @@ export function postcss(opts: d.PluginOptions = {}) {

const mappedWarnings = warnings
.map((warn: any) => {
return `${warn.type} ${warn.plugin ? `(${warn.plugin})` : ''}: ${warn.text}`;
return `${warn.type} ${
warn.plugin ? `(${warn.plugin})` : ''
}: ${warn.text}`;
})
.join(', ');

Expand All @@ -64,16 +72,20 @@ export function postcss(opts: d.PluginOptions = {}) {
// write this css content to memory only so it can be referenced
// later by other plugins (autoprefixer)
// but no need to actually write to disk
context.fs.writeFile(results.id, results.code, { inMemoryOnly: true }).then(() => {
resolve(results);
});
context.fs
.writeFile(results.id, results.code, { inMemoryOnly: true })
.then(() => {
resolve(results);
});
}

return results;
})
.catch((err: any) => {
loadDiagnostic(context, err, fileName);
results.code = `/** postcss error${err && err.message ? ': ' + err.message : ''} **/`;
results.code = `/** postcss error${
err && err.message ? ': ' + err.message : ''
} **/`;
resolve(results);
});
});
Expand Down
35 changes: 22 additions & 13 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
import * as d from './declarations';
import * as path from 'path';


export function usePlugin(fileName: string) {
return /(\.css|\.pcss)$/i.test(fileName);
}

export function getRenderOptions(opts: d.PluginOptions, sourceText: string, context: d.PluginCtx) {
export function getRenderOptions(
opts: d.PluginOptions,
sourceText: string,
context: d.PluginCtx
) {
const renderOpts: Partial<d.RendererOptions> = {
plugins: opts.plugins || []
};

// always set "data" from the source text
// always set 'data' from the source text
renderOpts.data = sourceText || '';

const injectGlobalPaths = Array.isArray(opts.injectGlobalPaths) ? opts.injectGlobalPaths.slice() : [];
const injectGlobalPaths = Array.isArray(opts.injectGlobalPaths)
? opts.injectGlobalPaths.slice()
: [];

if (injectGlobalPaths.length > 0) {
// automatically inject each of these paths into the source text
const injectText = injectGlobalPaths.map(injectGlobalPath => {
if (!path.isAbsolute(injectGlobalPath)) {
// convert any relative paths to absolute paths relative to the project root
injectGlobalPath = path.join(context.config.rootDir, injectGlobalPath);
}

return `@import "${injectGlobalPath}";`;
}).join('');
const injectText = injectGlobalPaths
.map(injectGlobalPath => {
if (!path.isAbsolute(injectGlobalPath)) {
// convert any relative paths to absolute paths relative to the project root
injectGlobalPath = path.join(
context.config.rootDir,
injectGlobalPath
);
}

return `@import "${injectGlobalPath}";`;
})
.join('');

renderOpts.data = injectText + renderOpts.data;
}

return renderOpts;
}


export function createResultsId(fileName: string) {
// create what the new path is post transform (.css)
const pathParts = fileName.split('.');
Expand Down
8 changes: 2 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"alwaysStrict": true,
"declaration": true,
"forceConsistentCasingInFileNames": true,
"lib": [
"es2017"
],
"lib": ["es2017"],
"module": "es2015",
"moduleResolution": "node",
"noImplicitAny": true,
Expand All @@ -19,7 +17,5 @@
"removeComments": false,
"target": "es2015"
},
"files": [
"src/index.ts"
]
"files": ["src/index.ts"]
}