@nx/next
Version:
64 lines (63 loc) • 2.63 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCopyPlugin = createCopyPlugin;
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path_1 = require("nx/src/utils/path");
const path_2 = require("path");
const fs_1 = require("fs");
function normalizeAssets(assets, root, sourceRoot) {
return assets.map((asset) => {
if (typeof asset === 'string') {
const assetPath = (0, path_1.normalizePath)(asset);
const resolvedAssetPath = (0, path_2.resolve)(root, assetPath);
const resolvedSourceRoot = (0, path_2.resolve)(root, sourceRoot);
if (!resolvedAssetPath.startsWith(resolvedSourceRoot)) {
throw new Error(`The ${resolvedAssetPath} asset path must start with the project source root: ${sourceRoot}`);
}
const isDirectory = (0, fs_1.statSync)(resolvedAssetPath).isDirectory();
const input = isDirectory
? resolvedAssetPath
: (0, path_2.dirname)(resolvedAssetPath);
const output = (0, path_2.relative)(resolvedSourceRoot, (0, path_2.resolve)(root, input));
const glob = isDirectory ? '**/*' : (0, path_2.basename)(resolvedAssetPath);
return {
input,
output,
glob,
};
}
else {
if (asset.output.startsWith('..')) {
throw new Error('An asset cannot be written to a location outside of the output path.');
}
const assetPath = (0, path_1.normalizePath)(asset.input);
const resolvedAssetPath = (0, path_2.resolve)(root, assetPath);
return {
...asset,
input: resolvedAssetPath,
// Now we remove starting slash to make Webpack place it from the output root.
output: asset.output.replace(/^\//, ''),
};
}
});
}
function createCopyPlugin(assets, root, sourceRoot) {
return new CopyWebpackPlugin({
patterns: normalizeAssets(assets, root, sourceRoot).map((asset) => {
return {
context: asset.input,
to: (0, path_2.join)('../public', asset.output),
from: asset.glob,
globOptions: {
ignore: [
'.gitkeep',
'**/.DS_Store',
'**/Thumbs.db',
...(asset.ignore ?? []),
],
dot: true,
},
};
}),
});
}
;