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
32 changes: 26 additions & 6 deletions lib/plugins/ramdisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,36 @@
*/
/* eslint-disable no-param-reassign */
const crypto = require('crypto');
const { symlinkSync } = require('fs');
const { symlinkSync, readFileSync } = require('fs');
const { basename, join, resolve } = require('path');

const isCwd = require('is-path-cwd');
const readPkgUp = require('read-pkg-up');
const escalade = require('escalade/sync');
const rm = require('rimraf');
const { WebpackPluginRamdisk } = require('webpack-plugin-ramdisk');

const { PluginExistsError, RamdiskPathError } = require('../errors');

const readPkgName = () => {
const pkgPath = escalade(process.cwd(), (dir, names) => {
if (names.includes('package.json')) {
return 'package.json';
}
return false;
});
// istanbul ignore if
if (pkgPath == null) {
return null;
}
try {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
return pkg.name;
} catch (error) {
// istanbul ignore next
return null;
}
};

module.exports = {
init(compiler, options) {
const hasPlugin = compiler.options.plugins.some(
Expand All @@ -34,7 +54,7 @@ module.exports = {
throw new PluginExistsError('WebpackRamdiskPlugin exists in the specified configuration.');
}

const pkg = readPkgUp.sync() || {};
let pkgName = readPkgName();
const { path } = compiler.options.output;
const lastSegment = basename(path);
const errorInfo = `The ramdisk option creates a symlink from \`output.path\`, to the ramdisk build output path, and must remove any existing \`output.path\` to do so.`;
Expand All @@ -61,14 +81,14 @@ module.exports = {
);
}

if (!pkg.name) {
if (!pkgName) {
// use md5 for a short hash that'll be consistent between wps starts
const md5 = crypto.createHash('md5');
md5.update(path);
pkg.name = md5.digest('hex');
pkgName = md5.digest('hex');
}

const newPath = join(pkg.name, lastSegment);
const newPath = join(pkgName, lastSegment);

// clean up the output path in prep for the ramdisk plugin
compiler.options.output.path = newPath;
Expand Down
66 changes: 51 additions & 15 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"chalk": "^4.0.0",
"connect-history-api-fallback": "^1.5.0",
"escalade": "^3.1.0",
"globby": "^11.0.0",
"http-proxy-middleware": "^1.0.3",
"is-path-cwd": "^2.2.0",
Expand All @@ -54,7 +55,6 @@
"onetime": "^5.1.0",
"open": "^7.0.3",
"p-defer": "^3.0.0",
"read-pkg-up": "^7.0.1",
"rimraf": "^3.0.2",
"strip-ansi": "^6.0.0",
"superstruct": "^0.10.12",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ramdisk-empty-pkg/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const main = document.querySelector('main');
main.innerHTML = 'main';
1 change: 1 addition & 0 deletions test/fixtures/ramdisk-empty-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
29 changes: 29 additions & 0 deletions test/fixtures/ramdisk-empty-pkg/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { resolve } = require('path');

const getPort = require('get-port');

const { WebpackPluginServe: Serve } = require('../../../lib/');

module.exports = {
context: __dirname,
entry: ['./app.js', 'webpack-plugin-serve/client'],
mode: 'development',
output: {
filename: './output.js',
path: resolve(__dirname, './output'),
publicPath: 'output/'
},
plugins: [
new Serve({
host: 'localhost',
port: getPort({ port: 55555 }),
ramdisk: true
})
],
resolve: {
alias: {
'webpack-plugin-serve/client': resolve(__dirname, '../../../client')
}
},
watch: true
};
Loading