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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"dependencies": {
"autoprefixer": "^10.3.6",
"glob": "^8.0.3",
"postcss": "~8.3.8"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from '@stencil/core/internal';
export interface PluginOptions {
injectGlobalPaths?: string[];
plugins?: any[];
alwaysParseNonCachedCss?: boolean;
}

export interface RendererOptions {
Expand Down
28 changes: 16 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import postCss from 'postcss';
import { loadDiagnostic } from './diagnostics';
import type * as d from './declarations';
import * as util from './util';
//import path from 'path';
//import glob from 'glob';

export function postcss(opts: d.PluginOptions = {}): d.Plugin {
return {
Expand All @@ -16,6 +18,19 @@ export function postcss(opts: d.PluginOptions = {}): d.Plugin {
return null;
}

// Workaround for JIT handling of i.e. Tailwind: Read the source text from the CSS indepent from Stencil,
// in order to always get the raw input file.
if(opts.hasOwnProperty('alwaysParseNonCachedCss') && opts.alwaysParseNonCachedCss)
{
try {
sourceText = context.sys.readFileSync(fileName); // Read non-cached variant
}
catch(ex)
{
console.error("Reading the source CSS file from path " + fileName + " failed");
}
}

const renderOpts = util.getRenderOptions(opts, sourceText, context);

const results: d.PluginTransformResults = {
Expand Down Expand Up @@ -58,18 +73,7 @@ export function postcss(opts: d.PluginOptions = {}): d.Plugin {
resolve(results);
} else {
results.code = postCssResults.css.toString();
results.dependencies = postCssResults.messages
.filter((message) => message.type === 'dependency')
.map((dependency) => dependency.file);

// TODO(#38) https://github.com/ionic-team/stencil-postcss/issues/38
// determining how to pass back the dir-dependency message helps
// enable JIT behavior, such as Tailwind.
//
// Pseudocode:
// results.dependencies = postCssResults.messages
// .filter((message) => message.type === 'dir-dependency')
// .map((dependency) => () => dependency.file);
results.dependencies = []; // Can be left empty, since JIT with other Frameworks works after using non-cached CSS source.

// write this css content to memory only so it can be referenced
// later by other plugins (autoprefixer)
Expand Down