imports-loader
Advanced tools
Comparing version 4.0.1 to 5.0.0
"use strict"; | ||
const loader = require("./index"); | ||
module.exports = loader.default; |
@@ -7,11 +7,5 @@ "use strict"; | ||
exports.default = loader; | ||
var _sourceMap = require("source-map"); | ||
var _options = _interopRequireDefault(require("./options.json")); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* | ||
@@ -21,4 +15,4 @@ MIT License http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
const HEADER = "/*** IMPORTS FROM imports-loader ***/\n"; | ||
function loader(content, sourceMap) { | ||
@@ -30,3 +24,2 @@ const options = this.getOptions(_options.default); | ||
let imports; | ||
if (typeof options.imports !== "undefined") { | ||
@@ -38,15 +31,12 @@ try { | ||
return; | ||
} // We don't need to remove 'use strict' manually, because `terser` do it | ||
} | ||
// We don't need to remove 'use strict' manually, because `terser` do it | ||
const directive = (0, _utils.sourceHasUseStrict)(content); | ||
importsCode += Object.entries(imports).reduce((accumulator, item) => `${accumulator}${(0, _utils.renderImports)(this, type, item[0], item[1])}\n`, directive ? "'use strict';\n" : ""); | ||
} | ||
if (typeof options.additionalCode !== "undefined") { | ||
importsCode += `\n${options.additionalCode}\n`; | ||
} | ||
let codeAfterModule = ""; | ||
if (typeof options.wrapper !== "undefined") { | ||
@@ -56,3 +46,2 @@ let thisArg; | ||
let params; | ||
if (typeof options.wrapper === "boolean") { | ||
@@ -71,3 +60,2 @@ thisArg = ""; | ||
} = options.wrapper); | ||
if (Array.isArray(args)) { | ||
@@ -81,11 +69,14 @@ params = args.join(", "); | ||
} | ||
importsCode += `\n(function(${params}) {`; | ||
codeAfterModule += `\n}.call(${thisArg}${args ? `, ${args}` : ""}));\n`; | ||
} | ||
if (this.sourceMap) { | ||
const { | ||
SourceNode, | ||
SourceMapConsumer, | ||
SourceMapGenerator | ||
} = require("source-map-js"); // eslint-disable-line global-require | ||
if (this.sourceMap) { | ||
if (sourceMap) { | ||
const node = _sourceMap.SourceNode.fromStringWithSourceMap(content, new _sourceMap.SourceMapConsumer(sourceMap)); | ||
const node = SourceNode.fromStringWithSourceMap(content, new SourceMapConsumer(sourceMap)); | ||
node.prepend(`${importsCode}\n`); | ||
@@ -99,4 +90,3 @@ node.add(codeAfterModule); | ||
} | ||
const generator = new _sourceMap.SourceMapGenerator(); | ||
const generator = new SourceMapGenerator(); | ||
generator.setSourceContent(this.resourcePath, content); | ||
@@ -117,4 +107,3 @@ generator.addMapping({ | ||
} | ||
callback(null, `${importsCode}\n${content}${codeAfterModule}`, sourceMap); | ||
} |
@@ -9,20 +9,14 @@ "use strict"; | ||
exports.sourceHasUseStrict = sourceHasUseStrict; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _stripComments = _interopRequireDefault(require("strip-comments")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const matchRelativePath = /^\.\.?[/\\]/; | ||
function isAbsolutePath(str) { | ||
return _path.default.posix.isAbsolute(str) || _path.default.win32.isAbsolute(str); | ||
} | ||
function isRelativePath(str) { | ||
return matchRelativePath.test(str); | ||
} // TODO simplify for the next major release | ||
} | ||
// TODO simplify for the next major release | ||
function stringifyRequest(loaderContext, request) { | ||
@@ -32,3 +26,2 @@ if (typeof loaderContext.utils !== "undefined" && typeof loaderContext.utils.contextify === "function") { | ||
} | ||
const splitted = request.split("!"); | ||
@@ -43,6 +36,4 @@ const { | ||
let singlePath = splittedPart ? splittedPart[1] : part; | ||
if (isAbsolutePath(singlePath) && context) { | ||
singlePath = _path.default.relative(context, singlePath); | ||
if (isAbsolutePath(singlePath)) { | ||
@@ -54,3 +45,2 @@ // If singlePath still matches an absolute path, singlePath was on a different drive than context. | ||
} | ||
if (isRelativePath(singlePath) === false) { | ||
@@ -61,11 +51,8 @@ // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules). | ||
} | ||
return singlePath.replace(/\\/g, "/") + query; | ||
}).join("!")); | ||
} | ||
function forError(item) { | ||
return typeof item === "string" ? item : `\n${JSON.stringify(item, null, " ")}\n`; | ||
} | ||
function sourceHasUseStrict(source) { | ||
@@ -75,6 +62,4 @@ const str = (0, _stripComments.default)(source).trim(); | ||
} | ||
function splitCommand(command) { | ||
const result = command.split("|").map(item => item.split(" ")).reduce((acc, val) => acc.concat(val), []); | ||
for (const item of result) { | ||
@@ -85,23 +70,16 @@ if (!item) { | ||
} | ||
return result; | ||
} | ||
function resolveImports(type, item) { | ||
const defaultSyntax = type === "module" ? "default" : "single"; | ||
let result; | ||
if (typeof item === "string") { | ||
const noWhitespaceItem = item.trim(); | ||
if (noWhitespaceItem.length === 0) { | ||
throw new Error(`Invalid "${item}" value for import`); | ||
} | ||
const splittedItem = splitCommand(noWhitespaceItem); | ||
if (splittedItem.length > 4) { | ||
throw new Error(`Invalid "${item}" value for import`); | ||
} | ||
if (splittedItem.length === 1) { | ||
@@ -130,30 +108,22 @@ result = { | ||
} | ||
if (result.syntax === defaultSyntax && typeof result.name === "undefined") { | ||
result.name = result.moduleName; | ||
} | ||
if (["default", "side-effects", "single", "pure"].includes(result.syntax) && typeof result.alias !== "undefined") { | ||
throw new Error(`The "${result.syntax}" syntax does not support "${result.alias}" alias in "${forError(item)}" value`); | ||
} | ||
if (["side-effects", "pure"].includes(result.syntax) && typeof result.name !== "undefined") { | ||
throw new Error(`The "${result.syntax}" syntax does not support "${result.name}" name in "${forError(item)}" value`); | ||
} | ||
if (["default", "namespace", "named", "side-effects"].includes(result.syntax) && type === "commonjs") { | ||
throw new Error(`The "${type}" type does not support the "${result.syntax}" syntax in "${forError(item)}" value`); | ||
} | ||
if (["single", "multiple", "pure"].includes(result.syntax) && type === "module") { | ||
throw new Error(`The "${type}" format does not support the "${result.syntax}" syntax in "${forError(item)}" value`); | ||
} | ||
if (["namespace", "named", "multiple"].includes(result.syntax) && typeof result.name === "undefined") { | ||
throw new Error(`The "${result.syntax}" syntax needs the "name" option in "${forError(item)}" value`); | ||
} | ||
return result; | ||
} | ||
function getIdentifiers(array) { | ||
@@ -168,3 +138,2 @@ return array.reduce((accumulator, item) => { | ||
} | ||
if (typeof item.name !== "undefined") { | ||
@@ -176,15 +145,11 @@ accumulator.push({ | ||
} | ||
return accumulator; | ||
}, []); | ||
} | ||
function duplicateBy(array, key) { | ||
return array.filter((a, aIndex) => array.filter((b, bIndex) => b[key] === a[key] && aIndex !== bIndex).length > 0); | ||
} | ||
function getImports(type, imports) { | ||
let result; | ||
const importItems = typeof imports === "string" && imports.includes(",") ? imports.split(",") : imports; | ||
if (typeof importItems === "string") { | ||
@@ -195,12 +160,8 @@ result = [resolveImports(type, importItems)]; | ||
} | ||
const identifiers = getIdentifiers(result); | ||
const duplicates = duplicateBy(identifiers, "value"); | ||
if (duplicates.length > 0) { | ||
throw new Error(`Duplicate ${duplicates.map(identifier => `"${identifier.value}" (as "${identifier.type}")`).join(", ")} identifiers found in "\n${JSON.stringify(importItems, null, " ")}\n" value`); | ||
} | ||
const sortedResults = Object.create(null); | ||
for (const item of result) { | ||
@@ -210,7 +171,5 @@ const { | ||
} = item; | ||
if (!sortedResults[moduleName]) { | ||
sortedResults[moduleName] = []; | ||
} | ||
const { | ||
@@ -227,14 +186,12 @@ syntax, | ||
} | ||
return sortedResults; | ||
} | ||
function renderImports(loaderContext, type, moduleName, imports) { | ||
let code = ""; | ||
if (type === "commonjs") { | ||
const pure = imports.filter(({ | ||
syntax | ||
}) => syntax === "pure"); // Pure | ||
}) => syntax === "pure"); | ||
// Pure | ||
if (pure.length > 0) { | ||
@@ -246,7 +203,7 @@ pure.forEach((_, i) => { | ||
} | ||
const singleImports = imports.filter(({ | ||
syntax | ||
}) => syntax === "single"); // Single | ||
}) => syntax === "single"); | ||
// Single | ||
if (singleImports.length > 0) { | ||
@@ -262,7 +219,7 @@ code += pure.length > 0 ? "\n" : ""; | ||
} | ||
const multipleImports = imports.filter(({ | ||
syntax | ||
}) => syntax === "multiple"); // Multiple | ||
}) => syntax === "multiple"); | ||
// Multiple | ||
if (multipleImports.length > 0) { | ||
@@ -282,10 +239,9 @@ code += pure.length > 0 || singleImports.length > 0 ? "\n" : ""; | ||
} | ||
return code; | ||
} | ||
const sideEffectsImports = imports.filter(({ | ||
syntax | ||
}) => syntax === "side-effects"); // Side-effects | ||
}) => syntax === "side-effects"); | ||
// Side-effects | ||
if (sideEffectsImports.length > 0) { | ||
@@ -298,3 +254,2 @@ sideEffectsImports.forEach((_, i) => { | ||
} | ||
const defaultImports = imports.filter(({ | ||
@@ -308,4 +263,5 @@ syntax | ||
syntax | ||
}) => syntax === "namespace"); // Default | ||
}) => syntax === "namespace"); | ||
// Default | ||
if (defaultImports.length > 0) { | ||
@@ -319,5 +275,5 @@ defaultImports.forEach((defaultImport, i) => { | ||
}); | ||
} // Named | ||
} | ||
// Named | ||
if (namedImports.length > 0) { | ||
@@ -336,5 +292,5 @@ code += defaultImports.length > 0 ? "\n" : ""; | ||
code += ` } from ${stringifyRequest(loaderContext, moduleName)};`; | ||
} // Namespace | ||
} | ||
// Namespace | ||
if (namespaceImports.length > 0) { | ||
@@ -350,4 +306,3 @@ code += defaultImports.length > 0 || namedImports.length > 0 ? "\n" : ""; | ||
} | ||
return code; | ||
} |
{ | ||
"name": "imports-loader", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "imports loader module for webpack", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">= 14.15.0" | ||
"node": ">= 18.12.0" | ||
}, | ||
@@ -28,3 +28,7 @@ "scripts": { | ||
"lint:js": "eslint --cache .", | ||
"lint:spelling": "cspell --no-must-find-files --cache --quiet \"**/*.*\"", | ||
"lint": "npm-run-all -l -p \"lint:**\"", | ||
"fix:js": "npm run lint:js -- --fix", | ||
"fix:prettier": "npm run lint:prettier -- --write", | ||
"fix": "npm-run-all -l fix:js fix:prettier", | ||
"test:only": "cross-env NODE_ENV=test jest", | ||
@@ -45,28 +49,29 @@ "test:watch": "npm run test:only -- --watch", | ||
"dependencies": { | ||
"source-map": "^0.6.1", | ||
"source-map-js": "^1.0.2", | ||
"strip-comments": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.18.9", | ||
"@babel/core": "^7.18.9", | ||
"@babel/preset-env": "^7.18.9", | ||
"@commitlint/cli": "^17.0.3", | ||
"@commitlint/config-conventional": "^17.0.3", | ||
"@babel/cli": "^7.23.4", | ||
"@babel/core": "^7.23.7", | ||
"@babel/preset-env": "^7.23.8", | ||
"@commitlint/cli": "^18.4.4", | ||
"@commitlint/config-conventional": "^18.4.4", | ||
"@webpack-contrib/eslint-config-webpack": "^3.0.0", | ||
"babel-jest": "^28.1.3", | ||
"babel-loader": "^8.2.5", | ||
"babel-jest": "^29.7.0", | ||
"babel-loader": "^8.3.0", | ||
"cross-env": "^7.0.3", | ||
"del": "^6.1.1", | ||
"del-cli": "^4.0.1", | ||
"eslint": "^8.21.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"husky": "^8.0.1", | ||
"jest": "^28.1.3", | ||
"lint-staged": "^12.5.0", | ||
"memfs": "^3.4.7", | ||
"cspell": "^8.3.2", | ||
"del": "^7.1.0", | ||
"del-cli": "^5.1.0", | ||
"eslint": "^8.56.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-import": "^2.29.1", | ||
"husky": "^8.0.3", | ||
"jest": "^29.7.0", | ||
"lint-staged": "^15.2.0", | ||
"memfs": "^4.6.0", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.7.1", | ||
"standard-version": "^9.3.0", | ||
"webpack": "^5.74.0" | ||
"prettier": "^3.2.2", | ||
"standard-version": "^9.5.0", | ||
"webpack": "^5.89.0" | ||
}, | ||
@@ -73,0 +78,0 @@ "keywords": [ |
@@ -12,3 +12,3 @@ <div align="center"> | ||
[![cover][cover]][cover-url] | ||
[![chat][chat]][chat-url] | ||
[![discussion][discussion]][discussion-url] | ||
[![size][size]][size-url] | ||
@@ -115,3 +115,5 @@ | ||
```js | ||
const myLib = require(`imports-loader?type=commonjs&imports=single|jquery|$,angular!./example.js`); | ||
const myLib = require( | ||
`imports-loader?type=commonjs&imports=single|jquery|$,angular!./example.js`, | ||
); | ||
// `|` is separator in a query string, equivalently `single|jquery|$` and `angular` | ||
@@ -129,3 +131,5 @@ // Adds the following code to the beginning of example.js: | ||
```js | ||
const myLib = require(`imports-loader?type=commonjs&imports=single|myLib|myMethod&wrapper=window&!./example.js`); | ||
const myLib = require( | ||
`imports-loader?type=commonjs&imports=single|myLib|myMethod&wrapper=window&!./example.js`, | ||
); | ||
// `|` is separator in a query string, equivalently `single|myLib|myMethod` and `angular` | ||
@@ -164,3 +168,3 @@ // Adds the following code to the example.js: | ||
// You can use `regexp` | ||
// test: /example\.js/$ | ||
// test: /example\.js$/ | ||
test: require.resolve("example.js"), | ||
@@ -617,3 +621,3 @@ use: [ | ||
// ... | ||
}.call()); | ||
}).call(); | ||
``` | ||
@@ -658,3 +662,3 @@ | ||
// ... | ||
}.call(window)); | ||
}).call(window); | ||
``` | ||
@@ -702,3 +706,3 @@ | ||
// ... | ||
}.call(window, myVariable, myOtherVariable)); | ||
}).call(window, myVariable, myOtherVariable); | ||
``` | ||
@@ -749,3 +753,3 @@ | ||
// ... | ||
}.call(window, myVariable, myOtherVariable)); | ||
}).call(window, myVariable, myOtherVariable); | ||
``` | ||
@@ -866,5 +870,5 @@ | ||
[cover-url]: https://codecov.io/gh/webpack-contrib/imports-loader | ||
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg | ||
[chat-url]: https://gitter.im/webpack/webpack | ||
[discussion]: https://img.shields.io/github/discussions/webpack/webpack | ||
[discussion-url]: https://github.com/webpack/webpack/discussions | ||
[size]: https://packagephobia.now.sh/badge?p=imports-loader | ||
[size-url]: https://packagephobia.now.sh/result?p=imports-loader |
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
37052
505
866
23
+ Addedsource-map-js@^1.0.2
+ Addedsource-map-js@1.2.1(transitive)
- Removedsource-map@^0.6.1