Skip to content

Commit

Permalink
Miscellaneous updates (#142)
Browse files Browse the repository at this point in the history
* Improved usage with webpack-dev-server

- Updated inDevServer() to not check env var
- Updated shouldWriteToDisk() to check options.devServer.writeToDisk

* Merge entrypoints data

* Updated dependencies
  • Loading branch information
webdeveric authored Apr 2, 2021
1 parent fc1ca85 commit 1417929
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 46 deletions.
52 changes: 26 additions & 26 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 @@ -75,7 +75,7 @@
"chai-spies": "^1.0.0",
"compression-webpack-plugin": "^7.1.2",
"copy-webpack-plugin": "^8.1.0",
"cspell": "^5.2.4",
"cspell": "^5.3.10",
"css-loader": "^5.0.2",
"eslint": "^7.19.0",
"file-loader": "^6.2.0",
Expand Down
47 changes: 38 additions & 9 deletions src/WebpackAssetsManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,13 @@ class WebpackAssetsManifest
this.setRaw( key, entrypoints[ key ] );
}
} else {
this.setRaw( this.options.entrypointsKey, entrypoints );
this.setRaw(
this.options.entrypointsKey,
{
...this.get( this.options.entrypointsKey ),
...entrypoints,
},
);
}
}

Expand Down Expand Up @@ -672,12 +678,24 @@ class WebpackAssetsManifest
shouldWriteToDisk(compilation)
{
if ( this.options.writeToDisk === 'auto' ) {
// Return true if using webpack-dev-server and the manifest output is above the compiler outputPath.
return this.inDevServer() &&
path.relative(
this.compiler.outputPath,
this.getManifestPath( compilation, this.getOutputPath() ),
).startsWith('..');
if ( this.inDevServer() ) {
const wdsWriteToDisk = get( compilation, 'options.devServer.writeToDisk' );

if ( wdsWriteToDisk === true ) {
return false;
}

const manifestPath = this.getManifestPath( compilation, this.getOutputPath() );

if ( typeof wdsWriteToDisk === 'function' && wdsWriteToDisk( manifestPath ) === true ) {
return false;
}

// Return true if the manifest output is above the compiler outputPath.
return path.relative( this.compiler.outputPath, manifestPath ).startsWith('..');
}

return false;
}

return this.options.writeToDisk;
Expand Down Expand Up @@ -787,13 +805,24 @@ class WebpackAssetsManifest
/**
* Determine if webpack-dev-server is being used
*
* The WEBPACK_DEV_SERVER env var was added in webpack-dev-server 3.4.1
* The WEBPACK_DEV_SERVER / WEBPACK_SERVE env vars cannot be relied upon.
* See issue {@link https://github.com/webdeveric/webpack-assets-manifest/issues/125|#125}
*
* @return {boolean}
*/
inDevServer()
{
return !! process.env.WEBPACK_DEV_SERVER;
const [ , webpackPath, serve ] = process.argv;

if ( serve === 'serve' && webpackPath && path.basename(webpackPath) === 'webpack' ) {
return true;
}

if ( process.argv.some( arg => arg.includes('webpack-dev-server') ) ) {
return true;
}

return has(this, 'compiler.outputFileSystem') && this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem';
}

/**
Expand Down
113 changes: 103 additions & 10 deletions test/WebpackAssetsManifest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,26 +275,54 @@ describe('WebpackAssetsManifest', function() {
});

describe('inDevServer()', function() {
let originalEnv;
let originalArgv;

before(() => {
originalEnv = { ...process.env };
beforeEach(() => {
originalArgv = process.argv.slice(0);
});

after(() => {
process.env = originalEnv;
afterEach(() => {
process.argv = originalArgv;
});

it('Identifies webpack-dev-server from process.env', function() {
it('Identifies `webpack serve` from argv', function() {
const manifest = new WebpackAssetsManifest();

delete process.env.WEBPACK_DEV_SERVER;
assert.isFalse(manifest.inDevServer());

assert.isFalse( manifest.inDevServer() );
process.argv = [
originalArgv[ 0 ],
path.join(
path.dirname( originalArgv[ 1 ] ),
'webpack',
),
'serve',
];

assert.isTrue(manifest.inDevServer());
});

it('Identifies webpack-dev-server from argv', function() {
const manifest = new WebpackAssetsManifest();

assert.isFalse(manifest.inDevServer());

process.argv.push('webpack-dev-server');

assert.isTrue(manifest.inDevServer());
});

it('Identifies webpack-dev-server from outputFileSystem', function() {
const config = configs.hello();

config.output.path = '/';

const compiler = makeCompiler(config);
const manifest = new WebpackAssetsManifest();

process.env.WEBPACK_DEV_SERVER = true;
manifest.apply(compiler);

assert.isTrue( manifest.inDevServer() );
assert.isTrue(manifest.inDevServer());
});
});

Expand Down Expand Up @@ -920,6 +948,26 @@ describe('WebpackAssetsManifest', function() {
});
});

describe('entrypoints and assets', () => {
it('entrypoints in shared assets get merged', async () => {
const options = {
assets: Object.create(null),
entrypoints: true,
};

const { manifest, run: run1 } = create( configs.hello(), options );

const { run: run2 } = create( configs.client(), options );

await Promise.all([ run1(), run2() ]);

const entrypointsKeys = Object.keys( manifest.get('entrypoints') );

expect( entrypointsKeys ).to.contain('main');
expect( entrypointsKeys ).to.contain('client');
});
});

describe('done', function() {
it('is called when compilation is done', async () => {
const mock1 = chai.spy( async () => true );
Expand Down Expand Up @@ -961,6 +1009,51 @@ describe('WebpackAssetsManifest', function() {
});
});

describe('writeToDisk', function() {
it('Uses webpack config options.devServer.writeToDisk when available', async () => {
const { manifest } = create(
configs.devServer(),
{
writeToDisk: 'auto',
},
);

const mockCompilation = {
options: {
devServer: {
writeToDisk: true,
},
},
};

// The plugin shouldn't write to disk if the dev server is configured to do it.
expect( manifest.shouldWriteToDisk( mockCompilation ) ).to.be.false;
});

it('Calls options.devServer.writeToDisk() with manifest path', async () => {
const { manifest } = create(
configs.devServer( __dirname ),
{
writeToDisk: 'auto',
},
);

const mockCompilation = {
getPath: filename => filename,
options: {
devServer: {
writeToDisk(filePath) {
return manifest.getOutputPath() === filePath;
},
},
},
};

// The plugin shouldn't write to disk if the dev server is configured to do it.
expect( manifest.shouldWriteToDisk( mockCompilation ) ).to.be.false;
});
});

describe('Default options', function() {
it('Defaults are used', function() {
const { manifest } = create( configs.hello() );
Expand Down

0 comments on commit 1417929

Please sign in to comment.