Skip to content

Fix: Absolute paths being used when expose-loader exposes more than one global for a library #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ typings/
/local
/reports
/node_modules
/test/outputs
.DS_Store
Thumbs.db
.idea
.vscode
*.sublime-project
*.sublime-workspace
*.sublime-workspace
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ module: {
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: 'jQuery'
},{
loader: 'expose-loader',
options: '$'
options: {
expose: ['jQuery', '$']
}
}]
}]
}
Expand Down Expand Up @@ -133,4 +132,4 @@ Please take a moment to read our contributing guidelines if you haven't yet done
[cover-url]: https://codecov.io/gh/webpack-contrib/expose-loader

[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
[chat-url]: https://gitter.im/webpack/webpack
[chat-url]: https://gitter.im/webpack/webpack
56 changes: 43 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,76 @@

const path = require('path');

function accesorString(value) {
const exposeLoaderPathRegex = new RegExp(
`^(.*)${path.sep}expose-loader${path.sep}index.js`
);

function buildAccessorAndInitializersFor(value) {
const childProperties = value.split('.');
const { length } = childProperties;
let propertyString = 'global';
let result = '';
const initializers = [];

for (let i = 0; i < length; i++) {
if (i > 0) result += `if(!${propertyString}) ${propertyString} = {};\n`;
if (i > 0)
initializers.push(`if(!${propertyString}) ${propertyString} = {};`);
propertyString += `[${JSON.stringify(childProperties[i])}]`;
}

result += `module.exports = ${propertyString}`;
return result;
return [propertyString, initializers];
}

module.exports = function loader() {};
module.exports.pitch = function pitch(remainingRequest) {
// 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 newRequestPath = remainingRequest.replace(
let newRequestPath = remainingRequest.replace(
this.resourcePath,
`.${path.sep}${path.relative(this.context, this.resourcePath)}`
);
const exposeLoaderMatch = remainingRequest.match(exposeLoaderPathRegex);
if (exposeLoaderMatch) {
const exposeLoaderPath = exposeLoaderMatch[0];
newRequestPath = newRequestPath.replace(
exposeLoaderPath,
`.${path.sep}${path.relative(this.context, exposeLoaderPath)}`
);
}

if (this.cacheable) {
this.cacheable();
}

if (!this.query) throw new Error('query parameter is missing');

let exposed;
if (typeof this.query === 'string') {
exposed = [this.query.substr(1)];
} else if (this.query.expose) {
exposed = this.query.expose;
}

/*
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
*
* fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/55
* - https://github.com/webpack-contrib/expose-loader/issues/49
*/
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
*
* fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/55
* - https://github.com/webpack-contrib/expose-loader/issues/49
*/
this._module.userRequest = `${this._module.userRequest}-exposed`;

let initializers = [];
const accessors = [];
exposed.forEach((value) => {
const [accessor, valueInitializers] = buildAccessorAndInitializersFor(
value
);
accessors.push(accessor);
initializers = initializers.concat(valueInitializers);
});
return (
`${accesorString(this.query.substr(1))} = ` +
`${initializers.join('\n')}\nmodule.exports = ${accessors.join(' = ')} =` +
`require(${JSON.stringify(`-!${newRequestPath}`)});`
);
};
118 changes: 30 additions & 88 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"eslint-plugin-prettier": "^2.6.0",
"husky": "^0.14.3",
"jest": "^22.4.3",
"jsdom": "^11.11.0",
"lint-staged": "^7.0.4",
"memory-fs": "^0.4.1",
"nsp": "^3.2.1",
Expand Down
Loading