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: convert jsx, ts, and tsx files to js on build #517

Merged
merged 3 commits into from
Feb 25, 2021
Merged
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
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