diff --git a/lib/utils.js b/lib/utils.js index 37a8d11..fa5ce9f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 @@ -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]; @@ -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'); }; @@ -238,5 +249,6 @@ module.exports = { getRelativePath, getBuildId, validateNamedExport, + getPackageInfo, getPackageVersion }; diff --git a/package.json b/package.json index fa6b345..cf67bfd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..a7be705 --- /dev/null +++ b/test/package.json @@ -0,0 +1,5 @@ +{ + "name": "esbuild-css-modules-plugin-test", + "version": "0.0.0", + "private": true +} \ No newline at end of file diff --git a/test/styles/app.modules.css.ts b/test/styles/app.modules.css.ts new file mode 100644 index 0000000..f1c4694 --- /dev/null +++ b/test/styles/app.modules.css.ts @@ -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; diff --git a/test/styles/deep/styles/hello.modules.css.ts b/test/styles/deep/styles/hello.modules.css.ts new file mode 100644 index 0000000..553b12b --- /dev/null +++ b/test/styles/deep/styles/hello.modules.css.ts @@ -0,0 +1,2 @@ +export default {"helloText":"hello-modules__hello-text_NtWlFa000"} as const; +export const helloText = "hello-modules__hello-text_NtWlFa000" as const; diff --git a/test/test.js b/test/test.js index 1dbc040..f6dc537 100644 --- a/test/test.js +++ b/test/test.js @@ -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' });