Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function createStylesheetBundleOptions(
preserveSymlinks: options.preserveSymlinks,
external: options.externalDependencies,
publicPath: options.publicPath,
conditions: ['style', 'sass'],
conditions: ['style', 'sass', 'less'],
mainFields: ['style', 'sass'],
plugins: [
pluginFactory.create(SassStylesheetLanguage),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import type { OnLoadResult, PluginBuild } from 'esbuild';
import type { Location, OnLoadResult, PluginBuild } from 'esbuild';
import { readFile } from 'node:fs/promises';
import { StylesheetLanguage, StylesheetPluginOptions } from './stylesheet-plugin-factory';

Expand All @@ -32,7 +32,13 @@ export const LessStylesheetLanguage = Object.freeze<StylesheetLanguage>({
componentFilter: /^less;/,
fileFilter: /\.less$/,
process(data, file, _, options, build) {
return compileString(data, file, options, build.resolve.bind(build));
return compileString(
data,
file,
options,
build.resolve.bind(build),
/* unsafeInlineJavaScript */ false,
);
},
});

Expand All @@ -41,6 +47,7 @@ async function compileString(
filename: string,
options: StylesheetPluginOptions,
resolver: PluginBuild['resolve'],
unsafeInlineJavaScript: boolean,
): Promise<OnLoadResult> {
const less = (lessPreprocessor ??= (await import('less')).default);

Expand Down Expand Up @@ -92,6 +99,7 @@ async function compileString(
paths: options.includePaths,
plugins: [resolverPlugin],
rewriteUrls: 'all',
javascriptEnabled: unsafeInlineJavaScript,
sourceMap: options.sourcemap
? {
sourceMapFileInline: true,
Expand All @@ -107,17 +115,40 @@ async function compileString(
};
} catch (error) {
if (isLessException(error)) {
// Retry with a warning for less files requiring the deprecated inline JavaScript option
if (error.message.includes('Inline JavaScript is not enabled.')) {
const withJsResult = await compileString(
data,
filename,
options,
resolver,
/* unsafeInlineJavaScript */ true,
);
withJsResult.warnings = [
{
text: 'Deprecated inline execution of JavaScript has been enabled ("javascriptEnabled")',
location: convertExceptionLocation(error),
notes: [
{
location: null,
text: 'JavaScript found within less stylesheets may be executed at build time. [https://lesscss.org/usage/#less-options]',
},
{
location: null,
text: 'Support for "javascriptEnabled" may be removed from the Angular CLI starting with Angular v19.',
},
],
},
];

return withJsResult;
}

return {
errors: [
{
text: error.message,
location: {
file: error.filename,
line: error.line,
column: error.column,
// Middle element represents the line containing the error
lineText: error.extract && error.extract[Math.trunc(error.extract.length / 2)],
},
location: convertExceptionLocation(error),
},
],
loader: 'css',
Expand All @@ -127,3 +158,13 @@ async function compileString(
throw error;
}
}

function convertExceptionLocation(exception: LessException): Partial<Location> {
return {
file: exception.filename,
line: exception.line,
column: exception.column,
// Middle element represents the line containing the exception
lineText: exception.extract && exception.extract[Math.trunc(exception.extract.length / 2)],
};
}