Skip to content

Commit

Permalink
fix: convert jsx, ts, and tsx files to js on build (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
amcgee authored Feb 25, 2021
1 parent bac4830 commit 5af8a58
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
17 changes: 13 additions & 4 deletions cli/src/lib/compiler/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions cli/src/lib/compiler/extensionHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const extensionPattern = /\.[jt]sx?$/
const normalizeExtension = ext => ext.replace(extensionPattern, '.js')

module.exports.extensionPattern = extensionPattern
module.exports.normalizeExtension = normalizeExtension
5 changes: 4 additions & 1 deletion cli/src/lib/validators/validatePackageExports.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 =
'./' +
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-app/d2.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
}

Expand Down

0 comments on commit 5af8a58

Please sign in to comment.