Skip to content
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

fix: 1.7.3 to 1.7.4 regression #193

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
coverage
test/**/dist
test/unit/**/actual.js
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"license": "MIT",
"main": "./dist/index.js",
"scripts": {
"build": "ncc build src/asset-relocator.js -e resolve",
"build": "node ./scripts/build.js",
"codecov": "codecov",
"test": "npm run build && jest",
"test": "yarn run build && jest",
"test-pnp": "yarn pack -f test/yarn-pnp/assets-plugin-tarball.tgz && cd test/yarn-pnp && echo '' > yarn.lock && YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install && yarn run build && yarn run test",
"test-coverage": "jest --coverage --globals \"{\\\"coverage\\\":true}\" && codecov"
},
Expand Down
28 changes: 28 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs')
const path = require('path')
const ncc = require('@vercel/ncc')

const build = () => {
const entry = path.resolve(__dirname, '../src/asset-relocator.js')

ncc(entry, {
externals: ["resolve"],
minify: true
}).then(({ code }) => {
const distFile = path.resolve(__dirname, '../dist/index.js')

// replace all __nccwpck_require__ with __webpack_require__ caused by ncc
const newCode = code.replace(
/__nccwpck_require__/g,
'__webpack_require__'
)

fs.writeFileSync(distFile, newCode)
}).catch(err => {
console.error(err)

process.exit(1)
})
}

build()
43 changes: 34 additions & 9 deletions src/asset-relocator.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,23 +327,48 @@ function generateWildcardRequire(dir, wildcardPath, wildcardParam, wildcardBlock
return `__ncc_wildcard$${wildcardBlockIndex}(${wildcardParam})`;
}

