Skip to content

Commit bc2d114

Browse files
authored
switch to esm (jantimon#38)
1 parent 95a1c69 commit bc2d114

12 files changed

+46
-35
lines changed
File renamed without changes.

babel.config.js babel.config.cjs

File renamed without changes.

babel/hash.js babel/hash.cjs

File renamed without changes.

babel/index.js babel/index.cjs

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// @ts-check
22
const PACKAGE_NAME = "css-variable";
3-
const hash = require("./hash");
3+
const hash = require("./hash.cjs");
44
const pathRelative = require("path").relative;
55

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

8-
/**
8+
/**
99
* The context of a babel plugin run
1010
* @typedef {{
1111
* minifyVariables: boolean,
@@ -14,7 +14,7 @@ const pathRelative = require("path").relative;
1414
* isImportedInCurrentFile: boolean,
1515
* localVarNames: string[],
1616
* opts: babel.PluginOptions & { displayName?: boolean }
17-
* } & babel.PluginPass} PluginPass
17+
* } & babel.PluginPass} PluginPass
1818
*/
1919

2020
/**
@@ -36,10 +36,14 @@ module.exports = function (babel) {
3636
return;
3737
}
3838
const callee = path.node.callee;
39-
if (!("name" in callee) || !pluginPass.localVarNames.includes(callee.name)) {
39+
if (
40+
!("name" in callee) ||
41+
!pluginPass.localVarNames.includes(callee.name)
42+
) {
4043
return;
4144
}
42-
const readableName = !pluginPass.minifyVariables && dashed(getNameByUsage(path));
45+
const readableName =
46+
!pluginPass.minifyVariables && dashed(getNameByUsage(path));
4347
const readablePrefix = readableName ? `${readableName}--` : "";
4448
//
4549
// Inject the variable prefix
@@ -51,7 +55,9 @@ module.exports = function (babel) {
5155
const firstArg = constructorArguments[0];
5256
if (!firstArg || firstArg.type !== "StringLiteral") {
5357
constructorArguments.unshift(
54-
stringLiteral(readablePrefix + getUniqueHash(pluginPass) + pluginPass.varCount++)
58+
stringLiteral(
59+
readablePrefix + getUniqueHash(pluginPass) + pluginPass.varCount++
60+
)
5561
);
5662
}
5763
//
@@ -60,16 +66,17 @@ module.exports = function (babel) {
6066
// side effects and is save to be removed
6167
//
6268
path.addComment("leading", "@__PURE__");
63-
}
69+
};
6470

6571
return {
6672
name: `${PACKAGE_NAME} unique variable name injector`,
6773
pre() {
6874
this.isImportedInCurrentFile = false;
6975
this.varCount = 0;
70-
this.minifyVariables = this.opts.displayName !== undefined ?
71-
!this.opts.displayName
72-
: this.file.opts.envName !== "development";
76+
this.minifyVariables =
77+
this.opts.displayName !== undefined
78+
? !this.opts.displayName
79+
: this.file.opts.envName !== "development";
7380
this.localVarNames = [];
7481
},
7582
visitor: {
@@ -106,7 +113,6 @@ module.exports = function (babel) {
106113
};
107114
};
108115

109-
110116
/**
111117
* Tries to extract the name for readable names in developments
112118
* e.g.:
@@ -118,7 +124,7 @@ module.exports = function (babel) {
118124
*/
119125
function getNameByUsage(path) {
120126
const parent = path.parent;
121-
if (!parent) return;
127+
if (!parent) return "";
122128
if (parent.type === "ObjectProperty") {
123129
const key = parent.key;
124130
if (key && key.type === "Identifier" && key.name) {
@@ -138,9 +144,9 @@ function getNameByUsage(path) {
138144
function dashed(val) {
139145
/** handle camelCase and CONSTANT_CASE */
140146
return val
141-
.replace(/([0-9a-z])([A-Z])/g, "$1-$2")
142-
.toLowerCase()
143-
.replace(/_/g, "-");
147+
.replace(/([0-9a-z])([A-Z])/g, "$1-$2")
148+
.toLowerCase()
149+
.replace(/_/g, "-");
144150
}
145151

146152
/** @type {WeakMap<babel.PluginPass, string>} */

examples/styled-components/.babelrc

-7
This file was deleted.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function (api) {
2+
api.cache(true);
3+
return {
4+
presets: ["next/babel"],
5+
plugins: [
6+
["css-variable/babel", { async: false }],
7+
["babel-plugin-styled-components", { ssr: true }],
8+
],
9+
};
10+
};

examples/styled-components/next-env.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/basic-features/typescript for more information.
5+
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
pageExtensions: ['page.tsx'],
3-
};
2+
pageExtensions: ["page.tsx"],
3+
};

examples/styled-components/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
"styled-components": "^6.1.11",
2020
"typescript": "5.4.5"
2121
}
22-
}
22+
}

jest.config.js jest.config.cjs

File renamed without changes.

package.json

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"name": "css-variable",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "define CSS custom properties (variables) in JS",
55
"main": "./dist/index.js",
6+
"module": "./dist/index.mjs",
7+
"type": "module",
68
"exports": {
79
".": {
810
"types": "./dist/index.d.ts",
9-
"require": "./dist/index.js",
10-
"import": "./dist/index.mjs"
11+
"import": "./dist/index.mjs",
12+
"require": "./dist/index.js"
1113
},
12-
"./babel": "./babel/index.js",
14+
"./babel": "./babel/index.cjs",
1315
"./swc": "./swc/target/wasm32-wasi/release/swc_plugin_css_variable.wasm"
1416
},
1517
"types": "./dist/index.d.ts",
@@ -24,8 +26,8 @@
2426
"scripts": {
2527
"prepublishOnly": "npm run build && npm run build:swc",
2628
"build": "npm run build:types && npm run build:commonjs && npm run build:module && npm run build:modulemin",
27-
"build:commonjs": "babel --config-file=./babel.commonjs.js -o dist/index.js src/index.ts",
28-
"build:module": "babel --config-file=./babel.config.js -o dist/index.mjs src/index.ts",
29+
"build:commonjs": "babel --config-file=./babel.commonjs.cjs -o dist/index.js src/index.ts",
30+
"build:module": "babel --config-file=./babel.config.cjs -o dist/index.mjs src/index.ts",
2931
"build:types": "tsc --skipLibCheck --emitDeclarationOnly --declaration --target ESNext --outDir dist src/index.ts",
3032
"build:modulemin": "terser ./dist/index.mjs -o ./dist/index.min.mjs -m --ecma 2017 --module --toplevel -b -c",
3133
"build:swc": "cargo build --manifest-path ./swc/Cargo.toml --release --target=wasm32-wasi",
@@ -58,11 +60,11 @@
5860
"homepage": "https://css-variable.js.org/",
5961
"devDependencies": {
6062
"@babel/cli": "7.19.3",
61-
"@babel/core": "7.20.2",
63+
"@babel/core": "7.22.0",
6264
"@babel/plugin-transform-modules-commonjs": "^7.19.6",
6365
"@babel/preset-typescript": "^7.18.6",
6466
"@babel/runtime": "^7.20.1",
65-
"@babel/types": "^7.20.2",
67+
"@babel/types": "^7.22.0",
6668
"@swc/core": "1.4.17",
6769
"@swc/jest": "^0.2.36",
6870
"@types/jest": "^29.2.3",

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@
9797
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
9898
"skipLibCheck": true /* Skip type checking all .d.ts files. */
9999
},
100-
}
100+
}

0 commit comments

Comments
 (0)