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
4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: 'Minifier for Meteor with PostCSS processing - use Autoprefixer and others with ease',
version: '2.0.8',
version: '3.0.0-alpha.19',
name: 'juliancwirko:postcss',
git: 'https://github.com/juliancwirko/meteor-postcss.git'
});
Expand All @@ -9,7 +9,7 @@ Package.registerBuildPlugin({
name: 'minifier-postcss',
use: [
'ecmascript@0.15.2',
'minifier-css@1.5.4',
'minifier-css@1.5.4 || 2.0.0-alpha300.19',
'tmeasday:check-npm-versions@1.0.2'
],
npmDependencies: {
Expand Down
80 changes: 42 additions & 38 deletions plugin/minify-css.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Makes sure we can load peer dependencies from app's directory.
// See: https://github.com/juliancwirko/meteor-postcss/issues/15
// https://github.com/meteor/meteor/issues/10827
import postCSS from 'postcss'

Npm.require('app-module-path/cwd');

import { checkNpmVersions } from 'meteor/tmeasday:check-npm-versions';
import Future from 'fibers/future';
import sourcemap from 'source-map';

checkNpmVersions({
'postcss': '8.3.x',
'postcss': '8.4.x',
'postcss-load-config': '3.1.x'
}, 'juliancwirko:postcss');

Expand All @@ -28,14 +29,14 @@ var postcssConfigPlugins = [];
var postcssConfigParser = null;
var postcssConfigExcludedPackages = [];

var loadPostcssConfig = function () {
var loadPostcssConfig = async function () {
if (!loaded) {
loaded = true;

var config;
try {
const load = require('postcss-load-config');
config = Promise.await(load({meteor: true}));
config = await load({meteor: true});
postcssConfigPlugins = config.plugins || [];
postcssConfigParser = config.options.parser || null;
postcssConfigExcludedPackages = config.options.excludedPackages || [];
Expand Down Expand Up @@ -70,8 +71,8 @@ var isNotImport = function (inputFileUrl) {

function CssToolsMinifier() {};

CssToolsMinifier.prototype.processFilesForBundle = function (files, options) {
loadPostcssConfig();
CssToolsMinifier.prototype.processFilesForBundle = async function (files, options) {
await loadPostcssConfig();

var mode = options.minifyMode;

Expand All @@ -85,7 +86,7 @@ CssToolsMinifier.prototype.processFilesForBundle = function (files, options) {
}
});

var merged = mergeCss(filesToMerge);
var merged = await mergeCss(filesToMerge);

if (mode === 'development') {
files[0].addStylesheet({
Expand All @@ -110,18 +111,18 @@ CssToolsMinifier.prototype.processFilesForBundle = function (files, options) {
// Lints CSS files and merges them into one file, fixing up source maps and
// pulling any @import directives up to the top since the CSS spec does not
// allow them to appear in the middle of a file.
var mergeCss = function (css) {
var mergeCss = async function (css) {
// Filenames passed to AST manipulator mapped to their original files
var originals = {};
var postCSS = require('postcss');
var cssAsts = []

var cssAsts = css.map(function (file) {
// use for..of to sequencially process
// async results, which won't work in .map
for (const file of css) {
var filename = file.getPathInBundle();
originals[filename] = file;

var f = new Future;

var css;
var currentCss;
var postres;
var isFileForPostCSS;

Expand All @@ -131,41 +132,21 @@ var mergeCss = function (css) {
isFileForPostCSS = false;
}

postCSS(isFileForPostCSS ? postcssConfigPlugins : [])
.process(file.getContentsAsString(), {
from: process.cwd() + file._source.url?.replace('/__cordova', ''),
parser: postcssConfigParser
})
.then(function (result) {
result.warnings().forEach(function (warn) {
process.stderr.write(warn.toString());
});
f.return(result);
})
.catch(function (error) {
var errMsg = error.message;
if (error.name === 'CssSyntaxError') {
errMsg = error.message + '\n\n' + 'Css Syntax Error.' + '\n\n' + error.message + error.showSourceCode()
}
error.message = errMsg;
f.return(error);
});

try {
var parseOptions = {
source: filename,
position: true
};

postres = f.wait();
postres = await processFile({ file, isFileForPostCSS })

if (postres.name === 'CssSyntaxError') {
throw postres;
}

css = postres.css;
currentCss = postres.css;

var ast = CssTools.parseCss(css, parseOptions);
var ast = CssTools.parseCss(currentCss, parseOptions);
ast.filename = filename;
} catch (e) {

Expand Down Expand Up @@ -197,8 +178,8 @@ var mergeCss = function (css) {
};
}

return ast;
});
cssAsts.push(ast);
}

var warnCb = function (filename, msg) {
// XXX make this a buildmessage.warning call rather than a random log.
Expand Down Expand Up @@ -254,3 +235,26 @@ var mergeCss = function (css) {
sourceMap: newMap.toString()
};
};

const processFile = async ({ file, isFileForPostCSS }) => {
let result

try {
result = await postCSS(isFileForPostCSS ? postcssConfigPlugins : [])
.process(file.getContentsAsString(), {
from: process.cwd() + file._source.url?.replace('/__cordova', ''),
parser: postcssConfigParser
})
result.warnings().forEach(function (warn) {
process.stderr.write(warn.toString());
});
return result
} catch (error) {
var errMsg = error.message;
if (error.name === 'CssSyntaxError') {
errMsg = error.message + '\n\n' + 'Css Syntax Error.' + '\n\n' + error.message + error.showSourceCode()
}
error.message = errMsg;
return error
}
}