From ce75a9ce977eb3f2054837000945e96874bf55ce Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 7 Apr 2024 20:28:59 +1200 Subject: [PATCH] refactor: setup and apply Prettier (#463) --- .eslintrc.js | 14 ++-- jest.config.js | 5 +- package.json | 2 + package/babel/preset.js | 30 +++---- package/config.js | 28 +++---- package/dev_server.js | 6 +- package/env.js | 36 +++++---- package/environments/base.js | 44 +++++------ package/environments/development.js | 12 +-- package/environments/production.js | 32 ++++---- package/environments/test.js | 2 +- package/esbuild/index.js | 12 +-- package/index.js | 24 +++--- package/rules/babel.js | 14 ++-- package/rules/coffee.js | 4 +- package/rules/css.js | 2 +- package/rules/erb.js | 8 +- package/rules/esbuild.js | 12 ++- package/rules/file.js | 16 ++-- package/rules/index.js | 22 +++--- package/rules/jscommon.js | 10 +-- package/rules/less.js | 12 +-- package/rules/raw.js | 2 +- package/rules/sass.js | 8 +- package/rules/stylus.js | 12 +-- package/rules/swc.js | 12 ++- package/swc/index.js | 20 ++--- package/utils/configPath.js | 5 +- package/utils/defaultConfigPath.js | 2 +- package/utils/getStyleRule.js | 12 +-- package/utils/helpers.js | 4 +- package/utils/inliningCss.js | 7 +- package/utils/snakeToCamelCase.js | 2 +- package/webpackDevServerConfig.js | 53 +++++++------ prettier.config.js | 4 + spec/dummy/yarn.lock | 5 +- test/helpers.js | 2 +- test/package/config.test.js | 50 ++++++------ test/package/dev_server.test.js | 44 +++++------ test/package/development.test.js | 34 ++++---- test/package/env.test.js | 46 +++++------ test/package/environments/base.test.js | 77 +++++++++--------- test/package/environments/development.test.js | 38 ++++----- test/package/environments/production.test.js | 68 +++++++++------- test/package/index.test.js | 38 ++++----- test/package/production.test.js | 24 +++--- test/package/rules/babel.test.js | 4 +- test/package/rules/esbuild.test.js | 4 +- test/package/rules/file.test.js | 78 +++++++++---------- test/package/rules/index.test.js | 8 +- test/package/rules/raw.test.js | 20 ++--- test/package/rules/swc.test.js | 4 +- test/package/staging.test.js | 22 +++--- test/package/test.test.js | 20 ++--- yarn.lock | 43 ++++++++++ 55 files changed, 582 insertions(+), 537 deletions(-) create mode 100644 prettier.config.js diff --git a/.eslintrc.js b/.eslintrc.js index 8db977fb0..3f9549cc8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,12 +1,10 @@ module.exports = { - extends: ['airbnb', 'prettier'], + extends: ["airbnb", "plugin:prettier/recommended"], rules: { - 'comma-dangle': ['error', 'never'], - 'import/no-unresolved': 'off', - 'import/no-extraneous-dependencies': 'off', - 'import/extensions': 'off', - "indent": ["error", 2], - semi: ['error', 'never'] + "import/no-unresolved": "off", + "import/no-extraneous-dependencies": "off", + "import/extensions": "off", + indent: ["error", 2] }, env: { browser: true, @@ -18,7 +16,7 @@ module.exports = { // todo: these should be sourced from eslint-plugin-jest env: { jest: true }, rules: { - 'global-require': 'off' + "global-require": "off" } } ] diff --git a/jest.config.js b/jest.config.js index 248610d77..b2c9c58a0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,7 +1,4 @@ module.exports = { roots: ["/test"], - testPathIgnorePatterns: [ - "/__fixtures__/", - "/__utils__/" - ] + testPathIgnorePatterns: ["/__fixtures__/", "/__utils__/"] } diff --git a/package.json b/package.json index 90eecabab..f03381b30 100644 --- a/package.json +++ b/package.json @@ -35,10 +35,12 @@ "eslint-config-prettier": "^9.0.0", "eslint-plugin-import": "^2.24.2", "eslint-plugin-jsx-a11y": "^6.4.1", + "eslint-plugin-prettier": "^5.1.3", "eslint-plugin-react": "^7.26.0", "eslint-plugin-react-hooks": "^4.6.0", "jest": "^28.1.3", "memory-fs": "^0.5.0", + "prettier": "^3.2.5", "swc-loader": "^0.1.15", "thenify": "^3.3.1", "webpack": "^5.72.0", diff --git a/package/babel/preset.js b/package/babel/preset.js index 04bc59f09..960f9bf37 100644 --- a/package/babel/preset.js +++ b/package/babel/preset.js @@ -1,11 +1,11 @@ -const { moduleExists } = require('shakapacker') +const { moduleExists } = require("shakapacker") module.exports = function config(api) { - const validEnv = ['development', 'test', 'production'] + const validEnv = ["development", "test", "production"] const currentEnv = api.env() - const isDevelopmentEnv = api.env('development') - const isProductionEnv = api.env('production') - const isTestEnv = api.env('test') + const isDevelopmentEnv = api.env("development") + const isProductionEnv = api.env("production") + const isTestEnv = api.env("test") if (!validEnv.includes(currentEnv)) { throw new Error( @@ -17,21 +17,21 @@ module.exports = function config(api) { return { presets: [ - isTestEnv && ['@babel/preset-env', { targets: { node: 'current' } }], + isTestEnv && ["@babel/preset-env", { targets: { node: "current" } }], (isProductionEnv || isDevelopmentEnv) && [ - '@babel/preset-env', + "@babel/preset-env", { - useBuiltIns: 'entry', - corejs: '3.8', - modules: 'auto', + useBuiltIns: "entry", + corejs: "3.8", + modules: "auto", bugfixes: true, - exclude: ['transform-typeof-symbol'] + exclude: ["transform-typeof-symbol"] } ], - moduleExists('@babel/preset-typescript') && '@babel/preset-typescript' + moduleExists("@babel/preset-typescript") && "@babel/preset-typescript" ].filter(Boolean), - plugins: [ - ['@babel/plugin-transform-runtime', { helpers: false }] - ].filter(Boolean) + plugins: [["@babel/plugin-transform-runtime", { helpers: false }]].filter( + Boolean + ) } } diff --git a/package/config.js b/package/config.js index 2c46e45b9..1802fa668 100644 --- a/package/config.js +++ b/package/config.js @@ -1,16 +1,16 @@ -const { resolve } = require('path') -const { load } = require('js-yaml') -const { existsSync, readFileSync } = require('fs') +const { resolve } = require("path") +const { load } = require("js-yaml") +const { existsSync, readFileSync } = require("fs") -const { merge } = require('webpack-merge') -const { ensureTrailingSlash } = require('./utils/helpers') -const { railsEnv } = require('./env') -const configPath = require('./utils/configPath') +const { merge } = require("webpack-merge") +const { ensureTrailingSlash } = require("./utils/helpers") +const { railsEnv } = require("./env") +const configPath = require("./utils/configPath") -const defaultConfigPath = require('./utils/defaultConfigPath') +const defaultConfigPath = require("./utils/defaultConfigPath") const getDefaultConfig = () => { - const defaultConfig = load(readFileSync(defaultConfigPath), 'utf8') + const defaultConfig = load(readFileSync(defaultConfigPath), "utf8") return defaultConfig[railsEnv] || defaultConfig.production } @@ -18,12 +18,14 @@ const defaults = getDefaultConfig() let config if (existsSync(configPath)) { - const appYmlObject = load(readFileSync(configPath), 'utf8') + const appYmlObject = load(readFileSync(configPath), "utf8") const envAppConfig = appYmlObject[railsEnv] if (!envAppConfig) { /* eslint no-console:0 */ - console.warn(`Warning: ${railsEnv} key not found in the configuration file. Using production configuration as a fallback.`) + console.warn( + `Warning: ${railsEnv} key not found in the configuration file. Using production configuration as a fallback.` + ) } config = merge(defaults, envAppConfig || {}) @@ -36,7 +38,7 @@ config.outputPath = resolve(config.public_root_path, config.public_output_path) // Ensure that the publicPath includes our asset host so dynamic imports // (code-splitting chunks and static assets) load from the CDN instead of a relative path. const getPublicPath = () => { - const rootUrl = ensureTrailingSlash(process.env.SHAKAPACKER_ASSET_HOST || '/') + const rootUrl = ensureTrailingSlash(process.env.SHAKAPACKER_ASSET_HOST || "/") return `${rootUrl}${config.public_output_path}/` } @@ -46,7 +48,7 @@ config.publicPathWithoutCDN = `/${config.public_output_path}/` if (config.manifest_path) { config.manifestPath = resolve(config.manifest_path) } else { - config.manifestPath = resolve(config.outputPath, 'manifest.json') + config.manifestPath = resolve(config.outputPath, "manifest.json") } module.exports = config diff --git a/package/dev_server.js b/package/dev_server.js index bd9909497..bb1ed5ab6 100644 --- a/package/dev_server.js +++ b/package/dev_server.js @@ -1,6 +1,6 @@ // These are the raw shakapacker dev server config settings from the YML file with ENV overrides applied. -const { isBoolean } = require('./utils/helpers') -const config = require('./config') +const { isBoolean } = require("./utils/helpers") +const config = require("./config") const envFetch = (key) => { const value = process.env[key] @@ -10,7 +10,7 @@ const envFetch = (key) => { const devServerConfig = config.dev_server if (devServerConfig) { - const envPrefix = config.dev_server.env_prefix || 'SHAKAPACKER_DEV_SERVER' + const envPrefix = config.dev_server.env_prefix || "SHAKAPACKER_DEV_SERVER" Object.keys(devServerConfig).forEach((key) => { const envValue = envFetch(`${envPrefix}_${key.toUpperCase()}`) diff --git a/package/env.js b/package/env.js index 86065b4ca..d126969d4 100644 --- a/package/env.js +++ b/package/env.js @@ -1,40 +1,42 @@ -const { load } = require('js-yaml') -const { readFileSync } = require('fs') -const defaultConfigPath = require('./utils/defaultConfigPath') +const { load } = require("js-yaml") +const { readFileSync } = require("fs") +const defaultConfigPath = require("./utils/defaultConfigPath") -const NODE_ENVIRONMENTS = ['development', 'production', 'test'] -const DEFAULT = 'production' -const configPath = require('./utils/configPath') +const NODE_ENVIRONMENTS = ["development", "production", "test"] +const DEFAULT = "production" +const configPath = require("./utils/configPath") const railsEnv = process.env.RAILS_ENV const rawNodeEnv = process.env.NODE_ENV -const nodeEnv - = rawNodeEnv && NODE_ENVIRONMENTS.includes(rawNodeEnv) ? rawNodeEnv : DEFAULT -const isProduction = nodeEnv === 'production' -const isDevelopment = nodeEnv === 'development' +const nodeEnv = + rawNodeEnv && NODE_ENVIRONMENTS.includes(rawNodeEnv) ? rawNodeEnv : DEFAULT +const isProduction = nodeEnv === "production" +const isDevelopment = nodeEnv === "development" let config try { - config = load(readFileSync(configPath), 'utf8') + config = load(readFileSync(configPath), "utf8") } catch (error) { - if (error.code === 'ENOENT') { + if (error.code === "ENOENT") { // File not found, use default configuration - config = load(readFileSync(defaultConfigPath), 'utf8') + config = load(readFileSync(defaultConfigPath), "utf8") } else { throw error } } -const availableEnvironments = Object.keys(config).join('|') -const regex = new RegExp(`^(${availableEnvironments})$`, 'g') +const availableEnvironments = Object.keys(config).join("|") +const regex = new RegExp(`^(${availableEnvironments})$`, "g") -const runningWebpackDevServer = process.env.WEBPACK_SERVE === 'true' +const runningWebpackDevServer = process.env.WEBPACK_SERVE === "true" const validatedRailsEnv = railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT if (railsEnv && validatedRailsEnv !== railsEnv) { /* eslint no-console:0 */ - console.warn(`Warning: '${railsEnv}' environment not found in the configuration. Using '${DEFAULT}' configuration as a fallback.`) + console.warn( + `Warning: '${railsEnv}' environment not found in the configuration. Using '${DEFAULT}' configuration as a fallback.` + ) } module.exports = { diff --git a/package/environments/base.js b/package/environments/base.js index ecfeca044..26c99b9bf 100644 --- a/package/environments/base.js +++ b/package/environments/base.js @@ -1,15 +1,15 @@ /* eslint global-require: 0 */ /* eslint import/no-dynamic-require: 0 */ -const { existsSync, readdirSync } = require('fs') -const { basename, dirname, join, relative, resolve } = require('path') -const extname = require('path-complete-extname') -const WebpackAssetsManifest = require('webpack-assets-manifest') -const webpack = require('webpack') -const rules = require('../rules') -const config = require('../config') -const { isProduction } = require('../env') -const { moduleExists } = require('../utils/helpers') +const { existsSync, readdirSync } = require("fs") +const { basename, dirname, join, relative, resolve } = require("path") +const extname = require("path-complete-extname") +const WebpackAssetsManifest = require("webpack-assets-manifest") +const webpack = require("webpack") +const rules = require("../rules") +const config = require("../config") +const { isProduction } = require("../env") +const { moduleExists } = require("../utils/helpers") const getFilesInDirectory = (dir, includeNested) => { if (!existsSync(dir)) { @@ -32,10 +32,10 @@ const getFilesInDirectory = (dir, includeNested) => { const getEntryObject = () => { const entries = {} const rootPath = join(config.source_path, config.source_entry_path) - if (config.source_entry_path === '/' && config.nested_entries) { + if (config.source_entry_path === "/" && config.nested_entries) { throw new Error( "Your shakapacker config specified using a source_entry_path of '/' with 'nested_entries' == " + - "'true'. Doing this would result in packs for every one of your source files" + "'true'. Doing this would result in packs for every one of your source files" ) } @@ -67,7 +67,7 @@ const getModulePaths = () => { if (config.additional_paths) { config.additional_paths.forEach((path) => result.push(resolve(path))) } - result.push('node_modules') + result.push("node_modules") return result } @@ -84,9 +84,9 @@ const getPlugins = () => { }) ] - if (moduleExists('css-loader') && moduleExists('mini-css-extract-plugin')) { - const hash = isProduction || config.useContentHash ? '-[contenthash:8]' : '' - const MiniCssExtractPlugin = require('mini-css-extract-plugin') + if (moduleExists("css-loader") && moduleExists("mini-css-extract-plugin")) { + const hash = isProduction || config.useContentHash ? "-[contenthash:8]" : "" + const MiniCssExtractPlugin = require("mini-css-extract-plugin") plugins.push( new MiniCssExtractPlugin({ filename: `css/[name]${hash}.css`, @@ -104,35 +104,35 @@ const getPlugins = () => { // Don't use contentHash except for production for performance // https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling -const hash = isProduction || config.useContentHash ? '-[contenthash]' : '' +const hash = isProduction || config.useContentHash ? "-[contenthash]" : "" module.exports = { - mode: 'production', + mode: "production", output: { filename: `js/[name]${hash}.js`, chunkFilename: `js/[name]${hash}.chunk.js`, // https://webpack.js.org/configuration/output/#outputhotupdatechunkfilename - hotUpdateChunkFilename: 'js/[id].[fullhash].hot-update.js', + hotUpdateChunkFilename: "js/[id].[fullhash].hot-update.js", path: config.outputPath, publicPath: config.publicPath }, entry: getEntryObject(), resolve: { - extensions: ['.js', '.jsx', '.mjs', '.ts', '.tsx', '.coffee'], + extensions: [".js", ".jsx", ".mjs", ".ts", ".tsx", ".coffee"], modules: getModulePaths() }, plugins: getPlugins(), resolveLoader: { - modules: ['node_modules'] + modules: ["node_modules"] }, optimization: { - splitChunks: { chunks: 'all' }, + splitChunks: { chunks: "all" }, - runtimeChunk: 'single' + runtimeChunk: "single" }, module: { diff --git a/package/environments/development.js b/package/environments/development.js index 339d3bd8e..d2f0102b4 100644 --- a/package/environments/development.js +++ b/package/environments/development.js @@ -1,12 +1,12 @@ -const { merge } = require('webpack-merge') +const { merge } = require("webpack-merge") -const baseConfig = require('./base') -const webpackDevServerConfig = require('../webpackDevServerConfig') -const { runningWebpackDevServer } = require('../env') +const baseConfig = require("./base") +const webpackDevServerConfig = require("../webpackDevServerConfig") +const { runningWebpackDevServer } = require("../env") const devConfig = { - mode: 'development', - devtool: 'cheap-module-source-map', + mode: "development", + devtool: "cheap-module-source-map", ...(runningWebpackDevServer && { devServer: webpackDevServerConfig() }) } diff --git a/package/environments/production.js b/package/environments/production.js index abc7f1f98..83c8a04d3 100644 --- a/package/environments/production.js +++ b/package/environments/production.js @@ -1,29 +1,29 @@ /* eslint global-require: 0 */ /* eslint import/no-dynamic-require: 0 */ -const { merge } = require('webpack-merge') -const CompressionPlugin = require('compression-webpack-plugin') -const TerserPlugin = require('terser-webpack-plugin') -const baseConfig = require('./base') -const { moduleExists } = require('../utils/helpers') -const config = require('../config') +const { merge } = require("webpack-merge") +const CompressionPlugin = require("compression-webpack-plugin") +const TerserPlugin = require("terser-webpack-plugin") +const baseConfig = require("./base") +const { moduleExists } = require("../utils/helpers") +const config = require("../config") const getPlugins = () => { const plugins = [] plugins.push( new CompressionPlugin({ - filename: '[path][base].gz[query]', - algorithm: 'gzip', + filename: "[path][base].gz[query]", + algorithm: "gzip", test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/ }) ) - if ('brotli' in process.versions) { + if ("brotli" in process.versions) { plugins.push( new CompressionPlugin({ - filename: '[path][base].br[query]', - algorithm: 'brotliCompress', + filename: "[path][base].br[query]", + algorithm: "brotliCompress", test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/ }) ) @@ -34,10 +34,10 @@ const getPlugins = () => { const tryCssMinimizer = () => { if ( - moduleExists('css-loader') && - moduleExists('css-minimizer-webpack-plugin') + moduleExists("css-loader") && + moduleExists("css-minimizer-webpack-plugin") ) { - const CssMinimizerPlugin = require('css-minimizer-webpack-plugin') + const CssMinimizerPlugin = require("css-minimizer-webpack-plugin") return new CssMinimizerPlugin() } @@ -45,8 +45,8 @@ const tryCssMinimizer = () => { } const productionConfig = { - devtool: 'source-map', - stats: 'normal', + devtool: "source-map", + stats: "normal", bail: true, plugins: getPlugins(), optimization: { diff --git a/package/environments/test.js b/package/environments/test.js index 44b0a1091..e2622a766 100644 --- a/package/environments/test.js +++ b/package/environments/test.js @@ -1,3 +1,3 @@ -const baseConfig = require('./base') +const baseConfig = require("./base") module.exports = baseConfig diff --git a/package/esbuild/index.js b/package/esbuild/index.js index 400bda296..e13c7d89e 100644 --- a/package/esbuild/index.js +++ b/package/esbuild/index.js @@ -1,22 +1,22 @@ /* eslint global-require: 0 */ /* eslint import/no-dynamic-require: 0 */ -const { resolve } = require('path') -const { existsSync } = require('fs') -const { merge } = require('webpack-merge') +const { resolve } = require("path") +const { existsSync } = require("fs") +const { merge } = require("webpack-merge") const getLoaderExtension = (filename) => { const matchData = filename.match(/\.([jt]sx?)?(\.erb)?$/) if (!matchData) { - return 'js' + return "js" } return matchData[1] } const getCustomConfig = () => { - const path = resolve('config', 'esbuild.config.js') + const path = resolve("config", "esbuild.config.js") if (existsSync(path)) { return require(path) } @@ -26,7 +26,7 @@ const getCustomConfig = () => { const getEsbuildLoaderConfig = (filenameToProcess) => { const customConfig = getCustomConfig() const defaultConfig = { - loader: require.resolve('esbuild-loader'), + loader: require.resolve("esbuild-loader"), options: { loader: getLoaderExtension(filenameToProcess) } diff --git a/package/index.js b/package/index.js index a08e90789..fc3911054 100644 --- a/package/index.js +++ b/package/index.js @@ -1,26 +1,26 @@ /* eslint global-require: 0 */ /* eslint import/no-dynamic-require: 0 */ -const webpackMerge = require('webpack-merge') -const { resolve } = require('path') -const { existsSync } = require('fs') -const baseConfig = require('./environments/base') -const rules = require('./rules') -const config = require('./config') -const devServer = require('./dev_server') -const env = require('./env') -const { moduleExists, canProcess } = require('./utils/helpers') -const inliningCss = require('./utils/inliningCss') +const webpackMerge = require("webpack-merge") +const { resolve } = require("path") +const { existsSync } = require("fs") +const baseConfig = require("./environments/base") +const rules = require("./rules") +const config = require("./config") +const devServer = require("./dev_server") +const env = require("./env") +const { moduleExists, canProcess } = require("./utils/helpers") +const inliningCss = require("./utils/inliningCss") const generateWebpackConfig = (extraConfig = {}, ...extraArgs) => { if (extraArgs.length > 0) { throw new Error( - 'Only one extra config may be passed here - use webpack-merge to merge configs before passing them to Shakapacker' + "Only one extra config may be passed here - use webpack-merge to merge configs before passing them to Shakapacker" ) } const { nodeEnv } = env - const path = resolve(__dirname, 'environments', `${nodeEnv}.js`) + const path = resolve(__dirname, "environments", `${nodeEnv}.js`) const environmentConfig = existsSync(path) ? require(path) : baseConfig return webpackMerge.merge({}, environmentConfig, extraConfig) diff --git a/package/rules/babel.js b/package/rules/babel.js index dff0d0528..a6db87625 100644 --- a/package/rules/babel.js +++ b/package/rules/babel.js @@ -1,16 +1,14 @@ -const { loaderMatches } = require('../utils/helpers') -const { - webpack_loader: webpackLoader -} = require('../config') -const { isProduction } = require('../env') -const jscommon = require('./jscommon') +const { loaderMatches } = require("../utils/helpers") +const { webpack_loader: webpackLoader } = require("../config") +const { isProduction } = require("../env") +const jscommon = require("./jscommon") -module.exports = loaderMatches(webpackLoader, 'babel', () => ({ +module.exports = loaderMatches(webpackLoader, "babel", () => ({ test: /\.(js|jsx|mjs|ts|tsx|coffee)?(\.erb)?$/, ...jscommon, use: [ { - loader: require.resolve('babel-loader'), + loader: require.resolve("babel-loader"), options: { cacheDirectory: true, cacheCompression: isProduction, diff --git a/package/rules/coffee.js b/package/rules/coffee.js index c049a0c7f..c90dbe074 100644 --- a/package/rules/coffee.js +++ b/package/rules/coffee.js @@ -1,6 +1,6 @@ -const { canProcess } = require('../utils/helpers') +const { canProcess } = require("../utils/helpers") -module.exports = canProcess('coffee-loader', (resolvedPath) => ({ +module.exports = canProcess("coffee-loader", (resolvedPath) => ({ test: /\.coffee(\.erb)?$/, use: [{ loader: resolvedPath }] })) diff --git a/package/rules/css.js b/package/rules/css.js index 1b588cf2a..d9f6a6682 100644 --- a/package/rules/css.js +++ b/package/rules/css.js @@ -1,3 +1,3 @@ -const getStyleRule = require('../utils/getStyleRule') +const getStyleRule = require("../utils/getStyleRule") module.exports = getStyleRule(/\.(css)$/i) diff --git a/package/rules/erb.js b/package/rules/erb.js index 8a25dffc3..f6b9485a8 100644 --- a/package/rules/erb.js +++ b/package/rules/erb.js @@ -1,10 +1,10 @@ -const { canProcess } = require('../utils/helpers') +const { canProcess } = require("../utils/helpers") -const runner = /^win/.test(process.platform) ? 'ruby ' : '' +const runner = /^win/.test(process.platform) ? "ruby " : "" -module.exports = canProcess('rails-erb-loader', (resolvedPath) => ({ +module.exports = canProcess("rails-erb-loader", (resolvedPath) => ({ test: /\.erb$/, - enforce: 'pre', + enforce: "pre", exclude: /node_modules/, use: [ { diff --git a/package/rules/esbuild.js b/package/rules/esbuild.js index 8dc68cb7b..42527e007 100644 --- a/package/rules/esbuild.js +++ b/package/rules/esbuild.js @@ -1,11 +1,9 @@ -const { loaderMatches } = require('../utils/helpers') -const { getEsbuildLoaderConfig } = require('../esbuild') -const { - webpack_loader: webpackLoader -} = require('../config') -const jscommon = require('./jscommon') +const { loaderMatches } = require("../utils/helpers") +const { getEsbuildLoaderConfig } = require("../esbuild") +const { webpack_loader: webpackLoader } = require("../config") +const jscommon = require("./jscommon") -module.exports = loaderMatches(webpackLoader, 'esbuild', () => ({ +module.exports = loaderMatches(webpackLoader, "esbuild", () => ({ test: /\.(ts|tsx|js|jsx|mjs|coffee)?(\.erb)?$/, ...jscommon, use: ({ resource }) => getEsbuildLoaderConfig(resource) diff --git a/package/rules/file.js b/package/rules/file.js index a5514e005..a9d6013cf 100644 --- a/package/rules/file.js +++ b/package/rules/file.js @@ -1,26 +1,28 @@ -const { dirname } = require('path') +const { dirname } = require("path") const { additional_paths: additionalPaths, source_path: sourcePath -} = require('../config') +} = require("../config") module.exports = { test: /\.(bmp|gif|jpe?g|png|tiff|ico|avif|webp|eot|otf|ttf|woff|woff2|svg)$/, exclude: /\.(js|mjs|jsx|ts|tsx)$/, - type: 'asset/resource', + type: "asset/resource", generator: { filename: (pathData) => { const path = dirname(pathData.filename) const stripPaths = [...additionalPaths, sourcePath] - const selectedStripPath = stripPaths.find((includePath) => path.startsWith(includePath)) + const selectedStripPath = stripPaths.find((includePath) => + path.startsWith(includePath) + ) const folders = path - .replace(`${selectedStripPath}`, '') - .split('/') + .replace(`${selectedStripPath}`, "") + .split("/") .filter(Boolean) - const foldersWithStatic = ['static', ...folders].join('/') + const foldersWithStatic = ["static", ...folders].join("/") return `${foldersWithStatic}/[name]-[hash][ext][query]` } } diff --git a/package/rules/index.js b/package/rules/index.js index b41dda104..d6452e64b 100644 --- a/package/rules/index.js +++ b/package/rules/index.js @@ -2,17 +2,17 @@ /* eslint import/no-dynamic-require: 0 */ const rules = { - raw: require('./raw'), - file: require('./file'), - css: require('./css'), - sass: require('./sass'), - babel: require('./babel'), - swc: require('./swc'), - esbuild: require('./esbuild'), - erb: require('./erb'), - coffee: require('./coffee'), - less: require('./less'), - stylus: require('./stylus') + raw: require("./raw"), + file: require("./file"), + css: require("./css"), + sass: require("./sass"), + babel: require("./babel"), + swc: require("./swc"), + esbuild: require("./esbuild"), + erb: require("./erb"), + coffee: require("./coffee"), + less: require("./less"), + stylus: require("./stylus") } module.exports = Object.keys(rules) diff --git a/package/rules/jscommon.js b/package/rules/jscommon.js index 69a777cd9..dd70496a4 100644 --- a/package/rules/jscommon.js +++ b/package/rules/jscommon.js @@ -1,11 +1,11 @@ -const { resolve } = require('path') -const { realpathSync } = require('fs') +const { resolve } = require("path") +const { realpathSync } = require("fs") const { source_path: sourcePath, additional_paths: additionalPaths -} = require('../config') +} = require("../config") -const inclusions = [sourcePath, ...additionalPaths].map(p => { +const inclusions = [sourcePath, ...additionalPaths].map((p) => { try { return realpathSync(p) } catch (e) { @@ -18,7 +18,7 @@ module.exports = { exclude: [ { // exclude all node_modules from running through babel-loader - and: [resolve('node_modules')], + and: [resolve("node_modules")], // Do not exclude inclusions, as otherwise these won't be transpiled not: [...inclusions] } diff --git a/package/rules/less.js b/package/rules/less.js index b6afbfc4b..ca9079370 100644 --- a/package/rules/less.js +++ b/package/rules/less.js @@ -1,19 +1,19 @@ -const path = require('path') -const { canProcess } = require('../utils/helpers') -const getStyleRule = require('../utils/getStyleRule') +const path = require("path") +const { canProcess } = require("../utils/helpers") +const getStyleRule = require("../utils/getStyleRule") const { additional_paths: paths, source_path: sourcePath -} = require('../config') +} = require("../config") -module.exports = canProcess('less-loader', (resolvedPath) => +module.exports = canProcess("less-loader", (resolvedPath) => getStyleRule(/\.(less)(\.erb)?$/i, [ { loader: resolvedPath, options: { lessOptions: { - paths: [path.resolve(__dirname, 'node_modules'), sourcePath, ...paths] + paths: [path.resolve(__dirname, "node_modules"), sourcePath, ...paths] }, sourceMap: true } diff --git a/package/rules/raw.js b/package/rules/raw.js index c8d3a3ffb..ce4d09bd9 100644 --- a/package/rules/raw.js +++ b/package/rules/raw.js @@ -1,5 +1,5 @@ module.exports = { test: /\.html$/, exclude: /\.(js|mjs|jsx|ts|tsx)$/, - type: 'asset/source' + type: "asset/source" } diff --git a/package/rules/sass.js b/package/rules/sass.js index 2fb644198..4d71234a5 100644 --- a/package/rules/sass.js +++ b/package/rules/sass.js @@ -1,10 +1,10 @@ /* eslint global-require: 0 */ -const getStyleRule = require('../utils/getStyleRule') -const { canProcess } = require('../utils/helpers') -const { additional_paths: includePaths } = require('../config') +const getStyleRule = require("../utils/getStyleRule") +const { canProcess } = require("../utils/helpers") +const { additional_paths: includePaths } = require("../config") -module.exports = canProcess('sass-loader', (resolvedPath) => +module.exports = canProcess("sass-loader", (resolvedPath) => getStyleRule(/\.(scss|sass)(\.erb)?$/i, [ { loader: resolvedPath, diff --git a/package/rules/stylus.js b/package/rules/stylus.js index ca7d9e943..d233b6d8b 100644 --- a/package/rules/stylus.js +++ b/package/rules/stylus.js @@ -1,20 +1,20 @@ -const path = require('path') -const { canProcess } = require('../utils/helpers') -const getStyleRule = require('../utils/getStyleRule') +const path = require("path") +const { canProcess } = require("../utils/helpers") +const getStyleRule = require("../utils/getStyleRule") const { additional_paths: paths, source_path: sourcePath -} = require('../config') +} = require("../config") -module.exports = canProcess('stylus-loader', (resolvedPath) => +module.exports = canProcess("stylus-loader", (resolvedPath) => getStyleRule(/\.(styl(us)?)(\.erb)?$/i, [ { loader: resolvedPath, options: { stylusOptions: { include: [ - path.resolve(__dirname, 'node_modules'), + path.resolve(__dirname, "node_modules"), sourcePath, ...paths ] diff --git a/package/rules/swc.js b/package/rules/swc.js index 7a2831b13..0901fe249 100644 --- a/package/rules/swc.js +++ b/package/rules/swc.js @@ -1,11 +1,9 @@ -const { loaderMatches } = require('../utils/helpers') -const { getSwcLoaderConfig } = require('../swc') -const { - webpack_loader: webpackLoader -} = require('../config') -const jscommon = require('./jscommon') +const { loaderMatches } = require("../utils/helpers") +const { getSwcLoaderConfig } = require("../swc") +const { webpack_loader: webpackLoader } = require("../config") +const jscommon = require("./jscommon") -module.exports = loaderMatches(webpackLoader, 'swc', () => ({ +module.exports = loaderMatches(webpackLoader, "swc", () => ({ test: /\.(ts|tsx|js|jsx|mjs|coffee)?(\.erb)?$/, ...jscommon, use: ({ resource }) => getSwcLoaderConfig(resource) diff --git a/package/swc/index.js b/package/swc/index.js index 68af97409..0d28f0df3 100644 --- a/package/swc/index.js +++ b/package/swc/index.js @@ -1,16 +1,16 @@ /* eslint global-require: 0 */ /* eslint import/no-dynamic-require: 0 */ -const { resolve } = require('path') -const { existsSync } = require('fs') -const { merge } = require('webpack-merge') +const { resolve } = require("path") +const { existsSync } = require("fs") +const { merge } = require("webpack-merge") const isJsxFile = (filename) => !!filename.match(/\.(jsx|tsx)?(\.erb)?$/) const isTypescriptFile = (filename) => !!filename.match(/\.(ts|tsx)?(\.erb)?$/) const getCustomConfig = () => { - const path = resolve('config', 'swc.config.js') + const path = resolve("config", "swc.config.js") if (existsSync(path)) { return require(path) } @@ -20,15 +20,15 @@ const getCustomConfig = () => { const getSwcLoaderConfig = (filenameToProcess) => { const customConfig = getCustomConfig() const defaultConfig = { - loader: require.resolve('swc-loader'), + loader: require.resolve("swc-loader"), options: { jsc: { parser: { dynamicImport: true, syntax: isTypescriptFile(filenameToProcess) - ? 'typescript' - : 'ecmascript', - [isTypescriptFile(filenameToProcess) ? 'tsx' : 'jsx']: + ? "typescript" + : "ecmascript", + [isTypescriptFile(filenameToProcess) ? "tsx" : "jsx"]: isJsxFile(filenameToProcess) }, loose: true @@ -36,8 +36,8 @@ const getSwcLoaderConfig = (filenameToProcess) => { sourceMaps: true, env: { coreJs: 3, - exclude: ['transform-typeof-symbol'], - mode: 'entry' + exclude: ["transform-typeof-symbol"], + mode: "entry" } } } diff --git a/package/utils/configPath.js b/package/utils/configPath.js index 51a0332ee..736f4d224 100644 --- a/package/utils/configPath.js +++ b/package/utils/configPath.js @@ -1,3 +1,4 @@ -const { resolve } = require('path') +const { resolve } = require("path") -module.exports = process.env.SHAKAPACKER_CONFIG || resolve('config', 'shakapacker.yml') +module.exports = + process.env.SHAKAPACKER_CONFIG || resolve("config", "shakapacker.yml") diff --git a/package/utils/defaultConfigPath.js b/package/utils/defaultConfigPath.js index 37edb6ff5..5bdcb9a0f 100644 --- a/package/utils/defaultConfigPath.js +++ b/package/utils/defaultConfigPath.js @@ -1,2 +1,2 @@ -const path = require.resolve('../../lib/install/config/shakapacker.yml') +const path = require.resolve("../../lib/install/config/shakapacker.yml") module.exports = path diff --git a/package/utils/getStyleRule.js b/package/utils/getStyleRule.js index 243bb60c5..7ba8496ce 100644 --- a/package/utils/getStyleRule.js +++ b/package/utils/getStyleRule.js @@ -1,11 +1,11 @@ /* eslint global-require: 0 */ -const { canProcess, moduleExists } = require('./helpers') -const inliningCss = require('./inliningCss') +const { canProcess, moduleExists } = require("./helpers") +const inliningCss = require("./inliningCss") const getStyleRule = (test, preprocessors = []) => { - if (moduleExists('css-loader')) { + if (moduleExists("css-loader")) { const tryPostcss = () => - canProcess('postcss-loader', (loaderPath) => ({ + canProcess("postcss-loader", (loaderPath) => ({ loader: loaderPath, options: { sourceMap: true } })) @@ -13,9 +13,9 @@ const getStyleRule = (test, preprocessors = []) => { // style-loader is required when using css modules with HMR on the webpack-dev-server const use = [ - inliningCss ? 'style-loader' : require('mini-css-extract-plugin').loader, + inliningCss ? "style-loader" : require("mini-css-extract-plugin").loader, { - loader: require.resolve('css-loader'), + loader: require.resolve("css-loader"), options: { sourceMap: true, importLoaders: 2, diff --git a/package/utils/helpers.js b/package/utils/helpers.js index 86327b0a6..d550eb3f7 100644 --- a/package/utils/helpers.js +++ b/package/utils/helpers.js @@ -1,12 +1,12 @@ const isBoolean = (str) => /^true/.test(str) || /^false/.test(str) -const ensureTrailingSlash = (path) => (path.endsWith('/') ? path : `${path}/`) +const ensureTrailingSlash = (path) => (path.endsWith("/") ? path : `${path}/`) const resolvedPath = (packageName) => { try { return require.resolve(packageName) } catch (e) { - if (e.code !== 'MODULE_NOT_FOUND') { + if (e.code !== "MODULE_NOT_FOUND") { throw e } return null diff --git a/package/utils/inliningCss.js b/package/utils/inliningCss.js index 54875489d..6d64ae35d 100644 --- a/package/utils/inliningCss.js +++ b/package/utils/inliningCss.js @@ -1,7 +1,8 @@ -const { runningWebpackDevServer } = require('../env') -const devServer = require('../dev_server') +const { runningWebpackDevServer } = require("../env") +const devServer = require("../dev_server") // This logic is tied to lib/shakapacker/instance.rb -const inliningCss = runningWebpackDevServer && devServer.hmr && devServer.inline_css !== false +const inliningCss = + runningWebpackDevServer && devServer.hmr && devServer.inline_css !== false module.exports = inliningCss diff --git a/package/utils/snakeToCamelCase.js b/package/utils/snakeToCamelCase.js index 9a47b90e7..8f05d408c 100644 --- a/package/utils/snakeToCamelCase.js +++ b/package/utils/snakeToCamelCase.js @@ -1,5 +1,5 @@ function snakeToCamelCase(s) { - return s.replace(/(_\w)/g, match => match[1].toUpperCase()) + return s.replace(/(_\w)/g, (match) => match[1].toUpperCase()) } module.exports = snakeToCamelCase diff --git a/package/webpackDevServerConfig.js b/package/webpackDevServerConfig.js index 9550005a8..d0e5b47b9 100644 --- a/package/webpackDevServerConfig.js +++ b/package/webpackDevServerConfig.js @@ -1,35 +1,38 @@ -const shakapackerDevServerYamlConfig = require('./dev_server') -const snakeToCamelCase = require('./utils/snakeToCamelCase') -const { outputPath: contentBase, publicPath } = require('./config') +const shakapackerDevServerYamlConfig = require("./dev_server") +const snakeToCamelCase = require("./utils/snakeToCamelCase") +const { outputPath: contentBase, publicPath } = require("./config") const webpackDevServerMappedKeys = new Set([ // client, server, liveReload, devMiddleware are handled separately - 'allowedHosts', - 'bonjour', - 'compress', - 'headers', - 'historyApiFallback', - 'host', - 'hot', - 'http2', - 'https', - 'ipc', - 'magicHtml', - 'onAfterSetupMiddleware', - 'onBeforeSetupMiddleware', - 'open', - 'port', - 'proxy', - 'server', - 'setupExitSignals', - 'setupMiddlewares', - 'watchFiles', - 'webSocketServer' + "allowedHosts", + "bonjour", + "compress", + "headers", + "historyApiFallback", + "host", + "hot", + "http2", + "https", + "ipc", + "magicHtml", + "onAfterSetupMiddleware", + "onBeforeSetupMiddleware", + "open", + "port", + "proxy", + "server", + "setupExitSignals", + "setupMiddlewares", + "watchFiles", + "webSocketServer" ]) function createDevServerConfig() { const devServerYamlConfig = { ...shakapackerDevServerYamlConfig } - const liveReload = devServerYamlConfig.live_reload !== undefined ? devServerYamlConfig.live_reload : !devServerYamlConfig.hmr + const liveReload = + devServerYamlConfig.live_reload !== undefined + ? devServerYamlConfig.live_reload + : !devServerYamlConfig.hmr delete devServerYamlConfig.live_reload const config = { diff --git a/prettier.config.js b/prettier.config.js new file mode 100644 index 000000000..92eb82ac1 --- /dev/null +++ b/prettier.config.js @@ -0,0 +1,4 @@ +module.exports = { + semi: false, + trailingComma: "none" +} diff --git a/spec/dummy/yarn.lock b/spec/dummy/yarn.lock index a7ccb68d8..813aa5f1e 100644 --- a/spec/dummy/yarn.lock +++ b/spec/dummy/yarn.lock @@ -2571,7 +2571,7 @@ glob-to-regexp@^0.4.1: resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e" integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw== -glob@^7.1.3, glob@^7.1.6, glob@^7.2.0: +glob@^7.1.3, glob@^7.1.6: version "7.2.3" resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b" integrity sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q== @@ -4060,9 +4060,8 @@ setprototypeof@1.2.0: integrity sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw== "shakapacker@file:.yalc/shakapacker": - version "7.2.2" + version "7.2.3" dependencies: - glob "^7.2.0" js-yaml "^4.1.0" path-complete-extname "^1.0.0" diff --git a/test/helpers.js b/test/helpers.js index 0a3298512..9b779e7bd 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -42,7 +42,7 @@ const createTestCompiler = (config, fs = createInMemoryFs()) => { const chdirTestApp = () => { try { - return process.chdir('spec/shakapacker/test_app') + return process.chdir("spec/shakapacker/test_app") } catch (e) { return null } diff --git a/test/package/config.test.js b/test/package/config.test.js index ee02832e4..bc1d37b02 100644 --- a/test/package/config.test.js +++ b/test/package/config.test.js @@ -1,46 +1,46 @@ -const { resolve } = require('path') -const { chdirTestApp, resetEnv } = require('../helpers') +const { resolve } = require("path") +const { chdirTestApp, resetEnv } = require("../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Config', () => { +describe("Config", () => { beforeEach(() => jest.resetModules() && resetEnv()) afterAll(() => process.chdir(rootPath)) - test('public path', () => { - process.env.RAILS_ENV = 'development' - const config = require('../../package/config') - expect(config.publicPath).toEqual('/packs/') + test("public path", () => { + process.env.RAILS_ENV = "development" + const config = require("../../package/config") + expect(config.publicPath).toEqual("/packs/") }) - test('public path with asset host', () => { - process.env.RAILS_ENV = 'development' - process.env.SHAKAPACKER_ASSET_HOST = 'http://foo.com/' - const config = require('../../package/config') - expect(config.publicPath).toEqual('http://foo.com/packs/') + test("public path with asset host", () => { + process.env.RAILS_ENV = "development" + process.env.SHAKAPACKER_ASSET_HOST = "http://foo.com/" + const config = require("../../package/config") + expect(config.publicPath).toEqual("http://foo.com/packs/") }) - test('should return additional paths as listed in app config, with resolved paths', () => { - const config = require('../../package/config') + test("should return additional paths as listed in app config, with resolved paths", () => { + const config = require("../../package/config") expect(config.additional_paths).toEqual([ - 'app/assets', - '/etc/yarn', - 'some.config.js', - 'app/elm' + "app/assets", + "/etc/yarn", + "some.config.js", + "app/elm" ]) }) - test('should default manifestPath to the public dir', () => { - const config = require('../../package/config') + test("should default manifestPath to the public dir", () => { + const config = require("../../package/config") - expect(config.manifestPath).toEqual(resolve('public/packs/manifest.json')) + expect(config.manifestPath).toEqual(resolve("public/packs/manifest.json")) }) - test('should allow overriding manifestPath', () => { - process.env.SHAKAPACKER_CONFIG = 'config/shakapacker_manifest_path.yml' - const config = require('../../package/config') - expect(config.manifestPath).toEqual(resolve('app/javascript/manifest.json')) + test("should allow overriding manifestPath", () => { + process.env.SHAKAPACKER_CONFIG = "config/shakapacker_manifest_path.yml" + const config = require("../../package/config") + expect(config.manifestPath).toEqual(resolve("app/javascript/manifest.json")) }) }) diff --git a/test/package/dev_server.test.js b/test/package/dev_server.test.js index 9b5974485..7c94e2aef 100644 --- a/test/package/dev_server.test.js +++ b/test/package/dev_server.test.js @@ -1,44 +1,44 @@ -const { chdirTestApp } = require('../helpers') +const { chdirTestApp } = require("../helpers") const rootPath = process.cwd() chdirTestApp() -describe('DevServer', () => { +describe("DevServer", () => { beforeEach(() => jest.resetModules()) afterAll(() => process.chdir(rootPath)) - test('with NODE_ENV and RAILS_ENV set to development', () => { - process.env.NODE_ENV = 'development' - process.env.RAILS_ENV = 'development' - process.env.SHAKAPACKER_DEV_SERVER_HOST = '0.0.0.0' + test("with NODE_ENV and RAILS_ENV set to development", () => { + process.env.NODE_ENV = "development" + process.env.RAILS_ENV = "development" + process.env.SHAKAPACKER_DEV_SERVER_HOST = "0.0.0.0" process.env.SHAKAPACKER_DEV_SERVER_PORT = 5000 process.env.SHAKAPACKER_DEV_SERVER_DISABLE_HOST_CHECK = false - const devServer = require('../../package/dev_server') + const devServer = require("../../package/dev_server") expect(devServer).toBeDefined() - expect(devServer.host).toEqual('0.0.0.0') - expect(devServer.port).toEqual('5000') + expect(devServer.host).toEqual("0.0.0.0") + expect(devServer.port).toEqual("5000") expect(devServer.disable_host_check).toBe(false) }) - test('with custom env prefix', () => { - const config = require('../../package/config') - config.dev_server.env_prefix = 'TEST_SHAKAPACKER_DEV_SERVER' + test("with custom env prefix", () => { + const config = require("../../package/config") + config.dev_server.env_prefix = "TEST_SHAKAPACKER_DEV_SERVER" - process.env.NODE_ENV = 'development' - process.env.RAILS_ENV = 'development' - process.env.TEST_SHAKAPACKER_DEV_SERVER_HOST = '0.0.0.0' + process.env.NODE_ENV = "development" + process.env.RAILS_ENV = "development" + process.env.TEST_SHAKAPACKER_DEV_SERVER_HOST = "0.0.0.0" process.env.TEST_SHAKAPACKER_DEV_SERVER_PORT = 5000 - const devServer = require('../../package/dev_server') + const devServer = require("../../package/dev_server") expect(devServer).toBeDefined() - expect(devServer.host).toEqual('0.0.0.0') - expect(devServer.port).toEqual('5000') + expect(devServer.host).toEqual("0.0.0.0") + expect(devServer.port).toEqual("5000") }) - test('with NODE_ENV and RAILS_ENV set to production', () => { - process.env.RAILS_ENV = 'production' - process.env.NODE_ENV = 'production' - expect(require('../../package/dev_server')).toEqual({}) + test("with NODE_ENV and RAILS_ENV set to production", () => { + process.env.RAILS_ENV = "production" + process.env.NODE_ENV = "production" + expect(require("../../package/dev_server")).toEqual({}) }) }) diff --git a/test/package/development.test.js b/test/package/development.test.js index 721cf3010..2dff9d9c9 100644 --- a/test/package/development.test.js +++ b/test/package/development.test.js @@ -1,38 +1,38 @@ -const { resolve } = require('path') -const { chdirTestApp, resetEnv } = require('../helpers') +const { resolve } = require("path") +const { chdirTestApp, resetEnv } = require("../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Development environment', () => { +describe("Development environment", () => { beforeEach(() => jest.resetModules() && resetEnv()) afterAll(() => process.chdir(rootPath)) - describe('generateWebpackConfig', () => { + describe("generateWebpackConfig", () => { beforeEach(() => jest.resetModules()) - test('should use development config and environment including devServer if WEBPACK_SERVE', () => { - process.env.RAILS_ENV = 'development' - process.env.NODE_ENV = 'development' - process.env.WEBPACK_SERVE = 'true' - const { generateWebpackConfig } = require('../../package/index') + test("should use development config and environment including devServer if WEBPACK_SERVE", () => { + process.env.RAILS_ENV = "development" + process.env.NODE_ENV = "development" + process.env.WEBPACK_SERVE = "true" + const { generateWebpackConfig } = require("../../package/index") const webpackConfig = generateWebpackConfig() - expect(webpackConfig.output.path).toEqual(resolve('public', 'packs')) - expect(webpackConfig.output.publicPath).toEqual('/packs/') + expect(webpackConfig.output.path).toEqual(resolve("public", "packs")) + expect(webpackConfig.output.publicPath).toEqual("/packs/") }) - test('should use development config and environment if WEBPACK_SERVE', () => { - process.env.RAILS_ENV = 'development' - process.env.NODE_ENV = 'development' + test("should use development config and environment if WEBPACK_SERVE", () => { + process.env.RAILS_ENV = "development" + process.env.NODE_ENV = "development" process.env.WEBPACK_SERVE = undefined - const { generateWebpackConfig } = require('../../package/index') + const { generateWebpackConfig } = require("../../package/index") const webpackConfig = generateWebpackConfig() - expect(webpackConfig.output.path).toEqual(resolve('public', 'packs')) - expect(webpackConfig.output.publicPath).toEqual('/packs/') + expect(webpackConfig.output.path).toEqual(resolve("public", "packs")) + expect(webpackConfig.output.publicPath).toEqual("/packs/") expect(webpackConfig.devServer).toEqual(undefined) }) }) diff --git a/test/package/env.test.js b/test/package/env.test.js index a7081284d..6ea021432 100644 --- a/test/package/env.test.js +++ b/test/package/env.test.js @@ -1,54 +1,54 @@ -const { chdirTestApp } = require('../helpers') +const { chdirTestApp } = require("../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Env', () => { +describe("Env", () => { beforeEach(() => jest.resetModules()) afterAll(() => process.chdir(rootPath)) - test('with NODE_ENV and RAILS_ENV set to development', () => { - process.env.RAILS_ENV = 'development' - process.env.NODE_ENV = 'development' - expect(require('../../package/env')).toEqual({ - railsEnv: 'development', - nodeEnv: 'development', + test("with NODE_ENV and RAILS_ENV set to development", () => { + process.env.RAILS_ENV = "development" + process.env.NODE_ENV = "development" + expect(require("../../package/env")).toEqual({ + railsEnv: "development", + nodeEnv: "development", isProduction: false, isDevelopment: true, runningWebpackDevServer: false }) }) - test('with undefined NODE_ENV and RAILS_ENV set to development', () => { - process.env.RAILS_ENV = 'development' + test("with undefined NODE_ENV and RAILS_ENV set to development", () => { + process.env.RAILS_ENV = "development" delete process.env.NODE_ENV - expect(require('../../package/env')).toEqual({ - railsEnv: 'development', - nodeEnv: 'production', + expect(require("../../package/env")).toEqual({ + railsEnv: "development", + nodeEnv: "production", isProduction: true, isDevelopment: false, runningWebpackDevServer: false }) }) - test('with undefined NODE_ENV and RAILS_ENV', () => { + test("with undefined NODE_ENV and RAILS_ENV", () => { delete process.env.NODE_ENV delete process.env.RAILS_ENV - expect(require('../../package/env')).toEqual({ - railsEnv: 'production', - nodeEnv: 'production', + expect(require("../../package/env")).toEqual({ + railsEnv: "production", + nodeEnv: "production", isProduction: true, isDevelopment: false, runningWebpackDevServer: false }) }) - test('with a non-standard environment', () => { - process.env.RAILS_ENV = 'staging' - process.env.NODE_ENV = 'staging' - expect(require('../../package/env')).toEqual({ - railsEnv: 'staging', - nodeEnv: 'production', + test("with a non-standard environment", () => { + process.env.RAILS_ENV = "staging" + process.env.NODE_ENV = "staging" + expect(require("../../package/env")).toEqual({ + railsEnv: "staging", + nodeEnv: "production", isProduction: true, isDevelopment: false, runningWebpackDevServer: false diff --git a/test/package/environments/base.test.js b/test/package/environments/base.test.js index dfc4b95a5..b94485eb8 100644 --- a/test/package/environments/base.test.js +++ b/test/package/environments/base.test.js @@ -1,75 +1,76 @@ // environment.js expects to find config/shakapacker.yml and resolved modules from // the root of a Rails project -const { resolve } = require('path') -const { chdirTestApp, resetEnv } = require('../../helpers') +const { resolve } = require("path") +const { chdirTestApp, resetEnv } = require("../../helpers") const rootPath = process.cwd() chdirTestApp() -const baseConfig = require('../../../package/environments/base') +const baseConfig = require("../../../package/environments/base") const config = require("../../../package/config") -describe('Base config', () => { +describe("Base config", () => { beforeEach(() => jest.resetModules() && resetEnv()) afterAll(() => process.chdir(rootPath)) - describe('config', () => { - test('should return entry', () => { + describe("config", () => { + test("should return entry", () => { expect(baseConfig.entry.application).toEqual( - resolve('app', 'javascript', 'entrypoints', 'application.js') + resolve("app", "javascript", "entrypoints", "application.js") ) }) - test('should return false for css_extract_ignore_order_warnings when using default config', () => { + test("should return false for css_extract_ignore_order_warnings when using default config", () => { expect(config.css_extract_ignore_order_warnings).toEqual(false) }) - test('should return true for css_extract_ignore_order_warnings when configured', () => { - process.env.SHAKAPACKER_CONFIG = 'config/shakapacker_css_extract_ignore_order_warnings.yml' + test("should return true for css_extract_ignore_order_warnings when configured", () => { + process.env.SHAKAPACKER_CONFIG = + "config/shakapacker_css_extract_ignore_order_warnings.yml" const config2 = require("../../../package/config") expect(config2.css_extract_ignore_order_warnings).toEqual(true) }) - test('should return only 2 entry points with config.nested_entries == false', () => { + test("should return only 2 entry points with config.nested_entries == false", () => { expect(config.nested_entries).toEqual(false) expect(baseConfig.entry.multi_entry.sort()).toEqual([ - resolve('app', 'javascript', 'entrypoints', 'multi_entry.css'), - resolve('app', 'javascript', 'entrypoints', 'multi_entry.js') + resolve("app", "javascript", "entrypoints", "multi_entry.css"), + resolve("app", "javascript", "entrypoints", "multi_entry.js") ]) - expect(baseConfig.entry['generated/something']).toEqual(undefined) + expect(baseConfig.entry["generated/something"]).toEqual(undefined) }) - test('should returns top level and nested entry points with config.nested_entries == true', () => { - process.env.SHAKAPACKER_CONFIG = 'config/shakapacker_nested_entries.yml' + test("should returns top level and nested entry points with config.nested_entries == true", () => { + process.env.SHAKAPACKER_CONFIG = "config/shakapacker_nested_entries.yml" const config2 = require("../../../package/config") - const baseConfig2 = require('../../../package/environments/base') + const baseConfig2 = require("../../../package/environments/base") expect(config2.nested_entries).toEqual(true) expect(baseConfig2.entry.application).toEqual( - resolve('app', 'javascript', 'entrypoints', 'application.js') + resolve("app", "javascript", "entrypoints", "application.js") ) expect(baseConfig2.entry.multi_entry.sort()).toEqual([ - resolve('app', 'javascript', 'entrypoints', 'multi_entry.css'), - resolve('app', 'javascript', 'entrypoints', 'multi_entry.js') + resolve("app", "javascript", "entrypoints", "multi_entry.css"), + resolve("app", "javascript", "entrypoints", "multi_entry.js") ]) - expect(baseConfig2.entry['generated/something']).toEqual( - resolve('app', 'javascript', 'entrypoints', 'generated', 'something.js') + expect(baseConfig2.entry["generated/something"]).toEqual( + resolve("app", "javascript", "entrypoints", "generated", "something.js") ) }) - test('should return output', () => { - expect(baseConfig.output.filename).toEqual('js/[name]-[contenthash].js') + test("should return output", () => { + expect(baseConfig.output.filename).toEqual("js/[name]-[contenthash].js") expect(baseConfig.output.chunkFilename).toEqual( - 'js/[name]-[contenthash].chunk.js' + "js/[name]-[contenthash].chunk.js" ) }) - test('should return default loader rules for each file in config/loaders', () => { - const rules = require('../../../package/rules') + test("should return default loader rules for each file in config/loaders", () => { + const rules = require("../../../package/rules") const defaultRules = Object.keys(rules) const configRules = baseConfig.module.rules @@ -78,26 +79,26 @@ describe('Base config', () => { expect(configRules.length).toEqual(3) }) - test('should return default plugins', () => { + test("should return default plugins", () => { expect(baseConfig.plugins.length).toEqual(2) }) - test('should return default resolveLoader', () => { - expect(baseConfig.resolveLoader.modules).toEqual(['node_modules']) + test("should return default resolveLoader", () => { + expect(baseConfig.resolveLoader.modules).toEqual(["node_modules"]) }) - test('should return default resolve.modules with additions', () => { + test("should return default resolve.modules with additions", () => { expect(baseConfig.resolve.modules).toEqual([ - resolve('app', 'javascript'), - resolve('app/assets'), - resolve('/etc/yarn'), - resolve('some.config.js'), - resolve('app/elm'), - 'node_modules' + resolve("app", "javascript"), + resolve("app/assets"), + resolve("/etc/yarn"), + resolve("some.config.js"), + resolve("app/elm"), + "node_modules" ]) }) - test('returns plugins property as Array', () => { + test("returns plugins property as Array", () => { expect(baseConfig.plugins).toBeInstanceOf(Array) }) }) diff --git a/test/package/environments/development.test.js b/test/package/environments/development.test.js index b2e8477d5..eec7b9bdb 100644 --- a/test/package/environments/development.test.js +++ b/test/package/environments/development.test.js @@ -1,51 +1,53 @@ -const { chdirTestApp, resetEnv } = require('../../helpers') +const { chdirTestApp, resetEnv } = require("../../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Development specific config', () => { +describe("Development specific config", () => { beforeEach(() => { jest.resetModules() resetEnv() - process.env.NODE_ENV = 'development' + process.env.NODE_ENV = "development" }) afterAll(() => process.chdir(rootPath)) - describe('with config.useContentHash = true', () => { - test('sets filename to use contentHash', () => { + describe("with config.useContentHash = true", () => { + test("sets filename to use contentHash", () => { const config = require("../../../package/config") config.useContentHash = true - const environmentConfig = require('../../../package/environments/development') + const environmentConfig = require("../../../package/environments/development") - expect(environmentConfig.output.filename).toEqual('js/[name]-[contenthash].js') + expect(environmentConfig.output.filename).toEqual( + "js/[name]-[contenthash].js" + ) expect(environmentConfig.output.chunkFilename).toEqual( - 'js/[name]-[contenthash].chunk.js' + "js/[name]-[contenthash].chunk.js" ) }) }) - describe('with config.useContentHash = false', () => { - test('sets filename without using contentHash', () => { + describe("with config.useContentHash = false", () => { + test("sets filename without using contentHash", () => { const config = require("../../../package/config") config.useContentHash = false - const environmentConfig = require('../../../package/environments/development') + const environmentConfig = require("../../../package/environments/development") - expect(environmentConfig.output.filename).toEqual('js/[name].js') + expect(environmentConfig.output.filename).toEqual("js/[name].js") expect(environmentConfig.output.chunkFilename).toEqual( - 'js/[name].chunk.js' + "js/[name].chunk.js" ) }) }) - describe('with unset config.useContentHash', () => { - test('sets filename without using contentHash', () => { + describe("with unset config.useContentHash", () => { + test("sets filename without using contentHash", () => { const config = require("../../../package/config") delete config.useContentHash - const environmentConfig = require('../../../package/environments/development') + const environmentConfig = require("../../../package/environments/development") - expect(environmentConfig.output.filename).toEqual('js/[name].js') + expect(environmentConfig.output.filename).toEqual("js/[name].js") expect(environmentConfig.output.chunkFilename).toEqual( - 'js/[name].chunk.js' + "js/[name].chunk.js" ) }) }) diff --git a/test/package/environments/production.test.js b/test/package/environments/production.test.js index c363b1a55..f1eee2128 100644 --- a/test/package/environments/production.test.js +++ b/test/package/environments/production.test.js @@ -1,91 +1,103 @@ -const { chdirTestApp, resetEnv } = require('../../helpers') +const { chdirTestApp, resetEnv } = require("../../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Production specific config', () => { +describe("Production specific config", () => { beforeEach(() => { jest.resetModules() resetEnv() - process.env.NODE_ENV = 'production' + process.env.NODE_ENV = "production" }) afterAll(() => process.chdir(rootPath)) - describe('with config.useContentHash = true', () => { - test('sets filename to use contentHash', () => { + describe("with config.useContentHash = true", () => { + test("sets filename to use contentHash", () => { const config = require("../../../package/config") config.useContentHash = true - const environmentConfig = require('../../../package/environments/production') + const environmentConfig = require("../../../package/environments/production") - expect(environmentConfig.output.filename).toEqual('js/[name]-[contenthash].js') + expect(environmentConfig.output.filename).toEqual( + "js/[name]-[contenthash].js" + ) expect(environmentConfig.output.chunkFilename).toEqual( - 'js/[name]-[contenthash].chunk.js' + "js/[name]-[contenthash].chunk.js" ) }) test("doesn't shows any warning message", () => { - const consoleWarnSpy = jest.spyOn(console, 'warn') + const consoleWarnSpy = jest.spyOn(console, "warn") const config = require("../../../package/config") config.useContentHash = true - require('../../../package/environments/production') + require("../../../package/environments/production") expect(consoleWarnSpy).not.toHaveBeenCalledWith( - expect.stringMatching(/Setting 'useContentHash' to 'false' in the production environment/) + expect.stringMatching( + /Setting 'useContentHash' to 'false' in the production environment/ + ) ) consoleWarnSpy.mockRestore() }) }) - describe('with config.useContentHash = false', () => { - test('sets filename to use contentHash', () => { + describe("with config.useContentHash = false", () => { + test("sets filename to use contentHash", () => { const config = require("../../../package/config") config.useContentHash = false - const environmentConfig = require('../../../package/environments/production') + const environmentConfig = require("../../../package/environments/production") - expect(environmentConfig.output.filename).toEqual('js/[name]-[contenthash].js') + expect(environmentConfig.output.filename).toEqual( + "js/[name]-[contenthash].js" + ) expect(environmentConfig.output.chunkFilename).toEqual( - 'js/[name]-[contenthash].chunk.js' + "js/[name]-[contenthash].chunk.js" ) }) - test('shows a warning message', () => { - const consoleWarnSpy = jest.spyOn(console, 'warn') + test("shows a warning message", () => { + const consoleWarnSpy = jest.spyOn(console, "warn") const config = require("../../../package/config") config.useContentHash = false - require('../../../package/environments/production') + require("../../../package/environments/production") expect(consoleWarnSpy).toHaveBeenCalledWith( - expect.stringMatching(/Setting 'useContentHash' to 'false' in the production environment/) + expect.stringMatching( + /Setting 'useContentHash' to 'false' in the production environment/ + ) ) consoleWarnSpy.mockRestore() }) }) - describe('with unset config.useContentHash', () => { - test('sets filename to use contentHash', () => { + describe("with unset config.useContentHash", () => { + test("sets filename to use contentHash", () => { const config = require("../../../package/config") delete config.useContentHash - const environmentConfig = require('../../../package/environments/production') + const environmentConfig = require("../../../package/environments/production") - expect(environmentConfig.output.filename).toEqual('js/[name]-[contenthash].js') + expect(environmentConfig.output.filename).toEqual( + "js/[name]-[contenthash].js" + ) expect(environmentConfig.output.chunkFilename).toEqual( - 'js/[name]-[contenthash].chunk.js' + "js/[name]-[contenthash].chunk.js" ) }) test("doesn't shows any warning message", () => { - const consoleWarnSpy = jest.spyOn(console, 'warn') + const consoleWarnSpy = jest.spyOn(console, "warn") const config = require("../../../package/config") delete config.useContentHash - require('../../../package/environments/production') + require("../../../package/environments/production") expect(consoleWarnSpy).not.toHaveBeenCalledWith( - expect.stringMatching(/Setting 'useContentHash' to 'false' in the production environment/) + expect.stringMatching( + /Setting 'useContentHash' to 'false' in the production environment/ + ) ) consoleWarnSpy.mockRestore() diff --git a/test/package/index.test.js b/test/package/index.test.js index c1d2637ac..bcf799886 100644 --- a/test/package/index.test.js +++ b/test/package/index.test.js @@ -1,45 +1,45 @@ -const index = require('../../package/index') +const index = require("../../package/index") -describe('index', () => { - test('exports webpack-merge v5 functions', () => { +describe("index", () => { + test("exports webpack-merge v5 functions", () => { expect(index.merge).toBeInstanceOf(Function) expect(index.mergeWithRules).toBeInstanceOf(Function) expect(index.mergeWithCustomize).toBeInstanceOf(Function) }) - test('webpackConfig returns an immutable object', () => { - const { generateWebpackConfig } = require('../../package/index') + test("webpackConfig returns an immutable object", () => { + const { generateWebpackConfig } = require("../../package/index") const webpackConfig1 = generateWebpackConfig() const webpackConfig2 = generateWebpackConfig() - webpackConfig1.newKey = 'new value' - webpackConfig1.output.path = 'new path' + webpackConfig1.newKey = "new value" + webpackConfig1.output.path = "new path" - expect(webpackConfig2).not.toHaveProperty('newKey') - expect(webpackConfig2.output.path).not.toEqual('new value') + expect(webpackConfig2).not.toHaveProperty("newKey") + expect(webpackConfig2.output.path).not.toEqual("new value") }) - test('webpackConfig merges extra config', () => { - const { generateWebpackConfig } = require('../../package/index') + test("webpackConfig merges extra config", () => { + const { generateWebpackConfig } = require("../../package/index") const webpackConfig = generateWebpackConfig({ - newKey: 'new value', + newKey: "new value", output: { - path: 'new path' + path: "new path" } }) - expect(webpackConfig).toHaveProperty('newKey', 'new value') - expect(webpackConfig).toHaveProperty('output.path', 'new path') - expect(webpackConfig).toHaveProperty('output.publicPath', '/packs/') + expect(webpackConfig).toHaveProperty("newKey", "new value") + expect(webpackConfig).toHaveProperty("output.path", "new path") + expect(webpackConfig).toHaveProperty("output.publicPath", "/packs/") }) - test('webpackConfig errors if multiple configs are provided', () => { - const { generateWebpackConfig } = require('../../package/index') + test("webpackConfig errors if multiple configs are provided", () => { + const { generateWebpackConfig } = require("../../package/index") expect(() => generateWebpackConfig({}, {})).toThrow( - 'use webpack-merge to merge configs before passing them to Shakapacker' + "use webpack-merge to merge configs before passing them to Shakapacker" ) }) }) diff --git a/test/package/production.test.js b/test/package/production.test.js index 43c1ff837..8da01bdfd 100644 --- a/test/package/production.test.js +++ b/test/package/production.test.js @@ -1,29 +1,29 @@ -const { resolve } = require('path') -const { chdirTestApp } = require('../helpers') +const { resolve } = require("path") +const { chdirTestApp } = require("../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Production environment', () => { +describe("Production environment", () => { afterAll(() => process.chdir(rootPath)) - describe('generateWebpackConfig', () => { + describe("generateWebpackConfig", () => { beforeEach(() => jest.resetModules()) - test('should use production config and environment', () => { - process.env.RAILS_ENV = 'production' - process.env.NODE_ENV = 'production' + test("should use production config and environment", () => { + process.env.RAILS_ENV = "production" + process.env.NODE_ENV = "production" - const { generateWebpackConfig } = require('../../package/index') + const { generateWebpackConfig } = require("../../package/index") const webpackConfig = generateWebpackConfig() - expect(webpackConfig.output.path).toEqual(resolve('public', 'packs')) - expect(webpackConfig.output.publicPath).toEqual('/packs/') + expect(webpackConfig.output.path).toEqual(resolve("public", "packs")) + expect(webpackConfig.output.publicPath).toEqual("/packs/") expect(webpackConfig).toMatchObject({ - devtool: 'source-map', - stats: 'normal' + devtool: "source-map", + stats: "normal" }) }) }) diff --git a/test/package/rules/babel.test.js b/test/package/rules/babel.test.js index b4eb19bb4..a05ec4f44 100644 --- a/test/package/rules/babel.test.js +++ b/test/package/rules/babel.test.js @@ -35,9 +35,7 @@ describe("babel", () => { test("process source path", async () => { const normalPath = `${pathToAppJavascript}/a.js` const [tracked, loader] = createTrackLoader() - const compiler = createTestCompiler( - createWebpackConfig(normalPath, loader) - ) + const compiler = createTestCompiler(createWebpackConfig(normalPath, loader)) await compiler.run() expect(tracked[normalPath]).toBeTruthy() }) diff --git a/test/package/rules/esbuild.test.js b/test/package/rules/esbuild.test.js index 2f666e9c1..52089019f 100644 --- a/test/package/rules/esbuild.test.js +++ b/test/package/rules/esbuild.test.js @@ -36,9 +36,7 @@ describe("swc", () => { test("process source path", async () => { const normalPath = `${pathToAppJavascript}/a.js` const [tracked, loader] = createTrackLoader() - const compiler = createTestCompiler( - createWebpackConfig(normalPath, loader) - ) + const compiler = createTestCompiler(createWebpackConfig(normalPath, loader)) await compiler.run() expect(tracked[normalPath]).toBeTruthy() }) diff --git a/test/package/rules/file.test.js b/test/package/rules/file.test.js index 98d9678d9..add6d0c16 100644 --- a/test/package/rules/file.test.js +++ b/test/package/rules/file.test.js @@ -1,4 +1,4 @@ -const file = require('../../../package/rules/file') +const file = require("../../../package/rules/file") jest.mock("../../../package/config", () => { const original = jest.requireActual("../../../package/config") @@ -8,80 +8,74 @@ jest.mock("../../../package/config", () => { } }) -describe('file', () => { - test('test expected file types', () => { +describe("file", () => { + test("test expected file types", () => { const types = [ - '.bmp', - '.gif', - '.jpg', - '.jpeg', - '.png', - '.tiff', - '.ico', - '.avif', - '.webp', - '.eot', - '.otf', - '.ttf', - '.woff', - '.woff2', - '.svg' + ".bmp", + ".gif", + ".jpg", + ".jpeg", + ".png", + ".tiff", + ".ico", + ".avif", + ".webp", + ".eot", + ".otf", + ".ttf", + ".woff", + ".woff2", + ".svg" ] - types.forEach(type => expect(file.test.test(type)).toBe(true)) + types.forEach((type) => expect(file.test.test(type)).toBe(true)) }) - test('exclude expected file types', () => { - const types = [ - '.js', - '.mjs', - '.jsx', - '.ts', - '.tsx' - ] - types.forEach(type => expect(file.exclude.test(type)).toBe(true)) + test("exclude expected file types", () => { + const types = [".js", ".mjs", ".jsx", ".ts", ".tsx"] + types.forEach((type) => expect(file.exclude.test(type)).toBe(true)) }) - test('correct generated output path is returned for top level files', () => { + test("correct generated output path is returned for top level files", () => { const pathData = { - filename: 'app/javascript/image.svg' + filename: "app/javascript/image.svg" } expect(file.generator.filename(pathData)).toEqual( - 'static/[name]-[hash][ext][query]' + "static/[name]-[hash][ext][query]" ) }) - test('correct generated output path is returned for nested files', () => { + test("correct generated output path is returned for nested files", () => { const pathData = { - filename: 'app/javascript/images/image.svg' + filename: "app/javascript/images/image.svg" } expect(file.generator.filename(pathData)).toEqual( - 'static/images/[name]-[hash][ext][query]' + "static/images/[name]-[hash][ext][query]" ) }) - test('correct generated output path is returned for deeply nested files', () => { + test("correct generated output path is returned for deeply nested files", () => { const pathData = { - filename: 'app/javascript/images/nested/deeply/image.svg' + filename: "app/javascript/images/nested/deeply/image.svg" } expect(file.generator.filename(pathData)).toEqual( - 'static/images/nested/deeply/[name]-[hash][ext][query]' + "static/images/nested/deeply/[name]-[hash][ext][query]" ) }) - test('correct generated output path is returned for additional_paths', () => { + test("correct generated output path is returned for additional_paths", () => { const pathData = { - filename: 'app/assets/images/image.svg' + filename: "app/assets/images/image.svg" } expect(file.generator.filename(pathData)).toEqual( - 'static/images/[name]-[hash][ext][query]' + "static/images/[name]-[hash][ext][query]" ) const pathData2 = { - filename: 'app/javascript/app/assets/image.svg' + filename: "app/javascript/app/assets/image.svg" } expect(file.generator.filename(pathData2)).toEqual( - 'static/app/assets/[name]-[hash][ext][query]' + "static/app/assets/[name]-[hash][ext][query]" ) }) }) diff --git a/test/package/rules/index.test.js b/test/package/rules/index.test.js index b2f25ccf2..32e62330e 100644 --- a/test/package/rules/index.test.js +++ b/test/package/rules/index.test.js @@ -1,7 +1,7 @@ -const rules = require('../../../package/rules/index') +const rules = require("../../../package/rules/index") -describe('index', () => { - test('rule tests are regexes', () => { - rules.forEach(rule => expect(rule.test instanceof RegExp).toBe(true)) +describe("index", () => { + test("rule tests are regexes", () => { + rules.forEach((rule) => expect(rule.test instanceof RegExp).toBe(true)) }) }) diff --git a/test/package/rules/raw.test.js b/test/package/rules/raw.test.js index b50a03159..b1d09ba79 100644 --- a/test/package/rules/raw.test.js +++ b/test/package/rules/raw.test.js @@ -1,18 +1,12 @@ -const raw = require('../../../package/rules/raw') +const raw = require("../../../package/rules/raw") -describe('raw', () => { - test('test expected file types', () => { - expect(raw.test.test('.html')).toBe(true) +describe("raw", () => { + test("test expected file types", () => { + expect(raw.test.test(".html")).toBe(true) }) - test('exclude expected file types', () => { - const types = [ - '.js', - '.mjs', - '.jsx', - '.ts', - '.tsx' - ] - types.forEach(type => expect(raw.exclude.test(type)).toBe(true)) + test("exclude expected file types", () => { + const types = [".js", ".mjs", ".jsx", ".ts", ".tsx"] + types.forEach((type) => expect(raw.exclude.test(type)).toBe(true)) }) }) diff --git a/test/package/rules/swc.test.js b/test/package/rules/swc.test.js index 797fb7f94..4069b59c6 100644 --- a/test/package/rules/swc.test.js +++ b/test/package/rules/swc.test.js @@ -36,9 +36,7 @@ describe("swc", () => { test("process files in source_path", async () => { const normalPath = `${pathToAppJavascript}/a.js` const [tracked, loader] = createTrackLoader() - const compiler = createTestCompiler( - createWebpackConfig(normalPath, loader) - ) + const compiler = createTestCompiler(createWebpackConfig(normalPath, loader)) await compiler.run() expect(tracked[normalPath]).toBeTruthy() }) diff --git a/test/package/staging.test.js b/test/package/staging.test.js index 9eb19f35c..b37db76c1 100644 --- a/test/package/staging.test.js +++ b/test/package/staging.test.js @@ -1,30 +1,30 @@ -const { resolve } = require('path') -const { chdirTestApp } = require('../helpers') +const { resolve } = require("path") +const { chdirTestApp } = require("../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Custom environment', () => { +describe("Custom environment", () => { afterAll(() => process.chdir(rootPath)) - describe('generateWebpackConfig', () => { + describe("generateWebpackConfig", () => { beforeEach(() => jest.resetModules()) - test('should use staging config and default production environment', () => { - process.env.RAILS_ENV = 'staging' + test("should use staging config and default production environment", () => { + process.env.RAILS_ENV = "staging" delete process.env.NODE_ENV - const { generateWebpackConfig } = require('../../package/index') + const { generateWebpackConfig } = require("../../package/index") const webpackConfig = generateWebpackConfig() expect(webpackConfig.output.path).toEqual( - resolve('public', 'packs-staging') + resolve("public", "packs-staging") ) - expect(webpackConfig.output.publicPath).toEqual('/packs-staging/') + expect(webpackConfig.output.publicPath).toEqual("/packs-staging/") expect(webpackConfig).toMatchObject({ - devtool: 'source-map', - stats: 'normal' + devtool: "source-map", + stats: "normal" }) }) }) diff --git a/test/package/test.test.js b/test/package/test.test.js index 860b52e6e..9bf1be096 100644 --- a/test/package/test.test.js +++ b/test/package/test.test.js @@ -1,25 +1,25 @@ -const { resolve } = require('path') -const { chdirTestApp } = require('../helpers') +const { resolve } = require("path") +const { chdirTestApp } = require("../helpers") const rootPath = process.cwd() chdirTestApp() -describe('Test environment', () => { +describe("Test environment", () => { afterAll(() => process.chdir(rootPath)) - describe('generateWebpackConfig', () => { + describe("generateWebpackConfig", () => { beforeEach(() => jest.resetModules()) - test('should use test config and production environment', () => { - process.env.RAILS_ENV = 'test' - process.env.NODE_ENV = 'test' + test("should use test config and production environment", () => { + process.env.RAILS_ENV = "test" + process.env.NODE_ENV = "test" - const { generateWebpackConfig } = require('../../package/index') + const { generateWebpackConfig } = require("../../package/index") const webpackConfig = generateWebpackConfig() - expect(webpackConfig.output.path).toEqual(resolve('public', 'packs-test')) - expect(webpackConfig.output.publicPath).toEqual('/packs-test/') + expect(webpackConfig.output.path).toEqual(resolve("public", "packs-test")) + expect(webpackConfig.output.publicPath).toEqual("/packs-test/") expect(webpackConfig.devServer).toEqual(undefined) }) }) diff --git a/yarn.lock b/yarn.lock index 314587799..b6824842c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -816,6 +816,11 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" +"@pkgr/core@^0.1.0": + version "0.1.1" + resolved "https://registry.yarnpkg.com/@pkgr/core/-/core-0.1.1.tgz#1ec17e2edbec25c8306d424ecfbf13c7de1aaa31" + integrity sha512-cq8o4cWH0ibXh9VGi5P20Tu9XF/0fFXl9EUinr9QfTM7a7p0oTA4iJRCQWppXR1Pg8dSM0UCItCkPwsk9qWWYA== + "@sinclair/typebox@^0.24.1": version "0.24.51" resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.51.tgz#645f33fe4e02defe26f2f5c0410e1c094eac7f5f" @@ -2219,6 +2224,14 @@ eslint-plugin-jsx-a11y@^6.4.1: object.entries "^1.1.7" object.fromentries "^2.0.7" +eslint-plugin-prettier@^5.1.3: + version "5.1.3" + resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-5.1.3.tgz#17cfade9e732cef32b5f5be53bd4e07afd8e67e1" + integrity sha512-C9GCVAs4Eq7ZC/XFQHITLiHJxQngdtraXaM+LoUFoFp/lHNl2Zn8f3WQbe9HvTBBQ9YnKFB0/2Ajdqwo5D1EAw== + dependencies: + prettier-linter-helpers "^1.0.0" + synckit "^0.8.6" + eslint-plugin-react-hooks@^4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz#4c3e697ad95b77e93f8646aaa1630c1ba607edd3" @@ -2397,6 +2410,11 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3: resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q== +fast-diff@^1.1.2: + version "1.3.0" + resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.3.0.tgz#ece407fa550a64d638536cd727e129c61616e0f0" + integrity sha512-VxPP4NqbUjj6MaAOafWeUn2cXWLcCtljklUtZf0Ind4XQ+QPtmA0b18zZy0jIQx+ExRVCR/ZQpBmik5lXshNsw== + fast-json-stable-stringify@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633" @@ -3962,6 +3980,18 @@ prelude-ls@^1.2.1: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== +prettier-linter-helpers@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz#d23d41fe1375646de2d0104d3454a3008802cf7b" + integrity sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w== + dependencies: + fast-diff "^1.1.2" + +prettier@^3.2.5: + version "3.2.5" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.5.tgz#e52bc3090586e824964a8813b09aba6233b28368" + integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A== + pretty-format@^28.1.3: version "28.1.3" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-28.1.3.tgz#c9fba8cedf99ce50963a11b27d982a9ae90970d5" @@ -4496,6 +4526,14 @@ swc-loader@^0.1.15: resolved "https://registry.yarnpkg.com/swc-loader/-/swc-loader-0.1.16.tgz#4c718d698e518f3e6ceb9f7872c1855cdb187066" integrity sha512-NKIm8aJjK/z/yfzk+v7YGwJMjBKaLaUs9ZKI2zoaIGKAjtkwjO92ZLI0fiTZuwzRqVLQl/29fBdSgFCBzquR0w== +synckit@^0.8.6: + version "0.8.8" + resolved "https://registry.yarnpkg.com/synckit/-/synckit-0.8.8.tgz#fe7fe446518e3d3d49f5e429f443cf08b6edfcd7" + integrity sha512-HwOKAP7Wc5aRGYdKH+dw0PRRpbO841v2DENBtjnR5HFWoiNByAl7vrx3p0G/rCyYXQsrxqtX48TImFtPcIHSpQ== + dependencies: + "@pkgr/core" "^0.1.0" + tslib "^2.6.2" + tapable@^2.0, tapable@^2.1.1, tapable@^2.2.0: version "2.2.1" resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0" @@ -4578,6 +4616,11 @@ tsconfig-paths@^3.15.0: minimist "^1.2.6" strip-bom "^3.0.0" +tslib@^2.6.2: + version "2.6.2" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae" + integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q== + type-check@^0.4.0, type-check@~0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"