postcss-loader
Advanced tools
Comparing version 7.0.1 to 7.0.2
@@ -25,7 +25,5 @@ "use strict"; | ||
this.message = `${this.name}\n\n`; | ||
if (typeof line !== "undefined") { | ||
this.message += `(${line}:${column}) `; | ||
} | ||
this.message += plugin ? `${plugin}: ` : ""; | ||
@@ -35,12 +33,8 @@ this.message += file ? `${file} ` : "<css input> "; | ||
const code = error.showSourceCode(); | ||
if (code) { | ||
this.message += `\n\n${code}\n`; | ||
} | ||
this.stack = false; | ||
} | ||
} | ||
module.exports = SyntaxError; |
@@ -7,18 +7,11 @@ "use strict"; | ||
exports.default = loader; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _semver = require("semver"); | ||
var _package = _interopRequireDefault(require("postcss/package.json")); | ||
var _Warning = _interopRequireDefault(require("./Warning")); | ||
var _options = _interopRequireDefault(require("./options.json")); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
let hasExplicitDependencyOnPostCSS = false; | ||
let hasExplicitDependencyOnPostCSS = false; | ||
/** | ||
@@ -37,3 +30,2 @@ * **PostCSS Loader** | ||
*/ | ||
async function loader(content, sourceMap, meta) { | ||
@@ -44,3 +36,2 @@ const options = this.getOptions(_options.default); | ||
const postcssFactory = (0, _utils.getPostcssImplementation)(this, options.implementation); | ||
if (!postcssFactory) { | ||
@@ -50,5 +41,3 @@ callback(new Error(`The Postcss implementation "${options.implementation}" not found`)); | ||
} | ||
let loadedConfig; | ||
if (configOption) { | ||
@@ -62,3 +51,2 @@ try { | ||
} | ||
const useSourceMap = typeof options.sourceMap !== "undefined" ? options.sourceMap : this.sourceMap; | ||
@@ -69,3 +57,2 @@ const { | ||
} = await (0, _utils.getPostcssOptions)(this, loadedConfig, options.postcssOptions); | ||
if (useSourceMap) { | ||
@@ -78,9 +65,8 @@ processOptions.map = { | ||
} | ||
if (sourceMap && processOptions.map) { | ||
processOptions.map.prev = (0, _utils.normalizeSourceMap)(sourceMap, this.context); | ||
} | ||
let root; | ||
let root; // Reuse PostCSS AST from other loaders | ||
// Reuse PostCSS AST from other loaders | ||
if (meta && meta.ast && meta.ast.type === "postcss" && (0, _semver.satisfies)(meta.ast.version, `^${_package.default.version}`)) { | ||
@@ -91,3 +77,2 @@ ({ | ||
} | ||
if (!root && options.execute) { | ||
@@ -97,6 +82,4 @@ // eslint-disable-next-line no-param-reassign | ||
} | ||
let result; | ||
let processor; | ||
try { | ||
@@ -114,19 +97,16 @@ processor = postcssFactory(plugins); | ||
const packageJSONDir = (0, _utils.findPackageJSONDir)(process.cwd(), this.fs.statSync); | ||
if (packageJSONDir) { | ||
let bufferOfPackageJSON; | ||
try { | ||
bufferOfPackageJSON = this.fs.readFileSync(_path.default.resolve(packageJSONDir, "package.json"), "utf8"); | ||
} catch (_error) {// Nothing | ||
} catch (_error) { | ||
// Nothing | ||
} | ||
if (bufferOfPackageJSON) { | ||
let pkg; | ||
try { | ||
pkg = JSON.parse(bufferOfPackageJSON); | ||
} catch (_error) {// Nothing | ||
} catch (_error) { | ||
// Nothing | ||
} | ||
if (pkg) { | ||
@@ -142,11 +122,8 @@ if (!pkg.dependencies.postcss && !pkg.devDependencies.postcss) { | ||
} | ||
(0, _utils.reportError)(this, callback, error); | ||
return; | ||
} | ||
for (const warning of result.warnings()) { | ||
this.emitWarning(new _Warning.default(warning)); | ||
} | ||
for (const message of result.messages) { | ||
@@ -158,19 +135,14 @@ // eslint-disable-next-line default-case | ||
break; | ||
case "build-dependency": | ||
this.addBuildDependency(message.file); | ||
break; | ||
case "missing-dependency": | ||
this.addMissingDependency(message.file); | ||
break; | ||
case "context-dependency": | ||
this.addContextDependency(message.file); | ||
break; | ||
case "dir-dependency": | ||
this.addContextDependency(message.dir); | ||
break; | ||
case "asset": | ||
@@ -180,15 +152,11 @@ if (message.content && message.file) { | ||
} | ||
} | ||
} // eslint-disable-next-line no-undefined | ||
} | ||
// eslint-disable-next-line no-undefined | ||
let map = result.map ? result.map.toJSON() : undefined; | ||
if (map && useSourceMap) { | ||
map = (0, _utils.normalizeSourceMapAfterPostcss)(map, this.context); | ||
} | ||
let ast; | ||
try { | ||
@@ -204,3 +172,2 @@ ast = { | ||
} | ||
callback(null, result.css, map, { | ||
@@ -207,0 +174,0 @@ ast |
@@ -14,17 +14,10 @@ "use strict"; | ||
exports.reportError = reportError; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _url = _interopRequireDefault(require("url")); | ||
var _module = _interopRequireDefault(require("module")); | ||
var _full = require("klona/full"); | ||
var _cosmiconfig = require("cosmiconfig"); | ||
var _Error = _interopRequireDefault(require("./Error")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const parentModule = module; | ||
const stat = (inputFileSystem, filePath) => new Promise((resolve, reject) => { | ||
@@ -35,7 +28,5 @@ inputFileSystem.stat(filePath, (err, stats) => { | ||
} | ||
resolve(stats); | ||
}); | ||
}); | ||
function exec(code, loaderContext) { | ||
@@ -46,16 +37,15 @@ const { | ||
} = loaderContext; | ||
const module = new _module.default(resource, parentModule); // eslint-disable-next-line no-underscore-dangle | ||
const module = new _module.default(resource, parentModule); | ||
// eslint-disable-next-line no-underscore-dangle | ||
module.paths = _module.default._nodeModulePaths(context); | ||
module.filename = resource; // eslint-disable-next-line no-underscore-dangle | ||
module.filename = resource; | ||
// eslint-disable-next-line no-underscore-dangle | ||
module._compile(code, resource); | ||
return module.exports; | ||
} | ||
async function loadConfig(loaderContext, config, postcssOptions) { | ||
const searchPath = typeof config === "string" ? _path.default.resolve(config) : _path.default.dirname(loaderContext.resourcePath); | ||
let stats; | ||
try { | ||
@@ -66,6 +56,47 @@ stats = await stat(loaderContext.fs, searchPath); | ||
} | ||
const explorer = (0, _cosmiconfig.cosmiconfig)("postcss"); | ||
const moduleName = "postcss"; | ||
const explorer = (0, _cosmiconfig.cosmiconfig)(moduleName, { | ||
searchPlaces: ["package.json", `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.mjs`, `.${moduleName}rc.cjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, `.config/${moduleName}rc.yml`, `.config/${moduleName}rc.js`, `.config/${moduleName}rc.mjs`, `.config/${moduleName}rc.cjs`, `${moduleName}.config.js`, `${moduleName}.config.mjs`, `${moduleName}.config.cjs`], | ||
loaders: { | ||
".js": async (...args) => { | ||
let result; | ||
try { | ||
result = _cosmiconfig.defaultLoaders[".js"](...args); | ||
} catch (error) { | ||
let importESM; | ||
try { | ||
// eslint-disable-next-line no-new-func | ||
importESM = new Function("id", "return import(id);"); | ||
} catch (e) { | ||
importESM = null; | ||
} | ||
if (error.code === "ERR_REQUIRE_ESM" && _url.default.pathToFileURL && importESM) { | ||
const urlForConfig = _url.default.pathToFileURL(args[0]); | ||
result = await importESM(urlForConfig); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
return result; | ||
}, | ||
".mjs": async (...args) => { | ||
let result; | ||
let importESM; | ||
try { | ||
// eslint-disable-next-line no-new-func | ||
importESM = new Function("id", "return import(id);"); | ||
} catch (e) { | ||
importESM = null; | ||
} | ||
if (_url.default.pathToFileURL && importESM) { | ||
const urlForConfig = _url.default.pathToFileURL(args[0]); | ||
result = await importESM(urlForConfig); | ||
} else { | ||
throw new Error("ESM is not supported"); | ||
} | ||
return result; | ||
} | ||
} | ||
}); | ||
let result; | ||
try { | ||
@@ -80,14 +111,10 @@ if (stats.isFile()) { | ||
} | ||
if (!result) { | ||
return {}; | ||
} | ||
loaderContext.addBuildDependency(result.filepath); | ||
loaderContext.addDependency(result.filepath); | ||
if (result.isEmpty) { | ||
return result; | ||
} | ||
if (typeof result.config === "function") { | ||
@@ -105,7 +132,5 @@ const api = { | ||
} | ||
result = (0, _full.klona)(result); | ||
return result; | ||
} | ||
function loadPlugin(plugin, options, file) { | ||
@@ -115,11 +140,8 @@ try { | ||
let loadedPlugin = require(plugin); | ||
if (loadedPlugin.default) { | ||
loadedPlugin = loadedPlugin.default; | ||
} | ||
if (!options || Object.keys(options).length === 0) { | ||
return loadedPlugin; | ||
} | ||
return loadedPlugin(options); | ||
@@ -130,3 +152,2 @@ } catch (error) { | ||
} | ||
function pluginFactory() { | ||
@@ -138,3 +159,2 @@ const listOfPlugins = new Map(); | ||
} | ||
if (Array.isArray(plugins)) { | ||
@@ -150,3 +170,2 @@ for (const plugin of plugins) { | ||
const options = plugin[name]; | ||
if (options === false) { | ||
@@ -163,3 +182,2 @@ listOfPlugins.delete(name); | ||
const objectPlugins = Object.entries(plugins); | ||
for (const [name, options] of objectPlugins) { | ||
@@ -173,10 +191,7 @@ if (options === false) { | ||
} | ||
return listOfPlugins; | ||
}; | ||
} | ||
async function tryRequireThenImport(module) { | ||
let exports; | ||
try { | ||
@@ -188,3 +203,2 @@ // eslint-disable-next-line import/no-dynamic-require, global-require | ||
let importESM; | ||
try { | ||
@@ -196,3 +210,2 @@ // eslint-disable-next-line no-new-func | ||
} | ||
if (requireError.code === "ERR_REQUIRE_ESM" && importESM) { | ||
@@ -202,32 +215,23 @@ exports = await importESM(module); | ||
} | ||
throw requireError; | ||
} | ||
} | ||
async function getPostcssOptions(loaderContext, loadedConfig = {}, postcssOptions = {}) { | ||
const file = loaderContext.resourcePath; | ||
let normalizedPostcssOptions = postcssOptions; | ||
if (typeof normalizedPostcssOptions === "function") { | ||
normalizedPostcssOptions = normalizedPostcssOptions(loaderContext); | ||
} | ||
let plugins = []; | ||
try { | ||
const factory = pluginFactory(); | ||
if (loadedConfig.config && loadedConfig.config.plugins) { | ||
factory(loadedConfig.config.plugins); | ||
} | ||
factory(normalizedPostcssOptions.plugins); | ||
plugins = [...factory()].map(item => { | ||
const [plugin, options] = item; | ||
if (typeof plugin === "string") { | ||
return loadPlugin(plugin, options, file); | ||
} | ||
return plugin; | ||
@@ -238,26 +242,21 @@ }); | ||
} | ||
const processOptionsFromConfig = loadedConfig.config || {}; | ||
if (processOptionsFromConfig.from) { | ||
processOptionsFromConfig.from = _path.default.resolve(_path.default.dirname(loadedConfig.filepath), processOptionsFromConfig.from); | ||
} | ||
if (processOptionsFromConfig.to) { | ||
processOptionsFromConfig.to = _path.default.resolve(_path.default.dirname(loadedConfig.filepath), processOptionsFromConfig.to); | ||
} // No need them for processOptions | ||
} | ||
// No need them for processOptions | ||
delete processOptionsFromConfig.plugins; | ||
const processOptionsFromOptions = (0, _full.klona)(normalizedPostcssOptions); | ||
if (processOptionsFromOptions.from) { | ||
processOptionsFromOptions.from = _path.default.resolve(loaderContext.rootContext, processOptionsFromOptions.from); | ||
} | ||
if (processOptionsFromOptions.to) { | ||
processOptionsFromOptions.to = _path.default.resolve(loaderContext.rootContext, processOptionsFromOptions.to); | ||
} // No need them for processOptions | ||
} | ||
// No need them for processOptions | ||
delete processOptionsFromOptions.config; | ||
@@ -272,3 +271,2 @@ delete processOptionsFromOptions.plugins; | ||
}; | ||
if (typeof processOptions.parser === "string") { | ||
@@ -281,3 +279,2 @@ try { | ||
} | ||
if (typeof processOptions.stringifier === "string") { | ||
@@ -290,3 +287,2 @@ try { | ||
} | ||
if (typeof processOptions.syntax === "string") { | ||
@@ -299,3 +295,2 @@ try { | ||
} | ||
if (processOptions.map === true) { | ||
@@ -307,3 +302,2 @@ // https://github.com/postcss/postcss/blob/master/docs/source-maps.md | ||
} | ||
return { | ||
@@ -314,6 +308,4 @@ plugins, | ||
} | ||
const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i; | ||
const ABSOLUTE_SCHEME = /^[a-z0-9+\-.]+:/i; | ||
function getURLType(source) { | ||
@@ -324,21 +316,17 @@ if (source[0] === "/") { | ||
} | ||
return "path-absolute"; | ||
} | ||
if (IS_NATIVE_WIN32_PATH.test(source)) { | ||
return "path-absolute"; | ||
} | ||
return ABSOLUTE_SCHEME.test(source) ? "absolute" : "path-relative"; | ||
} | ||
function normalizeSourceMap(map, resourceContext) { | ||
let newMap = map; | ||
function normalizeSourceMap(map, resourceContext) { | ||
let newMap = map; // Some loader emit source map as string | ||
// Some loader emit source map as string | ||
// Strip any JSON XSSI avoidance prefix from the string (as documented in the source maps specification), and then parse the string as JSON. | ||
if (typeof newMap === "string") { | ||
newMap = JSON.parse(newMap); | ||
} | ||
delete newMap.file; | ||
@@ -349,7 +337,7 @@ const { | ||
delete newMap.sourceRoot; | ||
if (newMap.sources) { | ||
newMap.sources = newMap.sources.map(source => { | ||
const sourceType = getURLType(source); // Do no touch `scheme-relative` and `absolute` URLs | ||
const sourceType = getURLType(source); | ||
// Do no touch `scheme-relative` and `absolute` URLs | ||
if (sourceType === "path-relative" || sourceType === "path-absolute") { | ||
@@ -359,19 +347,19 @@ const absoluteSource = sourceType === "path-relative" && sourceRoot ? _path.default.resolve(sourceRoot, _path.default.normalize(source)) : _path.default.normalize(source); | ||
} | ||
return source; | ||
}); | ||
} | ||
return newMap; | ||
} | ||
function normalizeSourceMapAfterPostcss(map, resourceContext) { | ||
const newMap = map; | ||
function normalizeSourceMapAfterPostcss(map, resourceContext) { | ||
const newMap = map; // result.map.file is an optional property that provides the output filename. | ||
// result.map.file is an optional property that provides the output filename. | ||
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it. | ||
// eslint-disable-next-line no-param-reassign | ||
delete newMap.file; | ||
delete newMap.file; // eslint-disable-next-line no-param-reassign | ||
// eslint-disable-next-line no-param-reassign | ||
newMap.sourceRoot = ""; | ||
newMap.sourceRoot = ""; // eslint-disable-next-line no-param-reassign | ||
// eslint-disable-next-line no-param-reassign | ||
newMap.sources = newMap.sources.map(source => { | ||
@@ -381,9 +369,8 @@ if (source.indexOf("<") === 0) { | ||
} | ||
const sourceType = getURLType(source); | ||
const sourceType = getURLType(source); // Do no touch `scheme-relative`, `path-absolute` and `absolute` types | ||
// Do no touch `scheme-relative`, `path-absolute` and `absolute` types | ||
if (sourceType === "path-relative") { | ||
return _path.default.resolve(resourceContext, source); | ||
} | ||
return source; | ||
@@ -393,6 +380,4 @@ }); | ||
} | ||
function findPackageJSONDir(cwd, statSync) { | ||
let dir = cwd; | ||
for (;;) { | ||
@@ -403,7 +388,6 @@ try { | ||
} | ||
} catch (error) {// Nothing | ||
} catch (error) { | ||
// Nothing | ||
} | ||
const parent = _path.default.dirname(dir); | ||
if (dir === parent) { | ||
@@ -413,15 +397,10 @@ dir = null; | ||
} | ||
dir = parent; | ||
} | ||
return dir; | ||
} | ||
function getPostcssImplementation(loaderContext, implementation) { | ||
let resolvedImplementation = implementation; | ||
if (!implementation || typeof implementation === "string") { | ||
const postcssImplPkg = implementation || "postcss"; | ||
try { | ||
@@ -431,12 +410,12 @@ // eslint-disable-next-line import/no-dynamic-require, global-require | ||
} catch (error) { | ||
loaderContext.emitError(error); // eslint-disable-next-line consistent-return | ||
loaderContext.emitError(error); | ||
// eslint-disable-next-line consistent-return | ||
return; | ||
} | ||
} // eslint-disable-next-line consistent-return | ||
} | ||
// eslint-disable-next-line consistent-return | ||
return resolvedImplementation; | ||
} | ||
function reportError(loaderContext, callback, error) { | ||
@@ -446,3 +425,2 @@ if (error.file) { | ||
} | ||
if (error.name === "CssSyntaxError") { | ||
@@ -449,0 +427,0 @@ callback(new _Error.default(error)); |
@@ -24,7 +24,5 @@ "use strict"; | ||
this.message = `${this.name}\n\n`; | ||
if (typeof line !== "undefined") { | ||
this.message += `(${line}:${column}) `; | ||
} | ||
this.message += plugin ? `${plugin}: ` : ""; | ||
@@ -34,5 +32,3 @@ this.message += `${text}`; | ||
} | ||
} | ||
module.exports = Warning; |
{ | ||
"name": "postcss-loader", | ||
"version": "7.0.1", | ||
"version": "7.0.2", | ||
"description": "PostCSS loader for webpack", | ||
@@ -27,2 +27,3 @@ "license": "MIT", | ||
"lint:js": "eslint --cache .", | ||
"lint:spelling": "cspell \"**/*.*\"", | ||
"lint": "npm-run-all -l -p \"lint:**\"", | ||
@@ -47,28 +48,29 @@ "test:only": "cross-env NODE_ENV=test jest", | ||
"klona": "^2.0.5", | ||
"semver": "^7.3.7" | ||
"semver": "^7.3.8" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.18.6", | ||
"@babel/core": "^7.18.6", | ||
"@babel/preset-env": "^7.18.6", | ||
"@commitlint/cli": "^17.0.3", | ||
"@commitlint/config-conventional": "^17.0.3", | ||
"@babel/cli": "^7.19.3", | ||
"@babel/core": "^7.19.6", | ||
"@babel/preset-env": "^7.19.4", | ||
"@commitlint/cli": "^17.2.0", | ||
"@commitlint/config-conventional": "^17.2.0", | ||
"@webpack-contrib/eslint-config-webpack": "^3.0.0", | ||
"babel-jest": "^28.1.2", | ||
"babel-jest": "^29.2.2", | ||
"cross-env": "^7.0.3", | ||
"cssnano": "^5.1.12", | ||
"cspell": "^6.14.3", | ||
"cssnano": "^5.1.14", | ||
"del": "^6.1.1", | ||
"del-cli": "^4.0.1", | ||
"eslint": "^8.18.0", | ||
"eslint": "^8.26.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"husky": "^8.0.1", | ||
"jest": "^28.1.2", | ||
"less": "^4.1.2", | ||
"less-loader": "^11.0.0", | ||
"jest": "^29.2.2", | ||
"less": "^4.1.3", | ||
"less-loader": "^11.1.0", | ||
"lint-staged": "^12.5.0", | ||
"memfs": "^3.4.7", | ||
"memfs": "^3.4.9", | ||
"midas": "^2.0.3", | ||
"npm-run-all": "^4.1.5", | ||
"postcss": "^8.4.13", | ||
"postcss": "^8.4.18", | ||
"postcss-dark-theme-class": "^0.7.3", | ||
@@ -80,8 +82,8 @@ "postcss-import": "^14.1.0", | ||
"prettier": "^2.7.1", | ||
"sass": "^1.53.0", | ||
"sass-loader": "^13.0.2", | ||
"sass": "^1.55.0", | ||
"sass-loader": "^13.1.0", | ||
"standard-version": "^9.3.2", | ||
"strip-ansi": "^6.0.0", | ||
"sugarss": "^4.0.1", | ||
"webpack": "^5.73.0" | ||
"webpack": "^5.74.0" | ||
}, | ||
@@ -88,0 +90,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50462
669
36
3
Updatedsemver@^7.3.8