From 66cdf96ccd0ab7aae87bdebae69ec7f4ece49242 Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Wed, 13 Oct 2021 12:15:59 -0400 Subject: [PATCH] fix: `require is not defined in ES module scope` - https://github.com/vercel/ncc/issues/791\#issuecomment-942454663 --- tools/cli/build.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tools/cli/build.ts b/tools/cli/build.ts index ab1942b..53ec788 100755 --- a/tools/cli/build.ts +++ b/tools/cli/build.ts @@ -225,7 +225,7 @@ async function build(): Promise { clean: { outDir: argv.clean || undefined } } - // Build project + // Build project (type checking not available) !dryRun && tsc.build(options) !dryRun && format === 'cjs' && useDualExports(`./${format}/**`) dryRun && logger(argv, `build ${format}`) @@ -242,23 +242,19 @@ async function build(): Promise { to: `${format === 'cjs' ? 'c' : 'm'}js` } - // Convert TypeScript output to .cjs or .mjs - !dryRun && (await trext<'js' | 'cjs' | 'mjs'>(`${format}/`, trext_opts)) - logger(argv, `use .${trext_opts.to} extensions`) - - // Create bundles + // Create bundles (type checking enabled) // See: https://github.com/vercel/ncc const BUNDLES = [$WNS, `${$WNS}.min`].map(async name => { const bundle = `${format}/${name}.${trext_opts.to}` - const filename = `${format}/index.${trext_opts.to}` - const minify = path.extname(name) === '.min' + // ! Bundle source code to enable type checking + const filename = 'src/index.ts' if (!dryRun) { const { code } = await ncc(`${CWD}/${filename}`, { esm: format === 'esm', externals: Object.keys($PACKAGE?.peerDependencies ?? {}), filename, - minify: minify, + minify: path.extname(name) === '.min', production: env === 'production', quiet: true, target: options.compilerOptions?.target @@ -267,9 +263,9 @@ async function build(): Promise { await fs.writeFile(bundle, code, { flag: 'wx+' }) await fs.copyFile(`${format}/index.d.ts`, `${format}/${name}.d.ts`) await replace.replaceInFile({ - files: bundle, - from: ';// CONCATENATED MODULE: ./src/', - to: ';// CONCATENATED MODULE: ../src/' + files: format, + from: '// CONCATENATED MODULE: .src/', + to: '// CONCATENATED MODULE: ../src/' }) } @@ -278,6 +274,10 @@ async function build(): Promise { // Complete bundle promises logger(argv, `bundle ${format}`, await Promise.all(BUNDLES)) + + // Convert TypeScript output to .cjs or .mjs + !dryRun && (await trext<'js' | 'cjs' | 'mjs'>(`${format}/`, trext_opts)) + logger(argv, `use .${trext_opts.to} extensions`) } }