@cypress/webpack-preprocessor
Advanced tools
Comparing version 5.12.2 to 5.13.0
/// <reference types="webpack-dev-server" /> | ||
/// <reference types="webpack-dev-server-3" /> | ||
/// <reference types="node" /> | ||
import * as Promise from 'bluebird'; | ||
import Bluebird from 'bluebird'; | ||
import * as events from 'events'; | ||
import * as webpack from 'webpack'; | ||
import webpack from 'webpack'; | ||
declare global { | ||
var __cypressCallbackReplacementCommands: string[] | undefined; | ||
} | ||
declare type FilePath = string; | ||
@@ -26,3 +29,3 @@ /** | ||
*/ | ||
declare type FilePreprocessor = (file: FileEvent) => Promise<FilePath>; | ||
declare type FilePreprocessor = (file: FileEvent) => Bluebird<FilePath>; | ||
declare type WebpackPreprocessorFn = (options: PreprocessorOptions) => FilePreprocessor; | ||
@@ -29,0 +32,0 @@ /** |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var bluebird_1 = __importDefault(require("bluebird")); | ||
var debug_1 = __importDefault(require("debug")); | ||
var lodash_1 = __importDefault(require("lodash")); | ||
var path = __importStar(require("path")); | ||
var webpack_1 = __importDefault(require("webpack")); | ||
var utils_1 = __importDefault(require("./lib/utils")); | ||
var cross_origin_callback_store_1 = require("./lib/cross-origin-callback-store"); | ||
var typescript_overrides_1 = require("./lib/typescript-overrides"); | ||
var Promise = require("bluebird"); | ||
var _ = require("lodash"); | ||
var webpack = require("webpack"); | ||
var deferred_1 = require("./deferred"); | ||
var path = require('path'); | ||
var debug = require('debug')('cypress:webpack'); | ||
var debugStats = require('debug')('cypress:webpack:stats'); | ||
var cross_origin_callback_compile_1 = require("./lib/cross-origin-callback-compile"); | ||
var debug = (0, debug_1.default)('cypress:webpack'); | ||
var debugStats = (0, debug_1.default)('cypress:webpack:stats'); | ||
// bundle promises from input spec filename to output bundled file paths | ||
@@ -38,5 +67,5 @@ var bundles = {}; | ||
if (replaceWith === void 0) { replaceWith = ''; } | ||
err.message = _.trim(err.message.replace(partToReplace, replaceWith)); | ||
err.message = lodash_1.default.trim(err.message.replace(partToReplace, replaceWith)); | ||
if (err.stack) { | ||
err.stack = _.trim(err.stack.replace(partToReplace, replaceWith)); | ||
err.stack = lodash_1.default.trim(err.stack.replace(partToReplace, replaceWith)); | ||
} | ||
@@ -50,3 +79,3 @@ return err; | ||
// Webpack 5 error messages are much less verbose. No need to clean. | ||
if ('NormalModule' in webpack) { | ||
if ('NormalModule' in webpack_1.default) { | ||
return err; | ||
@@ -87,2 +116,3 @@ } | ||
debug('user options: %o', options); | ||
var crossOriginCallbackLoaderAdded = false; | ||
// we return function that accepts the arguments provided by | ||
@@ -106,3 +136,3 @@ // the event 'file:preprocessor' | ||
if (bundles[filePath]) { | ||
debug("already have bundle for " + filePath); | ||
debug("already have bundle for ".concat(filePath)); | ||
return bundles[filePath].promise; | ||
@@ -116,7 +146,7 @@ } | ||
? file.outputPath | ||
: file.outputPath + ".js"; | ||
: "".concat(file.outputPath, ".js"); | ||
var entry = [filePath].concat(options.additionalEntries || []); | ||
var watchOptions = options.watchOptions || {}; | ||
// user can override the default options | ||
var webpackOptions = _ | ||
var webpackOptions = lodash_1.default | ||
.chain(options.webpackOptions) | ||
@@ -131,2 +161,4 @@ .defaultTo(defaultWebpackOptions) | ||
output: { | ||
// disable automatic publicPath | ||
publicPath: '', | ||
path: path.dirname(outputPath), | ||
@@ -146,4 +178,22 @@ filename: path.basename(outputPath), | ||
(0, typescript_overrides_1.overrideSourceMaps)(true, options.typescript); | ||
// To support dynamic imports, we have to disable any code splitting. | ||
debug('Limiting number of chunks to 1'); | ||
opts.plugins = (opts.plugins || []).concat(new webpack_1.default.optimize.LimitChunkCountPlugin({ maxChunks: 1 })); | ||
}) | ||
.value(); | ||
var callbackReplacementCommands = global.__cypressCallbackReplacementCommands; | ||
if (!crossOriginCallbackLoaderAdded && !!callbackReplacementCommands) { | ||
// webpack runs loaders last-to-first and we want ours to run last | ||
// so that it's working with plain javascript | ||
webpackOptions.module.rules.unshift({ | ||
test: /\.(js|ts|jsx|tsx)$/, | ||
use: [{ | ||
loader: path.join(__dirname, 'lib/cross-origin-callback-loader'), | ||
options: { | ||
commands: callbackReplacementCommands, | ||
}, | ||
}], | ||
}); | ||
crossOriginCallbackLoaderAdded = true; | ||
} | ||
debug('webpackOptions: %o', webpackOptions); | ||
@@ -153,6 +203,6 @@ debug('watchOptions: %o', watchOptions); | ||
debug('typescript: %s', options.typescript); | ||
debug("input: " + filePath); | ||
debug("output: " + outputPath); | ||
var compiler = webpack(webpackOptions); | ||
var firstBundle = (0, deferred_1.createDeferred)(); | ||
debug("input: ".concat(filePath)); | ||
debug("output: ".concat(outputPath)); | ||
var compiler = (0, webpack_1.default)(webpackOptions); | ||
var firstBundle = utils_1.default.createDeferred(); | ||
// cache the bundle promise, so it can be returned if this function | ||
@@ -170,3 +220,3 @@ // is invoked again with the same filePath | ||
err.filePath = filePath; | ||
debug("errored bundling " + outputPath, err.message); | ||
debug("errored bundling ".concat(outputPath), err.message); | ||
var lastBundle = bundles[filePath].deferreds[bundles[filePath].deferreds.length - 1]; | ||
@@ -186,3 +236,3 @@ lastBundle.reject(err); | ||
if (jsonStats.warnings.length > 0) { | ||
debug("warnings for " + outputPath + " %o", jsonStats.warnings); | ||
debug("warnings for ".concat(outputPath, " %o"), jsonStats.warnings); | ||
} | ||
@@ -196,3 +246,3 @@ if (stats.hasErrors()) { | ||
.join('\n\n'); | ||
err.message += "\n" + errorsToAppend; | ||
err.message += "\n".concat(errorsToAppend); | ||
debug('stats had error(s) %o', jsonStats.errors); | ||
@@ -206,21 +256,64 @@ return rejectWithErr(err); | ||
} | ||
// resolve with the outputPath so Cypress knows where to serve | ||
// the file from | ||
// Seems to be a race condition where changing file before next tick | ||
var resolveAllBundles = function () { | ||
bundles[filePath].deferreds.forEach(function (deferred) { | ||
// resolve with the outputPath so Cypress knows where to serve | ||
// the file from | ||
deferred.resolve(outputPath); | ||
}); | ||
bundles[filePath].deferreds.length = 0; | ||
}; | ||
// the cross-origin-callback-loader extracts any cy.origin() callback | ||
// functions that contains Cypress.require() and stores their sources | ||
// in the CrossOriginCallbackStore. it saves the callbacks per source | ||
// files, since that's the context it has. here we need to unfurl | ||
// what dependencies the input source file has so we can know which | ||
// files stored in the CrossOriginCallbackStore to compile | ||
var handleCrossOriginCallbackFiles = function () { | ||
// get the source file and any of its dependencies | ||
var sourceFiles = jsonStats.modules | ||
.filter(function (module) { | ||
// entries have duplicate modules whose ids are numbers | ||
return lodash_1.default.isString(module.id); | ||
}) | ||
.map(function (module) { | ||
// module id is the path relative to the cwd, | ||
// e.g. ./cypress/support/e2e.js, but we need it absolute | ||
return path.join(process.cwd(), module.id); | ||
}); | ||
if (!cross_origin_callback_store_1.crossOriginCallbackStore.hasFilesFor(sourceFiles)) { | ||
debug('no cross-origin callback files'); | ||
return resolveAllBundles(); | ||
} | ||
(0, cross_origin_callback_compile_1.compileCrossOriginCallbackFiles)(cross_origin_callback_store_1.crossOriginCallbackStore.getFilesFor(sourceFiles), { | ||
originalFilePath: filePath, | ||
webpackOptions: webpackOptions, | ||
}) | ||
.then(function () { | ||
debug('resolve all after handling cross-origin callback files'); | ||
resolveAllBundles(); | ||
}) | ||
.catch(function (err) { | ||
rejectWithErr(err); | ||
}) | ||
.finally(function () { | ||
cross_origin_callback_store_1.crossOriginCallbackStore.reset(filePath); | ||
}); | ||
}; | ||
// seems to be a race condition where changing file before next tick | ||
// does not cause build to rerun | ||
Promise.delay(0).then(function () { | ||
bluebird_1.default.delay(0).then(function () { | ||
if (!bundles[filePath]) { | ||
return; | ||
} | ||
bundles[filePath].deferreds.forEach(function (deferred) { | ||
deferred.resolve(outputPath); | ||
}); | ||
bundles[filePath].deferreds.length = 0; | ||
if (!callbackReplacementCommands) { | ||
return resolveAllBundles(); | ||
} | ||
handleCrossOriginCallbackFiles(); | ||
}); | ||
}; | ||
var plugin = { name: 'CypressWebpackPreprocessor' }; | ||
// this event is triggered when watching and a file is saved | ||
var plugin = { name: 'CypressWebpackPreprocessor' }; | ||
var onCompile = function () { | ||
debug('compile', filePath); | ||
var nextBundle = (0, deferred_1.createDeferred)(); | ||
var nextBundle = utils_1.default.createDeferred(); | ||
bundles[filePath].promise = nextBundle.promise; | ||
@@ -272,2 +365,10 @@ bundles[filePath].deferreds.push(nextBundle); | ||
} | ||
// clean up temp dir where cross-origin callback files are output | ||
var tmpdir = utils_1.default.tmpdir(utils_1.default.hash(filePath)); | ||
debug('remove temp directory:', tmpdir); | ||
utils_1.default.rmdir(tmpdir).catch(function (err) { | ||
// not the end of the world if removing the tmpdir fails, but we | ||
// don't want it to crash the whole process by going uncaught | ||
debug('failed removing temp directory: %s', err.stack); | ||
}); | ||
}); | ||
@@ -274,0 +375,0 @@ // return the promise, which will resolve with the outputPath or reject |
{ | ||
"name": "@cypress/webpack-preprocessor", | ||
"version": "5.12.2", | ||
"version": "5.13.0", | ||
"description": "Cypress preprocessor for bundling JavaScript via webpack", | ||
@@ -20,3 +20,3 @@ "private": false, | ||
"check-ts": "tsc --noEmit", | ||
"watch": "yarn build --watch" | ||
"watch": "rimraf dist && tsc --watch" | ||
}, | ||
@@ -26,3 +26,6 @@ "dependencies": { | ||
"debug": "^4.3.2", | ||
"lodash": "^4.17.20" | ||
"fs-extra": "^10.1.0", | ||
"loader-utils": "^2.0.0", | ||
"lodash": "^4.17.20", | ||
"webpack-virtual-modules": "^0.4.4" | ||
}, | ||
@@ -42,2 +45,3 @@ "devDependencies": { | ||
"chokidar-cli": "2.1.0", | ||
"common-tags": "^1.8.2", | ||
"cypress": "0.0.0-development", | ||
@@ -52,3 +56,3 @@ "dependency-check": "2.9.1", | ||
"find-webpack": "1.5.0", | ||
"fs-extra": "9.1.0", | ||
"md5": "2.3.0", | ||
"mocha": "^7.1.0", | ||
@@ -65,3 +69,3 @@ "mockery": "2.1.0", | ||
"snap-shot-it": "7.9.2", | ||
"ts-node": "^10.2.1", | ||
"ts-node": "^10.9.1", | ||
"webpack": "^4.44.2" | ||
@@ -84,3 +88,3 @@ }, | ||
}, | ||
"homepage": "https://github.com/cypress-io/cypress/tree/master/npm/webpack-preprocessor#readme", | ||
"homepage": "https://github.com/cypress-io/cypress/tree/develop/npm/webpack-preprocessor#readme", | ||
"bugs": "https://github.com/cypress-io/cypress/issues/new?assignees=&labels=npm%3A%20%40cypress%2Fwebpack-preprocessor&template=1-bug-report.md&title=", | ||
@@ -87,0 +91,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51635
15
911
10
36
3
+ Addedfs-extra@^10.1.0
+ Addedloader-utils@^2.0.0
+ Addedfs-extra@10.1.0(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addeduniversalify@2.0.1(transitive)
+ Addedwebpack-virtual-modules@0.4.6(transitive)