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

feat: add disablePlugins flag for render() options #3701

Merged
merged 7 commits into from May 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/less/bin/lessc
@@ -1,4 +1,7 @@
#!/usr/bin/env node

/* eslint indent: [2, 2, {"SwitchCase": 1}] */

"use strict";

var path = require('path');
Expand Down Expand Up @@ -87,6 +90,12 @@ function render() {
output = path.resolve(process.cwd(), output);
}

if (options.disablePluginRule && queuePlugins.length > 0) {
console.error('--plugin and --disable-plugin-rule may not be used at the same time');
process.exitCode = 1;
return;
}

if (options.sourceMap) {
sourceMapOptions.sourceMapInputFilename = input;

Expand All @@ -113,6 +122,7 @@ function render() {
var mapDir = path.dirname(mapFilename);
var outputDir = path.dirname(output); // find the path from the map to the output file

// eslint-disable-next-line max-len
sourceMapOptions.sourceMapOutputFilename = path.join(path.relative(mapDir, outputDir), path.basename(output)); // make the sourcemap filename point to the sourcemap relative to the css file output directory

sourceMapOptions.sourceMapFilename = path.join(path.relative(outputDir, mapDir), path.basename(sourceMapOptions.sourceMapFullFilename));
Expand Down Expand Up @@ -180,8 +190,8 @@ function render() {
var filename = sourceMapOptions.sourceMapFullFilename;
ensureDirectory(filename);

//To fix https://github.com/less/less.js/issues/3646
output=output.toString();
// To fix https://github.com/less/less.js/issues/3646
output = output.toString();

fs.writeFile(filename, output, 'utf8', function (err) {
if (err) {
Expand Down Expand Up @@ -444,7 +454,8 @@ function processPluginQueue() {
break;

case 'no-js':
console.error('The "--no-js" argument is deprecated, as inline JavaScript ' + 'is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
// eslint-disable-next-line max-len
console.error('The "--no-js" argument is deprecated, as inline JavaScript is disabled by default. Use "--js" to enable inline JavaScript (not recommended).');
break;

case 'include-path':
Expand Down Expand Up @@ -629,6 +640,10 @@ function processPluginQueue() {
});
break;

case 'disable-plugin-rule':
options.disablePluginRule = true;
break;

default:
queuePlugins.push({
name: arg,
Expand Down
2 changes: 1 addition & 1 deletion packages/less/src/less-browser/utils.js
Expand Up @@ -9,7 +9,7 @@ export function extractId(href) {
}

export function addDataAttr(options, tag) {
if(!tag) return; // in case of tag is null or undefined
if (!tag) {return;} // in case of tag is null or undefined
for (const opt in tag.dataset) {
if (tag.dataset.hasOwnProperty(opt)) {
if (opt === 'env' || opt === 'dumpLineNumbers' || opt === 'rootpath' || opt === 'errorReporting') {
Expand Down
1 change: 1 addition & 0 deletions packages/less/src/less-node/lessc-helper.js
Expand Up @@ -67,6 +67,7 @@ const lessc_helper = {
console.log(' --plugin=less-plugin-clean-css or just --clean-css');
console.log(' specify options afterwards e.g. --plugin=less-plugin-clean-css="advanced"');
console.log(' or --clean-css="advanced"');
console.log(' --disable-plugin-rule Disallow @plugin statements');
console.log('');
console.log('-------------------------- Deprecated ----------------');
console.log(' -sm=on|off Legacy parens-only math. Use --math');
Expand Down
16 changes: 13 additions & 3 deletions packages/less/src/less/parser/parser.js
Expand Up @@ -149,12 +149,22 @@ const Parser = function Parser(context, imports, fileInfo) {
//
parse: function (str, callback, additionalData) {
let root;
let error = null;
let err = null;
Copy link
Contributor Author

@broofa broofa Mar 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Had to rename this, as it was shadowing the error function.

let globalVars;
let modifyVars;
let ignored;
let preText = '';

// Optionally disable @plugin parsing
if (additionalData && additionalData.disablePluginRule) {
parsers.plugin = function() {
var dir = parserInput.$re(/^@plugin?\s+/);
if (dir) {
error('@plugin statements are not allowed when disablePluginRule is set to true');
}
}
};

globalVars = (additionalData && additionalData.globalVars) ? `${Parser.serializeVars(additionalData.globalVars)}\n` : '';
modifyVars = (additionalData && additionalData.modifyVars) ? `\n${Parser.serializeVars(additionalData.modifyVars)}` : '';

Expand Down Expand Up @@ -226,7 +236,7 @@ const Parser = function Parser(context, imports, fileInfo) {
}
}

error = new LessError({
err = new LessError({
type: 'Parse',
message,
index: endInfo.furthest,
Expand All @@ -235,7 +245,7 @@ const Parser = function Parser(context, imports, fileInfo) {
}

const finish = e => {
e = error || e || imports.error;
e = err || e || imports.error;

if (e) {
if (!(e instanceof LessError)) {
Expand Down
1 change: 1 addition & 0 deletions packages/less/test/index.js
Expand Up @@ -88,6 +88,7 @@ lessTester.testSyncronous({syncImport: true}, '_main/import');
lessTester.testSyncronous({syncImport: true}, '_main/plugin');
lessTester.testSyncronous({syncImport: true}, 'math/strict/css');
lessTester.testNoOptions();
lessTester.testDisablePluginRule();
lessTester.testJSImport();
lessTester.finished();

Expand Down
18 changes: 18 additions & 0 deletions packages/less/test/less-test.js
Expand Up @@ -563,6 +563,23 @@ module.exports = function() {
};
}

function testDisablePluginRule() {
less.render(
'@plugin "../../plugin/some_plugin";',
{disablePluginRule: true},
function(err) {
// TODO: Need a better way of identifing exactly which error is thrown. Checking
// text like this tends to be rather brittle.
const EXPECTED = '@plugin statements are not allowed when disablePluginRule is set to true';
if (!err || String(err).indexOf(EXPECTED) < 0) {
fail('ERROR: Expected "' + EXPECTED + '" error');
return;
}
ok(stylize('OK\n', 'green'));
}
);
}

return {
runTestSet: runTestSet,
runTestSetNormalOnly: runTestSetNormalOnly,
Expand All @@ -575,6 +592,7 @@ module.exports = function() {
testImportRedirect: testImportRedirect,
testEmptySourcemap: testEmptySourcemap,
testNoOptions: testNoOptions,
testDisablePluginRule: testDisablePluginRule,
testJSImport: testJSImport,
finished: finished
};
Expand Down