Skip to content
Open
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
20 changes: 14 additions & 6 deletions commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,25 @@ function cli (args) {
if (opts.help) displayHelp();

process.chdir(app.context);
import('chalk').then(({default: _chalk}) => {
chalk = _chalk;
import('strip-ansi').then(({default: _stripAnsi}) => {
import('chalk')
.then(({default: _chalk}) => {
chalk = _chalk;
return import('strip-ansi');
})
.then(({default: _stripAnsi}) => {
stripAnsi = _stripAnsi;
api(opts).catch(err => {
return api(opts);
})
.catch(err => {
if (chalk) {
printErrorDetails(err, () => {
process.exit(1);
});
});
} else {
console.error(err);
process.exit(1);
}
});
});
}

module.exports = {api, cli};
Expand Down
34 changes: 29 additions & 5 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const resolve = require('resolve');
const TerserPlugin = require('terser-webpack-plugin');
const {DefinePlugin, EnvironmentPlugin} = require('webpack');
const {
Expand All @@ -39,6 +38,27 @@ const {
} = require('@enact/dev-utils');
const createEnvironmentHash = require('./createEnvironmentHash');

// fork-ts-checker needs the classic TypeScript compiler API. TypeScript 7's
// package entry only exports a version, so resolution via `main` fails and the
// checker cannot run. Return null to skip it instead of failing the build.
function resolveLegacyTypeScript (context) {
let typescriptPath;
try {
typescriptPath = require.resolve('typescript', {paths: [context]});
} catch (e) {
return null;
}

try {
const typescript = require(typescriptPath);
if (typeof typescript.createProgram === 'function') return typescriptPath;
} catch (e) {
return null;
}

return null;
}

// This is the production and development configuration.
// It is focused on developer experience, fast rebuilds, and a minimal bundle.
module.exports = function (
Expand All @@ -60,6 +80,12 @@ module.exports = function (

// Check if TypeScript is setup
const useTypeScript = fs.existsSync('tsconfig.json');
const typeScriptPath = useTypeScript ? resolveLegacyTypeScript(app.context) : null;
if (useTypeScript && !typeScriptPath) {
console.warn(
'Skipping TypeScript type checking during the build. The installed "typescript" package does not provide the compiler API required by fork-ts-checker.'
);
}

// Check if Tailwind config exists
const useTailwind = fs.existsSync(path.join(app.context, 'tailwind.config.js'));
Expand Down Expand Up @@ -624,13 +650,11 @@ module.exports = function (
// and parses any to copy over any webOS meta assets at build time.
new WebOSMetaPlugin({htmlPlugin: HtmlWebpackPlugin}),
// TypeScript type checking
useTypeScript &&
typeScriptPath &&
new ForkTsCheckerWebpackPlugin({
async: !isEnvProduction,
typescript: {
typescriptPath: resolve.sync('typescript', {
basedir: 'node_modules'
}),
typescriptPath: typeScriptPath,
configOverwrite: {
compilerOptions: {
sourceMap: shouldUseSourceMap,
Expand Down
Loading