From c80bf5ace5deb3cb4cc253db3859f7b454eb25c6 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 28 Jan 2021 19:02:34 +0300 Subject: [PATCH 01/18] fix: support esm format for configurations --- package.json | 2 ++ packages/webpack-cli/bin/cli.js | 9 +++-- packages/webpack-cli/lib/bootstrap.js | 4 ++- .../lib/utils/dynamic-import-loader.js | 13 +++++++ packages/webpack-cli/lib/webpack-cli.js | 31 +++++++++------- test/config-format/coffee/coffee.test.js | 1 - test/config-format/coffee/package.json | 5 --- test/config-format/failure/failure.test.js | 16 ++++----- ...pack.config.coffee => webpack.config.iced} | 0 test/config-format/mjs/mjs.test.js | 12 ++----- .../main.ts | 0 .../tsconfig.json | 0 .../typescript-commonjs-default.test.js | 14 ++++++++ .../webpack.config.ts | 0 .../config-format/typescript-commonjs/main.ts | 1 + .../typescript-commonjs/tsconfig.json | 5 +++ .../typescript-commonjs.test.js | 14 ++++++++ .../typescript-commonjs/webpack.config.ts | 14 ++++++++ test/config-format/typescript/package.json | 7 ---- .../typescript/typescript.test.js | 20 ----------- .../custom-name/config.webpack.mjs | 10 ++++++ .../custom-name/custom-name.test.js | 10 +++++- .../mjs-config/default-mjs-config.test.js | 34 ++++++++---------- test/config/error-mjs/config-error.test.js | 11 +++--- yarn.lock | 36 +++++++++++++++++-- 25 files changed, 173 insertions(+), 96 deletions(-) create mode 100644 packages/webpack-cli/lib/utils/dynamic-import-loader.js delete mode 100644 test/config-format/coffee/package.json rename test/config-format/failure/{webpack.config.coffee => webpack.config.iced} (100%) rename test/config-format/{typescript => typescript-commonjs-default}/main.ts (100%) rename test/config-format/{typescript => typescript-commonjs-default}/tsconfig.json (100%) create mode 100644 test/config-format/typescript-commonjs-default/typescript-commonjs-default.test.js rename test/config-format/{typescript => typescript-commonjs-default}/webpack.config.ts (100%) create mode 100644 test/config-format/typescript-commonjs/main.ts create mode 100644 test/config-format/typescript-commonjs/tsconfig.json create mode 100644 test/config-format/typescript-commonjs/typescript-commonjs.test.js create mode 100644 test/config-format/typescript-commonjs/webpack.config.ts delete mode 100644 test/config-format/typescript/package.json delete mode 100644 test/config-format/typescript/typescript.test.js create mode 100644 test/config-lookup/custom-name/config.webpack.mjs diff --git a/package.json b/package.json index 504d74f5ff9..b5b5372c039 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "@typescript-eslint/eslint-plugin": "^2.34.0", "@typescript-eslint/parser": "^2.34.0", "@webpack-cli/migrate": "^1.1.2", + "coffeescript": "^2.5.1", "colorette": "^1.2.1", "commitlint": "^11.0.0", "commitlint-config-cz": "^0.13.2", @@ -91,6 +92,7 @@ "strip-ansi": "^6.0.0", "ts-jest": "^26.4.3", "typescript": "^4.1.3", + "ts-node": "^9.1.1", "webpack": "^5.18.0", "webpack-bundle-analyzer": "^4.3.0", "webpack-dev-server": "^3.11.1", diff --git a/packages/webpack-cli/bin/cli.js b/packages/webpack-cli/bin/cli.js index c78e0935e9f..db74156aa8b 100755 --- a/packages/webpack-cli/bin/cli.js +++ b/packages/webpack-cli/bin/cli.js @@ -1,6 +1,11 @@ #!/usr/bin/env node 'use strict'; + +const Module = require('module'); + +const originalModuleCompile = Module.prototype._compile; + require('v8-compile-cache'); const importLocal = require('import-local'); @@ -15,7 +20,7 @@ if (importLocal(__filename)) { process.title = 'webpack'; if (utils.packageExists('webpack')) { - runCLI(process.argv); + runCLI(process.argv, originalModuleCompile); } else { const { promptInstallation, logger, colors } = utils; @@ -25,7 +30,7 @@ if (utils.packageExists('webpack')) { .then(() => { logger.success(`${colors.bold('webpack')} was installed successfully.`); - runCLI(process.argv); + runCLI(process.argv, originalModuleCompile); }) .catch(() => { logger.error(`Action Interrupted, Please try once again or install ${colors.bold('webpack')} manually.`); diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index c01ca2b7ce7..0a83a81c9e2 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -1,11 +1,13 @@ const WebpackCLI = require('./webpack-cli'); const utils = require('./utils'); -const runCLI = async (args) => { +const runCLI = async (args, originalModuleCompile) => { try { // Create a new instance of the CLI object const cli = new WebpackCLI(); + cli._originalModuleCompile = originalModuleCompile; + await cli.run(args); } catch (error) { utils.logger.error(error); diff --git a/packages/webpack-cli/lib/utils/dynamic-import-loader.js b/packages/webpack-cli/lib/utils/dynamic-import-loader.js new file mode 100644 index 00000000000..a9dbedc197b --- /dev/null +++ b/packages/webpack-cli/lib/utils/dynamic-import-loader.js @@ -0,0 +1,13 @@ +function dynamicImportLoader() { + let importESM; + + try { + importESM = new Function('id', 'return import(id);'); + } catch (e) { + importESM = null; + } + + return importESM; +} + +module.exports = dynamicImportLoader; diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 04d28a9135e..38368e3df2f 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1,5 +1,7 @@ const fs = require('fs'); const path = require('path'); +const { pathToFileURL } = require('url'); +const Module = require('module'); const { program } = require('commander'); const utils = require('./utils'); @@ -1137,26 +1139,31 @@ class WebpackCLI { } } - const { pathToFileURL } = require('url'); - - let importESM; - - try { - importESM = new Function('id', 'return import(id);'); - } catch (e) { - importESM = null; - } - let options; try { try { options = require(configPath); } catch (error) { - if (pathToFileURL && importESM && error.code === 'ERR_REQUIRE_ESM') { + let previousModuleCompile; + + // TODO Workaround https://github.com/zertosh/v8-compile-cache/issues/30 + if (this._originalModuleCompile) { + previousModuleCompile = Module.prototype._compile; + + Module.prototype._compile = this._originalModuleCompile; + } + + const dynamicImportLoader = require('./utils/dynamic-import-loader')(); + + if (this._originalModuleCompile) { + Module.prototype._compile = previousModuleCompile; + } + + if (error.code === 'ERR_REQUIRE_ESM' && pathToFileURL && dynamicImportLoader) { const urlForConfig = pathToFileURL(configPath); - options = await importESM(urlForConfig); + options = await dynamicImportLoader(urlForConfig); options = options.default; return { options, path: configPath }; diff --git a/test/config-format/coffee/coffee.test.js b/test/config-format/coffee/coffee.test.js index 84f6ada8f75..1a31dbd4849 100644 --- a/test/config-format/coffee/coffee.test.js +++ b/test/config-format/coffee/coffee.test.js @@ -1,4 +1,3 @@ -// eslint-disable-next-line node/no-unpublished-require const { run } = require('../../utils/test-utils'); describe('webpack cli', () => { diff --git a/test/config-format/coffee/package.json b/test/config-format/coffee/package.json deleted file mode 100644 index 79463144cb1..00000000000 --- a/test/config-format/coffee/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "dependencies": { - "coffeescript": "^2.5.1" - } -} diff --git a/test/config-format/failure/failure.test.js b/test/config-format/failure/failure.test.js index 125c94e31bf..0befcb891ca 100644 --- a/test/config-format/failure/failure.test.js +++ b/test/config-format/failure/failure.test.js @@ -2,17 +2,15 @@ const path = require('path'); const { run } = require('../../utils/test-utils'); -describe('webpack cli', () => { - it('should support mjs config format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.coffee']); +describe('failure', () => { + it('should log error on not installed registers', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.iced']); expect(exitCode).toBe(2); - expect(stderr).toContain(`Unable load '${path.resolve(__dirname, './webpack.config.coffee')}'`); - expect(stderr).toContain('Unable to use specified module loaders for ".coffee".'); - expect(stderr).toContain("Cannot find module 'coffeescript/register'"); - expect(stderr).toContain("Cannot find module 'coffee-script/register'"); - expect(stderr).toContain("Cannot find module 'coffeescript'"); - expect(stderr).toContain("Cannot find module 'coffee-script'"); + expect(stderr).toContain(`Unable load '${path.resolve(__dirname, './webpack.config.iced')}'`); + expect(stderr).toContain('Unable to use specified module loaders for ".iced".'); + expect(stderr).toContain("Cannot find module 'iced-coffee-script/register'"); + expect(stderr).toContain("Cannot find module 'iced-coffee-script'"); expect(stderr).toContain('Please install one of them'); expect(stdout).toBeFalsy(); }); diff --git a/test/config-format/failure/webpack.config.coffee b/test/config-format/failure/webpack.config.iced similarity index 100% rename from test/config-format/failure/webpack.config.coffee rename to test/config-format/failure/webpack.config.iced diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index e8ada298f2a..9bc774b6ebd 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -4,14 +4,8 @@ describe('webpack cli', () => { it('should support mjs config format', () => { const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs'], [], { DISABLE_V8_COMPILE_CACHE: true }); - if (exitCode === 0) { - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); - } else { - expect(exitCode).toBe(2); - expect(/Cannot use import statement outside a module/.test(stderr) || /Unexpected token/.test(stderr)).toBe(true); - expect(stdout).toBeFalsy(); - } + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); }); }); diff --git a/test/config-format/typescript/main.ts b/test/config-format/typescript-commonjs-default/main.ts similarity index 100% rename from test/config-format/typescript/main.ts rename to test/config-format/typescript-commonjs-default/main.ts diff --git a/test/config-format/typescript/tsconfig.json b/test/config-format/typescript-commonjs-default/tsconfig.json similarity index 100% rename from test/config-format/typescript/tsconfig.json rename to test/config-format/typescript-commonjs-default/tsconfig.json diff --git a/test/config-format/typescript-commonjs-default/typescript-commonjs-default.test.js b/test/config-format/typescript-commonjs-default/typescript-commonjs-default.test.js new file mode 100644 index 00000000000..b511e69843e --- /dev/null +++ b/test/config-format/typescript-commonjs-default/typescript-commonjs-default.test.js @@ -0,0 +1,14 @@ +const { run } = require('../../utils/test-utils'); +const { existsSync } = require('fs'); +const { resolve } = require('path'); + +describe('webpack cli', () => { + it('should support typescript file', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['-c', './webpack.config.ts']); + + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(exitCode).toBe(0); + expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy(); + }); +}); diff --git a/test/config-format/typescript/webpack.config.ts b/test/config-format/typescript-commonjs-default/webpack.config.ts similarity index 100% rename from test/config-format/typescript/webpack.config.ts rename to test/config-format/typescript-commonjs-default/webpack.config.ts diff --git a/test/config-format/typescript-commonjs/main.ts b/test/config-format/typescript-commonjs/main.ts new file mode 100644 index 00000000000..5dbd072a4f6 --- /dev/null +++ b/test/config-format/typescript-commonjs/main.ts @@ -0,0 +1 @@ +console.log('Main typescript file'); diff --git a/test/config-format/typescript-commonjs/tsconfig.json b/test/config-format/typescript-commonjs/tsconfig.json new file mode 100644 index 00000000000..391488ab17f --- /dev/null +++ b/test/config-format/typescript-commonjs/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "module": "commonjs" + } +} diff --git a/test/config-format/typescript-commonjs/typescript-commonjs.test.js b/test/config-format/typescript-commonjs/typescript-commonjs.test.js new file mode 100644 index 00000000000..b511e69843e --- /dev/null +++ b/test/config-format/typescript-commonjs/typescript-commonjs.test.js @@ -0,0 +1,14 @@ +const { run } = require('../../utils/test-utils'); +const { existsSync } = require('fs'); +const { resolve } = require('path'); + +describe('webpack cli', () => { + it('should support typescript file', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['-c', './webpack.config.ts']); + + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + expect(exitCode).toBe(0); + expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy(); + }); +}); diff --git a/test/config-format/typescript-commonjs/webpack.config.ts b/test/config-format/typescript-commonjs/webpack.config.ts new file mode 100644 index 00000000000..bbc70963b3b --- /dev/null +++ b/test/config-format/typescript-commonjs/webpack.config.ts @@ -0,0 +1,14 @@ +/* eslint-disable node/no-unsupported-features/es-syntax */ +/** eslint-disable **/ +import * as path from 'path'; + +const config = { + mode: 'production', + entry: './main.ts', + output: { + path: path.resolve(__dirname, 'dist'), + filename: 'foo.bundle.js', + }, +}; + +export = config; diff --git a/test/config-format/typescript/package.json b/test/config-format/typescript/package.json deleted file mode 100644 index 43237a25bed..00000000000 --- a/test/config-format/typescript/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "dependencies": { - "typescript": "^3.8.2", - "ts-node": "^8.6.2", - "tsconfig-paths": "^3.9.0" - } -} diff --git a/test/config-format/typescript/typescript.test.js b/test/config-format/typescript/typescript.test.js deleted file mode 100644 index 21fadb957e7..00000000000 --- a/test/config-format/typescript/typescript.test.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-disable node/no-unpublished-require */ -const { run, runInstall } = require('../../utils/test-utils'); -const { existsSync } = require('fs'); -const { resolve } = require('path'); - -describe('webpack cli', () => { - it.skip( - 'should support typescript file', - async () => { - await runInstall(__dirname); - const { exitCode, stderr, stdout } = run(__dirname, ['-c', './webpack.config.ts']); - - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); - expect(exitCode).toBe(0); - expect(existsSync(resolve(__dirname, 'bin/foo.bundle.js'))).toBeTruthy(); - }, - 1000 * 60 * 5, - ); -}); diff --git a/test/config-lookup/custom-name/config.webpack.mjs b/test/config-lookup/custom-name/config.webpack.mjs new file mode 100644 index 00000000000..272b905bc1c --- /dev/null +++ b/test/config-lookup/custom-name/config.webpack.mjs @@ -0,0 +1,10 @@ +import { fileURLToPath } from 'url'; +import path from 'path'; + +export default { + entry: './a.js', + output: { + path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'dist'), + filename: 'a.bundle.js', + }, +}; diff --git a/test/config-lookup/custom-name/custom-name.test.js b/test/config-lookup/custom-name/custom-name.test.js index aa9bcd1c439..8ea589902cc 100644 --- a/test/config-lookup/custom-name/custom-name.test.js +++ b/test/config-lookup/custom-name/custom-name.test.js @@ -4,11 +4,19 @@ const { resolve } = require('path'); const { run } = require('../../utils/test-utils'); describe('custom config file', () => { - it('should work', () => { + it('should work with cjs format', () => { const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.js')], false); expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); }); + + it('should work with esm format', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], false); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + }); }); diff --git a/test/config/defaults/mjs-config/default-mjs-config.test.js b/test/config/defaults/mjs-config/default-mjs-config.test.js index b2fe3551bdc..2f683cf7116 100644 --- a/test/config/defaults/mjs-config/default-mjs-config.test.js +++ b/test/config/defaults/mjs-config/default-mjs-config.test.js @@ -6,27 +6,21 @@ describe('Default Config:', () => { it('Should be able to pick mjs config by default', () => { const { exitCode, stderr, stdout } = run(__dirname, [], [], { DISABLE_V8_COMPILE_CACHE: true }); - if (exitCode === 0) { - expect(exitCode).toEqual(0); - expect(stderr).toBeFalsy(); - // default entry should be used - expect(stdout).toContain('./src/index.js'); - // should pick up the output path from config - expect(stdout).toContain('test-output'); + expect(exitCode).toEqual(0); + expect(stderr).toBeFalsy(); + // default entry should be used + expect(stdout).toContain('./src/index.js'); + // should pick up the output path from config + expect(stdout).toContain('test-output'); - if (!isWebpack5) { - expect(stdout).toContain('Hash'); - expect(stdout).toContain('Version'); - expect(stdout).toContain('Built at'); - expect(stdout).toContain('Time'); - } - - // check that the output file exists - expect(fs.existsSync(path.join(__dirname, '/dist/test-output.js'))).toBeTruthy(); - } else { - expect(exitCode).toEqual(2); - expect(stderr).toContain('Unexpected token'); - expect(stdout).toBeFalsy(); + if (!isWebpack5) { + expect(stdout).toContain('Hash'); + expect(stdout).toContain('Version'); + expect(stdout).toContain('Built at'); + expect(stdout).toContain('Time'); } + + // check that the output file exists + expect(fs.existsSync(path.join(__dirname, '/dist/test-output.js'))).toBeTruthy(); }); }); diff --git a/test/config/error-mjs/config-error.test.js b/test/config/error-mjs/config-error.test.js index 4abbe2e5b48..12e0428e615 100644 --- a/test/config/error-mjs/config-error.test.js +++ b/test/config/error-mjs/config-error.test.js @@ -4,19 +4,16 @@ const { run } = require('../../utils/test-utils'); describe('config error', () => { it('should throw error with invalid configuration', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')], [], { - DISABLE_V8_COMPILE_CACHE: true, - }); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')]); expect(exitCode).toBe(2); - expect(/Invalid configuration object/.test(stderr) || /Unexpected token/.test(stderr)).toBe(true); + expect(stderr).toContain('Invalid configuration object'); + expect(stderr).toContain(`"development" | "production" | "none"`); expect(stdout).toBeFalsy(); }); it('should throw syntax error and exit with non-zero exit code', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')], [], { - DISABLE_V8_COMPILE_CACHE: true, - }); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')]); expect(exitCode).toBe(2); expect(stderr).toContain('SyntaxError: Unexpected token'); diff --git a/yarn.lock b/yarn.lock index c787ad26d6f..a4680bcc0c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2495,6 +2495,11 @@ are-we-there-yet@~1.1.2: delegates "^1.0.0" readable-stream "^2.0.6" +arg@^4.1.0: + version "4.1.3" + resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" + integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== + argparse@^1.0.7: version "1.0.10" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" @@ -3307,6 +3312,11 @@ code-point-at@^1.0.0: resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c= +coffeescript@^2.5.1: + version "2.5.1" + resolved "https://registry.yarnpkg.com/coffeescript/-/coffeescript-2.5.1.tgz#b2442a1f2c806139669534a54adc35010559d16a" + integrity sha512-J2jRPX0eeFh5VKyVnoLrfVFgLZtnnmp96WQSLAS8OrLm2wtQLcnikYKe1gViJKDH7vucjuhHvBKKBP3rKcD1tQ== + collect-v8-coverage@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/collect-v8-coverage/-/collect-v8-coverage-1.0.1.tgz#cc2c8e94fc18bbdffe64d6534570c8a673b27f59" @@ -3657,6 +3667,11 @@ create-error-class@^3.0.0: dependencies: capture-stack-trace "^1.0.0" +create-require@^1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" + integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== + cross-spawn@^6.0.0, cross-spawn@^6.0.5: version "6.0.5" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" @@ -7441,7 +7456,7 @@ make-dir@^3.0.0, make-dir@^3.0.2: dependencies: semver "^6.0.0" -make-error@1.x: +make-error@1.x, make-error@^1.1.1: version "1.3.6" resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== @@ -9894,7 +9909,7 @@ source-map-resolve@^0.5.0: source-map-url "^0.4.0" urix "^0.1.0" -source-map-support@^0.5.16, source-map-support@^0.5.6, source-map-support@~0.5.19: +source-map-support@^0.5.16, source-map-support@^0.5.17, source-map-support@^0.5.6, source-map-support@~0.5.19: version "0.5.19" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw== @@ -10598,6 +10613,18 @@ ts-jest@^26.4.3: semver "7.x" yargs-parser "20.x" +ts-node@^9.1.1: + version "9.1.1" + resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d" + integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg== + dependencies: + arg "^4.1.0" + create-require "^1.1.0" + diff "^4.0.1" + make-error "^1.1.1" + source-map-support "^0.5.17" + yn "3.1.1" + tslib@^1.8.1, tslib@^1.9.0: version "1.14.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" @@ -11495,6 +11522,11 @@ yeoman-test@^2.7.0: yeoman-environment "^2.10.0" yeoman-generator "^4.10.0" +yn@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50" + integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q== + yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" From 890dbd7a55591d38b36111f27295f5bc13bb827f Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 28 Jan 2021 19:14:03 +0300 Subject: [PATCH 02/18] tests: fix --- test/config-format/mjs/mjs.test.js | 13 ++++--- .../custom-name/custom-name.test.js | 11 ++++-- .../mjs-config/default-mjs-config.test.js | 35 +++++++++++-------- test/config/error-mjs/config-error.test.js | 8 +++-- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index 9bc774b6ebd..69f929eb824 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -2,10 +2,15 @@ const { run } = require('../../utils/test-utils'); describe('webpack cli', () => { it('should support mjs config format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs'], [], { DISABLE_V8_COMPILE_CACHE: true }); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs']); - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + if (/Unexpected token/.test(stderr)) { + expect(exitCode).toBe(2); + expect(stdout).toBeFalsy(); + } else { + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + } }); }); diff --git a/test/config-lookup/custom-name/custom-name.test.js b/test/config-lookup/custom-name/custom-name.test.js index 8ea589902cc..557bc914556 100644 --- a/test/config-lookup/custom-name/custom-name.test.js +++ b/test/config-lookup/custom-name/custom-name.test.js @@ -15,8 +15,13 @@ describe('custom config file', () => { it('should work with esm format', () => { const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], false); - expect(exitCode).toBe(0); - expect(stderr).toBeFalsy(); - expect(stdout).toBeTruthy(); + if (/Unexpected token/.test(stderr)) { + expect(exitCode).toBe(2); + expect(stdout).toBeFalsy(); + } else { + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + } }); }); diff --git a/test/config/defaults/mjs-config/default-mjs-config.test.js b/test/config/defaults/mjs-config/default-mjs-config.test.js index 2f683cf7116..5ca205d320f 100644 --- a/test/config/defaults/mjs-config/default-mjs-config.test.js +++ b/test/config/defaults/mjs-config/default-mjs-config.test.js @@ -4,23 +4,28 @@ const { run, isWebpack5 } = require('../../../utils/test-utils'); describe('Default Config:', () => { it('Should be able to pick mjs config by default', () => { - const { exitCode, stderr, stdout } = run(__dirname, [], [], { DISABLE_V8_COMPILE_CACHE: true }); + const { exitCode, stderr, stdout } = run(__dirname, []); - expect(exitCode).toEqual(0); - expect(stderr).toBeFalsy(); - // default entry should be used - expect(stdout).toContain('./src/index.js'); - // should pick up the output path from config - expect(stdout).toContain('test-output'); + if (/Unexpected token/.test(stderr)) { + expect(exitCode).toEqual(2); + expect(stdout).toBeFalsy(); + } else { + expect(exitCode).toEqual(0); + expect(stderr).toBeFalsy(); + // default entry should be used + expect(stdout).toContain('./src/index.js'); + // should pick up the output path from config + expect(stdout).toContain('test-output'); - if (!isWebpack5) { - expect(stdout).toContain('Hash'); - expect(stdout).toContain('Version'); - expect(stdout).toContain('Built at'); - expect(stdout).toContain('Time'); - } + if (!isWebpack5) { + expect(stdout).toContain('Hash'); + expect(stdout).toContain('Version'); + expect(stdout).toContain('Built at'); + expect(stdout).toContain('Time'); + } - // check that the output file exists - expect(fs.existsSync(path.join(__dirname, '/dist/test-output.js'))).toBeTruthy(); + // check that the output file exists + expect(fs.existsSync(path.join(__dirname, '/dist/test-output.js'))).toBeTruthy(); + } }); }); diff --git a/test/config/error-mjs/config-error.test.js b/test/config/error-mjs/config-error.test.js index 12e0428e615..db6424d4e83 100644 --- a/test/config/error-mjs/config-error.test.js +++ b/test/config/error-mjs/config-error.test.js @@ -7,8 +7,12 @@ describe('config error', () => { const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')]); expect(exitCode).toBe(2); - expect(stderr).toContain('Invalid configuration object'); - expect(stderr).toContain(`"development" | "production" | "none"`); + + if (!/Unexpected token/.test(stderr)) { + expect(stderr).toContain('Invalid configuration object'); + expect(stderr).toContain(`"development" | "production" | "none"`); + } + expect(stdout).toBeFalsy(); }); From ef2a72f6dd211a932b7e219247c45ffa4cda4622 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Thu, 28 Jan 2021 19:48:54 +0300 Subject: [PATCH 03/18] tests: debug CI --- test/config-format/mjs/mjs.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index 69f929eb824..87d8a8e1af5 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -4,6 +4,10 @@ describe('webpack cli', () => { it('should support mjs config format', () => { const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs']); + console.log(exitCode); + console.log(stderr); + console.log(stdout); + if (/Unexpected token/.test(stderr)) { expect(exitCode).toBe(2); expect(stdout).toBeFalsy(); From 968817806a2173bc3db8880fe2996a438fa0f4bf Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 16:18:16 +0300 Subject: [PATCH 04/18] chore: ci debug --- .github/workflows/nodejs.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index d2c0262b783..c9d52686c90 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -92,13 +92,13 @@ jobs: yarn build:ci yarn run lerna bootstrap - - name: Run Smoketests - run: yarn test:smoketests + # - name: Run Smoketests + # run: yarn test:smoketests - name: Test and Generate Coverage run: | yarn prepsuite - yarn test:coverage + node_modules/.bin/jest test/config-format/mjs/ - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 From 0bb7083b3ab52bb70cb94c0b8f187cf37d171e94 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 16:28:15 +0300 Subject: [PATCH 05/18] chore: ci debug --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index c9d52686c90..b3afbdd0be4 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -98,7 +98,7 @@ jobs: - name: Test and Generate Coverage run: | yarn prepsuite - node_modules/.bin/jest test/config-format/mjs/ + node_modules/.bin/jest test/config-format/ - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 From 3c9096097705d40a1efaea78b620e45ec771f7c4 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 16:39:50 +0300 Subject: [PATCH 06/18] chore: ci debug --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index b3afbdd0be4..9d0e464f0c1 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -98,7 +98,7 @@ jobs: - name: Test and Generate Coverage run: | yarn prepsuite - node_modules/.bin/jest test/config-format/ + node_modules/.bin/jest test/ - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 From 65a1bc7e8e1a6999d3bd5b3e0cf71f4cbc7b369c Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 16:57:18 +0300 Subject: [PATCH 07/18] chore: fix ci --- .github/workflows/nodejs.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 9d0e464f0c1..d2c0262b783 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -92,13 +92,13 @@ jobs: yarn build:ci yarn run lerna bootstrap - # - name: Run Smoketests - # run: yarn test:smoketests + - name: Run Smoketests + run: yarn test:smoketests - name: Test and Generate Coverage run: | yarn prepsuite - node_modules/.bin/jest test/ + yarn test:coverage - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 From bd97214e48708df7c6c14d89dfac5292fa259311 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 17:02:22 +0300 Subject: [PATCH 08/18] chore: remove unnecessary debug --- test/config-format/mjs/mjs.test.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index 87d8a8e1af5..69f929eb824 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -4,10 +4,6 @@ describe('webpack cli', () => { it('should support mjs config format', () => { const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs']); - console.log(exitCode); - console.log(stderr); - console.log(stdout); - if (/Unexpected token/.test(stderr)) { expect(exitCode).toBe(2); expect(stdout).toBeFalsy(); From 4e734161bc0395168ab5e8dd885614e5b18867a7 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 17:02:52 +0300 Subject: [PATCH 09/18] tests: fix --- test/watch/basic/basic.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index 771e4f81344..d3369456f8d 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -38,7 +38,7 @@ describe('basic', () => { if (!modified) { process.nextTick(() => { - writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`); + writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');\n`); }); modified = true; From d379859b9b5265e243a3597fe24c1a54e7ce9abf Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 17:36:41 +0300 Subject: [PATCH 10/18] tests: fix --- test/watch/basic/basic.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/watch/basic/basic.test.js b/test/watch/basic/basic.test.js index d3369456f8d..29013daeff3 100644 --- a/test/watch/basic/basic.test.js +++ b/test/watch/basic/basic.test.js @@ -71,7 +71,7 @@ describe('basic', () => { if (!modified) { process.nextTick(() => { - writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`); + writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');\n`); }); modified = true; @@ -106,7 +106,7 @@ describe('basic', () => { if (!modified) { process.nextTick(() => { - writeFileSync(resolve(__dirname, './src/entry.js'), `console.log('watch flag test');`); + writeFileSync(resolve(__dirname, './src/entry.js'), `console.log('watch flag test');\n`); }); modified = true; @@ -139,7 +139,7 @@ describe('basic', () => { if (!modified) { process.nextTick(() => { - writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`); + writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');\n`); }); modified = true; From 8be05173a3315398fc5f408a261d3cb845842588 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 17:59:35 +0300 Subject: [PATCH 11/18] chore: test ci --- .github/workflows/nodejs.yml | 2 +- test/watch/basic/src/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index d2c0262b783..ce3f3084215 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -98,7 +98,7 @@ jobs: - name: Test and Generate Coverage run: | yarn prepsuite - yarn test:coverage + node_modules/.bin/jest test/ - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 diff --git a/test/watch/basic/src/index.js b/test/watch/basic/src/index.js index efc51ca0a97..1d8734ee1c8 100644 --- a/test/watch/basic/src/index.js +++ b/test/watch/basic/src/index.js @@ -1 +1 @@ -console.log('watch flag test'); \ No newline at end of file +console.log('watch flag test'); From 770165d5ad40627795eaad38862570400a7cc777 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 19:13:35 +0300 Subject: [PATCH 12/18] chore: ci --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index ce3f3084215..d2c0262b783 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -98,7 +98,7 @@ jobs: - name: Test and Generate Coverage run: | yarn prepsuite - node_modules/.bin/jest test/ + yarn test:coverage - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 From e7f4585bea1cb25ff9973ad6be79e63cf534c9d8 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Fri, 29 Jan 2021 19:32:00 +0300 Subject: [PATCH 13/18] refactor: code --- packages/webpack-cli/lib/utils/index.js | 4 ++++ packages/webpack-cli/lib/webpack-cli.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/utils/index.js b/packages/webpack-cli/lib/utils/index.js index 223e9c0cf3f..045c98f2aab 100644 --- a/packages/webpack-cli/lib/utils/index.js +++ b/packages/webpack-cli/lib/utils/index.js @@ -19,6 +19,10 @@ module.exports = { return require('./capitalize-first-letter'); }, + get dynamicImportLoader() { + return require('./dynamic-import-loader'); + }, + get getPackageManager() { return require('./get-package-manager'); }, diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 38368e3df2f..c171608e498 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1154,7 +1154,7 @@ class WebpackCLI { Module.prototype._compile = this._originalModuleCompile; } - const dynamicImportLoader = require('./utils/dynamic-import-loader')(); + const dynamicImportLoader = this.utils.dynamicImportLoader(); if (this._originalModuleCompile) { Module.prototype._compile = previousModuleCompile; From 2ef9e82ceedf2d65bd38f5d68f1171ac1f462716 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Sat, 30 Jan 2021 18:35:31 +0300 Subject: [PATCH 14/18] chore: update test deps --- test/entry/scss/package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/entry/scss/package.json b/test/entry/scss/package.json index 2ddfa5b1c12..11b90db6ca3 100644 --- a/test/entry/scss/package.json +++ b/test/entry/scss/package.json @@ -1,9 +1,9 @@ { "dependencies": { - "css-loader": "^3.4.2", - "style-loader": "^1.1.3", - "sass-loader": "^8.0.2", - "mini-css-extract-plugin": "^0.9.0", - "node-sass": "^4.13.1" + "css-loader": "^5.0.1", + "style-loader": "^2.0.0", + "sass-loader": "^10.1.1", + "mini-css-extract-plugin": "^1.3.5", + "node-sass": "^5.0.0" } } From 7ce95cfd85c6ac35aa681f41c41875a3c6241980 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Sat, 30 Jan 2021 19:41:35 +0300 Subject: [PATCH 15/18] tests: fix --- package.json | 4 ++-- test/config-format/mjs/mjs.test.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b5b5372c039..be4d41658cf 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "pretest": "yarn build && yarn lint && yarn prepsuite", "test": "jest --reporters=default", "test:smoketests": "nyc node smoketests", - "test:coverage": "nyc jest --forceExit", + "test:coverage": "nyc --no-hook-require jest --forceExit", "test:cli": "jest test --reporters=default --forceExit", "test:packages": "jest packages/ --reporters=default --forceExit", "test:ci": "yarn test:cli && yarn test:packages", @@ -91,8 +91,8 @@ "rimraf": "^3.0.2", "strip-ansi": "^6.0.0", "ts-jest": "^26.4.3", - "typescript": "^4.1.3", "ts-node": "^9.1.1", + "typescript": "^4.1.3", "webpack": "^5.18.0", "webpack-bundle-analyzer": "^4.3.0", "webpack-dev-server": "^3.11.1", diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index 69f929eb824..af96ace1940 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -8,6 +8,8 @@ describe('webpack cli', () => { expect(exitCode).toBe(2); expect(stdout).toBeFalsy(); } else { + console.log(stderr); + expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); From 40650c080116b7d1f21098781c707505db7fc5f2 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 1 Feb 2021 20:54:39 +0300 Subject: [PATCH 16/18] tests: improve --- package.json | 2 +- packages/webpack-cli/lib/webpack-cli.js | 6 +++++- test/config-format/mjs/mjs.test.js | 6 +++--- test/config-lookup/custom-name/custom-name.test.js | 6 ++++-- .../config/defaults/mjs-config/default-mjs-config.test.js | 2 +- test/config/error-mjs/config-error.test.js | 8 ++++++-- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index be4d41658cf..27753e6f735 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "pretest": "yarn build && yarn lint && yarn prepsuite", "test": "jest --reporters=default", "test:smoketests": "nyc node smoketests", - "test:coverage": "nyc --no-hook-require jest --forceExit", + "test:coverage": "nyc jest --forceExit", "test:cli": "jest test --reporters=default --forceExit", "test:packages": "jest packages/ --reporters=default --forceExit", "test:ci": "yarn test:cli && yarn test:packages", diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index c171608e498..79f7070534c 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1160,7 +1160,11 @@ class WebpackCLI { Module.prototype._compile = previousModuleCompile; } - if (error.code === 'ERR_REQUIRE_ESM' && pathToFileURL && dynamicImportLoader) { + if ( + (error.code === 'ERR_REQUIRE_ESM' || process.env.WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG) && + pathToFileURL && + dynamicImportLoader + ) { const urlForConfig = pathToFileURL(configPath); options = await dynamicImportLoader(urlForConfig); diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index af96ace1940..4fa4e1cd12b 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -2,14 +2,14 @@ const { run } = require('../../utils/test-utils'); describe('webpack cli', () => { it('should support mjs config format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs']); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs'], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); if (/Unexpected token/.test(stderr)) { expect(exitCode).toBe(2); expect(stdout).toBeFalsy(); } else { - console.log(stderr); - expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); diff --git a/test/config-lookup/custom-name/custom-name.test.js b/test/config-lookup/custom-name/custom-name.test.js index 557bc914556..62c5fca458a 100644 --- a/test/config-lookup/custom-name/custom-name.test.js +++ b/test/config-lookup/custom-name/custom-name.test.js @@ -5,7 +5,7 @@ const { run } = require('../../utils/test-utils'); describe('custom config file', () => { it('should work with cjs format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.js')], false); + const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.js')]); expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); @@ -13,7 +13,9 @@ describe('custom config file', () => { }); it('should work with esm format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], false); + const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); if (/Unexpected token/.test(stderr)) { expect(exitCode).toBe(2); diff --git a/test/config/defaults/mjs-config/default-mjs-config.test.js b/test/config/defaults/mjs-config/default-mjs-config.test.js index 5ca205d320f..030ba6bd817 100644 --- a/test/config/defaults/mjs-config/default-mjs-config.test.js +++ b/test/config/defaults/mjs-config/default-mjs-config.test.js @@ -4,7 +4,7 @@ const { run, isWebpack5 } = require('../../../utils/test-utils'); describe('Default Config:', () => { it('Should be able to pick mjs config by default', () => { - const { exitCode, stderr, stdout } = run(__dirname, []); + const { exitCode, stderr, stdout } = run(__dirname, [], { env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true } }); if (/Unexpected token/.test(stderr)) { expect(exitCode).toEqual(2); diff --git a/test/config/error-mjs/config-error.test.js b/test/config/error-mjs/config-error.test.js index db6424d4e83..928760bdb03 100644 --- a/test/config/error-mjs/config-error.test.js +++ b/test/config/error-mjs/config-error.test.js @@ -4,7 +4,9 @@ const { run } = require('../../utils/test-utils'); describe('config error', () => { it('should throw error with invalid configuration', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')]); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); expect(exitCode).toBe(2); @@ -17,7 +19,9 @@ describe('config error', () => { }); it('should throw syntax error and exit with non-zero exit code', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')]); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); expect(exitCode).toBe(2); expect(stderr).toContain('SyntaxError: Unexpected token'); From b19d01578d2ef15304be6ab549050407d5657563 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 1 Feb 2021 21:04:32 +0300 Subject: [PATCH 17/18] tests: fix --- package.json | 2 +- .../commonjs-default.test.js} | 9 +++------ test/config-format/commonjs-default/main.js | 1 + .../commonjs-default/webpack.config.cjs | 12 ++++++++++++ .../typescript-commonjs-default/webpack.config.ts | 14 -------------- test/config-format/typescript-commonjs/main.ts | 1 - .../typescript-commonjs/tsconfig.json | 5 ----- .../main.ts | 0 .../tsconfig.json | 0 .../typescript-commonjs.test.js | 0 .../webpack.config.ts | 0 11 files changed, 17 insertions(+), 27 deletions(-) rename test/config-format/{typescript-commonjs-default/typescript-commonjs-default.test.js => commonjs-default/commonjs-default.test.js} (53%) create mode 100644 test/config-format/commonjs-default/main.js create mode 100644 test/config-format/commonjs-default/webpack.config.cjs delete mode 100644 test/config-format/typescript-commonjs-default/webpack.config.ts delete mode 100644 test/config-format/typescript-commonjs/main.ts delete mode 100644 test/config-format/typescript-commonjs/tsconfig.json rename test/config-format/{typescript-commonjs-default => typescript}/main.ts (100%) rename test/config-format/{typescript-commonjs-default => typescript}/tsconfig.json (100%) rename test/config-format/{typescript-commonjs => typescript}/typescript-commonjs.test.js (100%) rename test/config-format/{typescript-commonjs => typescript}/webpack.config.ts (100%) diff --git a/package.json b/package.json index 27753e6f735..9eb2b192424 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "pretest": "yarn build && yarn lint && yarn prepsuite", "test": "jest --reporters=default", "test:smoketests": "nyc node smoketests", - "test:coverage": "nyc jest --forceExit", + "test:coverage": "nyc --require ts-node/register jest --forceExit", "test:cli": "jest test --reporters=default --forceExit", "test:packages": "jest packages/ --reporters=default --forceExit", "test:ci": "yarn test:cli && yarn test:packages", diff --git a/test/config-format/typescript-commonjs-default/typescript-commonjs-default.test.js b/test/config-format/commonjs-default/commonjs-default.test.js similarity index 53% rename from test/config-format/typescript-commonjs-default/typescript-commonjs-default.test.js rename to test/config-format/commonjs-default/commonjs-default.test.js index b511e69843e..b31d62a3655 100644 --- a/test/config-format/typescript-commonjs-default/typescript-commonjs-default.test.js +++ b/test/config-format/commonjs-default/commonjs-default.test.js @@ -1,14 +1,11 @@ const { run } = require('../../utils/test-utils'); -const { existsSync } = require('fs'); -const { resolve } = require('path'); describe('webpack cli', () => { - it('should support typescript file', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', './webpack.config.ts']); + it('should support CommonJS file', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.cjs'], false); + expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); - expect(exitCode).toBe(0); - expect(existsSync(resolve(__dirname, 'dist/foo.bundle.js'))).toBeTruthy(); }); }); diff --git a/test/config-format/commonjs-default/main.js b/test/config-format/commonjs-default/main.js new file mode 100644 index 00000000000..8ed93e41fb2 --- /dev/null +++ b/test/config-format/commonjs-default/main.js @@ -0,0 +1 @@ +console.log('Hoshiumi'); diff --git a/test/config-format/commonjs-default/webpack.config.cjs b/test/config-format/commonjs-default/webpack.config.cjs new file mode 100644 index 00000000000..415d965a247 --- /dev/null +++ b/test/config-format/commonjs-default/webpack.config.cjs @@ -0,0 +1,12 @@ +const path = require('path'); + +const config = { + mode: 'production', + entry: './main.js', + output: { + path: path.resolve(__dirname, 'dist'), + filename: 'foo.bundle.js', + }, +}; + +module.exports.default = config; diff --git a/test/config-format/typescript-commonjs-default/webpack.config.ts b/test/config-format/typescript-commonjs-default/webpack.config.ts deleted file mode 100644 index 4a17fa148c6..00000000000 --- a/test/config-format/typescript-commonjs-default/webpack.config.ts +++ /dev/null @@ -1,14 +0,0 @@ -/* eslint-disable node/no-unsupported-features/es-syntax */ -/** eslint-disable **/ -import * as path from 'path'; - -const config = { - mode: 'production', - entry: './main.ts', - output: { - path: path.resolve(__dirname, 'dist'), - filename: 'foo.bundle.js', - }, -}; - -export default config; diff --git a/test/config-format/typescript-commonjs/main.ts b/test/config-format/typescript-commonjs/main.ts deleted file mode 100644 index 5dbd072a4f6..00000000000 --- a/test/config-format/typescript-commonjs/main.ts +++ /dev/null @@ -1 +0,0 @@ -console.log('Main typescript file'); diff --git a/test/config-format/typescript-commonjs/tsconfig.json b/test/config-format/typescript-commonjs/tsconfig.json deleted file mode 100644 index 391488ab17f..00000000000 --- a/test/config-format/typescript-commonjs/tsconfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "compilerOptions": { - "module": "commonjs" - } -} diff --git a/test/config-format/typescript-commonjs-default/main.ts b/test/config-format/typescript/main.ts similarity index 100% rename from test/config-format/typescript-commonjs-default/main.ts rename to test/config-format/typescript/main.ts diff --git a/test/config-format/typescript-commonjs-default/tsconfig.json b/test/config-format/typescript/tsconfig.json similarity index 100% rename from test/config-format/typescript-commonjs-default/tsconfig.json rename to test/config-format/typescript/tsconfig.json diff --git a/test/config-format/typescript-commonjs/typescript-commonjs.test.js b/test/config-format/typescript/typescript-commonjs.test.js similarity index 100% rename from test/config-format/typescript-commonjs/typescript-commonjs.test.js rename to test/config-format/typescript/typescript-commonjs.test.js diff --git a/test/config-format/typescript-commonjs/webpack.config.ts b/test/config-format/typescript/webpack.config.ts similarity index 100% rename from test/config-format/typescript-commonjs/webpack.config.ts rename to test/config-format/typescript/webpack.config.ts From 0fa244b09b133e01dd8d4ed82b1b2d0845633eeb Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 1 Feb 2021 21:28:47 +0300 Subject: [PATCH 18/18] tests: fix --- test/config-format/mjs/mjs.test.js | 2 +- .../{typescript-commonjs.test.js => typescript.test.js} | 0 test/config-lookup/custom-name/custom-name.test.js | 2 +- .../config/defaults/mjs-config/default-mjs-config.test.js | 2 +- test/config/error-mjs/config-error.test.js | 8 ++++++-- 5 files changed, 9 insertions(+), 5 deletions(-) rename test/config-format/typescript/{typescript-commonjs.test.js => typescript.test.js} (100%) diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index 4fa4e1cd12b..97853c18bfd 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -6,7 +6,7 @@ describe('webpack cli', () => { env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, }); - if (/Unexpected token/.test(stderr)) { + if (/Error: Not supported/.test(stderr)) { expect(exitCode).toBe(2); expect(stdout).toBeFalsy(); } else { diff --git a/test/config-format/typescript/typescript-commonjs.test.js b/test/config-format/typescript/typescript.test.js similarity index 100% rename from test/config-format/typescript/typescript-commonjs.test.js rename to test/config-format/typescript/typescript.test.js diff --git a/test/config-lookup/custom-name/custom-name.test.js b/test/config-lookup/custom-name/custom-name.test.js index 62c5fca458a..1f088a363e4 100644 --- a/test/config-lookup/custom-name/custom-name.test.js +++ b/test/config-lookup/custom-name/custom-name.test.js @@ -17,7 +17,7 @@ describe('custom config file', () => { env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, }); - if (/Unexpected token/.test(stderr)) { + if (/Error: Not supported/.test(stderr)) { expect(exitCode).toBe(2); expect(stdout).toBeFalsy(); } else { diff --git a/test/config/defaults/mjs-config/default-mjs-config.test.js b/test/config/defaults/mjs-config/default-mjs-config.test.js index 030ba6bd817..f5b443c9613 100644 --- a/test/config/defaults/mjs-config/default-mjs-config.test.js +++ b/test/config/defaults/mjs-config/default-mjs-config.test.js @@ -6,7 +6,7 @@ describe('Default Config:', () => { it('Should be able to pick mjs config by default', () => { const { exitCode, stderr, stdout } = run(__dirname, [], { env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true } }); - if (/Unexpected token/.test(stderr)) { + if (/Error: Not supported/.test(stderr)) { expect(exitCode).toEqual(2); expect(stdout).toBeFalsy(); } else { diff --git a/test/config/error-mjs/config-error.test.js b/test/config/error-mjs/config-error.test.js index 928760bdb03..eb90cbf1e76 100644 --- a/test/config/error-mjs/config-error.test.js +++ b/test/config/error-mjs/config-error.test.js @@ -10,7 +10,7 @@ describe('config error', () => { expect(exitCode).toBe(2); - if (!/Unexpected token/.test(stderr)) { + if (!/Error: Not supported/.test(stderr)) { expect(stderr).toContain('Invalid configuration object'); expect(stderr).toContain(`"development" | "production" | "none"`); } @@ -24,7 +24,11 @@ describe('config error', () => { }); expect(exitCode).toBe(2); - expect(stderr).toContain('SyntaxError: Unexpected token'); + + if (!/Error: Not supported/.test(stderr)) { + expect(stderr).toContain('SyntaxError: Unexpected token'); + } + expect(stdout).toBeFalsy(); }); });