Skip to content

Commit

Permalink
feat: allow svelte.config.cjs (#37)
Browse files Browse the repository at this point in the history
closes #35
  • Loading branch information
forrestjacobs authored Apr 12, 2021
1 parent 71da8cc commit 9e259c4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 37 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ To do this, update the file glob above, and follow the instructions in the [ts-j
### Preprocess
Preprocessors are loaded from `svelte.config.js`.
Preprocessors are loaded from `svelte.config.js` or `svelte.config.cjs`.
Add the following to your Jest config
Expand All @@ -161,21 +161,21 @@ Create a `svelte.config.js` file and configure it, see
## Options
`preprocess` (default: false): Pass in `true` if you are using Svelte preprocessors.
They are loaded from `svelte.config.js`.
They are loaded from `svelte.config.js` or `svelte.config.cjs`.
`debug` (default: false): If you'd like to see the output of the compiled code then pass in `true`.

`compilerOptions` (default: {}): Use this to pass in
[Svelte compiler options](https://svelte.dev/docs#svelte_compile).

`rootMode` (default: ""): Pass in `upward` to walk up from the transforming file's directory and use the first `svelte.config.js` found, or throw an error if no config file is discovered. This is particularly useful in a monorepo as it allows you to:
`rootMode` (default: ""): Pass in `upward` to walk up from the transforming file's directory and use the first `svelte.config.js` or `svelte.config.cjs` found, or throw an error if no config file is discovered. This is particularly useful in a monorepo as it allows you to:
- Run tests from the worktree root using Jest projects where you only want to put `svelte.config.js` in workspace folders, and not in the root.
- Run tests from the worktree root using Jest projects, but have different `svelte.config.js` configurations for individual workspaces.
- Have one common `svelte.config.js` in your worktree root (or any directory above the file being transformed) without needing individual `svelte.config.js` files in each workspace. _Note - this root config file can be overriden if necessary by putting another config file into a workspace folder_
The default mode is to load `svelte.config.js` from the current project root to avoid the risk of accidentally loading a `svelte.config.js` from outside the current project folder.
The default mode is to load `svelte.config.js` or `svelte.config.cjs` from the current project root to avoid the risk of accidentally loading a config file from outside the current project folder.
When `upward` is set it will stop at the first config file it finds above the file being transformed, but will walk up the directory structure all the way to the filesystem root if it cannot find any config file. This means that if there is no `svelte.config.js` file in the project above the file being transformed, it is always possible that someone will have a forgotten `svelte.config.js` in their home directory which could cause unexpected errors in your builds.
When `upward` is set it will stop at the first config file it finds above the file being transformed, but will walk up the directory structure all the way to the filesystem root if it cannot find any config file. This means that if there is no `svelte.config.js` or `svelte.config.cjs` file in the project above the file being transformed, it is always possible that someone will have a forgotten config file in their home directory which could cause unexpected errors in your builds.
`maxBuffer` (default: 10485760): Sets limit for buffer when `preprocess` is true. It defines the largest amount of data in bytes allowed on stdout or stderr for [child_process.spawnSync](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options). If exceeded, the child process is terminated and any output is truncated. The default value of 10Mb overrides Node's default value of 1Mb.

Expand Down
32 changes: 14 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"svelte": ">= 3"
},
"dependencies": {
"cosmiconfig": "^6.0.0"
"cosmiconfig": "^7.0.0"
},
"devDependencies": {
"@types/jest": "^25.1.2",
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/fixtures/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { replace } = require("svelte-preprocess");

module.exports = {
preprocess: [
replace([[/Hello/gi, "Bye"]]),
],
};
7 changes: 7 additions & 0 deletions src/__tests__/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe('transformer', () => {
runTransformer('SassComp', { preprocess: preprocessPath })
})

it('should search for "svelte.config.cjs" as well as "svelte.config.js"', () => {
const results = runTransformer('BasicComp', { preprocess: true, rootMode: "upward" })
// this is a little brittle, but it demonstrates that the replacements in
// "svelte.config.cjs" are working
expect(results).toContain('text("Bye ");')
})

// TODO: it works but it's really slow, it might have to do with the preprocessor.
// it('should transform when using typescript preprocessor', () => {
// runTransformer('TypescriptComp', { preprocess: true })
Expand Down
37 changes: 24 additions & 13 deletions src/svelteconfig.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
const fs = require('fs')
const path = require('path')

const configFilename = 'svelte.config.js'
const configFilenames = ['svelte.config.js', 'svelte.config.cjs']

exports.getSvelteConfig = (rootMode, filename, preprocess) => {
let configFile = ''
let configFile = null

if ('boolean' === typeof preprocess) {
const configDir = rootMode === 'upward'
? getConfigDir(path.dirname(filename))
: process.cwd()
configFile = path.resolve(configDir, configFilename)
configFile = rootMode === 'upward'
? findConfigFile(path.dirname(filename))
: getConfigFile(process.cwd())
} else if('string' === typeof preprocess) {
configFile = preprocess
}

if (!fs.existsSync(configFile)) {
throw Error(`Could not find ${configFilename}`)
if (configFile === null || !fs.existsSync(configFile)) {
throw Error(`Could not find ${configFilenames.join(' or ')}`)
}

return configFile
}

const getConfigDir = (searchDir) => {
if (fs.existsSync(path.join(searchDir, configFilename))) {
return searchDir
const getConfigFile = (searchDir) => {
for (const configFilename of configFilenames) {
const filePath = path.resolve(searchDir, configFilename)
if (fs.existsSync(filePath)) {
return filePath
}
}

return null
}

const findConfigFile = (searchDir) => {
const filePath = getConfigFile(searchDir)
if (filePath !== null) {
return filePath
}

const parentDir = path.resolve(searchDir, '..')
return parentDir !== searchDir
? getConfigDir(parentDir)
: searchDir // Stop walking at filesystem root
? findConfigFile(parentDir)
: null // Stop walking at filesystem root
}

0 comments on commit 9e259c4

Please sign in to comment.