gatsby-plugin-asset-path
Version:
Gatsby plugin that copies JS and CSS files and static folder to a custom folder
125 lines (97 loc) • 5.08 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.onPostBuild = exports.onCreateWebpackConfig = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var fs = _interopRequireWildcard(require("fs-extra"));
var path = _interopRequireWildcard(require("path"));
/* eslint no-console: 0 */
/**
* Removes the .map files
* @see {@link https://next.gatsbyjs.org/docs/node-apis/#onCreateWebpackConfig}
* @see {@link https://github.com/gatsbyjs/gatsby/blob/v2.0.0-alpha.20/packages/gatsby/src/utils/webpack.config.js#L411}
* @see {@link https://github.com/gatsbyjs/gatsby/blob/v2.0.0-alpha.20/packages/gatsby/src/utils/webpack.config.js#L240}
* @param {*} param0
*/
var onCreateWebpackConfig = function onCreateWebpackConfig(_ref, _ref2) {
var stage = _ref.stage,
actions = _ref.actions;
var removeMapFiles = _ref2.removeMapFiles;
if (removeMapFiles === true && stage === "build-javascript") {
actions.setWebpackConfig({
devtool: false
});
}
};
/**
* Moves assets to assets folder
* @see {@link https://next.gatsbyjs.org/docs/node-apis/#onPostBuild}
*/
exports.onCreateWebpackConfig = onCreateWebpackConfig;
var onPostBuild = /*#__PURE__*/function () {
var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_ref3, // this is specified by the 'assetPrefix' parameter in gatsby-config (note 'asset' instead of 'path'!)
_ref4) {
var pathPrefix, _ref4$additionalPaths, additionalPaths, _ref4$paths, paths, _ref4$fileTypes, fileTypes, publicFolder, assetFolder, copy, filesExtensions, filesRegex, filterFilesIn, filesInPublicFolder, thingsToCopy;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
pathPrefix = _ref3.pathPrefix;
_ref4$additionalPaths = _ref4.additionalPaths, additionalPaths = _ref4$additionalPaths === void 0 ? [] : _ref4$additionalPaths, _ref4$paths = _ref4.paths, paths = _ref4$paths === void 0 ? ["static", "page-data"] : _ref4$paths, _ref4$fileTypes = _ref4.fileTypes, fileTypes = _ref4$fileTypes === void 0 ? ["js", "css"] : _ref4$fileTypes;
// this is for deploying to external domains...
// it writes the files correctly with the domain coded into the generated files,
// but prevents them from being written into an incorrect folder like `/public/.https:/foobar.netlify.com/assets/`
if (pathPrefix.indexOf("//") !== -1) {
// eslint-disable-next-line no-useless-escape
pathPrefix = pathPrefix.replace(/^.*\/\/[^\/]+/, "");
}
publicFolder = "./public";
assetFolder = path.join(publicFolder, ".".concat(pathPrefix));
if (additionalPaths.length) {
console.warn("gatsby-plugin-asset-path argument 'additionalPaths' is deprecated, use 'paths'");
}
if (pathPrefix === "") {
console.error("gatsby-plugin-asset-path requires both:\n- 'assetPrefix' set in gatsby-config\n- 'gatsby build' to be run with the '--prefix-paths' flag");
}
if (paths.includes("icons")) {
console.error("gatsby-plugin-asset-path needs to copy 'icons' to work with 'gatsby-plugin-manifest'\ndo not add 'icons' to paths parameter!");
}
copy = function copy(fileOrFolder) {
var currentPath = path.join(publicFolder, fileOrFolder);
var newPath = path.join(assetFolder, fileOrFolder);
try {
if (fs.existsSync(currentPath)) {
return fs.copy(currentPath, newPath);
}
} catch (err) {
console.error(err);
return Promise.resolve();
}
};
filesExtensions = fileTypes.join("|");
filesRegex = RegExp(".*.(".concat(filesExtensions, ")$"));
filterFilesIn = function filterFilesIn(folder) {
return fs.readdirSync(folder).filter(function (file) {
return filesRegex.test(file);
});
};
filesInPublicFolder = filterFilesIn(publicFolder);
thingsToCopy = ["icons"].concat(paths).concat(additionalPaths).concat(filesInPublicFolder); // Copy all files to the new prefix
_context.next = 16;
return Promise.all(thingsToCopy.map(copy));
case 16:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function onPostBuild(_x, _x2) {
return _ref5.apply(this, arguments);
};
}();
exports.onPostBuild = onPostBuild;