|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const del = require("del"); |
| 4 | +const {readFile, writeFile} = require("./files"); |
| 5 | + |
| 6 | +let maybeCompatible = true; |
| 7 | + |
| 8 | +// Location of the module |
| 9 | +const fileLoc = "node_modules/stylelint/"; |
| 10 | + |
| 11 | +// Files to delete; to ensure browserify doesn't bundle them |
| 12 | +const remove = [ |
| 13 | + "bin", |
| 14 | + "docs", |
| 15 | + "*.md", |
| 16 | + "lib/**/__tests__", |
| 17 | + "lib/rules/**/__tests__", |
| 18 | + "lib/testUtils", |
| 19 | + "lib/utils/isAutoprefixable.js", |
| 20 | + "lib/rules/at-rule-no-vendor-prefix", |
| 21 | + "lib/rules/media-feature-name-no-vendor-prefix", |
| 22 | + "lib/rules/property-no-vendor-prefix", |
| 23 | + "lib/rules/selector-no-vendor-prefix", |
| 24 | + "lib/rules/value-no-vendor-prefix", |
| 25 | + "lib/formatters/needlessDisablesStringFormatter.js", |
| 26 | + "lib/formatters/stringFormatter.js", |
| 27 | + "lib/formatters/verboseFormatter.js" |
| 28 | +]; |
| 29 | + |
| 30 | +// Specific file modifications |
| 31 | +// Remove use of "fs", "path" and |
| 32 | +// "autoprefixer" - which includes prefixes downloaded from caniuse |
| 33 | +const modify = { |
| 34 | + "lib/formatters/index.js": file => { |
| 35 | + return replaceBlocks(file, [ |
| 36 | + [ |
| 37 | + // Replace https://github.com/stylelint/stylelint/blob/master/lib/formatters/index.js#L5-L6 |
| 38 | + // with empty functions |
| 39 | + /(string: require\(\"\.\/stringFormatter"\),\s*verbose: require\("\.\/verboseFormatter"\))/, |
| 40 | + " string: () => {},\n verbose: () => {}" |
| 41 | + ] |
| 42 | + ]); |
| 43 | + }, |
| 44 | + "lib/index.js": file => { |
| 45 | + return commentOut(file, [ |
| 46 | + "const createRuleTester =", |
| 47 | + "api.createRuleTester =" |
| 48 | + ]); |
| 49 | + }, |
| 50 | + "lib/rules/index.js": file => { |
| 51 | + return commentOut(file, [ |
| 52 | + "const atRuleNoVendorPrefix =", |
| 53 | + "const mediaFeatureNameNoVendorPrefix =", |
| 54 | + "const propertyNoVendorPrefix =", |
| 55 | + "const selectorNoVendorPrefix =", |
| 56 | + "const valueNoVendorPrefix =", |
| 57 | + "\"at-rule-no-vendor-prefix\"", |
| 58 | + "\"media-feature-name-no-vendor-prefix\"", |
| 59 | + "\"property-no-vendor-prefix\"", |
| 60 | + "\"selector-no-vendor-prefix\"", |
| 61 | + "\"value-no-vendor-prefix\"" |
| 62 | + ]); |
| 63 | + }, |
| 64 | + "lib/standalone.js": file => { |
| 65 | + file = commentOut(file, [ |
| 66 | + "const fs = require(\"fs\");", |
| 67 | + "const path = require(\"path\");", |
| 68 | + "ignoreText = fs.", |
| 69 | + "if (readError.code !==" |
| 70 | + ]); |
| 71 | + return replaceBlocks( |
| 72 | + file, |
| 73 | + [ |
| 74 | + [ |
| 75 | + // Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L60-L62 |
| 76 | + // without using path |
| 77 | + /(const absoluteIgnoreFilePath = path\.isAbsolute\(ignoreFilePath\)\s*\? ignoreFilePath\s*: path\.resolve\(process.cwd\(\), ignoreFilePath\);)/, |
| 78 | + " const absoluteIgnoreFilePath = ignoreFilePath;" |
| 79 | + ], [ |
| 80 | + // Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L110-L113 |
| 81 | + // without using path |
| 82 | + /(const absoluteCodeFilename =\s*codeFilename \!== undefined && \!path\.isAbsolute\(codeFilename\)\s*\? path\.join\(process\.cwd\(\), codeFilename\)\s*: codeFilename;)/, |
| 83 | + " const absoluteCodeFilename = codeFilename;" |
| 84 | + ], [ |
| 85 | + // Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L127-L133 |
| 86 | + // immediately resolve |
| 87 | + /(fs\.stat\(absoluteCodeFilename, err => {\s*if \(err\) {\s*reject\(\);\s*} else {\s*resolve\(\);\s*}\s*}\);)/, |
| 88 | + " resolve();" |
| 89 | + ], [ |
| 90 | + // Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L178-L253 |
| 91 | + // return empty string |
| 92 | + /(if \(useCache\) \{\s*const stylelintVersion =[\s\S]+function prepareReturnValue)/, |
| 93 | + " return \"\";\n\n function prepareReturnValue" |
| 94 | + ] |
| 95 | + ] |
| 96 | + ); |
| 97 | + }, |
| 98 | + "package.json": file => { |
| 99 | + return file |
| 100 | + .replace(/"autoprefixer": ".+",/, "") |
| 101 | + .replace(/"chalk": ".+",/, ""); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +function commentOut(file, lines) { |
| 106 | + lines.forEach(line => { |
| 107 | + const index = file.indexOf(line); |
| 108 | + if (index > -1) { |
| 109 | + file = file |
| 110 | + .replace(line, `// ${line}`) |
| 111 | + // Don't let these accumulate |
| 112 | + .replace("// //", "//"); |
| 113 | + } else { |
| 114 | + maybeCompatible = false; |
| 115 | + console.log(`*** Error: Could not comment out "${line}"`); |
| 116 | + } |
| 117 | + }); |
| 118 | + return file; |
| 119 | +} |
| 120 | + |
| 121 | +// Non-destructive replacement. The block is commented out by adding an |
| 122 | +// opening /* and closing */. It does not account for internal block comments! |
| 123 | +function replaceBlocks(file, blocks) { |
| 124 | + blocks.forEach(block => { |
| 125 | + const index = file.search(block[0]); |
| 126 | + if (index > -1) { |
| 127 | + // Don't comment out blocks that have already been processed |
| 128 | + if (file.substring(index - 3, index) !== "/* ") { |
| 129 | + file = file.replace(block[0], "/* $1 */\n" + block[1]); |
| 130 | + } |
| 131 | + } else { |
| 132 | + maybeCompatible = false; |
| 133 | + console.log(`*** Error: RegExp ${block[0].toString()} did not match anything`); |
| 134 | + } |
| 135 | + }); |
| 136 | + return file; |
| 137 | +} |
| 138 | + |
| 139 | +Promise.all(remove.map(file => del(fileLoc + file))) |
| 140 | + .then( |
| 141 | + Promise.all( |
| 142 | + Object.keys(modify).map(file => { |
| 143 | + readFile(fileLoc + file) |
| 144 | + .then(data => modify[file](data)) |
| 145 | + .then(data => writeFile(fileLoc + file, data)); |
| 146 | + }) |
| 147 | + ) |
| 148 | + ) |
0 commit comments