Skip to content

Commit 1bdf7c6

Browse files
committed
v2.6.2 fix issue of build id
1 parent 6d77e62 commit 1bdf7c6

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

lib/utils.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,30 @@ const getRootDir = (build) => {
107107
};
108108

109109
/**
110-
* getPackageVersion
110+
* getPackageInfo
111111
* @param {import('..').Build} build
112-
* @returns {string}
112+
* @returns {{name: string; version: string;}}
113113
*/
114-
const getPackageVersion = (build) => {
114+
const getPackageInfo = (build) => {
115115
const rootDir = getRootDir(build);
116116
const packageJsonFile = path.resolve(rootDir, './package.json');
117117
try {
118118
fs.accessSync(packageJsonFile, fs.constants.R_OK);
119-
return require(packageJsonFile).version ?? '';
119+
return require(packageJsonFile);
120120
} catch (error) {
121-
return '';
121+
return { name: '', version: '' };
122122
}
123123
};
124124

125+
/**
126+
* getPackageVersion
127+
* @param {import('..').Build} build
128+
* @returns {string}
129+
*/
130+
const getPackageVersion = (build) => {
131+
return getPackageInfo(build).version;
132+
};
133+
125134
/**
126135
* getRelativePath
127136
* @description get relative path from build root
@@ -146,7 +155,7 @@ const getRelativePath = (build, to) => {
146155
const getBuildId = async (build) => {
147156
const { entryPoints } = build.initialOptions;
148157
const buildRoot = getRootDir(build);
149-
const packageVersion = getPackageVersion(build);
158+
const { version: packageVersion, name: packageName } = getPackageInfo(build);
150159
let entries = [];
151160
if (Array.isArray(entryPoints)) {
152161
entries = [...entryPoints];
@@ -157,14 +166,16 @@ const getBuildId = async (build) => {
157166
entries.push(entryPoints[k]);
158167
});
159168
}
160-
const entryContents = packageVersion + (
161-
await Promise.all(
162-
entries.map(async (p) => {
163-
const absPath = path.isAbsolute(p) ? p : path.resolve(buildRoot, p);
164-
return (await readFile(absPath, { encoding: 'utf8' })).trim();
165-
})
166-
)
167-
).join('');
169+
const entryContents =
170+
`// ${packageName}@${packageVersion}\n` +
171+
(
172+
await Promise.all(
173+
entries.map(async (p) => {
174+
const absPath = path.isAbsolute(p) ? p : path.resolve(buildRoot, p);
175+
return (await readFile(absPath, { encoding: 'utf8' })).trim();
176+
})
177+
)
178+
).join('\n');
168179
return createHash('sha256').update(entryContents).digest('hex');
169180
};
170181

@@ -238,5 +249,6 @@ module.exports = {
238249
getRelativePath,
239250
getBuildId,
240251
validateNamedExport,
252+
getPackageInfo,
241253
getPackageVersion
242254
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esbuild-css-modules-plugin",
3-
"version": "2.6.1-beta.1",
3+
"version": "2.6.2",
44
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x).",
55
"main": "./index.js",
66
"types": "./index.d.ts",

test/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "esbuild-css-modules-plugin-test",
3+
"version": "0.0.0",
4+
"private": true
5+
}

test/styles/app.modules.css.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {"helloWorld":"app-modules__hello-world_kRpaBq000","someOtherSelector":"app-modules__some-other-selector_kRpaBq000"} as const;
2+
export const helloWorld = "app-modules__hello-world_kRpaBq000" as const;
3+
export const someOtherSelector = "app-modules__some-other-selector_kRpaBq000" as const;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default {"helloText":"hello-modules__hello-text_NtWlFa000"} as const;
2+
export const helloText = "hello-modules__hello-text_NtWlFa000" as const;

test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ fse.emptyDirSync('./dist');
9292
},
9393
plugins: [cssModulesPlugin({
9494
v2: true,
95-
inject: '#my-custom-element-with-shadow-dom'
95+
inject: '#my-custom-element-with-shadow-dom',
96+
generateTsFile: true
9697
})],
9798
logLevel: 'debug'
9899
});

0 commit comments

Comments
 (0)