const hooked = new WeakSet();
function injectPathHook (compilation, outputAssetBase) {
const esm = compilation.outputOptions.module;
const { mainTemplate } = compilation;
if (!hooked.has(mainTemplate)) {
hooked.add(mainTemplate);

mainTemplate.hooks.requireExtensions.tap("asset-relocator-loader", (source, chunk) => {
const { RuntimeModule, RuntimeGlobals } = compilation.compiler.webpack

class AssetRelocatorLoaderRuntimeModule extends RuntimeModule {
constructor({
relBase
}) {
super('asset-relocator-loader');

this.relBase = relBase
}

generate() {
const requireBase = `${esm ? "new URL('', import.meta.url).pathname.slice(import.meta.url.match(/^file:\\/\\/\\/\\w:/) ? 1 : 0, -1)" : '__dirname'} + ${JSON.stringify(this.relBase + '/' + assetBase(outputAssetBase))}`;

return `if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = ${requireBase};`
}
}

compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.require)
.tap('asset-relocator-loader', (chunk) => {
let relBase = '';

if (chunk.name) {
relBase = path.relative(path.dirname(chunk.name), '.').replace(/\\/g, '/');
if (relBase.length)

if (relBase.length) {
relBase = '/' + relBase;
}
}
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))};`;
});
}

try {
compilation.addRuntimeModule(chunk, new AssetRelocatorLoaderRuntimeModule({ relBase }));
} catch (error) {
console.error(error);
}

return true;
});
}

module.exports = async function (content, map) {
Expand Down
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
try {
code = mfs.readFileSync("/index.js", "utf8");
}
catch (e) {
catch (err) {
throw new Error(stats.toString());
}

Expand All @@ -112,10 +112,10 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
.replace(/\r/g, "");
try {
expect(actual).toBe(expected);
} catch (e) {
} catch (err) {
// useful for updating fixtures
fs.writeFileSync(`${testDir}/actual.js`, actual);
throw e;
throw err;
}

// very simple asset validation in unit tests
Expand Down
5 changes: 0 additions & 5 deletions test/unit/amd-disable/output-coverage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/******/ (() => { // webpackBootstrap
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
if (typeof define === 'function' && define.amd)
define(function (require) {
Expand Down
5 changes: 0 additions & 5 deletions test/unit/amd-disable/output.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/******/ (() => { // webpackBootstrap
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
if (typeof define === 'function' && define.amd)
define(function (require) {
Expand Down
7 changes: 4 additions & 3 deletions test/unit/array-emission/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ module.exports = require("fs");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand Down
11 changes: 6 additions & 5 deletions test/unit/array-emission/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ module.exports = require("fs");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand All @@ -49,8 +50,8 @@ var __webpack_exports__ = {};
const fs = __webpack_require__(147);

const REPORT_JAVASCRIPT = [
fs.readFileSync(__nccwpck_require__.ab + "util.js", 'utf8'),
fs.readFileSync(__nccwpck_require__.ab + "dom.js", 'utf8'),
fs.readFileSync(__webpack_require__.ab + "util.js", 'utf8'),
fs.readFileSync(__webpack_require__.ab + "dom.js", 'utf8'),
].join(';\n');

})();
Expand Down
5 changes: 0 additions & 5 deletions test/unit/array-holes/output-coverage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/******/ (() => { // webpackBootstrap
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
var a = [1,,2];

Expand Down
5 changes: 0 additions & 5 deletions test/unit/array-holes/output.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/******/ (() => { // webpackBootstrap
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
var a = [1,,2];

Expand Down
7 changes: 4 additions & 3 deletions test/unit/asset-conditional/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ module.exports = require("path");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand Down
9 changes: 5 additions & 4 deletions test/unit/asset-conditional/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ module.exports = require("path");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
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 path = __webpack_require__(17);
let moduleJsPath = isHarmony ? __nccwpck_require__.ab + "asset1.txt" : __nccwpck_require__.ab + "asset2.txt";
let moduleJsPath = isHarmony ? __webpack_require__.ab + "asset1.txt" : __webpack_require__.ab + "asset2.txt";
})();

module.exports = __webpack_exports__;
Expand Down
7 changes: 4 additions & 3 deletions test/unit/asset-fs-array-expr/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ module.exports = require("path");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand Down
9 changes: 5 additions & 4 deletions test/unit/asset-fs-array-expr/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ module.exports = require("path");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand All @@ -55,7 +56,7 @@ var __webpack_exports__ = {};
const { spawn } = __webpack_require__(81);
const { join } = __webpack_require__(17);

const child = spawn(gifsicle, ['--colors', '256', __nccwpck_require__.ab + "asset1.txt"]);
const child = spawn(gifsicle, ['--colors', '256', __webpack_require__.ab + "asset1.txt"]);

})();

Expand Down
7 changes: 4 additions & 3 deletions test/unit/asset-fs-existing-asset-name/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ module.exports = require("fs");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand Down
9 changes: 5 additions & 4 deletions test/unit/asset-fs-existing-asset-name/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ module.exports = require("fs");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
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__(147);
console.log(fs.readFileSync(__nccwpck_require__.ab + "existing1.txt"));
console.log(fs.readFileSync(__webpack_require__.ab + "existing1.txt"));

})();

Expand Down
7 changes: 4 additions & 3 deletions test/unit/asset-fs-inline-assign/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ module.exports = require("path");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand Down
11 changes: 6 additions & 5 deletions test/unit/asset-fs-inline-assign/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ module.exports = require("path");
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
Expand All @@ -55,11 +56,11 @@ var __webpack_exports__ = {};
const fs = __webpack_require__(147);
const { join } = __webpack_require__(17);

console.log(fs.readFileSync(__nccwpck_require__.ab + "asset.txt", 'utf8'));
console.log(fs.readFileSync(__webpack_require__.ab + "asset.txt", 'utf8'));

(function () {
var join = () => 'nope';
console.log(fs.readFileSync(join(__nccwpck_require__.ab + "asset-fs-inline-assign", 'asset.txt'), 'utf8'));
console.log(fs.readFileSync(join(__webpack_require__.ab + "asset-fs-inline-assign", 'asset.txt'), 'utf8'));
})();
})();

Expand Down
9 changes: 5 additions & 4 deletions test/unit/asset-fs-inline-path-enc-es-2/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/asset-relocator-loader */
/******/ (() => {
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/ })();
/******/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
Expand Down Expand Up @@ -67,10 +72,6 @@
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
// ESM COMPAT FLAG
Expand Down
Loading
Loading