Skip to content

Commit

Permalink
switch to esm (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvetere authored Oct 15, 2024
1 parent 95a1c69 commit bc2d114
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 35 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 21 additions & 15 deletions babel/index.js → babel/index.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @ts-check
const PACKAGE_NAME = "css-variable";
const hash = require("./hash");
const hash = require("./hash.cjs");
const pathRelative = require("path").relative;

/** @typedef {import("@babel/core")} babel */

/**
/**
* The context of a babel plugin run
* @typedef {{
* minifyVariables: boolean,
Expand All @@ -14,7 +14,7 @@ const pathRelative = require("path").relative;
* isImportedInCurrentFile: boolean,
* localVarNames: string[],
* opts: babel.PluginOptions & { displayName?: boolean }
* } & babel.PluginPass} PluginPass
* } & babel.PluginPass} PluginPass
*/

/**
Expand All @@ -36,10 +36,14 @@ module.exports = function (babel) {
return;
}
const callee = path.node.callee;
if (!("name" in callee) || !pluginPass.localVarNames.includes(callee.name)) {
if (
!("name" in callee) ||
!pluginPass.localVarNames.includes(callee.name)
) {
return;
}
const readableName = !pluginPass.minifyVariables && dashed(getNameByUsage(path));
const readableName =
!pluginPass.minifyVariables && dashed(getNameByUsage(path));
const readablePrefix = readableName ? `${readableName}--` : "";
//
// Inject the variable prefix
Expand All @@ -51,7 +55,9 @@ module.exports = function (babel) {
const firstArg = constructorArguments[0];
if (!firstArg || firstArg.type !== "StringLiteral") {
constructorArguments.unshift(
stringLiteral(readablePrefix + getUniqueHash(pluginPass) + pluginPass.varCount++)
stringLiteral(
readablePrefix + getUniqueHash(pluginPass) + pluginPass.varCount++
)
);
}
//
Expand All @@ -60,16 +66,17 @@ module.exports = function (babel) {
// side effects and is save to be removed
//
path.addComment("leading", "@__PURE__");
}
};

return {
name: `${PACKAGE_NAME} unique variable name injector`,
pre() {
this.isImportedInCurrentFile = false;
this.varCount = 0;
this.minifyVariables = this.opts.displayName !== undefined ?
!this.opts.displayName
: this.file.opts.envName !== "development";
this.minifyVariables =
this.opts.displayName !== undefined
? !this.opts.displayName
: this.file.opts.envName !== "development";
this.localVarNames = [];
},
visitor: {
Expand Down Expand Up @@ -106,7 +113,6 @@ module.exports = function (babel) {
};
};


/**
* Tries to extract the name for readable names in developments
* e.g.:
Expand All @@ -118,7 +124,7 @@ module.exports = function (babel) {
*/
function getNameByUsage(path) {
const parent = path.parent;
if (!parent) return;
if (!parent) return "";
if (parent.type === "ObjectProperty") {
const key = parent.key;
if (key && key.type === "Identifier" && key.name) {
Expand All @@ -138,9 +144,9 @@ function getNameByUsage(path) {
function dashed(val) {
/** handle camelCase and CONSTANT_CASE */
return val
.replace(/([0-9a-z])([A-Z])/g, "$1-$2")
.toLowerCase()
.replace(/_/g, "-");
.replace(/([0-9a-z])([A-Z])/g, "$1-$2")
.toLowerCase()
.replace(/_/g, "-");
}

/** @type {WeakMap<babel.PluginPass, string>} */
Expand Down
7 changes: 0 additions & 7 deletions examples/styled-components/.babelrc

This file was deleted.

10 changes: 10 additions & 0 deletions examples/styled-components/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function (api) {
api.cache(true);
return {
presets: ["next/babel"],
plugins: [
["css-variable/babel", { async: false }],
["babel-plugin-styled-components", { ssr: true }],
],
};
};
2 changes: 1 addition & 1 deletion examples/styled-components/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
4 changes: 2 additions & 2 deletions examples/styled-components/next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
pageExtensions: ['page.tsx'],
};
pageExtensions: ["page.tsx"],
};
2 changes: 1 addition & 1 deletion examples/styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"styled-components": "^6.1.11",
"typescript": "5.4.5"
}
}
}
File renamed without changes.
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"name": "css-variable",
"version": "4.0.0",
"version": "4.0.1",
"description": "define CSS custom properties (variables) in JS",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./babel": "./babel/index.js",
"./babel": "./babel/index.cjs",
"./swc": "./swc/target/wasm32-wasi/release/swc_plugin_css_variable.wasm"
},
"types": "./dist/index.d.ts",
Expand All @@ -24,8 +26,8 @@
"scripts": {
"prepublishOnly": "npm run build && npm run build:swc",
"build": "npm run build:types && npm run build:commonjs && npm run build:module && npm run build:modulemin",
"build:commonjs": "babel --config-file=./babel.commonjs.js -o dist/index.js src/index.ts",
"build:module": "babel --config-file=./babel.config.js -o dist/index.mjs src/index.ts",
"build:commonjs": "babel --config-file=./babel.commonjs.cjs -o dist/index.js src/index.ts",
"build:module": "babel --config-file=./babel.config.cjs -o dist/index.mjs src/index.ts",
"build:types": "tsc --skipLibCheck --emitDeclarationOnly --declaration --target ESNext --outDir dist src/index.ts",
"build:modulemin": "terser ./dist/index.mjs -o ./dist/index.min.mjs -m --ecma 2017 --module --toplevel -b -c",
"build:swc": "cargo build --manifest-path ./swc/Cargo.toml --release --target=wasm32-wasi",
Expand Down Expand Up @@ -58,11 +60,11 @@
"homepage": "https://css-variable.js.org/",
"devDependencies": {
"@babel/cli": "7.19.3",
"@babel/core": "7.20.2",
"@babel/core": "7.22.0",
"@babel/plugin-transform-modules-commonjs": "^7.19.6",
"@babel/preset-typescript": "^7.18.6",
"@babel/runtime": "^7.20.1",
"@babel/types": "^7.20.2",
"@babel/types": "^7.22.0",
"@swc/core": "1.4.17",
"@swc/jest": "^0.2.36",
"@types/jest": "^29.2.3",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
}
}

0 comments on commit bc2d114

Please sign in to comment.