Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 14.x]
webpack-version: [4, latest]
webpack-version: [latest]

runs-on: ${{ matrix.os }}

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The documentation and syntax examples can be read [here](#syntax).
> ⚠ `%20` is space in a query string, because you can't use spaces in URLs

```js
import $ from "expose-loader?exposes[]=$&exposes[]=jQuery!jquery";
import $ from "expose-loader?exposes=$,jQuery!jquery";
//
// Adds the `jquery` to the global object under the names `$` and `jQuery`
```
Expand All @@ -51,7 +51,7 @@ import { concat } from "expose-loader?exposes=_.concat!lodash/concat";
import {
map,
reduce,
} from "expose-loader?exposes[]=_.map|map&exposes[]=_.reduce|reduce!underscore";
} from "expose-loader?exposes=_.map|map,_.reduce|reduce!underscore";
//
// Adds the `map` and `reduce` method from `underscore` to the global object under the name `_.map` and `_.reduce`
```
Expand Down
2,040 changes: 841 additions & 1,199 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 13 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,36 @@
"dist"
],
"peerDependencies": {
"webpack": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0"
"webpack": "^5.0.0"
},
"devDependencies": {
"@babel/cli": "^7.12.8",
"@babel/core": "^7.12.9",
"@babel/preset-env": "^7.12.7",
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.1",
"cross-env": "^7.0.2",
"babel-loader": "^8.2.2",
"cross-env": "^7.0.3",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.1.4",
"husky": "^4.3.0",
"husky": "^4.3.8",
"jest": "^26.6.3",
"lint-staged": "^10.5.2",
"lint-staged": "^10.5.3",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.0",
"prettier": "^2.2.1",
"react": "^17.0.1",
"rx": "^4.1.0",
"standard-version": "^9.0.0",
"standard-version": "^9.1.0",
"styled-components": "^5.2.1",
"webpack": "^5.6.0"
"webpack": "^5.19.0"
},
"keywords": [
"webpack"
Expand Down
25 changes: 8 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@
Author Tobias Koppers @sokra
*/

import schema from "./options.json";

import {
getOptions,
getExposes,
contextify,
getNewUserRequest,
stringifyRequest,
getRemainingRequest,
interpolateName,
} from "loader-utils";

import { validate } from "schema-utils";

import schema from "./options.json";

import { getExposes, contextify, getNewUserRequest } from "./utils";
} from "./utils";

export default function loader() {
const options = getOptions(this);

validate(schema, options, {
name: "Expose Loader",
baseDataPath: "options",
});

const options = this.getOptions(schema);
const callback = this.async();

let exposes;
Expand Down Expand Up @@ -57,7 +48,7 @@ export default function loader() {

// Change the request from an /abolute/path.js to a relative ./path.js.
// This prevents [chunkhash] values from changing when running webpack builds in different directories.
const newRequest = contextify(this.context, getRemainingRequest(this));
const newRequest = contextify(this.context, this.remainingRequest);
const stringifiedNewRequest = stringifyRequest(this, `-!${newRequest}`);

let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;
Expand Down
1 change: 1 addition & 0 deletions src/options.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Expose Loader options",
"definitions": {
"ObjectPattern": {
"type": "object",
Expand Down
60 changes: 55 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ function resolveExposes(item) {
}

function getExposes(items) {
let result = [];
let result;
const exposeItems =
typeof items === "string" && items.includes(",") ? items.split(",") : items;

if (typeof items === "string") {
result.push(resolveExposes(items));
if (typeof exposeItems === "string") {
result = [resolveExposes(exposeItems)];
} else {
result = [].concat(items).map((item) => resolveExposes(item));
result = [].concat(exposeItems).map((item) => resolveExposes(item));
}

return result;
Expand Down Expand Up @@ -118,4 +120,52 @@ function contextify(context, request) {
.join("!");
}

export { getNewUserRequest, getExposes, contextify };
function isAbsolutePath(str) {
return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
}

function stringifyRequest(loaderContext, request) {
const splitted = request.split("!");
const context =
loaderContext.context ||
(loaderContext.options && loaderContext.options.context);

return JSON.stringify(
splitted
.map((part) => {
// First, separate singlePath from query, because the query might contain paths again
const splittedPart = part.match(/^(.*?)(\?.*)/);
const query = splittedPart ? splittedPart[2] : "";
let singlePath = splittedPart ? splittedPart[1] : part;

if (isAbsolutePath(singlePath) && context) {
singlePath = path.relative(context, singlePath);
}

return singlePath.replace(/\\/g, "/") + query;
})
.join("!")
);
}

function interpolateName(loaderContext, filename) {
let basename = "file";

if (loaderContext.resourcePath) {
const parsed = path.parse(loaderContext.resourcePath);

if (parsed.dir) {
basename = parsed.name;
}
}

return filename.replace(/\[name\]/gi, () => basename);
}

export {
getNewUserRequest,
getExposes,
contextify,
stringifyRequest,
interpolateName,
};
2 changes: 1 addition & 1 deletion test/fixtures/inline-import-2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import myExports from '../../src/cjs.js?exposes=myGlobal!./simple-commonjs2-single-export.js';
import myExports2 from '../../src/cjs.js?exposes[]=myOtherGlobal.globalObject2|globalObject2&exposes[]=myOtherGlobal.globalObject3|globalObject3!./simple-commonjs2-multiple-export.js';
import myExports2 from '../../src/cjs.js?exposes=myOtherGlobal.globalObject2|globalObject2,myOtherGlobal.globalObject3|globalObject3!./simple-commonjs2-multiple-export.js';

export { myExports, myExports2 };
5 changes: 2 additions & 3 deletions test/helpers/compile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default (compiler) => {
return new Promise((resolve, reject) => {
export default (compiler) =>
new Promise((resolve, reject) => {
compiler.run((error, stats) => {
if (error) {
return reject(error);
Expand All @@ -8,4 +8,3 @@ export default (compiler) => {
return resolve(stats);
});
});
};
6 changes: 1 addition & 5 deletions test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
const compiler = webpack(fullConfig);

if (!config.outputFileSystem) {
const outputFileSystem = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);

compiler.outputFileSystem = outputFileSystem;
compiler.outputFileSystem = createFsFromVolume(new Volume());
}

return compiler;
Expand Down
4 changes: 1 addition & 3 deletions test/helpers/getErrors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import normalizeErrors from "./normalizeErrors";

export default (stats) => {
return normalizeErrors(stats.compilation.errors);
};
export default (stats) => normalizeErrors(stats.compilation.errors);
4 changes: 1 addition & 3 deletions test/helpers/getWarnings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import normalizeErrors from "./normalizeErrors";

export default (stats) => {
return normalizeErrors(stats.compilation.warnings);
};
export default (stats) => normalizeErrors(stats.compilation.warnings);
5 changes: 2 additions & 3 deletions test/helpers/normalizeErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ function removeCWD(str) {
.replace(new RegExp(cwd, "g"), "");
}

export default (errors) => {
return errors.map((error) =>
export default (errors) =>
errors.map((error) =>
removeCWD(error.toString().split("\n").slice(0, 2).join("\n"))
);
};
19 changes: 8 additions & 11 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/
import path from "path";

import webpack from "webpack";

import {
compile,
execute,
Expand Down Expand Up @@ -292,9 +290,7 @@ describe("loader", () => {
}
);
const stats = await compile(compiler);

const isWebpack5 = webpack.version[0] === "5";
const refRegexp = isWebpack5 ? /\?ruleSet\[\d+\].*!/ : /\?ref--[0-9-]+!/;
const refRegexp = /\?ruleSet\[\d+\].*!/;

expect(
getModuleSource(
Expand Down Expand Up @@ -380,18 +376,19 @@ describe("loader", () => {
}
);
const stats = await compile(compiler);
const { chunkGraph } = stats.compilation;

const module = Array.from(stats.compilation.modules).find((m) =>
m.id.endsWith("./simple-commonjs2-single-export-exposed.js")
chunkGraph
.getModuleId(m)
.endsWith("./simple-commonjs2-single-export-exposed.js")
);
const isWebpack5 = webpack.version[0] === "5";

expect(
getModuleSource("./simple-commonjs2-single-export-exposed.js", stats)
).toMatchSnapshot("module");
expect(module.hash).toBe(
isWebpack5
? "53b5c93a2ac82d2e55921ab5bcf9649e"
: "c3e516476bee11406ecca2a29b66c743"
expect(chunkGraph.getModuleHash(module)).toBe(
"34429e229c60b66e2bb98684f7f32605"
);
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
Expand Down