-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove rollup application compiler, delegate compilation to the…
… shell (#187) * Support named imports * Support ES modules treeshaking * Support static file imports * Support static files in a public directory. * **Significantly** improve compilation performance * Avoid out-of-memory issues when compiling large applications * Automatically update cached shell when `@dhis2/cli-app-scripts` is upgraded BREAKING CHANGE: This may break some applications which use the former named import workaround, but removing that workaround should make treeshaking work!!
- Loading branch information
Showing
39 changed files
with
1,527 additions
and
14,894 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,32 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
node_modules | ||
build | ||
dist | ||
yarn-error.log | ||
.d2 | ||
.pnp | ||
.pnp.js | ||
.vscode | ||
|
||
# testing | ||
**/coverage | ||
|
||
# production | ||
**/build | ||
**/dist | ||
|
||
# DHIS2 | ||
.d2 | ||
**/src/locales | ||
cli/assets | ||
|
||
# Editors | ||
.vscode | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
plugins: [ | ||
require('@babel/plugin-proposal-class-properties'), | ||
|
||
// Always build in "production" mode even when styled-jsx runtime may select "development" | ||
[require('styled-jsx/babel'), { optimizeForSpeed: true }], | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'test-file-stub' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const compileLibrary = require('./compileLibrary') | ||
const compileApp = require('./compileApp') | ||
|
||
const compile = async ({ | ||
config, | ||
paths, | ||
mode = 'development', | ||
watch = false, | ||
} = {}) => { | ||
if (config.type === 'lib') { | ||
return await compileLibrary({ config, paths, mode, watch }) | ||
} else { | ||
return await compileApp({ config, paths, mode, watch }) | ||
} | ||
} | ||
|
||
module.exports = compile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
const { reporter, chalk } = require('@dhis2/cli-helpers-engine') | ||
|
||
const path = require('path') | ||
const fs = require('fs-extra') | ||
const chokidar = require('chokidar') | ||
const babel = require('@babel/core') | ||
|
||
const babelOptions = require('../../../config/app.babel.config') | ||
|
||
const overwriteEntrypoint = async ({ config, paths }) => { | ||
const shellAppSource = await fs.readFile(paths.shellSourceEntrypoint) | ||
|
||
const entrypoint = config.entryPoints.app | ||
if (!entrypoint.match(/^(\.\/)?src\//)) { | ||
const msg = `App entrypoint ${chalk.bold( | ||
entrypoint | ||
)} must be located within the ${chalk.bold('./src')} directory` | ||
reporter.error(msg) | ||
throw new Error(msg) | ||
} | ||
|
||
const relativeEntrypoint = entrypoint.replace(/^(\.\/)?src\//, '') | ||
|
||
try { | ||
require.resolve(path.join(paths.base, entrypoint)) | ||
} catch (e) { | ||
const msg = `Could not resolve app entrypoint ${chalk.bold(entrypoint)}` | ||
reporter.error(msg) | ||
throw new Error(msg) | ||
} | ||
|
||
await fs.writeFile( | ||
paths.shellAppEntrypoint, | ||
shellAppSource | ||
.toString() | ||
.replace(/'.\/D2App\/app'/g, `'./D2App/${relativeEntrypoint}'`) | ||
) | ||
} | ||
|
||
const watchFiles = ({ inputDir, outputDir, processFileCallback, watch }) => { | ||
const compileFile = async source => { | ||
const relative = path.relative(inputDir, source) | ||
const destination = path.join(outputDir, relative) | ||
reporter.debug( | ||
`File ${relative} changed or added... dest: `, | ||
path.relative(inputDir, relative) | ||
) | ||
await fs.ensureDir(path.dirname(destination)) | ||
await processFileCallback(source, destination) | ||
} | ||
|
||
const removeFile = async file => { | ||
const relative = path.relative(inputDir, file) | ||
const outFile = path.join(outputDir, relative) | ||
reporter.debug(`File ${relative} removed... removing: `, outFile) | ||
fs.remove(outFile) | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const watcher = chokidar.watch(inputDir, { persistent: true }) | ||
|
||
watcher | ||
.on('ready', async () => { | ||
if (watch) { | ||
reporter.debug('watching...') | ||
} else { | ||
await watcher.close() | ||
} | ||
resolve() | ||
}) | ||
.on('add', compileFile) | ||
.on('change', compileFile) | ||
.on('unlink', removeFile) | ||
.on('error', error => { | ||
reporter.debugError('Chokidar error:', error) | ||
reject('Chokidar error!') | ||
}) | ||
|
||
process.on('SIGINT', async () => { | ||
reporter.debug('Caught interrupt signal') | ||
await watcher.close() | ||
}) | ||
}) | ||
} | ||
|
||
const compileApp = async ({ config, paths, mode, watch }) => { | ||
await overwriteEntrypoint({ config, paths }) | ||
|
||
fs.removeSync(paths.shellApp) | ||
fs.ensureDirSync(paths.shellApp) | ||
|
||
fs.removeSync(paths.shellPublic) | ||
fs.copySync(paths.shellSourcePublic, paths.shellPublic) | ||
|
||
const copyFile = async (source, destination) => { | ||
await fs.copy(source, destination) | ||
} | ||
const compileFile = async (source, destination) => { | ||
if (path.extname(source) === '.js') { | ||
const result = await babel.transformFileAsync(source, babelOptions) | ||
await fs.writeFile(destination, result.code) | ||
} else { | ||
await copyFile(source, destination) | ||
} | ||
} | ||
|
||
return Promise.all([ | ||
watchFiles({ | ||
inputDir: paths.src, | ||
outputDir: paths.shellApp, | ||
processFileCallback: compileFile, | ||
watch, | ||
}), | ||
watchFiles({ | ||
inputDir: paths.public, | ||
outputDir: paths.shellPublic, | ||
processFileCallback: copyFile, | ||
watch, | ||
}), | ||
]) | ||
} | ||
|
||
module.exports = compileApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
compile: require('./compile'), | ||
} |
Oops, something went wrong.