Skip to content

Commit

Permalink
v2.6.2 fix issue of build id
Browse files Browse the repository at this point in the history
  • Loading branch information
indooorsman committed Oct 28, 2022
1 parent 6d77e62 commit 1bdf7c6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
40 changes: 26 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,30 @@ const getRootDir = (build) => {
};

/**
* getPackageVersion
* getPackageInfo
* @param {import('..').Build} build
* @returns {string}
* @returns {{name: string; version: string;}}
*/
const getPackageVersion = (build) => {
const getPackageInfo = (build) => {
const rootDir = getRootDir(build);
const packageJsonFile = path.resolve(rootDir, './package.json');
try {
fs.accessSync(packageJsonFile, fs.constants.R_OK);
return require(packageJsonFile).version ?? '';
return require(packageJsonFile);
} catch (error) {
return '';
return { name: '', version: '' };
}
};

/**
* getPackageVersion
* @param {import('..').Build} build
* @returns {string}
*/
const getPackageVersion = (build) => {
return getPackageInfo(build).version;
};

/**
* getRelativePath
* @description get relative path from build root
Expand All @@ -146,7 +155,7 @@ const getRelativePath = (build, to) => {
const getBuildId = async (build) => {
const { entryPoints } = build.initialOptions;
const buildRoot = getRootDir(build);
const packageVersion = getPackageVersion(build);
const { version: packageVersion, name: packageName } = getPackageInfo(build);
let entries = [];
if (Array.isArray(entryPoints)) {
entries = [...entryPoints];
Expand All @@ -157,14 +166,16 @@ const getBuildId = async (build) => {
entries.push(entryPoints[k]);
});
}
const entryContents = packageVersion + (
await Promise.all(
entries.map(async (p) => {
const absPath = path.isAbsolute(p) ? p : path.resolve(buildRoot, p);
return (await readFile(absPath, { encoding: 'utf8' })).trim();
})
)
).join('');
const entryContents =
`// ${packageName}@${packageVersion}\n` +
(
await Promise.all(
entries.map(async (p) => {
const absPath = path.isAbsolute(p) ? p : path.resolve(buildRoot, p);
return (await readFile(absPath, { encoding: 'utf8' })).trim();
})
)
).join('\n');
return createHash('sha256').update(entryContents).digest('hex');
};

Expand Down Expand Up @@ -238,5 +249,6 @@ module.exports = {
getRelativePath,
getBuildId,
validateNamedExport,
getPackageInfo,
getPackageVersion
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esbuild-css-modules-plugin",
"version": "2.6.1-beta.1",
"version": "2.6.2",
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x).",
"main": "./index.js",
"types": "./index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "esbuild-css-modules-plugin-test",
"version": "0.0.0",
"private": true
}
3 changes: 3 additions & 0 deletions test/styles/app.modules.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {"helloWorld":"app-modules__hello-world_kRpaBq000","someOtherSelector":"app-modules__some-other-selector_kRpaBq000"} as const;
export const helloWorld = "app-modules__hello-world_kRpaBq000" as const;
export const someOtherSelector = "app-modules__some-other-selector_kRpaBq000" as const;
2 changes: 2 additions & 0 deletions test/styles/deep/styles/hello.modules.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {"helloText":"hello-modules__hello-text_NtWlFa000"} as const;
export const helloText = "hello-modules__hello-text_NtWlFa000" as const;
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ fse.emptyDirSync('./dist');
},
plugins: [cssModulesPlugin({
v2: true,
inject: '#my-custom-element-with-shadow-dom'
inject: '#my-custom-element-with-shadow-dom',
generateTsFile: true
})],
logLevel: 'debug'
});
Expand Down

0 comments on commit 1bdf7c6

Please sign in to comment.