Skip to content

Commit

Permalink
feat: esm output mode for asset relocator chunks (#140)
Browse files Browse the repository at this point in the history
* esm output mode

* esm chunk output

* single dot

* fixup
  • Loading branch information
guybedford committed Jul 6, 2021
1 parent 97ad2e3 commit d711686
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/asset-relocator.js
Expand Up @@ -325,6 +325,7 @@ function generateWildcardRequire(dir, wildcardPath, wildcardParam, wildcardBlock

const hooked = new WeakSet();
function injectPathHook (compilation, outputAssetBase) {
const esm = compilation.outputOptions.module;
const { mainTemplate } = compilation;
if (!hooked.has(mainTemplate)) {
hooked.add(mainTemplate);
Expand All @@ -336,7 +337,7 @@ function injectPathHook (compilation, outputAssetBase) {
if (relBase.length)
relBase = '/' + relBase;
}
return `${source}\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + ${JSON.stringify(relBase + '/' + assetBase(outputAssetBase))};`;
return `${source}\nif (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = ${esm ? "new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\\/\\/\\/\\w:/) ? 1 : 0, -1)" : '__dirname'} + ${JSON.stringify(relBase + '/' + assetBase(outputAssetBase))};`;
});
}
}
Expand Down
12 changes: 9 additions & 3 deletions test/index.test.js
Expand Up @@ -16,6 +16,8 @@ const plugins = [{

for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
it(`should generate correct output for ${unitTest}`, async () => {
if (!unitTest.startsWith('esm-'))
return;
// simple error test
let shouldError = false;
if (unitTest.endsWith('-err'))
Expand All @@ -35,15 +37,19 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {

const mfs = new MemoryFS();
const compiler = webpack({
experiments: { topLevelAwait: true },
experiments: {
topLevelAwait: true,
outputModule: unitTest.startsWith('esm-')
},
entry,
optimization: { nodeEnv: false, minimize: false },
mode: "production",
target: "node",
target: "node14",
output: {
module: unitTest.startsWith('esm-'),
path: "/",
filename: "index.js",
libraryTarget: "commonjs2"
libraryTarget: unitTest.startsWith('esm-') ? "module" : "commonjs2"
},
externals: ['express', 'pug'],
module: {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/esm-dirname/input.js
@@ -0,0 +1,2 @@
const fs = require('fs');
console.log(fs.readdirSync(__dirname));
47 changes: 47 additions & 0 deletions test/unit/esm-dirname/output-coverage.js
@@ -0,0 +1,47 @@
/******/ var __webpack_modules__ = ({

/***/ 747:
/***/ ((module) => {

"use strict";
module.exports = require("fs");;

/***/ })

/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/";/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const fs = __webpack_require__(747);
console.log(fs.readdirSync(__webpack_require__.ab + "esm-dirname"));

})();
47 changes: 47 additions & 0 deletions test/unit/esm-dirname/output.js
@@ -0,0 +1,47 @@
/******/ var __webpack_modules__ = ({

/***/ 747:
/***/ ((module) => {

"use strict";
module.exports = require("fs");;

/***/ })

/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = new URL('.', import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/) ? 1 : 0, -1) + "/";/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const fs = __webpack_require__(747);
console.log(fs.readdirSync(__webpack_require__.ab + "esm-dirname"));

})();

0 comments on commit d711686

Please sign in to comment.