Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct build outputs #3211

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"module": "dist/redux-toolkit.modern.js",
"main": "dist/cjs/index.js",
"module": "dist/redux-toolkit.modern.js",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/redux-toolkit.modern.js",
"import": "./dist/redux-toolkit.modern.mjs",
"default": "./dist/cjs/index.js"
},
"./query": {
"types": "./dist/query/index.d.ts",
"import": "./dist/query/rtk-query.modern.js",
"import": "./dist/query/rtk-query.modern.mjs",
"default": "./dist/query/cjs/index.js"
},
"./query/react": {
"types": "./dist/query/react/index.d.ts",
"import": "./dist/query/react/rtk-query-react.modern.js",
"import": "./dist/query/react/rtk-query-react.modern.mjs",
"default": "./dist/query/react/cjs/index.js"
}
},
Expand Down Expand Up @@ -110,6 +109,7 @@
},
"files": [
"dist/**/*.js",
"dist/**/*.mjs",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/package.json",
Expand Down
16 changes: 15 additions & 1 deletion packages/toolkit/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
}

const outputFolder = path.join(...folderSegments)
const outputFilename = `${prefix}.${name}.js`
const outputFilename = `${prefix}.${name}.${format === 'cjs' ? 'js' : 'mjs'}`

await fs.ensureDir(outputFolder)

Expand Down Expand Up @@ -346,6 +346,20 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) {
// await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
}

// Copy .modern.mjs -> .modern.js to support the top-level `"module"` field
for (let entryPoint of entryPoints) {
const { folder } = entryPoint
const outputPath = path.join('dist', folder)
const outputFiles = await fs.readdir(outputPath)
const copyPromises = outputFiles
.filter((file: string) => /\.mjs$/.test(file))
.map((file: string) => {
const fileNameNoExtension = path.parse(file).name
return fs.copy(`${outputPath}/${file}`, `${outputPath}/${fileNameNoExtension + '.js'}`)
})
await Promise.all(copyPromises)
}

if (!skipExtraction) {
for (let entryPoint of entryPoints) {
try {
Expand Down