Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in fusionConfig: jsExtPattern, resolveExtensions (enable typescript) #411

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions fusion-cli/build/get-webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
const runtime = COMPILATIONS[id];
const env = dev ? 'development' : 'production';
const shouldMinify = !dev && minify;
const jsExtPattern = fusionConfig.jsExtPattern || JS_EXT_PATTERN;

// Both options default to true, but if `--zopfli=false`
// it should be respected for backwards compatibility
Expand Down Expand Up @@ -174,7 +175,7 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
const {experimentalBundleTest, experimentalTransformTest} = fusionConfig;
const babelTester = experimentalTransformTest
? modulePath => {
if (!JS_EXT_PATTERN.test(modulePath)) {
if (!jsExtPattern.test(modulePath)) {
return false;
}
const transform = experimentalTransformTest(
Expand All @@ -191,7 +192,7 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
);
}
}
: JS_EXT_PATTERN;
: jsExtPattern;

return {
name: runtime,
Expand Down Expand Up @@ -436,6 +437,7 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
__FUSION_ENTRY_PATH__: path.join(dir, main),
__ENV__: env,
},
extensions: fusionConfig.resolveExtensions,
},
resolveLoader: {
alias: {
Expand Down
42 changes: 25 additions & 17 deletions fusion-cli/build/jest/base-jest-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,27 @@ const {dirname} = require('path');

const rootDir = dirname(fs.realpathSync(`${process.cwd()}/package.json`));

function getFusionrc() {
try {
const path = require('path');
// $FlowFixMe
const fusionrc = require(path.resolve(process.cwd(), '.fusionrc.js'));
return fusionrc;
} catch (e) {
return {};
}
}

const {jest: jestConfig, jsExtPattern} = getFusionrc();
const testFileExt = jsExtPattern ? jsExtPattern.source : '\\.js$';
const globFileExt = (jestConfig && jestConfig.globFileExt) || '.js';

const TRANSFORM_IGNORE_PATTERNS = ['/node_modules/(?!(fusion-cli.*build))'];
const matchField = process.env.TEST_REGEX ? 'testRegex' : 'testMatch';
const matchValue = process.env.TEST_FOLDER
? [`**/${process.env.TEST_FOLDER || '__tests__'}/**/*.js`]
? [`**/${process.env.TEST_FOLDER || '__tests__'}/**/*${globFileExt}`]
: process.env.TEST_REGEX ||
(process.env.TEST_MATCH || '**/__tests__/**/*.js').split(',');
(process.env.TEST_MATCH || `**/__tests__/**/*${globFileExt}`).split(',');

function getReactVersion() {
// $FlowFixMe
Expand All @@ -30,6 +46,7 @@ function getReactVersion() {
.shift()
.match(/\d+/);
}

function getReactSetup() {
try {
return [require.resolve(`./jest-framework-setup-${getReactVersion()}.js`)];
Expand All @@ -40,36 +57,27 @@ function getReactSetup() {

const reactSetup = getReactSetup();

function getTransformIgnorePatterns() {
const defaults = ['/node_modules/(?!(fusion-cli.*build))'];
try {
const path = require('path');
// $FlowFixMe
const fusionrc = require(path.resolve(process.cwd(), '.fusionrc.js'));
return fusionrc.jest.transformIgnorePatterns;
} catch (e) {
return defaults;
}
}

const transformIgnorePatterns = getTransformIgnorePatterns();
const transformIgnorePatterns =
(jestConfig && jestConfig.transformIgnorePatterns) ||
TRANSFORM_IGNORE_PATTERNS;

module.exports = {
coverageDirectory: `${rootDir}/coverage`,
coverageReporters: ['json'],
rootDir,
transform: {
'\\.js$': require.resolve('./jest-transformer.js'),
[testFileExt]: require.resolve('./jest-transformer.js'),
'\\.(gql|graphql)$': require.resolve('./graphql-jest-transformer.js'),
},
transformIgnorePatterns,
setupFiles: [require.resolve('./jest-framework-shims.js'), ...reactSetup],
setupFilesAfterEnv: jestConfig && jestConfig.setupFilesAfterEnv,
snapshotSerializers:
reactSetup.length > 0 ? [require.resolve('enzyme-to-json/serializer')] : [],
[matchField]: matchValue,
testURL: 'http://localhost:3000/',
collectCoverageFrom: [
'src/**/*.js',
`src/**/*${globFileExt}`,
'!**/__generated__/**',
'!**/__integration__/**',
'!**/__tests__/**',
Expand Down
6 changes: 5 additions & 1 deletion fusion-cli/build/jest/jsdom/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
/* eslint-env node */

const baseJestConfig = require('../base-jest-config.js');
const loadFusionRC = require('../../load-fusionrc.js');

const {jsExtPattern} = loadFusionRC(process.cwd());
const testFileExt = jsExtPattern ? jsExtPattern.source : '\\.js$';

module.exports = {
...baseJestConfig,
Expand All @@ -17,5 +21,5 @@ module.exports = {
name: 'browser',
// Exposes global.jsdom so we can reconfigure the url on each simulator.render call
testEnvironment: 'jest-environment-jsdom-global',
testPathIgnorePatterns: ['.*\\.node\\.js'],
testPathIgnorePatterns: ['.*\\.node' + testFileExt],
};
6 changes: 5 additions & 1 deletion fusion-cli/build/jest/node/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
/* eslint-env node */

const baseJestConfig = require('../base-jest-config.js');
const loadFusionRC = require('../../load-fusionrc.js');

const {jsExtPattern} = loadFusionRC(process.cwd());
const testFileExt = jsExtPattern ? jsExtPattern.source : '\\.js$';

module.exports = {
...baseJestConfig,
displayName: 'node',
browser: false,
name: 'node',
testEnvironment: 'node',
testPathIgnorePatterns: ['.*\\.browser\\.js'],
testPathIgnorePatterns: ['.*\\.browser' + testFileExt],
};
6 changes: 5 additions & 1 deletion fusion-cli/build/load-fusionrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export type FusionRC = {
experimentalTransformTest?: (modulePath: string, defaults: TransformResult) => TransformResult,
experimentalBundleTest?: (modulePath: string, defaults: BundleResult) => BundleResult,
nodeBuiltins?: {[string]: any},
jest?: {transformIgnorePatterns?: Array<string>},
jest?: {globFileExt? string, transformIgnorePatterns?: Array<string>, setupFilesAfterEnv?: Array<string>},
jsExtPattern?: RegExp,
resolveExtensions?: Array<string>,
zopfli?: boolean,
gzip?: boolean,
brotli?:boolean,
Expand Down Expand Up @@ -65,6 +67,8 @@ function isValid(config, silent) {
'experimentalBundleTest',
'nodeBuiltins',
'jest',
'jsExtPattern',
'resolveExtensions',
'brotli',
'zopfli', // TODO: Remove redundant zopfli option
'gzip',
Expand Down
4 changes: 3 additions & 1 deletion fusion-cli/build/loaders/babel-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ async function runTransformation(
overrides: [],
};

const {jsExtPattern = JS_EXT_PATTERN} = loadFusionRC(rootContext);

if (loaderOptions.overrides != undefined) {
for (let i = 0; i < loaderOptions.overrides.length; i++) {
let override = getBabelConfigFromCache(
Expand All @@ -57,7 +59,7 @@ async function runTransformation(
);
//$FlowFixMe
override.test = modulePath => {
if (!JS_EXT_PATTERN.test(modulePath)) {
if (!jsExtPattern.test(modulePath)) {
return false;
}

Expand Down
54 changes: 54 additions & 0 deletions fusion-cli/docs/fusionrc.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ module.exports = {

**Please note that custom Babel config is an unstable API and may not be supported in future releases.**

## `jsExtPattern`

By default this is `/\.(mjs|js|jsx)$/`

For example, this enables Typescript support by adding the appropriate Babel preset:

```js
module.exports = {
jsExtPattern: /[jt]sx?$/,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a dot before [jt] please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what should be a flow now, as this PR was forked (imported) to the internal github repository... is updating this PR still something valuable ? @AlexMSmithCA

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to make these changes in this PR. We can re-import any additional commits once this has been fully reviewed.

babel: {
presets: ["@babel/preset-typescript"],
}
};
```

```js
module.exports = {
resolveExtensions: ['.wasm', '.mjs', '.js', '.json', '.ts', '.tsx'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the following to enable Typescript-compatible tests.

jest: {
  globFileExt: '!(.d).(ts|tsx)'
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what should be a flow now, as this PR was forked (imported) to the internal github repository... is updating this PR still something valuable ? @AlexMSmithCA

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See above)

};
```

## `resolveExtensions`

By default this is the Webpack default which is: `['.wasm', '.mjs', '.js', '.json']`

## `assumeNoImportSideEffects`

By default this is `false`.
Expand Down Expand Up @@ -49,6 +74,35 @@ module.exports = {
};
```

## `jest`

```
globFileExt?: string
transformIgnorePatterns?: Array<string>
setupFilesAfterEnv?: Array<string>
```

#### globFileExt
default: `.js`

This glob file extension pattern is used by jest to find test files and
aggregate coverage raport.

#### transformIgnorePatterns
default: `['/node_modules/(?!(fusion-cli.*build))']`

An array of regexp pattern strings that are matched against all source file
paths before transformation. If the test path matches any of the patterns, it
will not be transformed.

#### setupFilesAfterEnv
default: `undefined`

A list of paths to modules that run some code to configure or set up the
testing framework before each test. This script file presents you the
opportunity of running some code immediately after the test framework has been
installed in the environment.

## `gzip`

This is an optional property that can be used to override the Fusion.js defaults for compressing projects using zlib in production builds.
Expand Down