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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
"types": "src/modules.d.ts",
"dependencies": {
"autoprefixer": "^10.2.5",
"bulma": "^0.9.3",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't get why you add this as production dependency to get only the css. You should pay more attention and use less css. For the test it would be totally sufficient to create a test css file which contains the problematic css code.

"fs-extra": "^9.1.0",
"less": "^4.x",
"postcss": "8.x",
"postcss-modules": "^4.0.0",
"resolve-file": "^0.3.0",
"resolve": "^1.20.0",
"sass": "^1.x",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually sass, less, stylus etc should also be just peerDependencies and no hard dependencies.

"stylus": "^0.x",
"tmp": "^0.2.1"
Expand Down
32 changes: 20 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import postcss from "postcss";
import postcssModules from "postcss-modules";
import less from "less";
import stylus from "stylus";
import resolveFile from "resolve-file";
import resolve from "resolve";

type StylusRenderOptions = Parameters<typeof stylus.render>[1]; // The Stylus.RenderOptions interface doesn't seem to be exported... So next best

Expand Down Expand Up @@ -51,13 +51,13 @@ export const defaultOptions: PostCSSPluginOptions = {
};

const postCSSPlugin = ({
plugins,
modules,
rootDir,
sassOptions,
lessOptions,
stylusOptions,
writeToFile
plugins = [],
modules = true,
rootDir = process.cwd(),
sassOptions = {},
lessOptions = {},
stylusOptions = {},
writeToFile = true
Comment on lines +54 to +60

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this back to pass the test.

}: PostCSSPluginOptions = defaultOptions): Plugin => ({
name: "postcss2",
setup(build) {
Expand Down Expand Up @@ -94,12 +94,20 @@ const postCSSPlugin = ({
// Namespace is empty when using CSS as an entrypoint
if (args.namespace !== "file" && args.namespace !== "") return;

// Resolve files from node_modules (ex: npm install normalize.css)
let sourceFullPath = resolveFile(args.path);
if (!sourceFullPath)
sourceFullPath = path.resolve(args.resolveDir, args.path);
let sourceFullPath = resolve.sync(args.path, {
basedir: args.resolveDir
});
Comment on lines +97 to +99

@baurine baurine Dec 10, 2021

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can resolve all imported files correctly, not only local but from node_modules.


const sourceExt = path.extname(sourceFullPath);
if (
sourceExt !== ".css" &&
sourceExt !== ".sass" &&
sourceExt !== ".scss" &&
sourceExt !== ".less" &&
sourceExt !== ".styl"
) {
return;
}
Comment on lines +102 to +110

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file may be a .js file, if so, we can skip it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using a regular expression? if (!/\.(scss|sass|css|less|styl)/.test(sourceExt)) {. Or even extract that check into a separate function to make it more readable

function isStyle(extension) {
  return /\.(scss|sass|css|less|styl)/.test(extension);
}

...

if (!isStyle(sourceExt) {

const sourceBaseName = path.basename(sourceFullPath, sourceExt);
const isModule = sourceBaseName.match(/\.module$/);
const sourceDir = path.dirname(sourceFullPath);
Expand Down
26 changes: 26 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const autoprefixer = require("autoprefixer"),
fs = require("fs");

describe("PostCSS esbuild tests", () => {
it("Works with empty options", (done) => {
testWithoutOptions(["tests/basic.ts"])
.then((res) => {
assert(res);
done();
})
.catch(done);
});
it("Works with basic CSS imports", (done) => {
test(["tests/basic.ts"])
.then((res) => {
Expand Down Expand Up @@ -46,6 +54,15 @@ describe("PostCSS esbuild tests", () => {
})
.catch(done);
});
it("Works with fake css file", (done) => {
test(["tests/BasePicker.js"])
.then((res) => {
assert(res);
assert(!fs.existsSync("./dist/BasePicker.css"));
done();
})
.catch(done);
});
it("Works while waching css files directly", (done) => {
let notTriggerTimeout = null;
build({
Expand Down Expand Up @@ -113,6 +130,15 @@ describe("PostCSS esbuild tests", () => {
});
});

function testWithoutOptions(entryPoint) {
return build({
entryPoints: entryPoint,
bundle: true,
outdir: "dist",
plugins: [postCSS.default()]
}).catch(() => process.exit(1));
}

function test(entryPoint) {
return build({
entryPoints: entryPoint,
Expand Down
4 changes: 4 additions & 0 deletions test/tests/BasePicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This usage comes from office-ui-fabric-react (renamed to fluent-ui later)
// After installing office-ui-fabric-react
// The below code exists in node_modules/office-ui-fabric-react/lib/components/pickers/BasePicker.js
import * as stylesImport from "./BasePicker.scss";
36 changes: 36 additions & 0 deletions test/tests/BasePicker.scss.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/tests/node_modules.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import "normalize.css";
import "bulma/css/bulma.css";
Loading