@financial-times/dotcom-build-js 7.3.1 → 7.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/babel.ts +65 -0
- package/src/index.ts +21 -0
- package/src/types.d.ts +6 -0
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@financial-times/dotcom-build-js",
|
3
|
-
"version": "7.3.
|
3
|
+
"version": "7.3.2",
|
4
4
|
"description": "",
|
5
5
|
"main": "dist/node/index.js",
|
6
6
|
"types": "src/index.ts",
|
@@ -40,7 +40,8 @@
|
|
40
40
|
"npm": "7.x || 8.x"
|
41
41
|
},
|
42
42
|
"files": [
|
43
|
-
"dist/"
|
43
|
+
"dist/",
|
44
|
+
"src/"
|
44
45
|
],
|
45
46
|
"repository": {
|
46
47
|
"type": "git",
|
package/src/babel.ts
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
import { PluginOptions } from './types'
|
2
|
+
|
3
|
+
function getBabelConfig(options: PluginOptions = {}) {
|
4
|
+
const presetEnvOpts = {
|
5
|
+
targets: ['last 1 Chrome versions', 'Safari >= 13', 'ff ESR', 'last 1 Edge versions'],
|
6
|
+
// Exclude transforms that make all code slower
|
7
|
+
// See https://github.com/facebook/create-react-app/pull/5278
|
8
|
+
exclude: ['transform-typeof-symbol']
|
9
|
+
}
|
10
|
+
|
11
|
+
const presetReactOptions = {
|
12
|
+
pragma: options.jsxPragma,
|
13
|
+
pragmaFrag: options.jsxPragmaFrag
|
14
|
+
}
|
15
|
+
|
16
|
+
const presetTypescriptOptions = {
|
17
|
+
jsxPragma: options.jsxPragma
|
18
|
+
}
|
19
|
+
|
20
|
+
const pluginTransformRuntimeOptions = {
|
21
|
+
// You might think we'd want to abstract the helper functions so they can be reused but doing so
|
22
|
+
// means we generate unstable hashes because the generated helper modules are at the bottom of
|
23
|
+
// the dependency tree but their contents depends on the features each app uses. Inlining them
|
24
|
+
// adds little (usually <1kb) to our total JS payload because the minimizer can usually reduce
|
25
|
+
// them down and actually result in fewer function calls overall!
|
26
|
+
// <https://github.com/Financial-Times/dotcom-page-kit/issues/576>
|
27
|
+
helpers: false
|
28
|
+
}
|
29
|
+
|
30
|
+
const config = {
|
31
|
+
// By default Babel assumes all source code is ESM so force it to check for CJS
|
32
|
+
sourceType: 'unambiguous',
|
33
|
+
babelrc: true,
|
34
|
+
cacheDirectory: true,
|
35
|
+
presets: [
|
36
|
+
[require.resolve('@babel/preset-env'), presetEnvOpts],
|
37
|
+
[require.resolve('@babel/preset-react'), presetReactOptions],
|
38
|
+
// This only enables the parsing of TypeScript, it does not check types
|
39
|
+
[require.resolve('@babel/preset-typescript'), presetTypescriptOptions]
|
40
|
+
],
|
41
|
+
plugins: [
|
42
|
+
// This is required by @babel/preset-typescript
|
43
|
+
// https://github.com/tc39/proposal-class-fields
|
44
|
+
[require.resolve('@babel/plugin-proposal-class-properties')],
|
45
|
+
// This enables Babel's built-in 'dynamicImport' flag which defines import() function usage
|
46
|
+
[require.resolve('@babel/plugin-syntax-dynamic-import')],
|
47
|
+
[require.resolve('@babel/plugin-transform-runtime'), pluginTransformRuntimeOptions]
|
48
|
+
]
|
49
|
+
}
|
50
|
+
|
51
|
+
return config
|
52
|
+
}
|
53
|
+
|
54
|
+
export default function getBabelRule(userOptions: PluginOptions) {
|
55
|
+
return {
|
56
|
+
test: [/\.(js|jsx|mjs|ts|tsx)$/],
|
57
|
+
// NOTE: Do not exclude bower_components or node_modules directories
|
58
|
+
// https://github.com/Financial-Times/dotcom-page-kit/issues/366
|
59
|
+
exclude: [],
|
60
|
+
use: {
|
61
|
+
loader: require.resolve('babel-loader'),
|
62
|
+
options: getBabelConfig(userOptions)
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
import getBabelRule from './babel'
|
2
|
+
import { PluginOptions } from './types'
|
3
|
+
import type webpack from 'webpack'
|
4
|
+
|
5
|
+
const defaultOptions: PluginOptions = {
|
6
|
+
jsxPragma: 'h',
|
7
|
+
jsxPragmaFrag: 'Fragment'
|
8
|
+
}
|
9
|
+
|
10
|
+
export class PageKitJsPlugin {
|
11
|
+
options: PluginOptions
|
12
|
+
|
13
|
+
constructor(userOptions: PluginOptions = {}) {
|
14
|
+
this.options = { ...defaultOptions, ...userOptions }
|
15
|
+
}
|
16
|
+
|
17
|
+
apply(compiler: webpack.Compiler) {
|
18
|
+
compiler.options.resolve.extensions = ['.js', '.jsx', '.mjs', '.json', '.ts', '.tsx']
|
19
|
+
compiler.options.module.rules.push(getBabelRule(this.options))
|
20
|
+
}
|
21
|
+
}
|