diff --git a/cli/src/lib/compiler/compile.js b/cli/src/lib/compiler/compile.js index d166cb8d3..45006d061 100644 --- a/cli/src/lib/compiler/compile.js +++ b/cli/src/lib/compiler/compile.js @@ -4,6 +4,10 @@ const { reporter, chalk } = require('@dhis2/cli-helpers-engine') const chokidar = require('chokidar') const fs = require('fs-extra') const makeBabelConfig = require('../../../config/makeBabelConfig.js') +const { + extensionPattern, + normalizeExtension, +} = require('./extensionHelpers.js') const overwriteEntrypoint = async ({ config, paths }) => { const isApp = config.type === 'app' @@ -26,24 +30,29 @@ const overwriteEntrypoint = async ({ config, paths }) => { throw new Error(msg) } + const outRelativeEntrypoint = normalizeExtension(relativeEntrypoint) + if (isApp) { const shellAppSource = await fs.readFile(paths.shellSourceEntrypoint) await fs.writeFile( paths.shellAppEntrypoint, shellAppSource .toString() - .replace(/'.\/D2App\/app'/g, `'./D2App/${relativeEntrypoint}'`) + .replace( + /'.\/D2App\/app'/g, + `'./D2App/${outRelativeEntrypoint}'` + ) ) } } const watchFiles = ({ inputDir, outputDir, processFileCallback, watch }) => { const compileFile = async source => { - const relative = path.relative(inputDir, source) + const relative = normalizeExtension(path.relative(inputDir, source)) const destination = path.join(outputDir, relative) reporter.debug( `File ${relative} changed or added... dest: `, - path.relative(inputDir, relative) + path.relative(inputDir, destination) ) await fs.ensureDir(path.dirname(destination)) await processFileCallback(source, destination) @@ -111,7 +120,7 @@ const compile = async ({ await fs.copy(source, destination) } const compileFile = async (source, destination) => { - if (source.match(/\.[jt]sx?$/)) { + if (source.match(extensionPattern)) { const result = await babel.transformFileAsync(source, babelConfig) await fs.writeFile(destination, result.code) } else { diff --git a/cli/src/lib/compiler/extensionHelpers.js b/cli/src/lib/compiler/extensionHelpers.js new file mode 100644 index 000000000..7577c755e --- /dev/null +++ b/cli/src/lib/compiler/extensionHelpers.js @@ -0,0 +1,5 @@ +const extensionPattern = /\.[jt]sx?$/ +const normalizeExtension = ext => ext.replace(extensionPattern, '.js') + +module.exports.extensionPattern = extensionPattern +module.exports.normalizeExtension = normalizeExtension diff --git a/cli/src/lib/validators/validatePackageExports.js b/cli/src/lib/validators/validatePackageExports.js index 76663304b..4d32009cf 100644 --- a/cli/src/lib/validators/validatePackageExports.js +++ b/cli/src/lib/validators/validatePackageExports.js @@ -1,6 +1,7 @@ const path = require('path') const { reporter, prompt } = require('@dhis2/cli-helpers-engine') const { writeJSON } = require('fs-extra') +const { normalizeExtension } = require('../compiler/extensionHelpers.js') /* * Ensure that package.main, package.module, and package.exports are valid @@ -30,7 +31,9 @@ module.exports.validatePackageExports = async ( const baseDir = path.dirname(paths.package) let valid = true - const entrypointBasename = path.basename(config.entryPoints.lib) + const entrypointBasename = normalizeExtension( + path.basename(config.entryPoints.lib) + ) const expectedESMExport = './' + diff --git a/examples/simple-app/d2.config.js b/examples/simple-app/d2.config.js index 39c435b55..5ed877ea5 100644 --- a/examples/simple-app/d2.config.js +++ b/examples/simple-app/d2.config.js @@ -7,7 +7,7 @@ const config = { // standalone: true, // Don't bake-in a DHIS2 base URL, allow the user to choose entryPoints: { - app: './src/App', + app: './src/App.js', }, }