UNPKG

5.96 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6var webpack_1 = __importDefault(require("webpack"));
7var util_1 = __importDefault(require("util"));
8// Cypress webpack bundler adaptor
9// https://github.com/cypress-io/cypress-webpack-preprocessor
10var webpackPreprocessor = require('@cypress/webpack-preprocessor');
11var debug = require('debug')('cypress-vue-unit-test');
12var VueLoaderPlugin = require('vue-loader/lib/plugin');
13// const { VueLoaderPlugin } = require('vue-loader')
14var fw = require('find-webpack');
15// Preventing chunks because we don't serve static assets
16function preventChunking(options) {
17 if (options === void 0) { options = {}; }
18 if (options && options.optimization && options.optimization.splitChunks) {
19 delete options.optimization.splitChunks;
20 }
21 options.plugins = options.plugins || [];
22 options.plugins.push(new webpack_1.default.optimize.LimitChunkCountPlugin({
23 maxChunks: 1,
24 }));
25 return options;
26}
27// Base 64 all the things because we don't serve static assets
28function inlineUrlLoadedAssets(options) {
29 if (options === void 0) { options = {}; }
30 var isUrlLoader = function (use) {
31 return use && use.loader && use.loader.indexOf('url-loader') > -1;
32 };
33 var mergeUrlLoaderOptions = function (use) {
34 if (isUrlLoader(use)) {
35 use.options = use.options || {};
36 use.options.limit = Number.MAX_SAFE_INTEGER;
37 }
38 return use;
39 };
40 if (options.module && options.module.rules) {
41 options.module.rules = options.module.rules.map(function (rule) {
42 if (Array.isArray(rule.use)) {
43 rule.use = rule.use.map(mergeUrlLoaderOptions);
44 }
45 return rule;
46 });
47 }
48 return options;
49}
50function compileTemplate(options) {
51 if (options === void 0) { options = {}; }
52 options.resolve = options.resolve || {};
53 options.resolve.alias = options.resolve.alias || {};
54 options.resolve.alias['vue$'] = 'vue/dist/vue.esm.js';
55}
56/**
57 * Warning: modifies the input object
58 * @param {WebpackOptions} options
59 */
60function removeForkTsCheckerWebpackPlugin(options) {
61 if (!Array.isArray(options.plugins)) {
62 return;
63 }
64 options.plugins = options.plugins.filter(function (plugin) {
65 return plugin.typescript === undefined;
66 });
67}
68/**
69 * Warning: modifies the input object
70 * @param {Cypress.ConfigOptions} config
71 * @param {WebpackOptions} options
72 */
73function insertBabelLoader(config, options) {
74 var skipCodeCoverage = config && config.env && config.env.coverage === false;
75 if (!options.devtool) {
76 options.devtool = '#eval-source-map';
77 }
78 var babelRule = {
79 test: /\.js$/,
80 loader: 'babel-loader',
81 exclude: /node_modules/,
82 options: {
83 plugins: [
84 // this plugin allows ES6 imports mocking
85 [
86 '@babel/plugin-transform-modules-commonjs',
87 {
88 loose: true,
89 },
90 ],
91 ],
92 },
93 };
94 if (skipCodeCoverage) {
95 debug('not adding code instrument plugin');
96 }
97 else {
98 debug('adding code coverage plugin');
99 // this plugin instruments the loaded code
100 // which allows us to collect code coverage
101 var instrumentPlugin = [
102 'babel-plugin-istanbul',
103 {
104 // specify some options for NYC instrumentation here
105 // like tell it to instrument both JavaScript and Vue files
106 extension: ['.js', '.vue'],
107 },
108 ];
109 babelRule.options.plugins.push(instrumentPlugin);
110 }
111 options.module.rules.push(babelRule);
112 options.plugins = options.plugins || [];
113 var pluginFound = options.plugins.find(function (plugin) {
114 return (plugin.constructor && plugin.constructor.name === VueLoaderPlugin.name);
115 });
116 if (!pluginFound) {
117 debug('inserting VueLoaderPlugin');
118 options.plugins.push(new VueLoaderPlugin());
119 }
120 else {
121 debug('found plugin VueLoaderPlugin already');
122 }
123}
124/**
125 * Basic Cypress Vue Webpack file loader for .vue files.
126 */
127var onFileDefaultPreprocessor = function (config) {
128 var webpackOptions = fw.getWebpackOptions();
129 if (!webpackOptions) {
130 debug('Could not find webpack options, starting with default');
131 webpackOptions = {};
132 }
133 webpackOptions.mode = 'development';
134 inlineUrlLoadedAssets(webpackOptions);
135 preventChunking(webpackOptions);
136 compileTemplate(webpackOptions);
137 insertBabelLoader(config, webpackOptions);
138 // if I remove it, then get another message
139 // [VueLoaderPlugin Error] No matching use for vue-loader is found.
140 // removeForkTsCheckerWebpackPlugin(webpackOptions)
141 if (debug.enabled) {
142 console.error('final webpack');
143 console.error(util_1.default.inspect(webpackOptions, false, 2, true));
144 }
145 return webpackPreprocessor({
146 webpackOptions: webpackOptions,
147 });
148};
149/**
150 * Custom Vue loader from the client projects that already have `webpack.config.js`
151 *
152 * @example
153 * const {
154 * onFilePreprocessor
155 * } = require('cypress-vue-unit-test/preprocessor/webpack')
156 * module.exports = on => {
157 * on('file:preprocessor', onFilePreprocessor('../path/to/webpack.config'))
158 * }
159 */
160var onFilePreprocessor = function (webpackOptions) {
161 if (typeof webpackOptions === 'string') {
162 // load webpack config from the given path
163 webpackOptions = require(webpackOptions);
164 }
165 return webpackPreprocessor({
166 webpackOptions: webpackOptions,
167 });
168};
169module.exports = { onFilePreprocessor: onFilePreprocessor, onFileDefaultPreprocessor: onFileDefaultPreprocessor };