├── .husky ├── pre-commit └── commit-msg ├── test ├── fixtures │ ├── lib_1.js │ ├── lib_2.js │ ├── lib_3.js │ ├── lib_4.js │ ├── inline.js │ ├── inline4.js │ ├── inline-broken.js │ ├── inline3.js │ ├── some-library.js │ ├── use-strict-in-function.js │ ├── inline2.js │ └── use-strict.js ├── helpers │ ├── getErrors.js │ ├── getWarnings.js │ ├── getModuleSource.js │ ├── compile.js │ ├── readAssets.js │ ├── index.js │ ├── normalizeErrors.js │ ├── readAsset.js │ └── getCompiler.js ├── cjs.test.js ├── validate-options.test.js ├── __snapshots__ │ ├── validate-options.test.js.snap │ └── loader.test.js.snap └── loader.test.js ├── .prettierignore ├── src ├── cjs.js ├── index.js ├── options.json └── utils.js ├── .gitattributes ├── lint-staged.config.js ├── commitlint.config.js ├── eslint.config.mjs ├── .editorconfig ├── .gitignore ├── babel.config.js ├── .github └── workflows │ ├── dependency-review.yml │ └── nodejs.yml ├── .cspell.json ├── LICENSE ├── package.json ├── CHANGELOG.md └── README.md /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | lint-staged 2 | -------------------------------------------------------------------------------- /.husky/commit-msg: -------------------------------------------------------------------------------- 1 | commitlint --edit $1 2 | -------------------------------------------------------------------------------- /test/fixtures/lib_1.js: -------------------------------------------------------------------------------- 1 | export default function lib() {} 2 | -------------------------------------------------------------------------------- /test/fixtures/lib_2.js: -------------------------------------------------------------------------------- 1 | export default function lib() {} 2 | -------------------------------------------------------------------------------- /test/fixtures/lib_3.js: -------------------------------------------------------------------------------- 1 | export default function lib() {} 2 | -------------------------------------------------------------------------------- /test/fixtures/lib_4.js: -------------------------------------------------------------------------------- 1 | export default function lib() {} 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | /coverage 2 | /dist 3 | /node_modules 4 | /test/fixtures 5 | CHANGELOG.md -------------------------------------------------------------------------------- /src/cjs.js: -------------------------------------------------------------------------------- 1 | const loader = require("./index"); 2 | 3 | module.exports = loader.default; 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | package-lock.json -diff 2 | * text=auto 3 | bin/* eol=lf 4 | yarn.lock -diff 5 | -------------------------------------------------------------------------------- /test/fixtures/inline.js: -------------------------------------------------------------------------------- 1 | require('../../src/cjs.js?imports=lib_1,lib_2!./some-library.js'); 2 | -------------------------------------------------------------------------------- /test/fixtures/inline4.js: -------------------------------------------------------------------------------- 1 | require('../../src/cjs.js?additionalCode=var%20require%20=%20false;!./some-library.js'); 2 | -------------------------------------------------------------------------------- /test/fixtures/inline-broken.js: -------------------------------------------------------------------------------- 1 | require('../../src/cjs.js?imports=named|lib_2|name|alias|something!./some-library.js'); 2 | -------------------------------------------------------------------------------- /test/fixtures/inline3.js: -------------------------------------------------------------------------------- 1 | require('../../src/cjs.js?wrapper=window&imports=default%20lib_2|$!./some-library.js'); 2 | 3 | -------------------------------------------------------------------------------- /test/fixtures/some-library.js: -------------------------------------------------------------------------------- 1 | var someCode = { 2 | number: 123, 3 | object: { existingSubProperty: 123 } 4 | }; 5 | -------------------------------------------------------------------------------- /lint-staged.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "*": ["prettier --write --ignore-unknown", "cspell"], 3 | "*.js": ["eslint --cache --fix"], 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/use-strict-in-function.js: -------------------------------------------------------------------------------- 1 | var myObject = { foo: 'bar' }; 2 | 3 | function test() { 4 | 'use strict'; 5 | 6 | return 'test'; 7 | } 8 | -------------------------------------------------------------------------------- /test/helpers/getErrors.js: -------------------------------------------------------------------------------- 1 | import normalizeErrors from "./normalizeErrors"; 2 | 3 | export default (stats) => normalizeErrors(stats.compilation.errors); 4 | -------------------------------------------------------------------------------- /test/helpers/getWarnings.js: -------------------------------------------------------------------------------- 1 | import normalizeErrors from "./normalizeErrors"; 2 | 3 | export default (stats) => normalizeErrors(stats.compilation.warnings); 4 | -------------------------------------------------------------------------------- /test/fixtures/inline2.js: -------------------------------------------------------------------------------- 1 | require('../../src/cjs.js?type=commonjs&imports=single|lib_2|lib_2_all,multiple|lib_2|lib_2_method|lib_2_method_alias!./some-library.js'); 2 | -------------------------------------------------------------------------------- /test/cjs.test.js: -------------------------------------------------------------------------------- 1 | import src from "../src"; 2 | import cjs from "../src/cjs"; 3 | 4 | describe("cjs", () => { 5 | it("should exported", () => { 6 | expect(cjs).toEqual(src); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /test/fixtures/use-strict.js: -------------------------------------------------------------------------------- 1 | /* Comment */ 2 | 3 | 'use strict'; 4 | 5 | var myObject = { foo: 'bar' }; 6 | 7 | function test() { 8 | 'use strict'; 9 | 10 | return 'test'; 11 | } 12 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: ["@commitlint/config-conventional"], 3 | rules: { 4 | "header-max-length": [0], 5 | "body-max-line-length": [0], 6 | "footer-max-line-length": [0], 7 | }, 8 | }; 9 | -------------------------------------------------------------------------------- /test/helpers/getModuleSource.js: -------------------------------------------------------------------------------- 1 | export default (name, stats) => { 2 | const { modules } = stats.toJson({ source: true }); 3 | 4 | const module = modules.find((m) => m.name.includes(name)); 5 | 6 | return module.source; 7 | }; 8 | -------------------------------------------------------------------------------- /eslint.config.mjs: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "eslint/config"; 2 | import configs from "eslint-config-webpack/configs.js"; 3 | 4 | export default defineConfig([ 5 | { 6 | extends: [configs["recommended-dirty"]], 7 | }, 8 | ]); 9 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /test/helpers/compile.js: -------------------------------------------------------------------------------- 1 | export default (compiler) => 2 | new Promise((resolve, reject) => { 3 | compiler.run((error, stats) => { 4 | if (error) { 5 | return reject(error); 6 | } 7 | 8 | return resolve(stats); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | logs 3 | *.log 4 | npm-debug.log* 5 | .eslintcache 6 | .cspellcache 7 | /coverage 8 | /dist 9 | /local 10 | /reports 11 | /node_modules 12 | .DS_Store 13 | Thumbs.db 14 | .idea 15 | *.iml 16 | .vscode 17 | *.sublime-project 18 | *.sublime-workspace 19 | -------------------------------------------------------------------------------- /test/helpers/readAssets.js: -------------------------------------------------------------------------------- 1 | import readAsset from "./readAsset"; 2 | 3 | export default function readAssets(compiler, stats) { 4 | const assets = {}; 5 | 6 | for (const asset of Object.keys(stats.compilation.assets)) { 7 | assets[asset] = readAsset(asset, compiler, stats); 8 | } 9 | 10 | return assets; 11 | } 12 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | const MIN_BABEL_VERSION = 7; 2 | 3 | module.exports = (api) => { 4 | api.assertVersion(MIN_BABEL_VERSION); 5 | api.cache(true); 6 | 7 | return { 8 | presets: [ 9 | [ 10 | "@babel/preset-env", 11 | { 12 | targets: { 13 | node: "18.12.0", 14 | }, 15 | }, 16 | ], 17 | ], 18 | }; 19 | }; 20 | -------------------------------------------------------------------------------- /.github/workflows/dependency-review.yml: -------------------------------------------------------------------------------- 1 | name: "Dependency Review" 2 | on: [pull_request] 3 | 4 | permissions: 5 | contents: read 6 | 7 | jobs: 8 | dependency-review: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: "Checkout Repository" 12 | uses: actions/checkout@v5 13 | - name: "Dependency Review" 14 | uses: actions/dependency-review-action@v4 15 | -------------------------------------------------------------------------------- /.cspell.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2", 3 | "language": "en,en-gb", 4 | "words": [ 5 | "memfs", 6 | "sokra", 7 | "Koppers", 8 | "commitlint", 9 | "eslintcache", 10 | "cspellcache" 11 | ], 12 | "ignorePaths": [ 13 | "CHANGELOG.md", 14 | "package.json", 15 | "dist/**", 16 | "**/__snapshots__/**", 17 | "package-lock.json", 18 | "node_modules", 19 | "coverage", 20 | "*.log" 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /test/helpers/index.js: -------------------------------------------------------------------------------- 1 | export { default as compile } from "./compile"; 2 | export { default as getErrors } from "./getErrors"; 3 | export { default as getCompiler } from "./getCompiler"; 4 | export { default as getWarnings } from "./getWarnings"; 5 | export { default as getModuleSource } from "./getModuleSource"; 6 | export { default as readAsset } from "./readAsset"; 7 | export { default as normalizeErrors } from "./normalizeErrors"; 8 | export { default as readsAssets } from "./readAssets"; 9 | -------------------------------------------------------------------------------- /test/helpers/normalizeErrors.js: -------------------------------------------------------------------------------- 1 | function removeCWD(str) { 2 | const isWin = process.platform === "win32"; 3 | let cwd = process.cwd(); 4 | 5 | if (isWin) { 6 | str = str.replaceAll("\\", "/"); 7 | 8 | cwd = cwd.replaceAll("\\", "/"); 9 | } 10 | 11 | return str 12 | .replace(/\(from .*?\)/, "(from `replaced original path`)") 13 | .replaceAll(new RegExp(cwd, "g"), ""); 14 | } 15 | 16 | export default (errors) => 17 | errors.map((error) => 18 | removeCWD(error.toString().split("\n").slice(0, 2).join("\n")), 19 | ); 20 | -------------------------------------------------------------------------------- /test/helpers/readAsset.js: -------------------------------------------------------------------------------- 1 | import path from "node:path"; 2 | 3 | export default (asset, compiler, stats) => { 4 | const usedFs = compiler.outputFileSystem; 5 | const outputPath = stats.compilation.outputOptions.path; 6 | 7 | let data = ""; 8 | let targetFile = asset; 9 | 10 | const queryStringIdx = targetFile.indexOf("?"); 11 | 12 | if (queryStringIdx >= 0) { 13 | targetFile = targetFile.slice(0, queryStringIdx); 14 | } 15 | 16 | try { 17 | data = usedFs.readFileSync(path.join(outputPath, targetFile)).toString(); 18 | } catch (error) { 19 | data = error.toString(); 20 | } 21 | 22 | return data; 23 | }; 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright JS Foundation and other contributors 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | 'Software'), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/helpers/getCompiler.js: -------------------------------------------------------------------------------- 1 | import path from "node:path"; 2 | 3 | import { Volume, createFsFromVolume } from "memfs"; 4 | import webpack from "webpack"; 5 | 6 | export default ( 7 | fixture, 8 | loaderOptions = {}, 9 | config = {}, 10 | disableLoader = false, 11 | ) => { 12 | const loaders = []; 13 | 14 | if (!disableLoader) { 15 | loaders.push({ 16 | test: path.resolve(__dirname, "../fixtures", fixture), 17 | use: [ 18 | { 19 | loader: path.resolve(__dirname, "../../src"), 20 | options: loaderOptions || {}, 21 | }, 22 | ], 23 | }); 24 | } 25 | 26 | const fullConfig = { 27 | mode: "development", 28 | devtool: config.devtool || false, 29 | context: path.resolve(__dirname, "../fixtures"), 30 | entry: path.resolve(__dirname, "../fixtures", fixture), 31 | output: { 32 | path: path.resolve(__dirname, "../outputs"), 33 | filename: "[name].bundle.js", 34 | chunkFilename: "[name].chunk.js", 35 | library: "ImportsLoader", 36 | // libraryTarget: 'var', 37 | }, 38 | module: { 39 | rules: loaders, 40 | }, 41 | plugins: [], 42 | resolve: { 43 | alias: { 44 | // eslint-disable-next-line camelcase 45 | lib_1: path.resolve(__dirname, "../", "fixtures", "lib_1"), 46 | // eslint-disable-next-line camelcase 47 | lib_2: path.resolve(__dirname, "../", "fixtures", "lib_2"), 48 | // eslint-disable-next-line camelcase 49 | lib_3: path.resolve(__dirname, "../", "fixtures", "lib_3"), 50 | // eslint-disable-next-line camelcase 51 | lib_4: path.resolve(__dirname, "../", "fixtures", "lib_4"), 52 | }, 53 | }, 54 | ...config, 55 | }; 56 | 57 | const compiler = webpack(fullConfig); 58 | 59 | if (!config.outputFileSystem) { 60 | compiler.outputFileSystem = createFsFromVolume(new Volume()); 61 | } 62 | 63 | return compiler; 64 | }; 65 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imports-loader", 3 | "version": "5.0.0", 4 | "description": "imports loader module for webpack", 5 | "keywords": [ 6 | "webpack" 7 | ], 8 | "homepage": "https://github.com/webpack/imports-loader", 9 | "bugs": "https://github.com/webpack/imports-loader/issues", 10 | "repository": "webpack/imports-loader", 11 | "funding": { 12 | "type": "opencollective", 13 | "url": "https://opencollective.com/webpack" 14 | }, 15 | "license": "MIT", 16 | "author": "Tobias Koppers @sokra", 17 | "main": "dist/cjs.js", 18 | "files": [ 19 | "dist" 20 | ], 21 | "scripts": { 22 | "start": "npm run build -- -w", 23 | "clean": "del-cli dist", 24 | "prebuild": "npm run clean", 25 | "build": "cross-env NODE_ENV=production babel src -d dist --copy-files", 26 | "commitlint": "commitlint --from=main", 27 | "security": "npm audit", 28 | "lint:prettier": "prettier --list-different .", 29 | "lint:js": "eslint --cache .", 30 | "lint:spelling": "cspell --no-must-find-files --cache --quiet \"**/*.*\"", 31 | "lint": "npm-run-all -l -p \"lint:**\"", 32 | "fix:js": "npm run lint:js -- --fix", 33 | "fix:prettier": "npm run lint:prettier -- --write", 34 | "fix": "npm-run-all -l fix:js fix:prettier", 35 | "test:only": "cross-env NODE_ENV=test jest", 36 | "test:watch": "npm run test:only -- --watch", 37 | "test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage", 38 | "pretest": "npm run lint", 39 | "test": "npm run test:coverage", 40 | "prepare": "husky && npm run build", 41 | "release": "standard-version" 42 | }, 43 | "dependencies": { 44 | "source-map-js": "^1.2.0", 45 | "strip-comments": "^2.0.1" 46 | }, 47 | "devDependencies": { 48 | "@babel/cli": "^7.24.8", 49 | "@babel/core": "^7.25.2", 50 | "@babel/preset-env": "^7.25.3", 51 | "@commitlint/cli": "^19.3.0", 52 | "@commitlint/config-conventional": "^19.2.2", 53 | "@eslint/js": "^9.32.0", 54 | "@eslint/markdown": "^7.1.0", 55 | "@stylistic/eslint-plugin": "^5.2.2", 56 | "babel-jest": "^30.0.0", 57 | "babel-loader": "^10.0.0", 58 | "cross-env": "^7.0.3", 59 | "cspell": "^8.13.1", 60 | "del": "^7.1.0", 61 | "del-cli": "^5.1.0", 62 | "eslint": "^9.32.0", 63 | "eslint-config-prettier": "^10.1.8", 64 | "eslint-config-webpack": "^4.5.1", 65 | "eslint-plugin-import": "^2.32.0", 66 | "eslint-plugin-jest": "^29.0.1", 67 | "eslint-plugin-n": "^17.21.1", 68 | "eslint-plugin-prettier": "^5.5.3", 69 | "eslint-plugin-unicorn": "^60.0.0", 70 | "globals": "^16.3.0", 71 | "husky": "^9.1.4", 72 | "jest": "^30.0.0", 73 | "lint-staged": "^15.2.8", 74 | "memfs": "^4.11.1", 75 | "npm-run-all": "^4.1.5", 76 | "prettier": "^3.3.3", 77 | "standard-version": "^9.5.0", 78 | "typescript-eslint": "^8.38.0", 79 | "webpack": "^5.93.0" 80 | }, 81 | "peerDependencies": { 82 | "webpack": "^5.0.0" 83 | }, 84 | "engines": { 85 | "node": ">= 18.12.0" 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: imports-loader 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - next 8 | pull_request: 9 | branches: 10 | - main 11 | - next 12 | 13 | permissions: 14 | contents: read 15 | 16 | jobs: 17 | lint: 18 | name: Lint - ${{ matrix.os }} - Node v${{ matrix.node-version }} 19 | 20 | env: 21 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 22 | 23 | strategy: 24 | matrix: 25 | os: [ubuntu-latest] 26 | node-version: [lts/*] 27 | 28 | runs-on: ${{ matrix.os }} 29 | 30 | concurrency: 31 | group: lint-${{ matrix.os }}-v${{ matrix.node-version }}-${{ github.ref }} 32 | cancel-in-progress: true 33 | 34 | steps: 35 | - uses: actions/checkout@v5 36 | with: 37 | fetch-depth: 0 38 | 39 | - name: Use Node.js ${{ matrix.node-version }} 40 | uses: actions/setup-node@v4 41 | with: 42 | node-version: ${{ matrix.node-version }} 43 | cache: "npm" 44 | 45 | - name: Install dependencies 46 | run: npm ci 47 | 48 | - name: Lint 49 | run: npm run lint 50 | 51 | # - name: Security audit 52 | # run: npm run security 53 | 54 | - name: Validate PR commits with commitlint 55 | if: github.event_name == 'pull_request' 56 | run: npx commitlint --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }} --verbose 57 | 58 | test: 59 | name: Test - ${{ matrix.os }} - Node v${{ matrix.node-version }}, Webpack ${{ matrix.webpack-version }} 60 | 61 | strategy: 62 | matrix: 63 | os: [ubuntu-latest, windows-latest, macos-latest] 64 | node-version: [18.x, 20.x, 22.x, 24.x] 65 | webpack-version: [latest] 66 | 67 | runs-on: ${{ matrix.os }} 68 | 69 | concurrency: 70 | group: test-${{ matrix.os }}-v${{ matrix.node-version }}-${{ matrix.webpack-version }}-${{ github.ref }} 71 | cancel-in-progress: true 72 | 73 | steps: 74 | - name: Setup Git 75 | if: matrix.os == 'windows-latest' 76 | run: git config --global core.autocrlf input 77 | 78 | - uses: actions/checkout@v5 79 | 80 | - name: Use Node.js ${{ matrix.node-version }} 81 | uses: actions/setup-node@v4 82 | with: 83 | node-version: ${{ matrix.node-version }} 84 | cache: "npm" 85 | 86 | - name: Install dependencies 87 | run: npm ci 88 | 89 | - name: Install webpack ${{ matrix.webpack-version }} 90 | if: matrix.webpack-version != 'latest' 91 | run: npm i webpack@${{ matrix.webpack-version }} 92 | 93 | - name: Run tests for webpack version ${{ matrix.webpack-version }} 94 | run: npm run test:coverage -- --ci 95 | 96 | - name: Submit coverage data to codecov 97 | uses: codecov/codecov-action@v5 98 | with: 99 | token: ${{ secrets.CODECOV_TOKEN }} 100 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License http://www.opensource.org/licenses/mit-license.php 3 | Author Tobias Koppers @sokra 4 | */ 5 | 6 | import schema from "./options.json"; 7 | 8 | import { getImports, renderImports, sourceHasUseStrict } from "./utils"; 9 | 10 | const HEADER = "/*** IMPORTS FROM imports-loader ***/\n"; 11 | 12 | export default function loader(content, sourceMap) { 13 | const options = this.getOptions(schema); 14 | const type = options.type || "module"; 15 | const callback = this.async(); 16 | 17 | let importsCode = HEADER; 18 | 19 | let imports; 20 | 21 | if (typeof options.imports !== "undefined") { 22 | try { 23 | imports = getImports(type, options.imports); 24 | } catch (error) { 25 | callback(error); 26 | 27 | return; 28 | } 29 | 30 | // We don't need to remove 'use strict' manually, because `terser` do it 31 | const directive = sourceHasUseStrict(content); 32 | 33 | importsCode += Object.entries(imports).reduce( 34 | (accumulator, item) => 35 | `${accumulator}${renderImports(this, type, item[0], item[1])}\n`, 36 | directive ? "'use strict';\n" : "", 37 | ); 38 | } 39 | 40 | if (typeof options.additionalCode !== "undefined") { 41 | importsCode += `\n${options.additionalCode}\n`; 42 | } 43 | 44 | let codeAfterModule = ""; 45 | 46 | if (typeof options.wrapper !== "undefined") { 47 | let thisArg; 48 | let args; 49 | let params; 50 | 51 | if (typeof options.wrapper === "boolean") { 52 | thisArg = ""; 53 | params = ""; 54 | args = ""; 55 | } else if (typeof options.wrapper === "string") { 56 | thisArg = options.wrapper; 57 | params = ""; 58 | args = ""; 59 | } else { 60 | ({ thisArg, args } = options.wrapper); 61 | 62 | if (Array.isArray(args)) { 63 | params = args.join(", "); 64 | args = params; 65 | } else { 66 | params = Object.keys(args).join(", "); 67 | args = Object.values(args).join(", "); 68 | } 69 | } 70 | 71 | importsCode += `\n(function(${params}) {`; 72 | codeAfterModule += `\n}.call(${thisArg}${args ? `, ${args}` : ""}));\n`; 73 | } 74 | 75 | if (this.sourceMap) { 76 | const { 77 | SourceMapConsumer, 78 | SourceMapGenerator, 79 | SourceNode, 80 | } = require("source-map-js"); 81 | 82 | if (sourceMap) { 83 | const node = SourceNode.fromStringWithSourceMap( 84 | content, 85 | new SourceMapConsumer(sourceMap), 86 | ); 87 | 88 | node.prepend(`${importsCode}\n`); 89 | node.add(codeAfterModule); 90 | 91 | const result = node.toStringWithSourceMap({ file: this.resourcePath }); 92 | 93 | callback(null, result.code, result.map.toJSON()); 94 | 95 | return; 96 | } 97 | 98 | const generator = new SourceMapGenerator(); 99 | 100 | generator.setSourceContent(this.resourcePath, content); 101 | generator.addMapping({ 102 | generated: { line: importsCode.split("\n").length + 1, column: 0 }, 103 | original: { line: 1, column: 0 }, 104 | source: this.resourcePath, 105 | }); 106 | 107 | callback( 108 | null, 109 | `${importsCode}\n${content}\n${codeAfterModule}`, 110 | generator.toJSON(), 111 | ); 112 | 113 | return; 114 | } 115 | 116 | callback(null, `${importsCode}\n${content}${codeAfterModule}`, sourceMap); 117 | } 118 | -------------------------------------------------------------------------------- /src/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "Imports Loader options", 3 | "definitions": { 4 | "ImportItemString": { 5 | "type": "string", 6 | "minLength": 1, 7 | "description": "Allows to use a string to describe an import.", 8 | "link": "https://webpack.js.org/loaders/imports-loader/#string" 9 | }, 10 | "ImportItemObject": { 11 | "type": "object", 12 | "additionalProperties": false, 13 | "description": "Allows to use an object to describe an import.", 14 | "link": "https://webpack.js.org/loaders/imports-loader/#object", 15 | "properties": { 16 | "syntax": { 17 | "enum": [ 18 | "default", 19 | "named", 20 | "namespace", 21 | "side-effects", 22 | "single", 23 | "multiple", 24 | "pure" 25 | ] 26 | }, 27 | "moduleName": { 28 | "type": "string", 29 | "minLength": 1 30 | }, 31 | "name": { 32 | "type": "string", 33 | "minLength": 1 34 | }, 35 | "alias": { 36 | "type": "string", 37 | "minLength": 1 38 | } 39 | }, 40 | "required": ["moduleName"] 41 | }, 42 | "ImportItem": { 43 | "anyOf": [ 44 | { 45 | "$ref": "#/definitions/ImportItemString" 46 | }, 47 | { 48 | "$ref": "#/definitions/ImportItemObject" 49 | } 50 | ] 51 | } 52 | }, 53 | "type": "object", 54 | "description": "Options for imports-loader", 55 | "properties": { 56 | "type": { 57 | "enum": ["module", "commonjs"], 58 | "description": "Format of generated exports.", 59 | "link": "https://github.com/webpack/imports-loader#type" 60 | }, 61 | "imports": { 62 | "anyOf": [ 63 | { 64 | "$ref": "#/definitions/ImportItemString" 65 | }, 66 | { 67 | "$ref": "#/definitions/ImportItem" 68 | }, 69 | { 70 | "type": "array", 71 | "minItems": 1, 72 | "items": { 73 | "$ref": "#/definitions/ImportItem" 74 | } 75 | } 76 | ] 77 | }, 78 | "wrapper": { 79 | "anyOf": [ 80 | { 81 | "type": "boolean" 82 | }, 83 | { 84 | "type": "string", 85 | "minLength": 1 86 | }, 87 | { 88 | "type": "object", 89 | "additionalProperties": false, 90 | "properties": { 91 | "thisArg": { 92 | "type": "string", 93 | "minLength": 1 94 | }, 95 | "args": { 96 | "anyOf": [ 97 | { 98 | "type": "array", 99 | "minItems": 1, 100 | "items": { 101 | "type": "string", 102 | "minLength": 1 103 | } 104 | }, 105 | { 106 | "type": "object", 107 | "additionalProperties": true 108 | } 109 | ] 110 | } 111 | }, 112 | "required": ["thisArg"] 113 | } 114 | ], 115 | "description": "Closes the module code in a function with a given 'thisArg' and 'args'", 116 | "link": "https://webpack.js.org/loaders/imports-loader/#wrapper" 117 | }, 118 | "additionalCode": { 119 | "type": "string", 120 | "minLength": 1, 121 | "description": "Adds custom code as a preamble before the module's code.", 122 | "link": "https://webpack.js.org/loaders/imports-loader/#additionalcode" 123 | } 124 | }, 125 | "anyOf": [ 126 | { "required": ["imports"] }, 127 | { "required": ["wrapper"] }, 128 | { "required": ["additionalCode"] } 129 | ], 130 | "additionalProperties": false 131 | } 132 | -------------------------------------------------------------------------------- /test/validate-options.test.js: -------------------------------------------------------------------------------- 1 | import { compile, getCompiler } from "./helpers"; 2 | 3 | describe("validate options", () => { 4 | const tests = { 5 | type: { 6 | success: ["module", "commonjs"], 7 | failure: ["string", "", {}, []], 8 | }, 9 | imports: { 10 | success: [ 11 | "lib_1", 12 | "globalObject1.foo", 13 | ["globalObject1"], 14 | ["globalObject1.foo"], 15 | { 16 | moduleName: "jQuery", 17 | name: "$", 18 | }, 19 | { 20 | syntax: "default", 21 | moduleName: "jQuery", 22 | name: "lib", 23 | }, 24 | { 25 | syntax: "named", 26 | moduleName: "jQuery", 27 | name: "lib", 28 | }, 29 | { 30 | syntax: "named", 31 | moduleName: "jQuery", 32 | name: "lib", 33 | alias: "lib_alias", 34 | }, 35 | { 36 | syntax: "namespace", 37 | moduleName: "jQuery", 38 | name: "$", 39 | }, 40 | { 41 | syntax: "side-effects", 42 | moduleName: "jQuery", 43 | }, 44 | { 45 | syntax: "single", 46 | moduleName: "jQuery", 47 | name: "lib", 48 | }, 49 | { 50 | syntax: "multiple", 51 | moduleName: "jQuery", 52 | name: "lib", 53 | alias: "lib_alias", 54 | }, 55 | { 56 | syntax: "multiple", 57 | moduleName: "jQuery", 58 | name: "lib", 59 | }, 60 | { 61 | syntax: "pure", 62 | moduleName: "jQuery", 63 | }, 64 | ], 65 | failure: [ 66 | false, 67 | true, 68 | /test/, 69 | "", 70 | [], 71 | [""], 72 | {}, 73 | { 74 | type: "string", 75 | moduleName: "jQuery", 76 | list: false, 77 | }, 78 | { 79 | syntax: "default", 80 | moduleName: "jQuery", 81 | name: "lib", 82 | alias: "lib_alias", 83 | }, 84 | ], 85 | }, 86 | wrapper: { 87 | success: [ 88 | true, 89 | false, 90 | "window", 91 | { thisArg: "window" }, 92 | { thisArg: "window", args: ["foo", "bar"] }, 93 | { thisArg: "window", args: { foo: "bar" } }, 94 | ], 95 | failure: [ 96 | [], 97 | [""], 98 | /test/, 99 | {}, 100 | { unknown: true }, 101 | { thisArg: 1 }, 102 | { thisArg: "window", args: true }, 103 | { thisArg: "window", args: [1, "bar"] }, 104 | ], 105 | }, 106 | additionalCode: { 107 | success: ["var x = 2;"], 108 | failure: [false, true, /test/, [], [""], {}], 109 | }, 110 | unknown: { 111 | success: [], 112 | failure: [1, true, false, "test", /test/, [], {}, { foo: "bar" }], 113 | }, 114 | }; 115 | 116 | function stringifyValue(value) { 117 | if ( 118 | Array.isArray(value) || 119 | (value && typeof value === "object" && value.constructor === Object) 120 | ) { 121 | return JSON.stringify(value); 122 | } 123 | 124 | return value; 125 | } 126 | 127 | async function createTestCase(key, value, type) { 128 | it(`should ${ 129 | type === "success" ? "successfully validate" : "throw an error on" 130 | } the "${key}" option with "${stringifyValue(value)}" value`, async () => { 131 | let compiler; 132 | 133 | if (key === "type") { 134 | compiler = getCompiler("some-library.js", { 135 | [key]: value, 136 | wrapper: "window", 137 | }); 138 | } else { 139 | compiler = getCompiler("some-library.js", { 140 | [key]: value, 141 | }); 142 | } 143 | 144 | let stats; 145 | 146 | try { 147 | stats = await compile(compiler); 148 | } finally { 149 | if (type === "success") { 150 | const validationErrors = []; 151 | 152 | for (const error of stats.compilation.errors) { 153 | if (error.message.includes("ValidationError")) { 154 | validationErrors.push(error); 155 | } 156 | } 157 | 158 | expect(validationErrors).toHaveLength(0); 159 | } else if (type === "failure") { 160 | const { 161 | compilation: { errors }, 162 | } = stats; 163 | 164 | expect(errors).toHaveLength(1); 165 | expect(() => { 166 | throw new Error(errors[0].error.message); 167 | }).toThrowErrorMatchingSnapshot(); 168 | } 169 | } 170 | }); 171 | } 172 | 173 | for (const [key, values] of Object.entries(tests)) { 174 | for (const type of Object.keys(values)) { 175 | for (const value of values[type]) { 176 | createTestCase(key, value, type); 177 | } 178 | } 179 | } 180 | }); 181 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [5.0.0](https://github.com/webpack-contrib/imports-loader/compare/v4.0.1...v5.0.0) (2024-01-16) 6 | 7 | 8 | ### ⚠ BREAKING CHANGES 9 | 10 | * minimum supported Node.js version is `18.12.0` ([#188](https://github.com/webpack-contrib/imports-loader/issues/188)) ([44163ca](https://github.com/webpack-contrib/imports-loader/commit/44163ca0be6b85bc8a6934d5ea7967cd9cc5e847)) 11 | 12 | ### [4.0.1](https://github.com/webpack-contrib/imports-loader/compare/v4.0.0...v4.0.1) (2022-08-12) 13 | 14 | 15 | ### Bug Fixes 16 | 17 | * source map for other loaders ([#146](https://github.com/webpack-contrib/imports-loader/issues/146)) ([5a1f866](https://github.com/webpack-contrib/imports-loader/commit/5a1f866e9755452ad991960cd2db58c93da3aa77)) 18 | 19 | ## [4.0.0](https://github.com/webpack-contrib/imports-loader/compare/v3.1.1...v4.0.0) (2022-05-17) 20 | 21 | 22 | ### ⚠ BREAKING CHANGES 23 | 24 | * minimum supported `Node.js` version is `14.15.0` 25 | 26 | ### [3.1.1](https://github.com/webpack-contrib/imports-loader/compare/v3.1.0...v3.1.1) (2021-11-01) 27 | 28 | 29 | ### Bug Fixes 30 | 31 | * sourcemaps generation ([#113](https://github.com/webpack-contrib/imports-loader/issues/113)) ([6f948c2](https://github.com/webpack-contrib/imports-loader/commit/6f948c2b69d09de95a4f18ecdb4fdd5da03fb18b)) 32 | 33 | ## [3.1.0](https://github.com/webpack-contrib/imports-loader/compare/v3.0.1...v3.1.0) (2021-10-22) 34 | 35 | 36 | ### Features 37 | 38 | * output links and descriptions on errors ([#110](https://github.com/webpack-contrib/imports-loader/issues/110)) ([b86cb8b](https://github.com/webpack-contrib/imports-loader/commit/b86cb8b9d22a397261d4fd3599f9a850084dd8cf)) 39 | 40 | ### [3.0.1](https://github.com/webpack-contrib/imports-loader/compare/v3.0.0...v3.0.1) (2021-10-21) 41 | 42 | 43 | ### Bug Fixes 44 | 45 | * small perf improvement ([#860](https://github.com/webpack-contrib/imports-loader/issues/860)) ([7fabfab](https://github.com/webpack-contrib/imports-loader/commit/7fabfab4b5eaf3326af8fbfc554ec0b0b4927fa9)) 46 | 47 | ## [3.0.0](https://github.com/webpack-contrib/imports-loader/compare/v2.0.0...v3.0.0) (2021-05-18) 48 | 49 | ### ⚠ BREAKING CHANGES 50 | 51 | * minimum supported `Node.js` version is `12.13.0` 52 | 53 | ## [2.0.0](https://github.com/webpack-contrib/imports-loader/compare/v1.2.0...v2.0.0) (2021-02-01) 54 | 55 | ### ⚠ BREAKING CHANGES 56 | 57 | * minimum supported `webpack` version is `5` 58 | * inline syntax was changed: `[]` is no longer supported (i.e. `imports-loader?imports[]=default|jquery|$&imports[]=angular!./example.js`), please use `,` comma separator (i.e. `imports-loader?imports=default|jquery|$,angular!./example.js`) 59 | 60 | ## [1.2.0](https://github.com/webpack-contrib/imports-loader/compare/v1.1.0...v1.2.0) (2020-10-07) 61 | 62 | 63 | ### Features 64 | 65 | * add custom parameter names for wrapper args ([#86](https://github.com/webpack-contrib/imports-loader/issues/86)) ([4314ecd](https://github.com/webpack-contrib/imports-loader/commit/4314ecd2b853dec1a4f5a3fa76f8559167732cb5)) 66 | 67 | ## [1.1.0](https://github.com/webpack-contrib/imports-loader/compare/v1.0.0...v1.1.0) (2020-06-24) 68 | 69 | 70 | ### Features 71 | 72 | * "|" character can be used as delimiter for inline string syntax ([00697de](https://github.com/webpack-contrib/imports-loader/commit/00697dee3d0108bf632b3f82bd3adc62bd7aa907)) 73 | 74 | ## [1.0.0](https://github.com/webpack-contrib/imports-loader/compare/v0.8.0...v1.0.0) (2020-06-17) 75 | 76 | 77 | ### ⚠ BREAKING CHANGES 78 | 79 | * minimum supported Node.js version is `10.13` 80 | * minimum supported `webpack` version is `4` 81 | * `inline` syntax was changed, please [read](https://github.com/webpack-contrib/imports-loader#inline) 82 | * list of imported modules moved to the `imports` option, please [read](https://github.com/webpack-contrib/imports-loader#imports) 83 | * wrapper moved to the `wrapper` option, please [read](https://github.com/webpack-contrib/imports-loader#wrapper) 84 | * custom variables moved to the `additionalCode` option, please [read](https://github.com/webpack-contrib/imports-loader#additionalcode) 85 | * generates ES module default import by default (`import Foo from 'foo';`) 86 | 87 | ### Features 88 | 89 | * validate options 90 | * support webpack 5 91 | * implemented the `type` option (imports can be CommonsJS or ES module format) 92 | * implemented the ability to generate multiple import in CommonJS or ES module format 93 | * improved support of `inline` usage 94 | * allowed to adding arguments for wrapper 95 | * allowed to inject any custom code 96 | 97 | ### Bug Fixes 98 | 99 | * do not crash on invalid inline syntax 100 | * respect `'use strict';` 101 | 102 | 103 | 104 | # [0.8.0](https://github.com/webpack-contrib/imports-loader/compare/v0.7.1...v0.8.0) (2018-02-20) 105 | 106 | 107 | ### Features 108 | 109 | * allow loading nested objects onto existing libraries ([#45](https://github.com/webpack-contrib/imports-loader/issues/45)) ([44d6f48](https://github.com/webpack-contrib/imports-loader/commit/44d6f48)) 110 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | import path from "node:path"; 2 | 3 | import strip from "strip-comments"; 4 | 5 | const matchRelativePath = /^\.\.?[/\\]/; 6 | 7 | function isAbsolutePath(str) { 8 | return path.posix.isAbsolute(str) || path.win32.isAbsolute(str); 9 | } 10 | 11 | function isRelativePath(str) { 12 | return matchRelativePath.test(str); 13 | } 14 | 15 | // TODO simplify for the next major release 16 | function stringifyRequest(loaderContext, request) { 17 | if ( 18 | typeof loaderContext.utils !== "undefined" && 19 | typeof loaderContext.utils.contextify === "function" 20 | ) { 21 | return JSON.stringify( 22 | loaderContext.utils.contextify(loaderContext.context, request), 23 | ); 24 | } 25 | 26 | const splitted = request.split("!"); 27 | const { context } = loaderContext; 28 | 29 | return JSON.stringify( 30 | splitted 31 | .map((part) => { 32 | // First, separate singlePath from query, because the query might contain paths again 33 | const splittedPart = part.match(/^(.*?)(\?.*)/); 34 | const query = splittedPart ? splittedPart[2] : ""; 35 | let singlePath = splittedPart ? splittedPart[1] : part; 36 | 37 | if (isAbsolutePath(singlePath) && context) { 38 | singlePath = path.relative(context, singlePath); 39 | 40 | if (isAbsolutePath(singlePath)) { 41 | // If singlePath still matches an absolute path, singlePath was on a different drive than context. 42 | // In this case, we leave the path platform-specific without replacing any separators. 43 | // @see https://github.com/webpack/loader-utils/pull/14 44 | return singlePath + query; 45 | } 46 | 47 | if (isRelativePath(singlePath) === false) { 48 | // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules). 49 | singlePath = `./${singlePath}`; 50 | } 51 | } 52 | 53 | return singlePath.replaceAll("\\", "/") + query; 54 | }) 55 | .join("!"), 56 | ); 57 | } 58 | 59 | function forError(item) { 60 | return typeof item === "string" 61 | ? item 62 | : `\n${JSON.stringify(item, null, " ")}\n`; 63 | } 64 | 65 | function sourceHasUseStrict(source) { 66 | const str = strip(source).trim(); 67 | 68 | return str.startsWith("'use strict'") || str.startsWith('"use strict"'); 69 | } 70 | 71 | function splitCommand(command) { 72 | const result = command.split("|").flatMap((item) => item.split(" ")); 73 | 74 | for (const item of result) { 75 | if (!item) { 76 | throw new Error( 77 | `Invalid command "${item}" in "${command}" for imports. There must be only one separator: " ", or "|"`, 78 | ); 79 | } 80 | } 81 | 82 | return result; 83 | } 84 | 85 | function resolveImports(type, item) { 86 | const defaultSyntax = type === "module" ? "default" : "single"; 87 | 88 | let result; 89 | 90 | if (typeof item === "string") { 91 | const noWhitespaceItem = item.trim(); 92 | 93 | if (noWhitespaceItem.length === 0) { 94 | throw new Error(`Invalid "${item}" value for import`); 95 | } 96 | 97 | const splittedItem = splitCommand(noWhitespaceItem); 98 | 99 | if (splittedItem.length > 4) { 100 | throw new Error(`Invalid "${item}" value for import`); 101 | } 102 | 103 | if (splittedItem.length === 1) { 104 | result = { 105 | syntax: defaultSyntax, 106 | moduleName: splittedItem[0], 107 | name: splittedItem[0], 108 | 109 | alias: undefined, 110 | }; 111 | } else { 112 | result = { 113 | syntax: splittedItem[0], 114 | moduleName: splittedItem[1], 115 | name: splittedItem[2], 116 | 117 | alias: splittedItem[3] || undefined, 118 | }; 119 | } 120 | } else { 121 | result = { syntax: defaultSyntax, ...item }; 122 | } 123 | 124 | if (result.syntax === defaultSyntax && typeof result.name === "undefined") { 125 | result.name = result.moduleName; 126 | } 127 | 128 | if ( 129 | ["default", "side-effects", "single", "pure"].includes(result.syntax) && 130 | typeof result.alias !== "undefined" 131 | ) { 132 | throw new Error( 133 | `The "${result.syntax}" syntax does not support "${ 134 | result.alias 135 | }" alias in "${forError(item)}" value`, 136 | ); 137 | } 138 | 139 | if ( 140 | ["side-effects", "pure"].includes(result.syntax) && 141 | typeof result.name !== "undefined" 142 | ) { 143 | throw new Error( 144 | `The "${result.syntax}" syntax does not support "${ 145 | result.name 146 | }" name in "${forError(item)}" value`, 147 | ); 148 | } 149 | 150 | if ( 151 | ["default", "namespace", "named", "side-effects"].includes(result.syntax) && 152 | type === "commonjs" 153 | ) { 154 | throw new Error( 155 | `The "${type}" type does not support the "${ 156 | result.syntax 157 | }" syntax in "${forError(item)}" value`, 158 | ); 159 | } 160 | 161 | if ( 162 | ["single", "multiple", "pure"].includes(result.syntax) && 163 | type === "module" 164 | ) { 165 | throw new Error( 166 | `The "${type}" format does not support the "${ 167 | result.syntax 168 | }" syntax in "${forError(item)}" value`, 169 | ); 170 | } 171 | 172 | if ( 173 | ["namespace", "named", "multiple"].includes(result.syntax) && 174 | typeof result.name === "undefined" 175 | ) { 176 | throw new Error( 177 | `The "${result.syntax}" syntax needs the "name" option in "${forError( 178 | item, 179 | )}" value`, 180 | ); 181 | } 182 | 183 | return result; 184 | } 185 | 186 | function getIdentifiers(array) { 187 | return array.reduce((accumulator, item) => { 188 | if (typeof item.alias !== "undefined") { 189 | accumulator.push({ type: "alias", value: item.alias }); 190 | 191 | return accumulator; 192 | } 193 | 194 | if (typeof item.name !== "undefined") { 195 | accumulator.push({ type: "name", value: item.name }); 196 | } 197 | 198 | return accumulator; 199 | }, []); 200 | } 201 | 202 | function duplicateBy(array, key) { 203 | return array.filter((a, aIndex) => 204 | array.some((b, bIndex) => b[key] === a[key] && aIndex !== bIndex), 205 | ); 206 | } 207 | 208 | function getImports(type, imports) { 209 | const importItems = 210 | typeof imports === "string" && imports.includes(",") 211 | ? imports.split(",") 212 | : imports; 213 | 214 | const result = 215 | typeof importItems === "string" 216 | ? [resolveImports(type, importItems)] 217 | : [importItems].flat().map((item) => resolveImports(type, item)); 218 | 219 | const identifiers = getIdentifiers(result); 220 | const duplicates = duplicateBy(identifiers, "value"); 221 | 222 | if (duplicates.length > 0) { 223 | throw new Error( 224 | `Duplicate ${duplicates 225 | .map((identifier) => `"${identifier.value}" (as "${identifier.type}")`) 226 | .join(", ")} identifiers found in "\n${JSON.stringify( 227 | importItems, 228 | null, 229 | " ", 230 | )}\n" value`, 231 | ); 232 | } 233 | 234 | const sortedResults = Object.create(null); 235 | 236 | for (const item of result) { 237 | const { moduleName } = item; 238 | 239 | if (!sortedResults[moduleName]) { 240 | sortedResults[moduleName] = []; 241 | } 242 | 243 | const { syntax, name, alias } = item; 244 | 245 | sortedResults[moduleName].push({ syntax, name, alias }); 246 | } 247 | 248 | return sortedResults; 249 | } 250 | 251 | function renderImports(loaderContext, type, moduleName, imports) { 252 | let code = ""; 253 | 254 | if (type === "commonjs") { 255 | const pure = imports.filter(({ syntax }) => syntax === "pure"); 256 | 257 | // Pure 258 | if (pure.length > 0) { 259 | for (const [i, _] of pure.entries()) { 260 | const needNewline = i < pure.length - 1 ? "\n" : ""; 261 | 262 | code += `require(${stringifyRequest( 263 | loaderContext, 264 | moduleName, 265 | )});${needNewline}`; 266 | } 267 | } 268 | 269 | const singleImports = imports.filter(({ syntax }) => syntax === "single"); 270 | 271 | // Single 272 | if (singleImports.length > 0) { 273 | code += pure.length > 0 ? "\n" : ""; 274 | 275 | for (const [i, singleImport] of singleImports.entries()) { 276 | const { name } = singleImport; 277 | const needNewline = i < singleImports.length - 1 ? "\n" : ""; 278 | 279 | code += `var ${name} = require(${stringifyRequest( 280 | loaderContext, 281 | moduleName, 282 | )});${needNewline}`; 283 | } 284 | } 285 | 286 | const multipleImports = imports.filter( 287 | ({ syntax }) => syntax === "multiple", 288 | ); 289 | 290 | // Multiple 291 | if (multipleImports.length > 0) { 292 | code += pure.length > 0 || singleImports.length > 0 ? "\n" : ""; 293 | code += "var { "; 294 | 295 | for (const [i, multipleImport] of multipleImports.entries()) { 296 | const needComma = i > 0 ? ", " : ""; 297 | const { name, alias } = multipleImport; 298 | const separator = ": "; 299 | 300 | code += alias 301 | ? `${needComma}${name}${separator}${alias}` 302 | : `${needComma}${name}`; 303 | } 304 | 305 | code += ` } = require(${stringifyRequest(loaderContext, moduleName)});`; 306 | } 307 | 308 | return code; 309 | } 310 | 311 | const sideEffectsImports = imports.filter( 312 | ({ syntax }) => syntax === "side-effects", 313 | ); 314 | 315 | // Side-effects 316 | if (sideEffectsImports.length > 0) { 317 | for (const [i, _] of sideEffectsImports.entries()) { 318 | const needNewline = i < sideEffectsImports.length - 1 ? "\n" : ""; 319 | 320 | code += `import ${stringifyRequest( 321 | loaderContext, 322 | moduleName, 323 | )};${needNewline}`; 324 | } 325 | 326 | return code; 327 | } 328 | 329 | const defaultImports = imports.filter(({ syntax }) => syntax === "default"); 330 | const namedImports = imports.filter(({ syntax }) => syntax === "named"); 331 | const namespaceImports = imports.filter( 332 | ({ syntax }) => syntax === "namespace", 333 | ); 334 | 335 | // Default 336 | if (defaultImports.length > 0) { 337 | for (const [i, defaultImport] of defaultImports.entries()) { 338 | const { name } = defaultImport; 339 | const needNewline = i < defaultImports.length - 1 ? "\n" : ""; 340 | 341 | code += `import ${name} from ${stringifyRequest( 342 | loaderContext, 343 | moduleName, 344 | )};${needNewline}`; 345 | } 346 | } 347 | 348 | // Named 349 | if (namedImports.length > 0) { 350 | code += defaultImports.length > 0 ? "\n" : ""; 351 | code += "import { "; 352 | 353 | for (const [i, namedImport] of namedImports.entries()) { 354 | const needComma = i > 0 ? ", " : ""; 355 | const { name, alias } = namedImport; 356 | const separator = " as "; 357 | 358 | code += alias 359 | ? `${needComma}${name}${separator}${alias}` 360 | : `${needComma}${name}`; 361 | } 362 | 363 | code += ` } from ${stringifyRequest(loaderContext, moduleName)};`; 364 | } 365 | 366 | // Namespace 367 | if (namespaceImports.length > 0) { 368 | code += defaultImports.length > 0 || namedImports.length > 0 ? "\n" : ""; 369 | 370 | for (const [i, namespaceImport] of namespaceImports.entries()) { 371 | const { name } = namespaceImport; 372 | const needNewline = i < namespaceImports.length - 1 ? "\n" : ""; 373 | 374 | code += `import * as ${name} from ${stringifyRequest( 375 | loaderContext, 376 | moduleName, 377 | )};${needNewline}`; 378 | } 379 | } 380 | 381 | return code; 382 | } 383 | 384 | export { getImports, renderImports, sourceHasUseStrict }; 385 | -------------------------------------------------------------------------------- /test/__snapshots__/validate-options.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`validate options should throw an error on the "additionalCode" option with "/test/" value 1`] = ` 4 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 5 | - options.additionalCode should be a non-empty string. 6 | -> Adds custom code as a preamble before the module's code. 7 | -> Read more at https://webpack.js.org/loaders/imports-loader/#additionalcode" 8 | `; 9 | 10 | exports[`validate options should throw an error on the "additionalCode" option with "[""]" value 1`] = ` 11 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 12 | - options.additionalCode should be a non-empty string. 13 | -> Adds custom code as a preamble before the module's code. 14 | -> Read more at https://webpack.js.org/loaders/imports-loader/#additionalcode" 15 | `; 16 | 17 | exports[`validate options should throw an error on the "additionalCode" option with "[]" value 1`] = ` 18 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 19 | - options.additionalCode should be a non-empty string. 20 | -> Adds custom code as a preamble before the module's code. 21 | -> Read more at https://webpack.js.org/loaders/imports-loader/#additionalcode" 22 | `; 23 | 24 | exports[`validate options should throw an error on the "additionalCode" option with "{}" value 1`] = ` 25 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 26 | - options.additionalCode should be a non-empty string. 27 | -> Adds custom code as a preamble before the module's code. 28 | -> Read more at https://webpack.js.org/loaders/imports-loader/#additionalcode" 29 | `; 30 | 31 | exports[`validate options should throw an error on the "additionalCode" option with "false" value 1`] = ` 32 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 33 | - options.additionalCode should be a non-empty string. 34 | -> Adds custom code as a preamble before the module's code. 35 | -> Read more at https://webpack.js.org/loaders/imports-loader/#additionalcode" 36 | `; 37 | 38 | exports[`validate options should throw an error on the "additionalCode" option with "true" value 1`] = ` 39 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 40 | - options.additionalCode should be a non-empty string. 41 | -> Adds custom code as a preamble before the module's code. 42 | -> Read more at https://webpack.js.org/loaders/imports-loader/#additionalcode" 43 | `; 44 | 45 | exports[`validate options should throw an error on the "imports" option with "" value 1`] = ` 46 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 47 | - options.imports should be one of these: 48 | non-empty string | non-empty string | object { moduleName, syntax?, name?, alias? } | [non-empty string | object { moduleName, syntax?, name?, alias? }, ...] (should not have fewer than 1 item) 49 | Details: 50 | * options.imports should be a non-empty string. 51 | -> Allows to use a string to describe an import. 52 | -> Read more at https://webpack.js.org/loaders/imports-loader/#string 53 | * options.imports should be a non-empty string. 54 | -> Allows to use a string to describe an import. 55 | -> Read more at https://webpack.js.org/loaders/imports-loader/#string" 56 | `; 57 | 58 | exports[`validate options should throw an error on the "imports" option with "/test/" value 1`] = ` 59 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 60 | - options.imports misses the property 'moduleName'. Should be: 61 | non-empty string" 62 | `; 63 | 64 | exports[`validate options should throw an error on the "imports" option with "[""]" value 1`] = ` 65 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 66 | - options.imports[0] should be a non-empty string. 67 | -> Allows to use a string to describe an import. 68 | -> Read more at https://webpack.js.org/loaders/imports-loader/#string" 69 | `; 70 | 71 | exports[`validate options should throw an error on the "imports" option with "[]" value 1`] = ` 72 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 73 | - options.imports should be a non-empty array." 74 | `; 75 | 76 | exports[`validate options should throw an error on the "imports" option with "{"syntax":"default","moduleName":"jQuery","name":"lib","alias":"lib_alias"}" value 1`] = ` 77 | "The "default" syntax does not support "lib_alias" alias in " 78 | { 79 | "syntax": "default", 80 | "moduleName": "jQuery", 81 | "name": "lib", 82 | "alias": "lib_alias" 83 | } 84 | " value" 85 | `; 86 | 87 | exports[`validate options should throw an error on the "imports" option with "{"type":"string","moduleName":"jQuery","list":false}" value 1`] = ` 88 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 89 | - options.imports should be one of these: 90 | non-empty string | non-empty string | object { moduleName, syntax?, name?, alias? } | [non-empty string | object { moduleName, syntax?, name?, alias? }, ...] (should not have fewer than 1 item) 91 | Details: 92 | * options.imports has an unknown property 'type'. These properties are valid: 93 | object { moduleName, syntax?, name?, alias? } 94 | -> Allows to use an object to describe an import. 95 | -> Read more at https://webpack.js.org/loaders/imports-loader/#object 96 | * options.imports has an unknown property 'list'. These properties are valid: 97 | object { moduleName, syntax?, name?, alias? } 98 | -> Allows to use an object to describe an import. 99 | -> Read more at https://webpack.js.org/loaders/imports-loader/#object" 100 | `; 101 | 102 | exports[`validate options should throw an error on the "imports" option with "{}" value 1`] = ` 103 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 104 | - options.imports misses the property 'moduleName'. Should be: 105 | non-empty string" 106 | `; 107 | 108 | exports[`validate options should throw an error on the "imports" option with "false" value 1`] = ` 109 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 110 | - options.imports should be one of these: 111 | non-empty string | non-empty string | object { moduleName, syntax?, name?, alias? } | [non-empty string | object { moduleName, syntax?, name?, alias? }, ...] (should not have fewer than 1 item) 112 | Details: 113 | * options.imports should be one of these: 114 | non-empty string | object { moduleName, syntax?, name?, alias? } 115 | Details: 116 | * options.imports should be a non-empty string. 117 | -> Allows to use a string to describe an import. 118 | -> Read more at https://webpack.js.org/loaders/imports-loader/#string 119 | * options.imports should be a non-empty string. 120 | -> Allows to use a string to describe an import. 121 | -> Read more at https://webpack.js.org/loaders/imports-loader/#string 122 | * options.imports should be an object: 123 | object { moduleName, syntax?, name?, alias? } 124 | -> Allows to use an object to describe an import. 125 | -> Read more at https://webpack.js.org/loaders/imports-loader/#object 126 | * options.imports should be an array: 127 | [non-empty string | object { moduleName, syntax?, name?, alias? }, ...] (should not have fewer than 1 item)" 128 | `; 129 | 130 | exports[`validate options should throw an error on the "imports" option with "true" value 1`] = ` 131 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 132 | - options.imports should be one of these: 133 | non-empty string | non-empty string | object { moduleName, syntax?, name?, alias? } | [non-empty string | object { moduleName, syntax?, name?, alias? }, ...] (should not have fewer than 1 item) 134 | Details: 135 | * options.imports should be one of these: 136 | non-empty string | object { moduleName, syntax?, name?, alias? } 137 | Details: 138 | * options.imports should be a non-empty string. 139 | -> Allows to use a string to describe an import. 140 | -> Read more at https://webpack.js.org/loaders/imports-loader/#string 141 | * options.imports should be a non-empty string. 142 | -> Allows to use a string to describe an import. 143 | -> Read more at https://webpack.js.org/loaders/imports-loader/#string 144 | * options.imports should be an object: 145 | object { moduleName, syntax?, name?, alias? } 146 | -> Allows to use an object to describe an import. 147 | -> Read more at https://webpack.js.org/loaders/imports-loader/#object 148 | * options.imports should be an array: 149 | [non-empty string | object { moduleName, syntax?, name?, alias? }, ...] (should not have fewer than 1 item)" 150 | `; 151 | 152 | exports[`validate options should throw an error on the "type" option with "" value 1`] = ` 153 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 154 | - options.type should be one of these: 155 | "module" | "commonjs" 156 | -> Format of generated exports. 157 | -> Read more at https://github.com/webpack/imports-loader#type" 158 | `; 159 | 160 | exports[`validate options should throw an error on the "type" option with "[]" value 1`] = ` 161 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 162 | - options.type should be one of these: 163 | "module" | "commonjs" 164 | -> Format of generated exports. 165 | -> Read more at https://github.com/webpack/imports-loader#type" 166 | `; 167 | 168 | exports[`validate options should throw an error on the "type" option with "{}" value 1`] = ` 169 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 170 | - options.type should be one of these: 171 | "module" | "commonjs" 172 | -> Format of generated exports. 173 | -> Read more at https://github.com/webpack/imports-loader#type" 174 | `; 175 | 176 | exports[`validate options should throw an error on the "type" option with "string" value 1`] = ` 177 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 178 | - options.type should be one of these: 179 | "module" | "commonjs" 180 | -> Format of generated exports. 181 | -> Read more at https://github.com/webpack/imports-loader#type" 182 | `; 183 | 184 | exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` 185 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 186 | - options has an unknown property 'unknown'. These properties are valid: 187 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 188 | -> Options for imports-loader" 189 | `; 190 | 191 | exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` 192 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 193 | - options has an unknown property 'unknown'. These properties are valid: 194 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 195 | -> Options for imports-loader" 196 | `; 197 | 198 | exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` 199 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 200 | - options has an unknown property 'unknown'. These properties are valid: 201 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 202 | -> Options for imports-loader" 203 | `; 204 | 205 | exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` 206 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 207 | - options has an unknown property 'unknown'. These properties are valid: 208 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 209 | -> Options for imports-loader" 210 | `; 211 | 212 | exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` 213 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 214 | - options has an unknown property 'unknown'. These properties are valid: 215 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 216 | -> Options for imports-loader" 217 | `; 218 | 219 | exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` 220 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 221 | - options has an unknown property 'unknown'. These properties are valid: 222 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 223 | -> Options for imports-loader" 224 | `; 225 | 226 | exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` 227 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 228 | - options has an unknown property 'unknown'. These properties are valid: 229 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 230 | -> Options for imports-loader" 231 | `; 232 | 233 | exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` 234 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 235 | - options has an unknown property 'unknown'. These properties are valid: 236 | object { imports, … } | object { wrapper, … } | object { additionalCode, … } 237 | -> Options for imports-loader" 238 | `; 239 | 240 | exports[`validate options should throw an error on the "wrapper" option with "/test/" value 1`] = ` 241 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 242 | - options.wrapper misses the property 'thisArg'. Should be: 243 | non-empty string" 244 | `; 245 | 246 | exports[`validate options should throw an error on the "wrapper" option with "[""]" value 1`] = ` 247 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 248 | - options.wrapper should be one of these: 249 | boolean | non-empty string | object { thisArg, args? } 250 | -> Closes the module code in a function with a given 'thisArg' and 'args' 251 | -> Read more at https://webpack.js.org/loaders/imports-loader/#wrapper 252 | Details: 253 | * options.wrapper should be a boolean. 254 | * options.wrapper should be a non-empty string. 255 | * options.wrapper should be an object: 256 | object { thisArg, args? }" 257 | `; 258 | 259 | exports[`validate options should throw an error on the "wrapper" option with "[]" value 1`] = ` 260 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 261 | - options.wrapper should be one of these: 262 | boolean | non-empty string | object { thisArg, args? } 263 | -> Closes the module code in a function with a given 'thisArg' and 'args' 264 | -> Read more at https://webpack.js.org/loaders/imports-loader/#wrapper 265 | Details: 266 | * options.wrapper should be a boolean. 267 | * options.wrapper should be a non-empty string. 268 | * options.wrapper should be an object: 269 | object { thisArg, args? }" 270 | `; 271 | 272 | exports[`validate options should throw an error on the "wrapper" option with "{"thisArg":"window","args":[1,"bar"]}" value 1`] = ` 273 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 274 | - options.wrapper.args[0] should be a non-empty string." 275 | `; 276 | 277 | exports[`validate options should throw an error on the "wrapper" option with "{"thisArg":"window","args":true}" value 1`] = ` 278 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 279 | - options.wrapper should be one of these: 280 | boolean | non-empty string | object { thisArg, args? } 281 | -> Closes the module code in a function with a given 'thisArg' and 'args' 282 | -> Read more at https://webpack.js.org/loaders/imports-loader/#wrapper 283 | Details: 284 | * options.wrapper.args should be one of these: 285 | [non-empty string, ...] (should not have fewer than 1 item) | object { … } 286 | Details: 287 | * options.wrapper.args should be an array: 288 | [non-empty string, ...] (should not have fewer than 1 item) 289 | * options.wrapper.args should be an object: 290 | object { … }" 291 | `; 292 | 293 | exports[`validate options should throw an error on the "wrapper" option with "{"thisArg":1}" value 1`] = ` 294 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 295 | - options.wrapper.thisArg should be a non-empty string." 296 | `; 297 | 298 | exports[`validate options should throw an error on the "wrapper" option with "{"unknown":true}" value 1`] = ` 299 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 300 | - options.wrapper should be one of these: 301 | boolean | non-empty string | object { thisArg, args? } 302 | -> Closes the module code in a function with a given 'thisArg' and 'args' 303 | -> Read more at https://webpack.js.org/loaders/imports-loader/#wrapper 304 | Details: 305 | * options.wrapper misses the property 'thisArg'. Should be: 306 | non-empty string 307 | * options.wrapper has an unknown property 'unknown'. These properties are valid: 308 | object { thisArg, args? }" 309 | `; 310 | 311 | exports[`validate options should throw an error on the "wrapper" option with "{}" value 1`] = ` 312 | "Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema. 313 | - options.wrapper misses the property 'thisArg'. Should be: 314 | non-empty string" 315 | `; 316 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 |
6 | 7 | [![npm][npm]][npm-url] 8 | [![node][node]][node-url] 9 | [![tests][tests]][tests-url] 10 | [![cover][cover]][cover-url] 11 | [![discussion][discussion]][discussion-url] 12 | [![size][size]][size-url] 13 | 14 | # imports-loader 15 | 16 | The imports loader allows you to use modules that depend on specific global variables. 17 | 18 | This is especially useful for third-party modules that rely on global variables like `$` or where `this` is expected to be the `window` object. The imports loader can add the necessary `require('whatever')` calls, so those modules work with webpack. 19 | 20 | For further hints on compatibility issues, see the official webpack documentation on [Shimming](https://webpack.js.org/guides/shimming/). 21 | 22 | > [!WARNING] 23 | > 24 | > By default, this loader generates ES module named syntax. 25 | 26 | > [!WARNING] 27 | > 28 | > Be careful, existing imports (`import`/`require`) in the original code and importing new values can cause failure. 29 | 30 | ## Getting Started 31 | 32 | To begin, you'll need to install `imports-loader`: 33 | 34 | ```console 35 | npm install imports-loader --save-dev 36 | ``` 37 | 38 | or 39 | 40 | ```console 41 | yarn add -D imports-loader 42 | ``` 43 | 44 | or 45 | 46 | ```console 47 | pnpm add -D imports-loader 48 | ``` 49 | 50 | Given you have this file: 51 | 52 | **example.js** 53 | 54 | ```js 55 | $("img").doSomeAwesomeJqueryPluginStuff(); 56 | ``` 57 | 58 | Then you can inject the `jquery` value into the module by configuring the `imports-loader` using two approaches. 59 | 60 | ### Inline 61 | 62 | The `|` or `%20` (space) allow to separate the `syntax`, `moduleName`, `name` and `alias` of import. 63 | The documentation and syntax examples can be read [here](#syntax). 64 | 65 | > [!WARNING] 66 | > 67 | > `%20` represents a `space` in a query string, because you can't use spaces in URLs 68 | 69 | ```js 70 | // Alternative syntax: 71 | // 72 | // import myLib from 'imports-loader?imports=default%20jquery%20$!./example.js'; 73 | // 74 | // `%20` is space in a query string, equivalently `default jquery $` 75 | import myLib from "imports-loader?imports=default|jquery|$!./example.js"; 76 | // Adds the following code to the beginning of example.js: 77 | // 78 | // import $ from "jquery"; 79 | // 80 | // ... 81 | // Code 82 | // ... 83 | ``` 84 | 85 | ```js 86 | import myLib from "imports-loader?imports=default|jquery|$,angular!./example.js"; 87 | // `|` is separator in a query string, equivalently `default|jquery|$` and `angular` 88 | // Adds the following code to the beginning of example.js: 89 | // 90 | // import $ from "jquery"; 91 | // import angular from "angular"; 92 | // 93 | // ... 94 | // Code 95 | // ... 96 | ``` 97 | 98 | ```js 99 | import myLib from "imports-loader?imports=named|library|myMethod,angular!./example.js"; 100 | // `|` is separator in a query string, equivalently `named|library|myMethod` and `angular` 101 | // Adds the following code to the beginning of example.js: 102 | // 103 | // import { myMethod } from "library"; 104 | // import angular from "angular"; 105 | // 106 | // ... 107 | // Code 108 | // ... 109 | ``` 110 | 111 | ```js 112 | const myLib = require("imports-loader?type=commonjs&imports=single|jquery|$,angular!./example.js"); 113 | // `|` is separator in a query string, equivalently `single|jquery|$` and `angular` 114 | // Adds the following code to the beginning of example.js: 115 | // 116 | // var $ = require("jquery"); 117 | // var angular = require("angular"); 118 | // 119 | // ... 120 | // Code 121 | // ... 122 | ``` 123 | 124 | ```js 125 | const myLib = require("imports-loader?type=commonjs&imports=single|myLib|myMethod&wrapper=window&!./example.js"); 126 | // `|` is separator in a query string, equivalently `single|myLib|myMethod` and `angular` 127 | // Adds the following code to the example.js: 128 | // 129 | // const myMethod = require('myLib'); 130 | // 131 | // (function () { 132 | // ... 133 | // Code 134 | // ... 135 | // }.call(window)); 136 | ``` 137 | 138 | ```js 139 | import myLib from "imports-loader?additionalCode=var%20myVariable%20=%20false;!./example.js"; 140 | // Adds the following code to the beginning of example.js: 141 | // 142 | // var myVariable = false; 143 | // 144 | // ... 145 | // Code 146 | // ... 147 | ``` 148 | 149 | ### Using Configuration 150 | 151 | **webpack.config.js** 152 | 153 | ```js 154 | module.exports = { 155 | module: { 156 | rules: [ 157 | { 158 | // You can use `regexp` 159 | // test: /example\.js$/ 160 | test: require.resolve("example.js"), 161 | use: [ 162 | { 163 | loader: "imports-loader", 164 | options: { 165 | imports: [ 166 | "default jquery $", 167 | "default lib_2 lib_2_default", 168 | "named lib_3 lib2_method_1", 169 | "named lib_3 lib2_method_2 lib_2_method_2_short", 170 | "namespace lib_4 my_namespace", 171 | "side-effects lib_5", 172 | { 173 | syntax: "default", 174 | moduleName: "angular", 175 | name: "angular", 176 | }, 177 | ], 178 | }, 179 | }, 180 | ], 181 | }, 182 | ], 183 | }, 184 | }; 185 | ``` 186 | 187 | Generate output: 188 | 189 | 190 | 191 | ```js 192 | import angular from "angular"; 193 | import $ from "jquery"; 194 | import lib_2_default from "lib_2"; 195 | import { lib2_method_1, lib2_method_2 as lib_2_method_2_short } from "lib_3"; 196 | import * as my_namespace from "lib_4"; 197 | import "lib_5"; 198 | ``` 199 | 200 | Finally, run `webpack` using the method you normally use (e.g., via CLI or an npm script). 201 | 202 | ## Options 203 | 204 | - **[`type`](#type)** 205 | - **[`imports`](#imports)** 206 | - **[`wrapper`](#wrapper)** 207 | - **[`additionalCode`](#additionalcode)** 208 | 209 | ### `type` 210 | 211 | Type: 212 | 213 | ```ts 214 | type type = string; 215 | ``` 216 | 217 | Default: `module` 218 | 219 | Defines the format of generated exports. 220 | 221 | Possible values: 222 | 223 | - `commonjs` (CommonJS module syntax) 224 | - `module` (ES module syntax). 225 | 226 | #### `commonjs` 227 | 228 | **webpack.config.js** 229 | 230 | ```js 231 | module.exports = { 232 | module: { 233 | rules: [ 234 | { 235 | test: require.resolve("example.js"), 236 | loader: "imports-loader", 237 | options: { 238 | syntax: "default", 239 | type: "commonjs", 240 | imports: "Foo", 241 | }, 242 | }, 243 | ], 244 | }, 245 | }; 246 | ``` 247 | 248 | Generate output: 249 | 250 | ```js 251 | const Foo = require("Foo"); 252 | 253 | // ... 254 | // Code 255 | // ... 256 | ``` 257 | 258 | #### `module` 259 | 260 | **webpack.config.js** 261 | 262 | ```js 263 | module.exports = { 264 | module: { 265 | rules: [ 266 | { 267 | test: require.resolve("example.js"), 268 | loader: "imports-loader", 269 | options: { 270 | type: "module", 271 | imports: "Foo", 272 | }, 273 | }, 274 | ], 275 | }, 276 | }; 277 | ``` 278 | 279 | Generate output: 280 | 281 | ```js 282 | import Foo from "Foo"; 283 | 284 | // ... 285 | // Code 286 | // ... 287 | ``` 288 | 289 | ### `imports` 290 | 291 | Type: 292 | 293 | ```ts 294 | type imports = 295 | | string 296 | | { 297 | syntax: 298 | | "default" 299 | | "named" 300 | | "namespace" 301 | | "side-effects" 302 | | "single" 303 | | "multiple" 304 | | "pure"; 305 | moduleName: string; 306 | name: string; 307 | alias: string; 308 | } 309 | | ( 310 | | string 311 | | { 312 | syntax: 313 | | "default" 314 | | "named" 315 | | "namespace" 316 | | "side-effects" 317 | | "single" 318 | | "multiple" 319 | | "pure"; 320 | moduleName: string; 321 | name: string; 322 | alias: string; 323 | } 324 | )[]; 325 | ``` 326 | 327 | Default: `undefined` 328 | 329 | List of imports. 330 | 331 | #### `string` 332 | 333 | Allows to use a string to describe an export. 334 | 335 | ##### `Syntax` 336 | 337 | The `|` or `%20` (space) allow to separate the `syntax`, `moduleName`, `name` and `alias` of import. 338 | 339 | String syntax - `[[syntax] [moduleName] [name] [alias]]` or `[[syntax]|[moduleName]|[name]|[alias]]`, where: 340 | 341 | - `[syntax]` (**may be omitted**): 342 | - if `type` is `module`- can be `default`, `named`, `namespace` or `side-effects`, the default value is `default`. 343 | - if `type` is `commonjs`- can be `single`, `multiple` or `pure`, the default value is `single`. 344 | 345 | - `[moduleName]` - name of an imported module (**required**) 346 | - `[name]` - name of an imported value (**required**) 347 | - `[alias]` - alias of an imported value (**may be omitted**) 348 | 349 | Examples: 350 | 351 | If type `module`: 352 | 353 | - `[Foo]` - generates `import Foo from "Foo";`. 354 | - `[default Foo]` - generates `import Foo from "Foo";`. 355 | - `[default ./my-lib Foo]` - generates `import Foo from "./my-lib";`. 356 | - `[named Foo FooA]` - generates `import { FooA } from "Foo";`. 357 | - `[named Foo FooA Bar]` - generates `import { FooA as Bar } from "Foo";`. 358 | - `[namespace Foo FooA]` - generates `import * as FooA from "Foo";`. 359 | - `[side-effects Foo]` - generates `import "Foo";`. 360 | 361 | If type `commonjs`: 362 | 363 | - `[Foo]` - generates `const Foo = require("Foo");`. 364 | - `[single Foo]` - generates `const Foo = require("Foo");`. 365 | - `[single ./my-lib Foo]` - generates `const Foo = require("./my-lib");`. 366 | - `[multiple Foo FooA Bar]` - generates `const { FooA: Bar } = require("Foo");`. 367 | - `[pure Foo]` - generates `require("Foo");`. 368 | 369 | > [!WARNING] 370 | > 371 | > You need to set `type: "commonjs"` to use `single`, `multiple` and `pure` syntaxes. 372 | 373 | > [!WARNING] 374 | > 375 | > Aliases can't be used together with `default`, `namespace`, `side-effects`, `single` and `pure` syntaxes. 376 | 377 | ###### Examples 378 | 379 | ###### ES Module Default Import 380 | 381 | **webpack.config.js** 382 | 383 | ```js 384 | module.exports = { 385 | module: { 386 | rules: [ 387 | { 388 | test: require.resolve("./path/to/example.js"), 389 | loader: "imports-loader", 390 | options: { 391 | imports: "default lib myName", 392 | }, 393 | }, 394 | ], 395 | }, 396 | }; 397 | ``` 398 | 399 | Generate output: 400 | 401 | ```js 402 | import myName from "lib"; 403 | 404 | // ... 405 | // Code 406 | // ... 407 | ``` 408 | 409 | ###### CommonJS Single Import 410 | 411 | **webpack.config.js** 412 | 413 | ```js 414 | module.exports = { 415 | module: { 416 | rules: [ 417 | { 418 | test: require.resolve("./path/to/example.js"), 419 | loader: "imports-loader", 420 | options: { 421 | type: "commonjs", 422 | imports: "single lib myName", 423 | }, 424 | }, 425 | ], 426 | }, 427 | }; 428 | ``` 429 | 430 | Generate output: 431 | 432 | ```js 433 | const myName = require("lib"); 434 | 435 | // ... 436 | // Code 437 | // ... 438 | ``` 439 | 440 | #### `object` 441 | 442 | Allows to use an object to describe an import. 443 | 444 | Properties: 445 | 446 | - `syntax`: 447 | - if `type` is `module`- can be `default`, `named`, `namespace` or `side-effects` 448 | - if `type` is `commonjs`- can be `single`, `multiple` or `pure` 449 | 450 | - `moduleName` - name of an imported module (**required**) 451 | - `name` - name of an imported value (**required**) 452 | - `alias` - alias of an imported value (**may be omitted**) 453 | 454 | > [!WARNING] 455 | > 456 | > Alias can't be used together with `default`, `namespace`, `side-effects`, `single` and `pure` syntaxes. 457 | 458 | ##### Examples 459 | 460 | **webpack.config.js** 461 | 462 | ```js 463 | module.exports = { 464 | module: { 465 | rules: [ 466 | { 467 | test: require.resolve("example.js"), 468 | use: [ 469 | { 470 | loader: "imports-loader", 471 | options: { 472 | imports: { 473 | syntax: "named", 474 | moduleName: "lib_2", 475 | name: "lib2_method_2", 476 | alias: "lib_2_method_2_alias", 477 | }, 478 | }, 479 | }, 480 | ], 481 | }, 482 | ], 483 | }, 484 | }; 485 | ``` 486 | 487 | Generate output: 488 | 489 | ```js 490 | import { lib2_method_2 as lib_2_method_2_alias } from "lib_2"; 491 | 492 | // ... 493 | // Code 494 | // ... 495 | ``` 496 | 497 | #### `array` 498 | 499 | Allow to specify multiple imports. 500 | Each item can be either a [`string`](https://github.com/webpack/imports-loader#string) or an [`object`](https://github.com/webpack/imports-loader#object). 501 | 502 | ##### Examples 503 | 504 | **webpack.config.js** 505 | 506 | ```js 507 | module.exports = { 508 | module: { 509 | rules: [ 510 | { 511 | test: require.resolve("example.js"), 512 | use: [ 513 | { 514 | loader: "imports-loader", 515 | options: { 516 | imports: [ 517 | { 518 | moduleName: "angular", 519 | }, 520 | { 521 | syntax: "default", 522 | moduleName: "jquery", 523 | name: "$", 524 | }, 525 | "default lib_2 lib_2_default", 526 | "named lib_2 lib2_method_1", 527 | "named lib_2 lib2_method_2 lib_2_method_2_alias", 528 | "namespace lib_3 lib_3_all", 529 | "side-effects lib_4", 530 | ], 531 | }, 532 | }, 533 | ], 534 | }, 535 | ], 536 | }, 537 | }; 538 | ``` 539 | 540 | Generate output: 541 | 542 | 543 | 544 | ```js 545 | import angular from "angular"; 546 | import $ from "jquery"; 547 | import lib_2_default from "lib_2"; 548 | import { lib2_method_1, lib2_method_2 as lib_2_method_2_alias } from "lib_2"; 549 | import * as lib_3_all from "lib_3"; 550 | import "lib_4"; 551 | 552 | // ... 553 | // Code 554 | // ... 555 | ``` 556 | 557 | ### `wrapper` 558 | 559 | Type: 560 | 561 | ```ts 562 | type wrapper = 563 | | boolean 564 | | string 565 | | { 566 | thisArg: string; 567 | args: Record | string[]; 568 | }; 569 | ``` 570 | 571 | Default: `undefined` 572 | 573 | Closes the module code in a function with a given `thisArg` and `args` (`(function () { ... }).call();`). 574 | 575 | > [!WARNING] 576 | > 577 | > Do not use this option if source code contains ES module import(s). It is intended for legacy or non-ESM compatible code. 578 | 579 | #### `boolean` 580 | 581 | Wraps the code in an IIFE (Immediately Invoked Function Expression) with default context. 582 | 583 | **webpack.config.js** 584 | 585 | ```js 586 | module.exports = { 587 | module: { 588 | rules: [ 589 | { 590 | test: require.resolve("example.js"), 591 | use: [ 592 | { 593 | loader: "imports-loader", 594 | options: { 595 | imports: { 596 | moduleName: "jquery", 597 | name: "$", 598 | }, 599 | wrapper: true, 600 | }, 601 | }, 602 | ], 603 | }, 604 | ], 605 | }, 606 | }; 607 | ``` 608 | 609 | Generate output: 610 | 611 | 612 | 613 | ```js 614 | import $ from "jquery"; 615 | 616 | (function () { 617 | // ... 618 | // Code 619 | // ... 620 | }).call(); 621 | ``` 622 | 623 | #### `string` 624 | 625 | Passes a custom thisArg to the .call() context. 626 | 627 | **webpack.config.js** 628 | 629 | ```js 630 | module.exports = { 631 | module: { 632 | rules: [ 633 | { 634 | test: require.resolve("example.js"), 635 | use: [ 636 | { 637 | loader: "imports-loader", 638 | options: { 639 | imports: { 640 | moduleName: "jquery", 641 | name: "$", 642 | }, 643 | wrapper: "window", 644 | }, 645 | }, 646 | ], 647 | }, 648 | ], 649 | }, 650 | }; 651 | ``` 652 | 653 | Generate output: 654 | 655 | 656 | 657 | ```js 658 | import $ from "jquery"; 659 | 660 | (function () { 661 | // ... 662 | // Code 663 | // ... 664 | }).call(globalThis); 665 | ``` 666 | 667 | #### `object` 668 | 669 | Allows advanced control: specify the this context and custom arguments passed into the IIFE 670 | 671 | **webpack.config.js** 672 | 673 | ```js 674 | module.exports = { 675 | module: { 676 | rules: [ 677 | { 678 | test: require.resolve("example.js"), 679 | use: [ 680 | { 681 | loader: "imports-loader", 682 | options: { 683 | imports: { 684 | moduleName: "jquery", 685 | name: "$", 686 | }, 687 | wrapper: { 688 | thisArg: "window", 689 | args: ["myVariable", "myOtherVariable"], 690 | }, 691 | }, 692 | }, 693 | ], 694 | }, 695 | ], 696 | }, 697 | }; 698 | ``` 699 | 700 | Generate output: 701 | 702 | 703 | 704 | ```js 705 | import $ from "jquery"; 706 | 707 | (function (myVariable, myOtherVariable) { 708 | // ... 709 | // Code 710 | // ... 711 | }).call(globalThis, myVariable, myOtherVariable); 712 | ``` 713 | 714 | #### `object` with different parameter names 715 | 716 | Allows remapping argument names in the function signature using a key-value object. 717 | 718 | **webpack.config.js** 719 | 720 | ```js 721 | module.exports = { 722 | module: { 723 | rules: [ 724 | { 725 | test: require.resolve("example.js"), 726 | use: [ 727 | { 728 | loader: "imports-loader", 729 | options: { 730 | imports: { 731 | moduleName: "jquery", 732 | name: "$", 733 | }, 734 | wrapper: { 735 | thisArg: "window", 736 | args: { 737 | myVariable: "var1", 738 | myOtherVariable: "var2", 739 | }, 740 | }, 741 | }, 742 | }, 743 | ], 744 | }, 745 | ], 746 | }, 747 | }; 748 | ``` 749 | 750 | Generate output: 751 | 752 | 753 | 754 | ```js 755 | import $ from "jquery"; 756 | 757 | (function (var1, var2) { 758 | // ... 759 | // Code 760 | // ... 761 | }).call(globalThis, myVariable, myOtherVariable); 762 | ``` 763 | 764 | ### `additionalCode` 765 | 766 | Type: 767 | 768 | ```ts 769 | type additionalCode = string; 770 | ``` 771 | 772 | Default: `undefined` 773 | 774 | Adds custom code as a preamble before the module's code. 775 | 776 | ##### Examples 777 | 778 | ###### Define custom variable 779 | 780 | **webpack.config.js** 781 | 782 | ```js 783 | module.exports = { 784 | module: { 785 | rules: [ 786 | { 787 | test: require.resolve("example.js"), 788 | use: [ 789 | { 790 | loader: "imports-loader", 791 | options: { 792 | imports: { 793 | moduleName: "jquery", 794 | name: "$", 795 | }, 796 | additionalCode: "var myVariable = false;", 797 | }, 798 | }, 799 | ], 800 | }, 801 | ], 802 | }, 803 | }; 804 | ``` 805 | 806 | Generate output: 807 | 808 | ```js 809 | import $ from "jquery"; 810 | 811 | const myVariable = false; 812 | 813 | // ... 814 | // Code 815 | // ... 816 | ``` 817 | 818 | ###### Disable AMD Import Syntax 819 | 820 | **webpack.config.js** 821 | 822 | ```js 823 | module.exports = { 824 | module: { 825 | rules: [ 826 | { 827 | test: require.resolve("example.js"), 828 | use: [ 829 | { 830 | loader: "imports-loader", 831 | options: { 832 | imports: { 833 | moduleName: "jquery", 834 | name: "$", 835 | }, 836 | additionalCode: 837 | "var define = false; /* Disable AMD for misbehaving libraries */", 838 | }, 839 | }, 840 | ], 841 | }, 842 | ], 843 | }, 844 | }; 845 | ``` 846 | 847 | Generate output: 848 | 849 | ```js 850 | import $ from "jquery"; 851 | 852 | const define = false; /* Disable AMD for misbehaving libraries */ 853 | 854 | // ... 855 | // Code 856 | // ... 857 | ``` 858 | 859 | ## Contributing 860 | 861 | We welcome contributions! 862 | If you’re interested in helping improve this loader, please take a moment to read our contributing guidelines. 863 | 864 | [CONTRIBUTING](https://github.com/webpack/imports-loader?tab=contributing-ov-file#contributing) 865 | 866 | ## License 867 | 868 | [MIT](./LICENSE) 869 | 870 | [npm]: https://img.shields.io/npm/v/imports-loader.svg 871 | [npm-url]: https://www.npmjs.com/package/imports-loader 872 | [node]: https://img.shields.io/node/v/imports-loader.svg 873 | [node-url]: https://nodejs.org 874 | [tests]: https://github.com/webpack/imports-loader/workflows/imports-loader/badge.svg 875 | [tests-url]: https://github.com/webpack/imports-loader/actions 876 | [cover]: https://codecov.io/gh/webpack/imports-loader/branch/main/graph/badge.svg 877 | [cover-url]: https://codecov.io/gh/webpack/imports-loader 878 | [discussion]: https://img.shields.io/github/discussions/webpack/webpack 879 | [discussion-url]: https://github.com/webpack/webpack/discussions 880 | [size]: https://packagephobia.now.sh/badge?p=imports-loader 881 | [size-url]: https://packagephobia.now.sh/result?p=imports-loader 882 | -------------------------------------------------------------------------------- /test/__snapshots__/loader.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing 2 | 3 | exports[`loader should throw an error on the empty string: errors 1`] = ` 4 | [ 5 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 6 | Error: Invalid " " value for import", 7 | ] 8 | `; 9 | 10 | exports[`loader should throw an error on the empty string: warnings 1`] = `[]`; 11 | 12 | exports[`loader should throw an error when "alias" can not be set using a string notation: errors 1`] = ` 13 | [ 14 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 15 | Error: The "side-effects" syntax does not support "some_alias" alias in "side-effects lib_1 name some_alias" value", 16 | ] 17 | `; 18 | 19 | exports[`loader should throw an error when "alias" can not be set using a string notation: warnings 1`] = `[]`; 20 | 21 | exports[`loader should throw an error when "alias" can not be set using an object notation: errors 1`] = ` 22 | [ 23 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 24 | Error: The "side-effects" syntax does not support "some_alias" alias in "", 25 | ] 26 | `; 27 | 28 | exports[`loader should throw an error when "alias" can not be set using an object notation: warnings 1`] = `[]`; 29 | 30 | exports[`loader should throw an error when "name" can not be used for a string notation: errors 1`] = ` 31 | [ 32 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 33 | Error: The "side-effects" syntax does not support "some_alias" name in "side-effects lib_1 some_alias" value", 34 | ] 35 | `; 36 | 37 | exports[`loader should throw an error when "name" can not be used for a string notation: warnings 1`] = `[]`; 38 | 39 | exports[`loader should throw an error when "name" can not be used for an object notation: errors 1`] = ` 40 | [ 41 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 42 | Error: The "side-effects" syntax does not support "some_name" name in "", 43 | ] 44 | `; 45 | 46 | exports[`loader should throw an error when "name" can not be used for an object notation: warnings 1`] = `[]`; 47 | 48 | exports[`loader should throw an error when "name" do not exist using a string notation: errors 1`] = ` 49 | [ 50 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 51 | Error: The "named" syntax needs the "name" option in "named lib_2.js" value", 52 | ] 53 | `; 54 | 55 | exports[`loader should throw an error when "name" do not exist using a string notation: warnings 1`] = `[]`; 56 | 57 | exports[`loader should throw an error when "name" do not exist using an object notation: errors 1`] = ` 58 | [ 59 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 60 | Error: The "named" syntax needs the "name" option in "", 61 | ] 62 | `; 63 | 64 | exports[`loader should throw an error when "name" do not exist using an object notation: warnings 1`] = `[]`; 65 | 66 | exports[`loader should throw an error when "namespace" ca not be used in CommonJS using a string notation: errors 1`] = ` 67 | [ 68 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 69 | Error: The "commonjs" type does not support the "namespace" syntax in "namespace lib_4 namespace" value", 70 | ] 71 | `; 72 | 73 | exports[`loader should throw an error when "namespace" ca not be used in CommonJS using a string notation: warnings 1`] = `[]`; 74 | 75 | exports[`loader should throw an error when "namespace" ca not be used in CommonJS using an object notation: errors 1`] = ` 76 | [ 77 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 78 | Error: The "commonjs" type does not support the "namespace" syntax in "", 79 | ] 80 | `; 81 | 82 | exports[`loader should throw an error when "namespace" ca not be used in CommonJS using an object notation: warnings 1`] = `[]`; 83 | 84 | exports[`loader should throw an error when "single" ca not be used in ES module using a string notation: errors 1`] = ` 85 | [ 86 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 87 | Error: The "module" format does not support the "single" syntax in "single lib_4 lib_4_all" value", 88 | ] 89 | `; 90 | 91 | exports[`loader should throw an error when "single" ca not be used in ES module using a string notation: warnings 1`] = `[]`; 92 | 93 | exports[`loader should throw an error when "single" ca not be used in ES module using an object notation: errors 1`] = ` 94 | [ 95 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 96 | Error: The "module" format does not support the "single" syntax in "", 97 | ] 98 | `; 99 | 100 | exports[`loader should throw an error when "single" ca not be used in ES module using an object notation: warnings 1`] = `[]`; 101 | 102 | exports[`loader should throw an error when duplicate of alias and name found in "multiple": errors 1`] = ` 103 | [ 104 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 105 | Error: Duplicate "lib1" (as "name"), "lib1" (as "alias"), "lib1" (as "name"), "toString" (as "name"), "toString" (as "name") identifiers found in "", 106 | ] 107 | `; 108 | 109 | exports[`loader should throw an error when duplicate of alias and name found in "multiple": warnings 1`] = `[]`; 110 | 111 | exports[`loader should throw an error when duplicate of alias and name found in "named": errors 1`] = ` 112 | [ 113 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 114 | Error: Duplicate "lib1" (as "name"), "lib1" (as "alias"), "lib1" (as "name"), "toString" (as "name"), "toString" (as "name") identifiers found in "", 115 | ] 116 | `; 117 | 118 | exports[`loader should throw an error when duplicate of alias and name found in "named": warnings 1`] = `[]`; 119 | 120 | exports[`loader should throw an error when duplicate of aliases found in "multiple": errors 1`] = ` 121 | [ 122 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 123 | Error: Duplicate "alias1" (as "alias"), "alias1" (as "alias") identifiers found in "", 124 | ] 125 | `; 126 | 127 | exports[`loader should throw an error when duplicate of aliases found in "multiple": warnings 1`] = `[]`; 128 | 129 | exports[`loader should throw an error when duplicate of aliases found in "named": errors 1`] = ` 130 | [ 131 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 132 | Error: Duplicate "alias1" (as "alias"), "alias1" (as "alias") identifiers found in "", 133 | ] 134 | `; 135 | 136 | exports[`loader should throw an error when duplicate of aliases found in "named": warnings 1`] = `[]`; 137 | 138 | exports[`loader should throw an error when duplicate of names found in "default" format: errors 1`] = ` 139 | [ 140 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 141 | Error: Duplicate "lib_1" (as "name"), "lib_1" (as "name") identifiers found in "", 142 | ] 143 | `; 144 | 145 | exports[`loader should throw an error when duplicate of names found in "default" format: warnings 1`] = `[]`; 146 | 147 | exports[`loader should throw an error when duplicate of names found in "multiple" format with other syntaxes: errors 1`] = ` 148 | [ 149 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 150 | Error: Duplicate "lib1" (as "name"), "lib1" (as "name"), "lib2" (as "name"), "lib2" (as "name") identifiers found in "", 151 | ] 152 | `; 153 | 154 | exports[`loader should throw an error when duplicate of names found in "multiple" format with other syntaxes: warnings 1`] = `[]`; 155 | 156 | exports[`loader should throw an error when duplicate of names found in "named" format with other syntaxes: errors 1`] = ` 157 | [ 158 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 159 | Error: Duplicate "lib1" (as "name"), "lib1" (as "name"), "lib2" (as "name"), "lib2" (as "name") identifiers found in "", 160 | ] 161 | `; 162 | 163 | exports[`loader should throw an error when duplicate of names found in "named" format with other syntaxes: warnings 1`] = `[]`; 164 | 165 | exports[`loader should throw an error when duplicate of names found in "named" format: errors 1`] = ` 166 | [ 167 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 168 | Error: Duplicate "foo" (as "name"), "foo" (as "name") identifiers found in "", 169 | ] 170 | `; 171 | 172 | exports[`loader should throw an error when duplicate of names found in "named" format: warnings 1`] = `[]`; 173 | 174 | exports[`loader should throw an error when duplicate of names found in "namespace" format: errors 1`] = ` 175 | [ 176 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 177 | Error: Duplicate "foo" (as "name"), "foo" (as "name") identifiers found in "", 178 | ] 179 | `; 180 | 181 | exports[`loader should throw an error when duplicate of names found in "namespace" format: warnings 1`] = `[]`; 182 | 183 | exports[`loader should throw an error when duplicate of names found in "single" format: errors 1`] = ` 184 | [ 185 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 186 | Error: Duplicate "lib_1" (as "name"), "lib_1" (as "name") identifiers found in "", 187 | ] 188 | `; 189 | 190 | exports[`loader should throw an error when duplicate of names found in "single" format: warnings 1`] = `[]`; 191 | 192 | exports[`loader should throw an error when invalid arguments for imports: errors 1`] = ` 193 | [ 194 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 195 | Error: The "default" syntax does not support "lib_2_method_2_short" alias in "", 196 | ] 197 | `; 198 | 199 | exports[`loader should throw an error when invalid arguments for imports: warnings 1`] = `[]`; 200 | 201 | exports[`loader should throw an error when more then one command separator: errors 1`] = ` 202 | [ 203 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 204 | Error: Invalid command "" in "default | lib_1 default_name" for imports. There must be only one separator: " ", or "|"", 205 | ] 206 | `; 207 | 208 | exports[`loader should throw an error when more then one command separator: warnings 1`] = `[]`; 209 | 210 | exports[`loader should throw an error when multiple duplicate of names found in "multiple" format: errors 1`] = ` 211 | [ 212 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 213 | Error: Duplicate "lib1" (as "name"), "lib1" (as "name"), "lib2" (as "name"), "lib2" (as "name") identifiers found in "", 214 | ] 215 | `; 216 | 217 | exports[`loader should throw an error when multiple duplicate of names found in "multiple" format: warnings 1`] = `[]`; 218 | 219 | exports[`loader should throw an error when no arguments for imports: errors 1`] = ` 220 | [ 221 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 222 | ValidationError: Invalid options object. Imports Loader has been initialized using an options object that does not match the API schema.", 223 | ] 224 | `; 225 | 226 | exports[`loader should throw an error when no arguments for imports: warnings 1`] = `[]`; 227 | 228 | exports[`loader should throw error on invalid inline syntax: errors 1`] = ` 229 | [ 230 | "ModuleBuildError: Module build failed (from \`replaced original path\`): 231 | Error: Invalid "named|lib_2|name|alias|something" value for import", 232 | ] 233 | `; 234 | 235 | exports[`loader should throw error on invalid inline syntax: warnings 1`] = `[]`; 236 | 237 | exports[`loader should work "pure" and "multiple" imports: errors 1`] = `[]`; 238 | 239 | exports[`loader should work "pure" and "multiple" imports: module 1`] = ` 240 | "/*** IMPORTS FROM imports-loader ***/ 241 | require("lib_1"); 242 | var { lib_2_n } = require("lib_1"); 243 | var { lib_2 } = require("lib_2"); 244 | 245 | var someCode = { 246 | number: 123, 247 | object: { existingSubProperty: 123 } 248 | }; 249 | " 250 | `; 251 | 252 | exports[`loader should work "pure" and "multiple" imports: warnings 1`] = `[]`; 253 | 254 | exports[`loader should work "pure" and "single" imports: errors 1`] = `[]`; 255 | 256 | exports[`loader should work "pure" and "single" imports: module 1`] = ` 257 | "/*** IMPORTS FROM imports-loader ***/ 258 | require("lib_1"); 259 | var lib_1 = require("lib_1"); 260 | var lib_2 = require("lib_2"); 261 | 262 | var someCode = { 263 | number: 123, 264 | object: { existingSubProperty: 123 } 265 | }; 266 | " 267 | `; 268 | 269 | exports[`loader should work "pure" and "single" imports: warnings 1`] = `[]`; 270 | 271 | exports[`loader should work "pure" imports: errors 1`] = `[]`; 272 | 273 | exports[`loader should work "pure" imports: module 1`] = ` 274 | "/*** IMPORTS FROM imports-loader ***/ 275 | require("lib_1"); 276 | 277 | var someCode = { 278 | number: 123, 279 | object: { existingSubProperty: 123 } 280 | }; 281 | " 282 | `; 283 | 284 | exports[`loader should work "pure" imports: warnings 1`] = `[]`; 285 | 286 | exports[`loader should work and prefer "default" syntax for CommonJS modules using a string notation: errors 1`] = `[]`; 287 | 288 | exports[`loader should work and prefer "default" syntax for CommonJS modules using a string notation: module 1`] = ` 289 | "/*** IMPORTS FROM imports-loader ***/ 290 | var lib_1 = require("lib_1"); 291 | 292 | var someCode = { 293 | number: 123, 294 | object: { existingSubProperty: 123 } 295 | }; 296 | " 297 | `; 298 | 299 | exports[`loader should work and prefer "default" syntax for CommonJS modules using a string notation: warnings 1`] = `[]`; 300 | 301 | exports[`loader should work and prefer "default" syntax for CommonJS modules using an object notation: errors 1`] = `[]`; 302 | 303 | exports[`loader should work and prefer "default" syntax for CommonJS modules using an object notation: module 1`] = ` 304 | "/*** IMPORTS FROM imports-loader ***/ 305 | var lib_1 = require("lib_1"); 306 | 307 | var someCode = { 308 | number: 123, 309 | object: { existingSubProperty: 123 } 310 | }; 311 | " 312 | `; 313 | 314 | exports[`loader should work and prefer "default" syntax for CommonJS modules using an object notation: warnings 1`] = `[]`; 315 | 316 | exports[`loader should work and prefer "default" syntax for ES modules using a string notation: errors 1`] = `[]`; 317 | 318 | exports[`loader should work and prefer "default" syntax for ES modules using a string notation: module 1`] = ` 319 | "/*** IMPORTS FROM imports-loader ***/ 320 | import lib_1 from "lib_1"; 321 | 322 | var someCode = { 323 | number: 123, 324 | object: { existingSubProperty: 123 } 325 | }; 326 | " 327 | `; 328 | 329 | exports[`loader should work and prefer "default" syntax for ES modules using a string notation: warnings 1`] = `[]`; 330 | 331 | exports[`loader should work and prefer "default" syntax for ES modules using an object notation: errors 1`] = `[]`; 332 | 333 | exports[`loader should work and prefer "default" syntax for ES modules using an object notation: module 1`] = ` 334 | "/*** IMPORTS FROM imports-loader ***/ 335 | import lib_1 from "lib_1"; 336 | 337 | var someCode = { 338 | number: 123, 339 | object: { existingSubProperty: 123 } 340 | }; 341 | " 342 | `; 343 | 344 | exports[`loader should work and prefer "default" syntax for ES modules using an object notation: warnings 1`] = `[]`; 345 | 346 | exports[`loader should work and union "default" with "named": errors 1`] = `[]`; 347 | 348 | exports[`loader should work and union "default" with "named": module 1`] = ` 349 | "/*** IMPORTS FROM imports-loader ***/ 350 | import lib_1 from "lib_1"; 351 | import { lib_method } from "lib_1"; 352 | 353 | var someCode = { 354 | number: 123, 355 | object: { existingSubProperty: 123 } 356 | }; 357 | " 358 | `; 359 | 360 | exports[`loader should work and union "default" with "named": warnings 1`] = `[]`; 361 | 362 | exports[`loader should work and union "default" with "namespace": errors 1`] = `[]`; 363 | 364 | exports[`loader should work and union "default" with "namespace": module 1`] = ` 365 | "/*** IMPORTS FROM imports-loader ***/ 366 | import lib_1 from "lib_1"; 367 | import * as ns from "lib_1"; 368 | 369 | var someCode = { 370 | number: 123, 371 | object: { existingSubProperty: 123 } 372 | }; 373 | " 374 | `; 375 | 376 | exports[`loader should work and union "default" with "namespace": warnings 1`] = `[]`; 377 | 378 | exports[`loader should work with "default" and "named" imports": errors 1`] = `[]`; 379 | 380 | exports[`loader should work with "default" and "named" imports": module 1`] = ` 381 | "/*** IMPORTS FROM imports-loader ***/ 382 | import lib2_default from "lib_2"; 383 | import { lib2_method_1, lib2_method_2 as lib_2_method_2_short } from "lib_2"; 384 | import { lib1_method } from "lib_1"; 385 | 386 | var someCode = { 387 | number: 123, 388 | object: { existingSubProperty: 123 } 389 | }; 390 | " 391 | `; 392 | 393 | exports[`loader should work with "default" and "named" imports": warnings 1`] = `[]`; 394 | 395 | exports[`loader should work with "default" and "namespace" imports": errors 1`] = `[]`; 396 | 397 | exports[`loader should work with "default" and "namespace" imports": module 1`] = ` 398 | "/*** IMPORTS FROM imports-loader ***/ 399 | import lib2_default from "lib_2"; 400 | import * as lib2_namespace from "lib_2"; 401 | import * as lib1_namespace from "lib_1"; 402 | 403 | var someCode = { 404 | number: 123, 405 | object: { existingSubProperty: 123 } 406 | }; 407 | " 408 | `; 409 | 410 | exports[`loader should work with "default" and "namespace" imports": warnings 1`] = `[]`; 411 | 412 | exports[`loader should work with "default" imports with syntax and name: errors 1`] = `[]`; 413 | 414 | exports[`loader should work with "default" imports with syntax and name: module 1`] = ` 415 | "/*** IMPORTS FROM imports-loader ***/ 416 | import lib_name from "lib_1"; 417 | 418 | var someCode = { 419 | number: 123, 420 | object: { existingSubProperty: 123 } 421 | }; 422 | " 423 | `; 424 | 425 | exports[`loader should work with "default" imports with syntax and name: warnings 1`] = `[]`; 426 | 427 | exports[`loader should work with "default" imports with syntax: errors 1`] = `[]`; 428 | 429 | exports[`loader should work with "default" imports with syntax: module 1`] = ` 430 | "/*** IMPORTS FROM imports-loader ***/ 431 | import lib_1 from "lib_1"; 432 | 433 | var someCode = { 434 | number: 123, 435 | object: { existingSubProperty: 123 } 436 | }; 437 | " 438 | `; 439 | 440 | exports[`loader should work with "default" imports with syntax: warnings 1`] = `[]`; 441 | 442 | exports[`loader should work with "default" imports without syntax: errors 1`] = `[]`; 443 | 444 | exports[`loader should work with "default" imports without syntax: module 1`] = ` 445 | "/*** IMPORTS FROM imports-loader ***/ 446 | import lib_1 from "lib_1"; 447 | 448 | var someCode = { 449 | number: 123, 450 | object: { existingSubProperty: 123 } 451 | }; 452 | " 453 | `; 454 | 455 | exports[`loader should work with "default" imports without syntax: warnings 1`] = `[]`; 456 | 457 | exports[`loader should work with "imports", "wrapper" and "additionalCode" options: errors 1`] = `[]`; 458 | 459 | exports[`loader should work with "imports", "wrapper" and "additionalCode" options: module 1`] = ` 460 | "/*** IMPORTS FROM imports-loader ***/ 461 | import "lib_1"; 462 | 463 | var someVariable = 1; 464 | 465 | (function() { 466 | var someCode = { 467 | number: 123, 468 | object: { existingSubProperty: 123 } 469 | }; 470 | 471 | }.call(window)); 472 | " 473 | `; 474 | 475 | exports[`loader should work with "imports", "wrapper" and "additionalCode" options: warnings 1`] = `[]`; 476 | 477 | exports[`loader should work with "multiple" imports: errors 1`] = `[]`; 478 | 479 | exports[`loader should work with "multiple" imports: module 1`] = ` 480 | "/*** IMPORTS FROM imports-loader ***/ 481 | var { lib_method } = require("lib_1"); 482 | 483 | var someCode = { 484 | number: 123, 485 | object: { existingSubProperty: 123 } 486 | }; 487 | " 488 | `; 489 | 490 | exports[`loader should work with "multiple" imports: warnings 1`] = `[]`; 491 | 492 | exports[`loader should work with "named" and "side-effects" imports: errors 1`] = `[]`; 493 | 494 | exports[`loader should work with "named" and "side-effects" imports: module 1`] = ` 495 | "/*** IMPORTS FROM imports-loader ***/ 496 | import { lib_2 } from "lib_2"; 497 | import "lib_1"; 498 | 499 | var someCode = { 500 | number: 123, 501 | object: { existingSubProperty: 123 } 502 | }; 503 | " 504 | `; 505 | 506 | exports[`loader should work with "named" and "side-effects" imports: warnings 1`] = `[]`; 507 | 508 | exports[`loader should work with "named" imports: errors 1`] = `[]`; 509 | 510 | exports[`loader should work with "named" imports: module 1`] = ` 511 | "/*** IMPORTS FROM imports-loader ***/ 512 | import { lib_1_name } from "lib_1"; 513 | 514 | var someCode = { 515 | number: 123, 516 | object: { existingSubProperty: 123 } 517 | }; 518 | " 519 | `; 520 | 521 | exports[`loader should work with "named" imports: warnings 1`] = `[]`; 522 | 523 | exports[`loader should work with "namespace" imports: errors 1`] = `[]`; 524 | 525 | exports[`loader should work with "namespace" imports: module 1`] = ` 526 | "/*** IMPORTS FROM imports-loader ***/ 527 | import * as lib_1_all from "lib_1"; 528 | 529 | var someCode = { 530 | number: 123, 531 | object: { existingSubProperty: 123 } 532 | }; 533 | " 534 | `; 535 | 536 | exports[`loader should work with "namespace" imports: warnings 1`] = `[]`; 537 | 538 | exports[`loader should work with "side-effects" and "default" imports: errors 1`] = `[]`; 539 | 540 | exports[`loader should work with "side-effects" and "default" imports: module 1`] = ` 541 | "/*** IMPORTS FROM imports-loader ***/ 542 | import "lib_1"; 543 | import lib_2 from "lib_2"; 544 | 545 | var someCode = { 546 | number: 123, 547 | object: { existingSubProperty: 123 } 548 | }; 549 | " 550 | `; 551 | 552 | exports[`loader should work with "side-effects" and "default" imports: warnings 1`] = `[]`; 553 | 554 | exports[`loader should work with "side-effects" and "named" imports: errors 1`] = `[]`; 555 | 556 | exports[`loader should work with "side-effects" and "named" imports: module 1`] = ` 557 | "/*** IMPORTS FROM imports-loader ***/ 558 | import "lib_1"; 559 | import { lib_2 } from "lib_2"; 560 | 561 | var someCode = { 562 | number: 123, 563 | object: { existingSubProperty: 123 } 564 | }; 565 | " 566 | `; 567 | 568 | exports[`loader should work with "side-effects" and "named" imports: warnings 1`] = `[]`; 569 | 570 | exports[`loader should work with "side-effects" and "namespace" imports: errors 1`] = `[]`; 571 | 572 | exports[`loader should work with "side-effects" and "namespace" imports: module 1`] = ` 573 | "/*** IMPORTS FROM imports-loader ***/ 574 | import "lib_1"; 575 | import * as lib_2 from "lib_2"; 576 | 577 | var someCode = { 578 | number: 123, 579 | object: { existingSubProperty: 123 } 580 | }; 581 | " 582 | `; 583 | 584 | exports[`loader should work with "side-effects" and "namespace" imports: warnings 1`] = `[]`; 585 | 586 | exports[`loader should work with "side-effects" imports: errors 1`] = `[]`; 587 | 588 | exports[`loader should work with "side-effects" imports: module 1`] = ` 589 | "/*** IMPORTS FROM imports-loader ***/ 590 | import "lib_1"; 591 | 592 | var someCode = { 593 | number: 123, 594 | object: { existingSubProperty: 123 } 595 | }; 596 | " 597 | `; 598 | 599 | exports[`loader should work with "side-effects" imports: warnings 1`] = `[]`; 600 | 601 | exports[`loader should work with "single" imports with syntax and alias: errors 1`] = `[]`; 602 | 603 | exports[`loader should work with "single" imports with syntax and alias: module 1`] = ` 604 | "/*** IMPORTS FROM imports-loader ***/ 605 | var lib_name = require("lib_1"); 606 | 607 | var someCode = { 608 | number: 123, 609 | object: { existingSubProperty: 123 } 610 | }; 611 | " 612 | `; 613 | 614 | exports[`loader should work with "single" imports with syntax and alias: warnings 1`] = `[]`; 615 | 616 | exports[`loader should work with "single" imports with syntax: errors 1`] = `[]`; 617 | 618 | exports[`loader should work with "single" imports with syntax: module 1`] = ` 619 | "/*** IMPORTS FROM imports-loader ***/ 620 | var lib_1 = require("lib_1"); 621 | 622 | var someCode = { 623 | number: 123, 624 | object: { existingSubProperty: 123 } 625 | }; 626 | " 627 | `; 628 | 629 | exports[`loader should work with "single" imports with syntax: warnings 1`] = `[]`; 630 | 631 | exports[`loader should work with "single" imports without syntax: errors 1`] = `[]`; 632 | 633 | exports[`loader should work with "single" imports without syntax: module 1`] = ` 634 | "/*** IMPORTS FROM imports-loader ***/ 635 | var lib_1 = require("lib_1"); 636 | 637 | var someCode = { 638 | number: 123, 639 | object: { existingSubProperty: 123 } 640 | }; 641 | " 642 | `; 643 | 644 | exports[`loader should work with "single" imports without syntax: warnings 1`] = `[]`; 645 | 646 | exports[`loader should work with "use-strict" not in program with CommonJS modules: errors 1`] = `[]`; 647 | 648 | exports[`loader should work with "use-strict" not in program with CommonJS modules: module 1`] = ` 649 | "/*** IMPORTS FROM imports-loader ***/ 650 | var lib_1 = require("lib_1"); 651 | 652 | var myObject = { foo: 'bar' }; 653 | 654 | function test() { 655 | 'use strict'; 656 | 657 | return 'test'; 658 | } 659 | " 660 | `; 661 | 662 | exports[`loader should work with "use-strict" not in program with CommonJS modules: warnings 1`] = `[]`; 663 | 664 | exports[`loader should work with "use-strict" not in program with ES modules: errors 1`] = `[]`; 665 | 666 | exports[`loader should work with "use-strict" not in program with ES modules: module 1`] = ` 667 | "/*** IMPORTS FROM imports-loader ***/ 668 | import lib_1 from "lib_1"; 669 | 670 | var myObject = { foo: 'bar' }; 671 | 672 | function test() { 673 | 'use strict'; 674 | 675 | return 'test'; 676 | } 677 | " 678 | `; 679 | 680 | exports[`loader should work with "use-strict" not in program with ES modules: warnings 1`] = `[]`; 681 | 682 | exports[`loader should work with "use-strict" with CommonJS modules: errors 1`] = `[]`; 683 | 684 | exports[`loader should work with "use-strict" with CommonJS modules: module 1`] = ` 685 | "/*** IMPORTS FROM imports-loader ***/ 686 | 'use strict'; 687 | var lib_1 = require("lib_1"); 688 | 689 | /* Comment */ 690 | 691 | 'use strict'; 692 | 693 | var myObject = { foo: 'bar' }; 694 | 695 | function test() { 696 | 'use strict'; 697 | 698 | return 'test'; 699 | } 700 | " 701 | `; 702 | 703 | exports[`loader should work with "use-strict" with CommonJS modules: warnings 1`] = `[]`; 704 | 705 | exports[`loader should work with "use-strict" with ES modules: errors 1`] = `[]`; 706 | 707 | exports[`loader should work with "use-strict" with ES modules: module 1`] = ` 708 | "/*** IMPORTS FROM imports-loader ***/ 709 | 'use strict'; 710 | import lib_1 from "lib_1"; 711 | 712 | /* Comment */ 713 | 714 | 'use strict'; 715 | 716 | var myObject = { foo: 'bar' }; 717 | 718 | function test() { 719 | 'use strict'; 720 | 721 | return 'test'; 722 | } 723 | " 724 | `; 725 | 726 | exports[`loader should work with "use-strict" with ES modules: warnings 1`] = `[]`; 727 | 728 | exports[`loader should work with a string syntax using CommonJS: errors 1`] = `[]`; 729 | 730 | exports[`loader should work with a string syntax using CommonJS: module 1`] = ` 731 | "/*** IMPORTS FROM imports-loader ***/ 732 | var lib_1 = require("lib_1"); 733 | var $ = require("lib_1"); 734 | var lib_2_all = require("lib_2"); 735 | var { lib2_method_1, lib2_method_2: lib_2_method_2_short } = require("lib_2"); 736 | require("lib_3"); 737 | 738 | var someCode = { 739 | number: 123, 740 | object: { existingSubProperty: 123 } 741 | }; 742 | " 743 | `; 744 | 745 | exports[`loader should work with a string syntax using CommonJS: warnings 1`] = `[]`; 746 | 747 | exports[`loader should work with a string syntax using ES modules: errors 1`] = `[]`; 748 | 749 | exports[`loader should work with a string syntax using ES modules: module 1`] = ` 750 | "/*** IMPORTS FROM imports-loader ***/ 751 | import lib_1 from "lib_1"; 752 | import default_name from "lib_1"; 753 | import $ from "lib_1"; 754 | import lib_2_all from "lib_2"; 755 | import { lib2_method_1, lib2_method_2 as lib_2_method_2_short } from "lib_2"; 756 | import lib_3_default from "lib_3"; 757 | import * as lib_3_all from "lib_3"; 758 | import "lib_4"; 759 | 760 | var someCode = { 761 | number: 123, 762 | object: { existingSubProperty: 123 } 763 | }; 764 | " 765 | `; 766 | 767 | exports[`loader should work with a string syntax using ES modules: warnings 1`] = `[]`; 768 | 769 | exports[`loader should work with a string value with spaces: errors 1`] = `[]`; 770 | 771 | exports[`loader should work with a string value with spaces: module 1`] = ` 772 | "/*** IMPORTS FROM imports-loader ***/ 773 | import lib_1 from "lib_1"; 774 | 775 | var someCode = { 776 | number: 123, 777 | object: { existingSubProperty: 123 } 778 | }; 779 | " 780 | `; 781 | 782 | exports[`loader should work with a string value with spaces: warnings 1`] = `[]`; 783 | 784 | exports[`loader should work with a string value: errors 1`] = `[]`; 785 | 786 | exports[`loader should work with a string value: module 1`] = ` 787 | "/*** IMPORTS FROM imports-loader ***/ 788 | import lib_1 from "lib_1"; 789 | 790 | var someCode = { 791 | number: 123, 792 | object: { existingSubProperty: 123 } 793 | }; 794 | " 795 | `; 796 | 797 | exports[`loader should work with a string value: warnings 1`] = `[]`; 798 | 799 | exports[`loader should work with an absolute path in moduleName: errors 1`] = `[]`; 800 | 801 | exports[`loader should work with an absolute path in moduleName: module 1`] = ` 802 | "/*** IMPORTS FROM imports-loader ***/ 803 | import $ from "./lib_1"; 804 | 805 | var someCode = { 806 | number: 123, 807 | object: { existingSubProperty: 123 } 808 | }; 809 | " 810 | `; 811 | 812 | exports[`loader should work with an absolute path in moduleName: warnings 1`] = `[]`; 813 | 814 | exports[`loader should work with an object value: errors 1`] = `[]`; 815 | 816 | exports[`loader should work with an object value: module 1`] = ` 817 | "/*** IMPORTS FROM imports-loader ***/ 818 | import $ from "lib_1"; 819 | 820 | var someCode = { 821 | number: 123, 822 | object: { existingSubProperty: 123 } 823 | }; 824 | " 825 | `; 826 | 827 | exports[`loader should work with an object value: warnings 1`] = `[]`; 828 | 829 | exports[`loader should work with array of objects and strings: errors 1`] = `[]`; 830 | 831 | exports[`loader should work with array of objects and strings: module 1`] = ` 832 | "/*** IMPORTS FROM imports-loader ***/ 833 | import lib_1 from "lib_1"; 834 | import { lib_2_name } from "lib_2"; 835 | 836 | var someCode = { 837 | number: 123, 838 | object: { existingSubProperty: 123 } 839 | }; 840 | " 841 | `; 842 | 843 | exports[`loader should work with array of objects and strings: warnings 1`] = `[]`; 844 | 845 | exports[`loader should work with array of strings: errors 1`] = `[]`; 846 | 847 | exports[`loader should work with array of strings: module 1`] = ` 848 | "/*** IMPORTS FROM imports-loader ***/ 849 | import lib_1 from "lib_1"; 850 | import lib_2 from "lib_2"; 851 | 852 | var someCode = { 853 | number: 123, 854 | object: { existingSubProperty: 123 } 855 | }; 856 | " 857 | `; 858 | 859 | exports[`loader should work with array of strings: warnings 1`] = `[]`; 860 | 861 | exports[`loader should work with inline syntax #1: errors 1`] = `[]`; 862 | 863 | exports[`loader should work with inline syntax #1: module 1`] = ` 864 | "/*** IMPORTS FROM imports-loader ***/ 865 | var lib_2_all = require("lib_2"); 866 | var { lib_2_method: lib_2_method_alias } = require("lib_2"); 867 | 868 | var someCode = { 869 | number: 123, 870 | object: { existingSubProperty: 123 } 871 | }; 872 | " 873 | `; 874 | 875 | exports[`loader should work with inline syntax #1: warnings 1`] = `[]`; 876 | 877 | exports[`loader should work with inline syntax #2: errors 1`] = `[]`; 878 | 879 | exports[`loader should work with inline syntax #2: module 1`] = ` 880 | "/*** IMPORTS FROM imports-loader ***/ 881 | import $ from "lib_2"; 882 | 883 | (function() { 884 | var someCode = { 885 | number: 123, 886 | object: { existingSubProperty: 123 } 887 | }; 888 | 889 | }.call(window)); 890 | " 891 | `; 892 | 893 | exports[`loader should work with inline syntax #2: warnings 1`] = `[]`; 894 | 895 | exports[`loader should work with inline syntax #3: errors 1`] = `[]`; 896 | 897 | exports[`loader should work with inline syntax #3: module 1`] = ` 898 | "/*** IMPORTS FROM imports-loader ***/ 899 | 900 | var require = false; 901 | 902 | var someCode = { 903 | number: 123, 904 | object: { existingSubProperty: 123 } 905 | }; 906 | " 907 | `; 908 | 909 | exports[`loader should work with inline syntax #3: warnings 1`] = `[]`; 910 | 911 | exports[`loader should work with inline syntax: errors 1`] = `[]`; 912 | 913 | exports[`loader should work with inline syntax: module 1`] = ` 914 | "/*** IMPORTS FROM imports-loader ***/ 915 | import lib_1 from "lib_1"; 916 | import lib_2 from "lib_2"; 917 | 918 | var someCode = { 919 | number: 123, 920 | object: { existingSubProperty: 123 } 921 | }; 922 | " 923 | `; 924 | 925 | exports[`loader should work with inline syntax: warnings 1`] = `[]`; 926 | 927 | exports[`loader should work with multiple "default" imports and different names: errors 1`] = `[]`; 928 | 929 | exports[`loader should work with multiple "default" imports and different names: module 1`] = ` 930 | "/*** IMPORTS FROM imports-loader ***/ 931 | import lib_2 from "lib_2"; 932 | import lib_3 from "lib_2"; 933 | 934 | var someCode = { 935 | number: 123, 936 | object: { existingSubProperty: 123 } 937 | }; 938 | " 939 | `; 940 | 941 | exports[`loader should work with multiple "default" imports and different names: warnings 1`] = `[]`; 942 | 943 | exports[`loader should work with multiple "multiple" imports from different libraries: errors 1`] = `[]`; 944 | 945 | exports[`loader should work with multiple "multiple" imports from different libraries: module 1`] = ` 946 | "/*** IMPORTS FROM imports-loader ***/ 947 | var { lib_2, lib_3: alias } = require("lib_2"); 948 | var { lib_4 } = require("lib_3"); 949 | 950 | var someCode = { 951 | number: 123, 952 | object: { existingSubProperty: 123 } 953 | }; 954 | " 955 | `; 956 | 957 | exports[`loader should work with multiple "multiple" imports from different libraries: warnings 1`] = `[]`; 958 | 959 | exports[`loader should work with multiple "multiple" imports from same library: errors 1`] = `[]`; 960 | 961 | exports[`loader should work with multiple "multiple" imports from same library: module 1`] = ` 962 | "/*** IMPORTS FROM imports-loader ***/ 963 | var { lib2_method_1, lib2_method_2: lib_2_method_2_short } = require("lib_2"); 964 | 965 | var someCode = { 966 | number: 123, 967 | object: { existingSubProperty: 123 } 968 | }; 969 | " 970 | `; 971 | 972 | exports[`loader should work with multiple "multiple" imports from same library: warnings 1`] = `[]`; 973 | 974 | exports[`loader should work with multiple "multiple" imports from the one lib: errors 1`] = `[]`; 975 | 976 | exports[`loader should work with multiple "multiple" imports from the one lib: module 1`] = ` 977 | "/*** IMPORTS FROM imports-loader ***/ 978 | var { lib_2, lib_3: alias, lib_4 } = require("lib_2"); 979 | 980 | var someCode = { 981 | number: 123, 982 | object: { existingSubProperty: 123 } 983 | }; 984 | " 985 | `; 986 | 987 | exports[`loader should work with multiple "multiple" imports from the one lib: warnings 1`] = `[]`; 988 | 989 | exports[`loader should work with multiple "named" imports from the one lib: errors 1`] = `[]`; 990 | 991 | exports[`loader should work with multiple "named" imports from the one lib: module 1`] = ` 992 | "/*** IMPORTS FROM imports-loader ***/ 993 | import { lib_2, lib_3 as alias, lib_4 } from "lib_2"; 994 | 995 | var someCode = { 996 | number: 123, 997 | object: { existingSubProperty: 123 } 998 | }; 999 | " 1000 | `; 1001 | 1002 | exports[`loader should work with multiple "named" imports from the one lib: warnings 1`] = `[]`; 1003 | 1004 | exports[`loader should work with multiple "named" imports: errors 1`] = `[]`; 1005 | 1006 | exports[`loader should work with multiple "named" imports: module 1`] = ` 1007 | "/*** IMPORTS FROM imports-loader ***/ 1008 | import { lib_2, lib_3 as alias } from "lib_2"; 1009 | import { lib_4 } from "lib_3"; 1010 | 1011 | var someCode = { 1012 | number: 123, 1013 | object: { existingSubProperty: 123 } 1014 | }; 1015 | " 1016 | `; 1017 | 1018 | exports[`loader should work with multiple "named" imports: warnings 1`] = `[]`; 1019 | 1020 | exports[`loader should work with multiple "namespace" imports: errors 1`] = `[]`; 1021 | 1022 | exports[`loader should work with multiple "namespace" imports: module 1`] = ` 1023 | "/*** IMPORTS FROM imports-loader ***/ 1024 | import * as lib_2 from "lib_2"; 1025 | import * as lib_3 from "lib_2"; 1026 | 1027 | var someCode = { 1028 | number: 123, 1029 | object: { existingSubProperty: 123 } 1030 | }; 1031 | " 1032 | `; 1033 | 1034 | exports[`loader should work with multiple "namespace" imports: warnings 1`] = `[]`; 1035 | 1036 | exports[`loader should work with multiple "pure" imports: errors 1`] = `[]`; 1037 | 1038 | exports[`loader should work with multiple "pure" imports: module 1`] = ` 1039 | "/*** IMPORTS FROM imports-loader ***/ 1040 | require("lib_2"); 1041 | require("lib_2"); 1042 | 1043 | var someCode = { 1044 | number: 123, 1045 | object: { existingSubProperty: 123 } 1046 | }; 1047 | " 1048 | `; 1049 | 1050 | exports[`loader should work with multiple "pure" imports: warnings 1`] = `[]`; 1051 | 1052 | exports[`loader should work with multiple "side-effects" imports: errors 1`] = `[]`; 1053 | 1054 | exports[`loader should work with multiple "side-effects" imports: module 1`] = ` 1055 | "/*** IMPORTS FROM imports-loader ***/ 1056 | import "lib_2"; 1057 | import "lib_2"; 1058 | 1059 | var someCode = { 1060 | number: 123, 1061 | object: { existingSubProperty: 123 } 1062 | }; 1063 | " 1064 | `; 1065 | 1066 | exports[`loader should work with multiple "side-effects" imports: warnings 1`] = `[]`; 1067 | 1068 | exports[`loader should work with multiple "single" imports and different names: errors 1`] = `[]`; 1069 | 1070 | exports[`loader should work with multiple "single" imports and different names: module 1`] = ` 1071 | "/*** IMPORTS FROM imports-loader ***/ 1072 | var lib_2 = require("lib_2"); 1073 | var lib_3 = require("lib_2"); 1074 | 1075 | var someCode = { 1076 | number: 123, 1077 | object: { existingSubProperty: 123 } 1078 | }; 1079 | " 1080 | `; 1081 | 1082 | exports[`loader should work with multiple "single" imports and different names: warnings 1`] = `[]`; 1083 | 1084 | exports[`loader should work with relative requests: errors 1`] = `[]`; 1085 | 1086 | exports[`loader should work with relative requests: module 1`] = ` 1087 | "/*** IMPORTS FROM imports-loader ***/ 1088 | import lib_1 from "./lib_1"; 1089 | import { lib_2_name } from "./lib_2"; 1090 | 1091 | var someCode = { 1092 | number: 123, 1093 | object: { existingSubProperty: 123 } 1094 | }; 1095 | " 1096 | `; 1097 | 1098 | exports[`loader should work with relative requests: warnings 1`] = `[]`; 1099 | 1100 | exports[`loader should work with the "additionalCode" option: errors 1`] = `[]`; 1101 | 1102 | exports[`loader should work with the "additionalCode" option: module 1`] = ` 1103 | "/*** IMPORTS FROM imports-loader ***/ 1104 | 1105 | var someVariable = 1; 1106 | 1107 | var someCode = { 1108 | number: 123, 1109 | object: { existingSubProperty: 123 } 1110 | }; 1111 | " 1112 | `; 1113 | 1114 | exports[`loader should work with the "additionalCode" option: warnings 1`] = `[]`; 1115 | 1116 | exports[`loader should work with the "wrapper" option as a boolean notation: errors 1`] = `[]`; 1117 | 1118 | exports[`loader should work with the "wrapper" option as a boolean notation: module 1`] = ` 1119 | "/*** IMPORTS FROM imports-loader ***/ 1120 | 1121 | (function() { 1122 | var someCode = { 1123 | number: 123, 1124 | object: { existingSubProperty: 123 } 1125 | }; 1126 | 1127 | }.call()); 1128 | " 1129 | `; 1130 | 1131 | exports[`loader should work with the "wrapper" option as a boolean notation: warnings 1`] = `[]`; 1132 | 1133 | exports[`loader should work with the "wrapper" option as a string notation: errors 1`] = `[]`; 1134 | 1135 | exports[`loader should work with the "wrapper" option as a string notation: module 1`] = ` 1136 | "/*** IMPORTS FROM imports-loader ***/ 1137 | 1138 | (function() { 1139 | var someCode = { 1140 | number: 123, 1141 | object: { existingSubProperty: 123 } 1142 | }; 1143 | 1144 | }.call(window)); 1145 | " 1146 | `; 1147 | 1148 | exports[`loader should work with the "wrapper" option as a string notation: warnings 1`] = `[]`; 1149 | 1150 | exports[`loader should work with the "wrapper" options as an object notation: errors 1`] = `[]`; 1151 | 1152 | exports[`loader should work with the "wrapper" options as an object notation: module 1`] = ` 1153 | "/*** IMPORTS FROM imports-loader ***/ 1154 | 1155 | (function(myGlobalVariable, myOtherGlobalVariable) { 1156 | var someCode = { 1157 | number: 123, 1158 | object: { existingSubProperty: 123 } 1159 | }; 1160 | 1161 | }.call(window, myGlobalVariable, myOtherGlobalVariable)); 1162 | " 1163 | `; 1164 | 1165 | exports[`loader should work with the "wrapper" options as an object notation: warnings 1`] = `[]`; 1166 | 1167 | exports[`loader should work with the "wrapper.args" options as an object notation: errors 1`] = `[]`; 1168 | 1169 | exports[`loader should work with the "wrapper.args" options as an object notation: module 1`] = ` 1170 | "/*** IMPORTS FROM imports-loader ***/ 1171 | 1172 | (function(foo1, foo2) { 1173 | var someCode = { 1174 | number: 123, 1175 | object: { existingSubProperty: 123 } 1176 | }; 1177 | 1178 | }.call(window, bar1, bar2)); 1179 | " 1180 | `; 1181 | 1182 | exports[`loader should work with the "wrapper.args" options as an object notation: warnings 1`] = `[]`; 1183 | -------------------------------------------------------------------------------- /test/loader.test.js: -------------------------------------------------------------------------------- 1 | import path from "node:path"; 2 | 3 | import { SourceMapConsumer } from "source-map-js"; 4 | 5 | import { 6 | compile, 7 | getCompiler, 8 | getErrors, 9 | getModuleSource, 10 | getWarnings, 11 | } from "./helpers"; 12 | import readAsset from "./helpers/readAsset"; 13 | 14 | describe("loader", () => { 15 | it("should work with a string value", async () => { 16 | const compiler = getCompiler("some-library.js", { 17 | imports: "lib_1", 18 | }); 19 | const stats = await compile(compiler); 20 | 21 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 22 | "module", 23 | ); 24 | expect(getErrors(stats)).toMatchSnapshot("errors"); 25 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 26 | }); 27 | 28 | it("should work with an object value", async () => { 29 | const compiler = getCompiler("some-library.js", { 30 | imports: { 31 | moduleName: "lib_1", 32 | name: "$", 33 | }, 34 | }); 35 | const stats = await compile(compiler); 36 | 37 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 38 | "module", 39 | ); 40 | expect(getErrors(stats)).toMatchSnapshot("errors"); 41 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 42 | }); 43 | 44 | it("should work with an absolute path in moduleName", async () => { 45 | const compiler = getCompiler("some-library.js", { 46 | imports: { 47 | moduleName: path.resolve(__dirname, "fixtures", "lib_1"), 48 | name: "$", 49 | }, 50 | }); 51 | const stats = await compile(compiler); 52 | 53 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 54 | "module", 55 | ); 56 | expect(getErrors(stats)).toMatchSnapshot("errors"); 57 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 58 | }); 59 | 60 | it("should work with array of strings", async () => { 61 | const compiler = getCompiler("some-library.js", { 62 | imports: ["lib_1", "lib_2"], 63 | }); 64 | const stats = await compile(compiler); 65 | 66 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 67 | "module", 68 | ); 69 | expect(getErrors(stats)).toMatchSnapshot("errors"); 70 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 71 | }); 72 | 73 | it("should work with array of objects and strings", async () => { 74 | const compiler = getCompiler("some-library.js", { 75 | imports: [ 76 | "lib_1", 77 | { 78 | syntax: "named", 79 | moduleName: "lib_2", 80 | name: "lib_2_name", 81 | }, 82 | ], 83 | }); 84 | const stats = await compile(compiler); 85 | 86 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 87 | "module", 88 | ); 89 | expect(getErrors(stats)).toMatchSnapshot("errors"); 90 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 91 | }); 92 | 93 | it("should work with relative requests", async () => { 94 | const compiler = getCompiler("some-library.js", { 95 | imports: [ 96 | "default ./lib_1 lib_1", 97 | { 98 | syntax: "named", 99 | moduleName: "./lib_2", 100 | name: "lib_2_name", 101 | }, 102 | ], 103 | }); 104 | const stats = await compile(compiler); 105 | 106 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 107 | "module", 108 | ); 109 | expect(getErrors(stats)).toMatchSnapshot("errors"); 110 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 111 | }); 112 | 113 | it('should work and prefer "default" syntax for ES modules using a string notation', async () => { 114 | const compiler = getCompiler("some-library.js", { 115 | imports: ["lib_1"], 116 | }); 117 | const stats = await compile(compiler); 118 | 119 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 120 | "module", 121 | ); 122 | expect(getErrors(stats)).toMatchSnapshot("errors"); 123 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 124 | }); 125 | 126 | it('should work and prefer "default" syntax for ES modules using an object notation', async () => { 127 | const compiler = getCompiler("some-library.js", { 128 | imports: { moduleName: "lib_1" }, 129 | }); 130 | const stats = await compile(compiler); 131 | 132 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 133 | "module", 134 | ); 135 | expect(getErrors(stats)).toMatchSnapshot("errors"); 136 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 137 | }); 138 | 139 | it('should work and prefer "default" syntax for CommonJS modules using a string notation', async () => { 140 | const compiler = getCompiler("some-library.js", { 141 | type: "commonjs", 142 | imports: ["lib_1"], 143 | }); 144 | const stats = await compile(compiler); 145 | 146 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 147 | "module", 148 | ); 149 | expect(getErrors(stats)).toMatchSnapshot("errors"); 150 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 151 | }); 152 | 153 | it('should work and prefer "default" syntax for CommonJS modules using an object notation', async () => { 154 | const compiler = getCompiler("some-library.js", { 155 | type: "commonjs", 156 | imports: [ 157 | { 158 | moduleName: "lib_1", 159 | }, 160 | ], 161 | }); 162 | const stats = await compile(compiler); 163 | 164 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 165 | "module", 166 | ); 167 | expect(getErrors(stats)).toMatchSnapshot("errors"); 168 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 169 | }); 170 | 171 | it('should work with "use-strict" with ES modules', async () => { 172 | const compiler = getCompiler("use-strict.js", { 173 | imports: "lib_1", 174 | }); 175 | const stats = await compile(compiler); 176 | 177 | expect(getModuleSource("./use-strict.js", stats)).toMatchSnapshot("module"); 178 | expect(getErrors(stats)).toMatchSnapshot("errors"); 179 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 180 | }); 181 | 182 | it('should work with "use-strict" with CommonJS modules', async () => { 183 | const compiler = getCompiler("use-strict.js", { 184 | type: "commonjs", 185 | imports: "lib_1", 186 | }); 187 | const stats = await compile(compiler); 188 | 189 | expect(getModuleSource("./use-strict.js", stats)).toMatchSnapshot("module"); 190 | expect(getErrors(stats)).toMatchSnapshot("errors"); 191 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 192 | }); 193 | 194 | it('should work with "use-strict" not in program with ES modules', async () => { 195 | const compiler = getCompiler("use-strict-in-function.js", { 196 | imports: "lib_1", 197 | }); 198 | const stats = await compile(compiler); 199 | 200 | expect( 201 | getModuleSource("./use-strict-in-function.js", stats), 202 | ).toMatchSnapshot("module"); 203 | expect(getErrors(stats)).toMatchSnapshot("errors"); 204 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 205 | }); 206 | 207 | it('should work with "use-strict" not in program with CommonJS modules', async () => { 208 | const compiler = getCompiler("use-strict-in-function.js", { 209 | type: "commonjs", 210 | imports: "lib_1", 211 | }); 212 | const stats = await compile(compiler); 213 | 214 | expect( 215 | getModuleSource("./use-strict-in-function.js", stats), 216 | ).toMatchSnapshot("module"); 217 | expect(getErrors(stats)).toMatchSnapshot("errors"); 218 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 219 | }); 220 | 221 | it('should work with "imports", "wrapper" and "additionalCode" options', async () => { 222 | const compiler = getCompiler("some-library.js", { 223 | imports: { 224 | moduleName: "lib_1", 225 | syntax: "side-effects", 226 | }, 227 | wrapper: "window", 228 | additionalCode: "var someVariable = 1;", 229 | }); 230 | const stats = await compile(compiler); 231 | 232 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 233 | "module", 234 | ); 235 | expect(getErrors(stats)).toMatchSnapshot("errors"); 236 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 237 | }); 238 | 239 | it('should work with the "wrapper" option as a boolean notation', async () => { 240 | const compiler = getCompiler("some-library.js", { 241 | wrapper: true, 242 | }); 243 | const stats = await compile(compiler); 244 | 245 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 246 | "module", 247 | ); 248 | expect(getErrors(stats)).toMatchSnapshot("errors"); 249 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 250 | }); 251 | 252 | it('should work with the "wrapper" option as a string notation', async () => { 253 | const compiler = getCompiler("some-library.js", { 254 | wrapper: "window", 255 | }); 256 | const stats = await compile(compiler); 257 | 258 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 259 | "module", 260 | ); 261 | expect(getErrors(stats)).toMatchSnapshot("errors"); 262 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 263 | }); 264 | 265 | it('should work with the "wrapper" options as an object notation', async () => { 266 | const compiler = getCompiler("some-library.js", { 267 | wrapper: { 268 | thisArg: "window", 269 | args: ["myGlobalVariable", "myOtherGlobalVariable"], 270 | }, 271 | }); 272 | const stats = await compile(compiler); 273 | 274 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 275 | "module", 276 | ); 277 | expect(getErrors(stats)).toMatchSnapshot("errors"); 278 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 279 | }); 280 | 281 | it('should work with the "wrapper.args" options as an object notation', async () => { 282 | const compiler = getCompiler("some-library.js", { 283 | wrapper: { 284 | thisArg: "window", 285 | args: { 286 | foo1: "bar1", 287 | foo2: "bar2", 288 | }, 289 | }, 290 | }); 291 | const stats = await compile(compiler); 292 | 293 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 294 | "module", 295 | ); 296 | expect(getErrors(stats)).toMatchSnapshot("errors"); 297 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 298 | }); 299 | 300 | it('should work with the "additionalCode" option', async () => { 301 | const compiler = getCompiler("some-library.js", { 302 | additionalCode: "var someVariable = 1;", 303 | }); 304 | const stats = await compile(compiler); 305 | 306 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 307 | "module", 308 | ); 309 | expect(getErrors(stats)).toMatchSnapshot("errors"); 310 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 311 | }); 312 | 313 | it('should generate source maps when the "devtool" option is enabled', async () => { 314 | const compiler = getCompiler( 315 | "some-library.js", 316 | {}, 317 | { 318 | devtool: "source-map", 319 | module: { 320 | rules: [ 321 | { 322 | test: /\.js$/i, 323 | rules: [ 324 | { 325 | loader: path.resolve(__dirname, "../src"), 326 | options: { imports: "lib_1" }, 327 | }, 328 | ], 329 | }, 330 | ], 331 | }, 332 | }, 333 | ); 334 | const stats = await compile(compiler); 335 | const bundle = readAsset( 336 | "main.bundle.js", 337 | stats.compilation.compiler, 338 | stats, 339 | ).split("\n"); 340 | const sourceMap = readAsset( 341 | "main.bundle.js.map", 342 | stats.compilation.compiler, 343 | stats, 344 | ); 345 | 346 | const consumer = new SourceMapConsumer(sourceMap); 347 | const result = consumer.generatedPositionFor({ 348 | line: 1, 349 | column: 0, 350 | source: "webpack://ImportsLoader/some-library.js", 351 | }); 352 | expect(bundle[result.line - 1 /* 1-indexed */]).toBe("var someCode = {"); 353 | }); 354 | 355 | it('should update source maps from previous loaders when the "devtool" option is enabled', async () => { 356 | const compiler = getCompiler( 357 | "some-library.js", 358 | {}, 359 | { 360 | devtool: "source-map", 361 | module: { 362 | rules: [ 363 | { 364 | test: /\.js$/i, 365 | rules: [ 366 | { 367 | loader: path.resolve(__dirname, "../src"), 368 | options: { imports: "lib_1" }, 369 | }, 370 | { loader: require.resolve("babel-loader") }, 371 | ], 372 | }, 373 | ], 374 | }, 375 | }, 376 | ); 377 | const stats = await compile(compiler); 378 | const bundle = readAsset( 379 | "main.bundle.js", 380 | stats.compilation.compiler, 381 | stats, 382 | ).split("\n"); 383 | const sourceMap = readAsset( 384 | "main.bundle.js.map", 385 | stats.compilation.compiler, 386 | stats, 387 | ); 388 | 389 | const consumer = new SourceMapConsumer(sourceMap); 390 | const result = consumer.generatedPositionFor({ 391 | line: 1, 392 | column: 0, 393 | source: "webpack://ImportsLoader/some-library.js", 394 | }); 395 | expect(bundle[result.line - 1 /* 1-indexed */]).toBe("var someCode = {"); 396 | }); 397 | 398 | it('should work with "default" imports without syntax', async () => { 399 | const compiler = getCompiler("some-library.js", { 400 | imports: "lib_1", 401 | }); 402 | const stats = await compile(compiler); 403 | 404 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 405 | "module", 406 | ); 407 | expect(getErrors(stats)).toMatchSnapshot("errors"); 408 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 409 | }); 410 | 411 | it('should work with "default" imports with syntax', async () => { 412 | const compiler = getCompiler("some-library.js", { 413 | imports: "default lib_1", 414 | }); 415 | const stats = await compile(compiler); 416 | 417 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 418 | "module", 419 | ); 420 | expect(getErrors(stats)).toMatchSnapshot("errors"); 421 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 422 | }); 423 | 424 | it('should work with "default" imports with syntax and name', async () => { 425 | const compiler = getCompiler("some-library.js", { 426 | imports: "default lib_1 lib_name", 427 | }); 428 | const stats = await compile(compiler); 429 | 430 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 431 | "module", 432 | ); 433 | expect(getErrors(stats)).toMatchSnapshot("errors"); 434 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 435 | }); 436 | 437 | it('should work with multiple "default" imports and different names', async () => { 438 | const compiler = getCompiler("some-library.js", { 439 | imports: ["default lib_2 lib_2", "default lib_2 lib_3"], 440 | }); 441 | 442 | const stats = await compile(compiler); 443 | 444 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 445 | "module", 446 | ); 447 | expect(getErrors(stats)).toMatchSnapshot("errors"); 448 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 449 | }); 450 | 451 | it('should work with "default" and "named" imports"', async () => { 452 | const compiler = getCompiler("some-library.js", { 453 | imports: [ 454 | "default lib_2 lib2_default", 455 | "named lib_1 lib1_method", 456 | "named lib_2 lib2_method_1", 457 | "named lib_2 lib2_method_2 lib_2_method_2_short", 458 | ], 459 | }); 460 | const stats = await compile(compiler); 461 | 462 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 463 | "module", 464 | ); 465 | expect(getErrors(stats)).toMatchSnapshot("errors"); 466 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 467 | }); 468 | 469 | it('should work with "default" and "namespace" imports"', async () => { 470 | const compiler = getCompiler("some-library.js", { 471 | imports: [ 472 | "default lib_2 lib2_default", 473 | "namespace lib_1 lib1_namespace", 474 | "namespace lib_2 lib2_namespace", 475 | ], 476 | }); 477 | const stats = await compile(compiler); 478 | 479 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 480 | "module", 481 | ); 482 | expect(getErrors(stats)).toMatchSnapshot("errors"); 483 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 484 | }); 485 | 486 | it('should work with "named" imports', async () => { 487 | const compiler = getCompiler("some-library.js", { 488 | imports: "named lib_1 lib_1_name", 489 | }); 490 | const stats = await compile(compiler); 491 | 492 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 493 | "module", 494 | ); 495 | expect(getErrors(stats)).toMatchSnapshot("errors"); 496 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 497 | }); 498 | 499 | it('should work with multiple "named" imports', async () => { 500 | const compiler = getCompiler("some-library.js", { 501 | imports: [ 502 | "named lib_2 lib_2", 503 | "named lib_2 lib_3 alias", 504 | "named lib_3 lib_4", 505 | ], 506 | }); 507 | const stats = await compile(compiler); 508 | 509 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 510 | "module", 511 | ); 512 | expect(getErrors(stats)).toMatchSnapshot("errors"); 513 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 514 | }); 515 | 516 | it('should work with multiple "named" imports from the one lib', async () => { 517 | const compiler = getCompiler("some-library.js", { 518 | imports: [ 519 | "named lib_2 lib_2", 520 | "named lib_2 lib_3 alias", 521 | "named lib_2 lib_4", 522 | ], 523 | }); 524 | const stats = await compile(compiler); 525 | 526 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 527 | "module", 528 | ); 529 | expect(getErrors(stats)).toMatchSnapshot("errors"); 530 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 531 | }); 532 | 533 | it('should work with "named" and "side-effects" imports', async () => { 534 | const compiler = getCompiler("some-library.js", { 535 | imports: ["named lib_2 lib_2", "side-effects lib_1"], 536 | }); 537 | const stats = await compile(compiler); 538 | 539 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 540 | "module", 541 | ); 542 | expect(getErrors(stats)).toMatchSnapshot("errors"); 543 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 544 | }); 545 | 546 | it('should work with "namespace" imports', async () => { 547 | const compiler = getCompiler("some-library.js", { 548 | imports: "namespace lib_1 lib_1_all", 549 | }); 550 | const stats = await compile(compiler); 551 | 552 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 553 | "module", 554 | ); 555 | expect(getErrors(stats)).toMatchSnapshot("errors"); 556 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 557 | }); 558 | 559 | it('should work with multiple "namespace" imports', async () => { 560 | const compiler = getCompiler("some-library.js", { 561 | imports: ["namespace lib_2 lib_2", "namespace lib_2 lib_3"], 562 | }); 563 | const stats = await compile(compiler); 564 | 565 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 566 | "module", 567 | ); 568 | expect(getErrors(stats)).toMatchSnapshot("errors"); 569 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 570 | }); 571 | 572 | it('should work with "side-effects" imports', async () => { 573 | const compiler = getCompiler("some-library.js", { 574 | imports: { 575 | moduleName: "lib_1", 576 | syntax: "side-effects", 577 | }, 578 | }); 579 | const stats = await compile(compiler); 580 | 581 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 582 | "module", 583 | ); 584 | expect(getErrors(stats)).toMatchSnapshot("errors"); 585 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 586 | }); 587 | 588 | it('should work with multiple "side-effects" imports', async () => { 589 | const compiler = getCompiler("some-library.js", { 590 | imports: ["side-effects lib_2", "side-effects lib_2"], 591 | }); 592 | const stats = await compile(compiler); 593 | 594 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 595 | "module", 596 | ); 597 | expect(getErrors(stats)).toMatchSnapshot("errors"); 598 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 599 | }); 600 | 601 | it('should work with "side-effects" and "default" imports', async () => { 602 | const compiler = getCompiler("some-library.js", { 603 | imports: ["side-effects lib_1", "default lib_2"], 604 | }); 605 | const stats = await compile(compiler); 606 | 607 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 608 | "module", 609 | ); 610 | expect(getErrors(stats)).toMatchSnapshot("errors"); 611 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 612 | }); 613 | 614 | it('should work with "side-effects" and "named" imports', async () => { 615 | const compiler = getCompiler("some-library.js", { 616 | imports: ["side-effects lib_1", "named lib_2 lib_2"], 617 | }); 618 | const stats = await compile(compiler); 619 | 620 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 621 | "module", 622 | ); 623 | expect(getErrors(stats)).toMatchSnapshot("errors"); 624 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 625 | }); 626 | 627 | it('should work with "side-effects" and "namespace" imports', async () => { 628 | const compiler = getCompiler("some-library.js", { 629 | imports: ["side-effects lib_1", "namespace lib_2 lib_2"], 630 | }); 631 | const stats = await compile(compiler); 632 | 633 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 634 | "module", 635 | ); 636 | expect(getErrors(stats)).toMatchSnapshot("errors"); 637 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 638 | }); 639 | 640 | it('should work with "single" imports without syntax', async () => { 641 | const compiler = getCompiler("some-library.js", { 642 | type: "commonjs", 643 | imports: ["lib_1"], 644 | }); 645 | const stats = await compile(compiler); 646 | 647 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 648 | "module", 649 | ); 650 | expect(getErrors(stats)).toMatchSnapshot("errors"); 651 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 652 | }); 653 | 654 | it('should work with "single" imports with syntax', async () => { 655 | const compiler = getCompiler("some-library.js", { 656 | type: "commonjs", 657 | imports: ["single lib_1"], 658 | }); 659 | const stats = await compile(compiler); 660 | 661 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 662 | "module", 663 | ); 664 | expect(getErrors(stats)).toMatchSnapshot("errors"); 665 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 666 | }); 667 | 668 | it('should work with "single" imports with syntax and alias', async () => { 669 | const compiler = getCompiler("some-library.js", { 670 | type: "commonjs", 671 | imports: ["single lib_1 lib_name"], 672 | }); 673 | const stats = await compile(compiler); 674 | 675 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 676 | "module", 677 | ); 678 | expect(getErrors(stats)).toMatchSnapshot("errors"); 679 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 680 | }); 681 | 682 | it('should work with multiple "single" imports and different names', async () => { 683 | const compiler = getCompiler("some-library.js", { 684 | type: "commonjs", 685 | imports: ["single lib_2 lib_2", "single lib_2 lib_3"], 686 | }); 687 | const stats = await compile(compiler); 688 | 689 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 690 | "module", 691 | ); 692 | expect(getErrors(stats)).toMatchSnapshot("errors"); 693 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 694 | }); 695 | 696 | it('should work with "multiple" imports', async () => { 697 | const compiler = getCompiler("some-library.js", { 698 | type: "commonjs", 699 | imports: ["multiple lib_1 lib_method"], 700 | }); 701 | const stats = await compile(compiler); 702 | 703 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 704 | "module", 705 | ); 706 | expect(getErrors(stats)).toMatchSnapshot("errors"); 707 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 708 | }); 709 | 710 | it('should work with multiple "multiple" imports from same library', async () => { 711 | const compiler = getCompiler("some-library.js", { 712 | type: "commonjs", 713 | imports: [ 714 | "multiple lib_2 lib2_method_1", 715 | "multiple lib_2 lib2_method_2 lib_2_method_2_short", 716 | ], 717 | }); 718 | const stats = await compile(compiler); 719 | 720 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 721 | "module", 722 | ); 723 | expect(getErrors(stats)).toMatchSnapshot("errors"); 724 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 725 | }); 726 | 727 | it('should work with multiple "multiple" imports from different libraries', async () => { 728 | const compiler = getCompiler("some-library.js", { 729 | type: "commonjs", 730 | imports: [ 731 | "multiple lib_2 lib_2", 732 | "multiple lib_2 lib_3 alias", 733 | "multiple lib_3 lib_4", 734 | ], 735 | }); 736 | const stats = await compile(compiler); 737 | 738 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 739 | "module", 740 | ); 741 | expect(getErrors(stats)).toMatchSnapshot("errors"); 742 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 743 | }); 744 | 745 | it('should work with multiple "multiple" imports from the one lib', async () => { 746 | const compiler = getCompiler("some-library.js", { 747 | type: "commonjs", 748 | imports: [ 749 | "multiple lib_2 lib_2", 750 | "multiple lib_2 lib_3 alias", 751 | "multiple lib_2 lib_4", 752 | ], 753 | }); 754 | const stats = await compile(compiler); 755 | 756 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 757 | "module", 758 | ); 759 | expect(getErrors(stats)).toMatchSnapshot("errors"); 760 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 761 | }); 762 | 763 | it('should work "pure" imports', async () => { 764 | const compiler = getCompiler("some-library.js", { 765 | type: "commonjs", 766 | imports: ["pure lib_1"], 767 | }); 768 | const stats = await compile(compiler); 769 | 770 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 771 | "module", 772 | ); 773 | expect(getErrors(stats)).toMatchSnapshot("errors"); 774 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 775 | }); 776 | 777 | it('should work with multiple "pure" imports', async () => { 778 | const compiler = getCompiler("some-library.js", { 779 | type: "commonjs", 780 | imports: ["pure lib_2", "pure lib_2"], 781 | }); 782 | const stats = await compile(compiler); 783 | 784 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 785 | "module", 786 | ); 787 | expect(getErrors(stats)).toMatchSnapshot("errors"); 788 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 789 | }); 790 | 791 | it('should work "pure" and "single" imports', async () => { 792 | const compiler = getCompiler("some-library.js", { 793 | type: "commonjs", 794 | imports: ["pure lib_1", "single lib_1", "single lib_2"], 795 | }); 796 | const stats = await compile(compiler); 797 | 798 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 799 | "module", 800 | ); 801 | expect(getErrors(stats)).toMatchSnapshot("errors"); 802 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 803 | }); 804 | 805 | it('should work "pure" and "multiple" imports', async () => { 806 | const compiler = getCompiler("some-library.js", { 807 | type: "commonjs", 808 | imports: ["pure lib_1", "multiple lib_1 lib_2_n", "multiple lib_2 lib_2"], 809 | }); 810 | const stats = await compile(compiler); 811 | 812 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 813 | "module", 814 | ); 815 | expect(getErrors(stats)).toMatchSnapshot("errors"); 816 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 817 | }); 818 | 819 | it("should work with a string syntax using ES modules", async () => { 820 | const compiler = getCompiler("some-library.js", { 821 | type: "module", 822 | imports: [ 823 | "lib_1", 824 | "default|lib_1 default_name", 825 | "default|lib_1|$", 826 | "default lib_2 lib_2_all", 827 | "named lib_2 lib2_method_1", 828 | "named lib_2 lib2_method_2 lib_2_method_2_short", 829 | "default lib_3 lib_3_default", 830 | "namespace lib_3 lib_3_all", 831 | "side-effects lib_4", 832 | ], 833 | }); 834 | const stats = await compile(compiler); 835 | 836 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 837 | "module", 838 | ); 839 | expect(getErrors(stats)).toMatchSnapshot("errors"); 840 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 841 | }); 842 | 843 | it("should work with a string syntax using CommonJS", async () => { 844 | const compiler = getCompiler("some-library.js", { 845 | type: "commonjs", 846 | imports: [ 847 | "lib_1", 848 | "single lib_1 $", 849 | "single lib_2 lib_2_all", 850 | "multiple lib_2 lib2_method_1", 851 | "multiple lib_2 lib2_method_2 lib_2_method_2_short", 852 | "pure lib_3", 853 | ], 854 | }); 855 | const stats = await compile(compiler); 856 | 857 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 858 | "module", 859 | ); 860 | expect(getErrors(stats)).toMatchSnapshot("errors"); 861 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 862 | }); 863 | 864 | it('should throw an error when "alias" can not be set using an object notation', async () => { 865 | const compiler = getCompiler("some-library.js", { 866 | imports: { 867 | moduleName: "lib_1", 868 | syntax: "side-effects", 869 | alias: "some_alias", 870 | }, 871 | }); 872 | const stats = await compile(compiler); 873 | 874 | expect(getErrors(stats)).toMatchSnapshot("errors"); 875 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 876 | }); 877 | 878 | it('should throw an error when "alias" can not be set using a string notation', async () => { 879 | const compiler = getCompiler("some-library.js", { 880 | imports: "side-effects lib_1 name some_alias", 881 | }); 882 | const stats = await compile(compiler); 883 | 884 | expect(getErrors(stats)).toMatchSnapshot("errors"); 885 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 886 | }); 887 | 888 | it('should throw an error when "name" can not be used for an object notation', async () => { 889 | const compiler = getCompiler("some-library.js", { 890 | imports: { 891 | moduleName: "lib_1", 892 | syntax: "side-effects", 893 | name: "some_name", 894 | }, 895 | }); 896 | const stats = await compile(compiler); 897 | 898 | expect(getErrors(stats)).toMatchSnapshot("errors"); 899 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 900 | }); 901 | 902 | it('should throw an error when "name" can not be used for a string notation', async () => { 903 | const compiler = getCompiler("some-library.js", { 904 | imports: "side-effects lib_1 some_alias", 905 | }); 906 | const stats = await compile(compiler); 907 | 908 | expect(getErrors(stats)).toMatchSnapshot("errors"); 909 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 910 | }); 911 | 912 | it('should throw an error when "name" do not exist using an object notation', async () => { 913 | const compiler = getCompiler("some-library.js", { 914 | imports: { 915 | moduleName: "lib_2.js", 916 | syntax: "named", 917 | }, 918 | }); 919 | const stats = await compile(compiler); 920 | 921 | expect(getErrors(stats)).toMatchSnapshot("errors"); 922 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 923 | }); 924 | 925 | it('should throw an error when "name" do not exist using a string notation', async () => { 926 | const compiler = getCompiler("some-library.js", { 927 | imports: "named lib_2.js", 928 | }); 929 | const stats = await compile(compiler); 930 | 931 | expect(getErrors(stats)).toMatchSnapshot("errors"); 932 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 933 | }); 934 | 935 | it('should throw an error when "namespace" ca not be used in CommonJS using an object notation', async () => { 936 | const compiler = getCompiler("some-library.js", { 937 | type: "commonjs", 938 | imports: [ 939 | { 940 | moduleName: "lib_4", 941 | name: "lib_4_all", 942 | syntax: "namespace", 943 | }, 944 | ], 945 | }); 946 | const stats = await compile(compiler); 947 | 948 | expect(getErrors(stats)).toMatchSnapshot("errors"); 949 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 950 | }); 951 | 952 | it('should throw an error when "namespace" ca not be used in CommonJS using a string notation', async () => { 953 | const compiler = getCompiler("some-library.js", { 954 | type: "commonjs", 955 | imports: "namespace lib_4 namespace", 956 | }); 957 | const stats = await compile(compiler); 958 | 959 | expect(getErrors(stats)).toMatchSnapshot("errors"); 960 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 961 | }); 962 | 963 | it('should throw an error when "single" ca not be used in ES module using an object notation', async () => { 964 | const compiler = getCompiler("some-library.js", { 965 | type: "module", 966 | imports: [ 967 | { 968 | moduleName: "lib_4", 969 | name: "lib_4_all", 970 | syntax: "single", 971 | }, 972 | ], 973 | }); 974 | const stats = await compile(compiler); 975 | 976 | expect(getErrors(stats)).toMatchSnapshot("errors"); 977 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 978 | }); 979 | 980 | it('should throw an error when "single" ca not be used in ES module using a string notation', async () => { 981 | const compiler = getCompiler("some-library.js", { 982 | type: "module", 983 | imports: "single lib_4 lib_4_all", 984 | }); 985 | const stats = await compile(compiler); 986 | 987 | expect(getErrors(stats)).toMatchSnapshot("errors"); 988 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 989 | }); 990 | 991 | it("should throw an error when invalid arguments for imports", async () => { 992 | const compiler = getCompiler("some-library.js", { 993 | imports: [ 994 | { 995 | moduleName: "lib_2", 996 | alias: "lib_2_method_2_short", 997 | }, 998 | ], 999 | }); 1000 | const stats = await compile(compiler); 1001 | 1002 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1003 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1004 | }); 1005 | 1006 | it("should work with inline syntax", async () => { 1007 | const compiler = getCompiler("inline.js", {}, {}, true); 1008 | const stats = await compile(compiler); 1009 | 1010 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 1011 | "module", 1012 | ); 1013 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1014 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1015 | }); 1016 | 1017 | it("should work with inline syntax #1", async () => { 1018 | const compiler = getCompiler("inline2.js", {}, {}, true); 1019 | const stats = await compile(compiler); 1020 | 1021 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 1022 | "module", 1023 | ); 1024 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1025 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1026 | }); 1027 | 1028 | it("should work with inline syntax #2", async () => { 1029 | const compiler = getCompiler("inline3.js", {}, {}, true); 1030 | const stats = await compile(compiler); 1031 | 1032 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 1033 | "module", 1034 | ); 1035 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1036 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1037 | }); 1038 | 1039 | it("should work with inline syntax #3", async () => { 1040 | const compiler = getCompiler("inline4.js", {}, {}, true); 1041 | const stats = await compile(compiler); 1042 | 1043 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 1044 | "module", 1045 | ); 1046 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1047 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1048 | }); 1049 | 1050 | it("should throw error on invalid inline syntax", async () => { 1051 | const compiler = getCompiler("inline-broken.js", {}, {}, true); 1052 | const stats = await compile(compiler); 1053 | 1054 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1055 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1056 | }); 1057 | 1058 | it("should work with a string value with spaces", async () => { 1059 | const compiler = getCompiler("some-library.js", { 1060 | imports: " lib_1 ", 1061 | }); 1062 | const stats = await compile(compiler); 1063 | 1064 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 1065 | "module", 1066 | ); 1067 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1068 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1069 | }); 1070 | 1071 | it("should throw an error on the empty string", async () => { 1072 | const compiler = getCompiler("some-library.js", { 1073 | imports: " ", 1074 | }); 1075 | const stats = await compile(compiler); 1076 | 1077 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1078 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1079 | }); 1080 | 1081 | it("should throw an error when no arguments for imports", async () => { 1082 | const compiler = getCompiler("some-library.js", {}); 1083 | const stats = await compile(compiler); 1084 | 1085 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1086 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1087 | }); 1088 | 1089 | it('should throw an error when duplicate of names found in "default" format', async () => { 1090 | const compiler = getCompiler("some-library.js", { 1091 | imports: ["default lib_1", "default lib_1"], 1092 | }); 1093 | const stats = await compile(compiler); 1094 | 1095 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1096 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1097 | }); 1098 | 1099 | it('should throw an error when duplicate of names found in "named" format', async () => { 1100 | const compiler = getCompiler("some-library.js", { 1101 | imports: ["named lib_1 foo", "named lib_1 foo"], 1102 | }); 1103 | const stats = await compile(compiler); 1104 | 1105 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1106 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1107 | }); 1108 | 1109 | it('should throw an error when duplicate of names found in "namespace" format', async () => { 1110 | const compiler = getCompiler("some-library.js", { 1111 | imports: ["namespace lib_1 foo", "namespace lib_1 foo"], 1112 | }); 1113 | const stats = await compile(compiler); 1114 | 1115 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1116 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1117 | }); 1118 | 1119 | it('should throw an error when duplicate of names found in "single" format', async () => { 1120 | const compiler = getCompiler("some-library.js", { 1121 | type: "commonjs", 1122 | imports: ["single lib_1", "single lib_1"], 1123 | }); 1124 | const stats = await compile(compiler); 1125 | 1126 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1127 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1128 | }); 1129 | 1130 | it('should throw an error when multiple duplicate of names found in "multiple" format', async () => { 1131 | const compiler = getCompiler("some-library.js", { 1132 | type: "commonjs", 1133 | imports: [ 1134 | "multiple lib_1 lib1", 1135 | "multiple lib_1 lib1", 1136 | "multiple lib_1 lib2", 1137 | "multiple lib_1 lib2", 1138 | ], 1139 | }); 1140 | const stats = await compile(compiler); 1141 | 1142 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1143 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1144 | }); 1145 | 1146 | it('should throw an error when duplicate of names found in "named" format with other syntaxes', async () => { 1147 | const compiler = getCompiler("some-library.js", { 1148 | imports: [ 1149 | "named lib_1 lib1", 1150 | "named lib_1 lib1", 1151 | "named lib_1 lib2", 1152 | "named lib_1 lib2", 1153 | "side-effects lib_2", 1154 | ], 1155 | }); 1156 | const stats = await compile(compiler); 1157 | 1158 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1159 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1160 | }); 1161 | 1162 | it('should throw an error when duplicate of names found in "multiple" format with other syntaxes', async () => { 1163 | const compiler = getCompiler("some-library.js", { 1164 | type: "commonjs", 1165 | imports: [ 1166 | "multiple lib_1 lib1", 1167 | "multiple lib_1 lib1", 1168 | "multiple lib_1 lib2", 1169 | "multiple lib_1 lib2", 1170 | "pure lib_2", 1171 | ], 1172 | }); 1173 | const stats = await compile(compiler); 1174 | 1175 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1176 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1177 | }); 1178 | 1179 | it('should throw an error when duplicate of aliases found in "named"', async () => { 1180 | const compiler = getCompiler("some-library.js", { 1181 | imports: ["named lib_1 lib1 alias1", "named lib_1 lib2 alias1"], 1182 | }); 1183 | const stats = await compile(compiler); 1184 | 1185 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1186 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1187 | }); 1188 | 1189 | it('should throw an error when duplicate of aliases found in "multiple"', async () => { 1190 | const compiler = getCompiler("some-library.js", { 1191 | type: "commonjs", 1192 | imports: ["multiple lib_1 lib1 alias1", "multiple lib_1 lib2 alias1"], 1193 | }); 1194 | const stats = await compile(compiler); 1195 | 1196 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1197 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1198 | }); 1199 | 1200 | it('should throw an error when duplicate of alias and name found in "named"', async () => { 1201 | const compiler = getCompiler("some-library.js", { 1202 | imports: [ 1203 | "named lib_1 lib1", 1204 | "named lib_1 lib2 lib1", 1205 | "named lib_1 lib3", 1206 | "named lib_1 lib3 foo", 1207 | "named lib_2 lib1", 1208 | "named lib_3 toString", 1209 | "named lib_3 toString", 1210 | ], 1211 | }); 1212 | const stats = await compile(compiler); 1213 | 1214 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1215 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1216 | }); 1217 | 1218 | it('should throw an error when duplicate of alias and name found in "multiple"', async () => { 1219 | const compiler = getCompiler("some-library.js", { 1220 | type: "commonjs", 1221 | imports: [ 1222 | "multiple lib_1 lib1", 1223 | "multiple lib_1 lib2 lib1", 1224 | "multiple lib_1 lib3", 1225 | "multiple lib_1 lib3 foo", 1226 | "multiple lib_2 lib1", 1227 | "multiple lib_3 toString", 1228 | "multiple lib_3 toString", 1229 | ], 1230 | }); 1231 | const stats = await compile(compiler); 1232 | 1233 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1234 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1235 | }); 1236 | 1237 | it("should throw an error when more then one command separator", async () => { 1238 | const compiler = getCompiler("some-library.js", { 1239 | type: "module", 1240 | imports: ["default | lib_1 default_name"], 1241 | }); 1242 | const stats = await compile(compiler); 1243 | 1244 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1245 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1246 | }); 1247 | 1248 | it('should work and union "default" with "named"', async () => { 1249 | const compiler = getCompiler("some-library.js", { 1250 | imports: ["default lib_1", "named lib_1 lib_method"], 1251 | }); 1252 | const stats = await compile(compiler); 1253 | 1254 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 1255 | "module", 1256 | ); 1257 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1258 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1259 | }); 1260 | 1261 | it('should work and union "default" with "namespace"', async () => { 1262 | const compiler = getCompiler("some-library.js", { 1263 | imports: ["default lib_1", "namespace lib_1 ns"], 1264 | }); 1265 | const stats = await compile(compiler); 1266 | 1267 | expect(getModuleSource("./some-library.js", stats)).toMatchSnapshot( 1268 | "module", 1269 | ); 1270 | expect(getErrors(stats)).toMatchSnapshot("errors"); 1271 | expect(getWarnings(stats)).toMatchSnapshot("warnings"); 1272 | }); 1273 | }); 1274 | --------------------------------------------------------------------------------