Skip to content

Commit 9a30501

Browse files
wadahiroSimenB
authored andcommitted
Add outputPath option (#16)
* Add outputPath option * Add a test for `outputPath` option
1 parent 6fde40c commit 9a30501

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ Type: `boolean`, default: `true`
8888

8989
If `true`, will add `filepath + '.map'` to the compilation as well.
9090

91+
#### `outputPath`
92+
Type: `string`
93+
94+
If set, will be used as the output directory of the file.
95+
9196
#### `publicPath`
9297
Type: `string`
9398

addAssetHtmlPlugin.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ function resolvePublicPath(compilation, filename) {
2020
return ensureTrailingSlash(publicPath);
2121
}
2222

23-
function addFileToAssets(compilation, htmlPluginData, { filepath, typeOfAsset = 'js', includeSourcemap = true, hash = false, publicPath }) {
23+
function resolveOutput(compilation, addedFilename, outputPath) {
24+
if (outputPath && outputPath.length) {
25+
compilation.assets[`${outputPath}/${addedFilename}`] = compilation.assets[addedFilename]; // eslint-disable-line no-param-reassign
26+
delete compilation.assets[addedFilename]; // eslint-disable-line no-param-reassign
27+
}
28+
}
29+
30+
function addFileToAssets(compilation, htmlPluginData,
31+
{ filepath, typeOfAsset = 'js', includeSourcemap = true, hash = false, publicPath, outputPath }) {
2432
if (!filepath) {
2533
const error = new Error('No filepath defined');
2634
compilation.errors.push(error);
@@ -43,11 +51,17 @@ function addFileToAssets(compilation, htmlPluginData, { filepath, typeOfAsset =
4351

4452
htmlPluginData.assets[typeOfAsset].unshift(resolvedPath);
4553

54+
resolveOutput(compilation, addedFilename, outputPath);
55+
4656
return resolvedPath;
4757
})
4858
.then(() => {
4959
if (includeSourcemap) {
50-
return htmlPluginData.plugin.addFileToAssets(`${filepath}.map`, compilation);
60+
return htmlPluginData.plugin.addFileToAssets(`${filepath}.map`, compilation)
61+
.then(addedFilename => {
62+
resolveOutput(compilation, addedFilename, outputPath);
63+
return null;
64+
});
5165
}
5266
return null;
5367
});

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ interface PluginOptions {
55
typeOfAsset?: string;
66
includeSourcemap?: boolean;
77
hash?: boolean;
8+
outputPath?: string;
89
publicPath?: string;
910
}
1011

test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,28 @@ test('should add to css if `typeOfAsset` is css', async t => {
161161
t.true(callback.calledOnce);
162162
t.true(callback.calledWithExactly(null, pluginData));
163163
});
164+
165+
test('should replace compilation assets key if `outputPath` is set', async t => {
166+
const callback = sinon.stub();
167+
const source = { source: () => 'test' };
168+
const addFileToAssetsMock = (filename, compilation) => {
169+
const name = path.basename(filename);
170+
compilation.assets[name] = source; // eslint-disable-line no-param-reassign
171+
return Promise.resolve(name);
172+
};
173+
const compilation = {
174+
options: { output: {} },
175+
assets: {},
176+
};
177+
const pluginData = { assets: { js: [], css: [] }, plugin: { addFileToAssets: addFileToAssetsMock } };
178+
179+
await addAllAssetsToCompilation([{ filepath: 'my-file.js', outputPath: 'assets' }], compilation, pluginData, callback);
180+
181+
t.deepEqual(pluginData.assets.css, []);
182+
t.deepEqual(pluginData.assets.js, ['my-file.js']);
183+
184+
t.is(compilation.assets['my-file.js'], undefined);
185+
t.deepEqual(compilation.assets['assets/my-file.js'], source);
186+
t.is(compilation.assets['my-file.js.map'], undefined);
187+
t.deepEqual(compilation.assets['assets/my-file.js.map'], source);
188+
});

0 commit comments

Comments
 (0)