Merge pull request #5 from g45t345rt/master

update packages to latest + css entry-point fix and feature to resolve css from node_modules
This commit is contained in:
Marton Lederer 2021-03-26 23:39:20 +01:00 committed by GitHub
commit 48ca8b76ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1305 additions and 1063 deletions

View File

@ -27,34 +27,35 @@
"module": "dist/index.esm.js", "module": "dist/index.esm.js",
"types": "src/modules.d.ts", "types": "src/modules.d.ts",
"dependencies": { "dependencies": {
"autoprefixer": "^10.2.4", "autoprefixer": "^10.2.5",
"fs-extra": "^9.1.0", "fs-extra": "^9.1.0",
"less": "^4.x", "less": "^4.x",
"postcss": "8.x", "postcss": "8.x",
"postcss-modules": "^4.0.0", "postcss-modules": "^4.0.0",
"resolve-file": "^0.3.0",
"sass": "^1.x", "sass": "^1.x",
"stylus": "^0.x", "stylus": "^0.x",
"tmp": "^0.2.1" "tmp": "^0.2.1"
}, },
"devDependencies": { "devDependencies": {
"@types/chai": "^4.2.15", "@types/chai": "^4.2.15",
"@types/fs-extra": "^9.0.7", "@types/fs-extra": "^9.0.8",
"@types/less": "^3.0.2", "@types/less": "^3.0.2",
"@types/mocha": "^8.2.1", "@types/mocha": "^8.2.2",
"@types/node": "^14.14.28", "@types/node": "^14.14.35",
"@types/sass": "^1.16.0", "@types/sass": "^1.16.0",
"@types/stylus": "^0.48.33", "@types/stylus": "^0.48.33",
"@types/tmp": "^0.2.0", "@types/tmp": "^0.2.0",
"chai": "^4.3.0", "chai": "^4.3.4",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"esbuild": "^0.8.49", "esbuild": "^0.9.6",
"mocha": "^8.3.0", "mocha": "^8.3.2",
"normalize.css": "^8.0.1",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"typescript": "^4.1.5", "typescript": "^4.2.3",
"yorkie": "^2.0.0" "yorkie": "^2.0.0"
}, },
"peerDependencies": { "peerDependencies": {
"esbuild": "^0.8.46",
"less": "^4.x", "less": "^4.x",
"postcss": "8.x", "postcss": "8.x",
"sass": "^1.x", "sass": "^1.x",

View File

@ -13,6 +13,7 @@ import postcss from "postcss";
import postcssModules from "postcss-modules"; import postcssModules from "postcss-modules";
import less from "less"; import less from "less";
import stylus from "stylus"; import stylus from "stylus";
import resolveFile from "resolve-file";
interface PostCSSPluginOptions { interface PostCSSPluginOptions {
plugins: PostCSSPlugin[]; plugins: PostCSSPlugin[];
@ -56,21 +57,31 @@ const postCSSPlugin = ({
}); });
build.onResolve( build.onResolve(
{ filter: /.\.(css|sass|scss|less|styl)$/, namespace: "file" }, { filter: /.\.(css|sass|scss|less|styl)$/ },
async (args) => { async (args) => {
const sourceFullPath = path.resolve(args.resolveDir, args.path), // Namespace is empty when using CSS as an entrypoint
sourceExt = path.extname(sourceFullPath), if (args.namespace !== "file" && args.namespace !== "") return;
sourceBaseName = path.basename(sourceFullPath, sourceExt),
sourceDir = path.dirname(sourceFullPath), // Resolve files also from node_modules (ex: npm normalize.css)
sourceRelDir = path.relative(path.dirname(rootDir), sourceDir), let sourceFullPath = resolveFile(args.path);
isModule = sourceBaseName.match(/\.module$/), if (!sourceFullPath)
tmpDir = path.resolve(tmpDirPath, sourceRelDir), sourceFullPath = path.resolve(args.resolveDir, args.path);
tmpFilePath = path.resolve(
tmpDir, const sourceExt = path.extname(sourceFullPath);
`${sourceBaseName}-tmp-${Date.now()}-${sourceExt.replace(".", "")}${ const sourceBaseName = path.basename(sourceFullPath, sourceExt);
isModule ? ".module" : "" const sourceDir = path.dirname(sourceFullPath);
}.css` const sourceRelDir = path.relative(path.dirname(rootDir), sourceDir);
); const isModule = sourceBaseName.match(/\.module$/);
const tmpDir = path.resolve(tmpDirPath, sourceRelDir);
let tmpFilePath = path.resolve(
tmpDir,
`${Date.now()}-${sourceBaseName}.css`
);
// When CSS is an entry-point we don't want to append Date.now()
if (args.kind === "entry-point")
tmpFilePath = path.resolve(tmpDir, `${sourceBaseName}.css`);
await ensureDir(tmpDir); await ensureDir(tmpDir);

View File

@ -5,7 +5,7 @@ const autoprefixer = require("autoprefixer"),
describe("PostCSS esbuild tests", () => { describe("PostCSS esbuild tests", () => {
it("Works with basic CSS imports", (done) => { it("Works with basic CSS imports", (done) => {
test("basic") test(["tests/basic.ts"])
.then((res) => { .then((res) => {
assert(res); assert(res);
done(); done();
@ -13,7 +13,7 @@ describe("PostCSS esbuild tests", () => {
.catch(done); .catch(done);
}); });
it("Works with preprocessors", (done) => { it("Works with preprocessors", (done) => {
test("preprocessors") test(["tests/preprocessors.ts"])
.then((res) => { .then((res) => {
assert(res); assert(res);
done(); done();
@ -21,7 +21,23 @@ describe("PostCSS esbuild tests", () => {
.catch(done); .catch(done);
}); });
it("Works with CSS modules", (done) => { it("Works with CSS modules", (done) => {
test("modules") test(["tests/modules.ts"])
.then((res) => {
assert(res);
done();
})
.catch(done);
});
it("Works with CSS as entrypoint", (done) => {
test(["tests/styles.css", "tests/styles2.css"])
.then((res) => {
assert(res);
done();
})
.catch(done);
});
it("Works with node_modules import", (done) => {
test(["tests/node_modules.ts"])
.then((res) => { .then((res) => {
assert(res); assert(res);
done(); done();
@ -30,9 +46,9 @@ describe("PostCSS esbuild tests", () => {
}); });
}); });
function test(test) { function test(entryPoint) {
return build({ return build({
entryPoints: [`tests/${test}.ts`], entryPoints: entryPoint,
bundle: true, bundle: true,
outdir: "dist", outdir: "dist",
plugins: [ plugins: [

View File

@ -0,0 +1 @@
import "normalize.css";

6
test/tests/styles.css Normal file
View File

@ -0,0 +1,6 @@
.example {
display: grid;
transition: all 0.5s;
user-select: none;
background: linear-gradient(to bottom, white, black);
}

6
test/tests/styles2.css Normal file
View File

@ -0,0 +1,6 @@
.example {
display: grid;
transition: all 0.5s;
user-select: none;
background: linear-gradient(to bottom, white, black);
}

2271
yarn.lock

File diff suppressed because it is too large Load Diff