Skip to content

Commit a1d9a56

Browse files
authored
feat: use preset-env for appropriate client/server/modern targets (#5)
1 parent a0763ed commit a1d9a56

File tree

3 files changed

+51
-78
lines changed

3 files changed

+51
-78
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
"devDependencies": {
2525
"@nuxt/types": "^2.15.8",
2626
"@nuxtjs/eslint-config-typescript": "^6.0.0",
27+
"browserslist": "^4.0.0",
2728
"eslint": "^7.22.0",
2829
"nuxt": "^2.15.3",
2930
"siroc": "^0.10.1",
3031
"standard-version": "^9.1.1"
32+
},
33+
"engines": {
34+
"node": ">=12"
3135
}
3236
}

src/index.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
11
import defu from 'defu'
22
import { name, version } from '../package.json'
3-
import type { Options } from '@swc/core'
3+
import type { ModuleThis } from '@nuxt/types/config/module'
44
import type { NuxtOptionsBuild } from '@nuxt/types/config/build'
5+
import type { Options } from '@swc/core'
56

6-
function swcModule () {
7-
const { nuxt } = this
8-
9-
const swcOptions: Options = defu(nuxt.options.build.swc, {
10-
// sync: true,
11-
sourceMaps: false,
12-
jsc: {
13-
parser: {
14-
dynamicImport: true
15-
}
16-
}
17-
} as Options)
18-
19-
const swcTSOptions = defu(swcOptions, {
20-
jsc: {
21-
parser: {
22-
syntax: 'typescript'
23-
}
24-
}
25-
} as Options)
7+
const defaultTargets = {
8+
server: { node: 12 },
9+
client: { ie: 9 },
10+
modern: {
11+
chrome: 60,
12+
firefox: 54
13+
}
14+
}
2615

16+
function swcModule (this: ModuleThis) {
17+
const { nuxt } = this
2718
nuxt.options.extensions.push('ts')
2819
nuxt.options.build.additionalExtensions = ['ts', 'tsx']
2920

21+
// Auto detect corejs version:
22+
// https://github.com/nuxt/nuxt.js/tree/dev/packages/webpack/src/config/base.js#L124-L132
23+
let coreJs: string
24+
if (nuxt.options.build.corejs === 'auto') {
25+
try {
26+
coreJs = require('core-js/package.json').version.split('.')[0]
27+
} catch (_err) {
28+
coreJs = '2'
29+
}
30+
} else {
31+
coreJs = String(nuxt.options.build.corejs)
32+
}
33+
3034
nuxt.hook('webpack:config', (configs) => {
3135
for (const config of configs) {
36+
const swcOptions: Options = defu(nuxt.options.build.swc, {
37+
// sync: true,
38+
sourceMaps: false,
39+
minify: nuxt.options.build.optimization?.minimize ?? (!nuxt.options.dev && config.name !== 'server'),
40+
env: {
41+
coreJs,
42+
targets: defaultTargets[config.name]
43+
},
44+
jsc: {
45+
parser: {
46+
dynamicImport: true
47+
}
48+
}
49+
} as Options)
50+
51+
const swcTSOptions = defu(swcOptions, {
52+
jsc: {
53+
parser: {
54+
syntax: 'typescript'
55+
}
56+
}
57+
} as Options)
3258
config.resolve!.extensions!.push('.ts', '.tsx')
3359
config.module.rules = [
3460
...config.module.rules.filter(r => '.vue'.match(r.test)),

yarn.lock

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,26 +1147,6 @@
11471147
node-html-parser "^3.2.0"
11481148
ufo "^0.7.4"
11491149

1150-
"@nuxt/kit@^0.6.4":
1151-
version "0.6.4"
1152-
resolved "https://registry.yarnpkg.com/@nuxt/kit/-/kit-0.6.4.tgz#76a81e135bfeb91c0b5eb58435c8e5640046c912"
1153-
integrity sha512-NsV8TEVN+oTOgJvrsaIuyMaq/mFW+O7Rnz+sGUMlJlUskBC0KyqkrLkclNFyXsrnf4MEudAV+1WwtOyFVODltw==
1154-
dependencies:
1155-
consola "^2.15.3"
1156-
create-require "^1.1.1"
1157-
defu "^5.0.0"
1158-
dotenv "^10.0.0"
1159-
globby "^11.0.4"
1160-
hash-sum "^2.0.0"
1161-
jiti "^1.10.1"
1162-
rc9 "^1.2.0"
1163-
scule "^0.2.1"
1164-
std-env "^2.3.0"
1165-
ufo "^0.7.5"
1166-
unctx "^0.0.3"
1167-
untyped "^0.2.5"
1168-
upath "^2.0.1"
1169-
11701150
"@nuxt/loading-screen@^2.0.3":
11711151
version "2.0.4"
11721152
resolved "https://registry.yarnpkg.com/@nuxt/loading-screen/-/loading-screen-2.0.4.tgz#756abd861f77c57001be4d21d47534723afb4f3a"
@@ -4105,11 +4085,6 @@ dot-prop@^5.1.0, dot-prop@^5.2.0:
41054085
dependencies:
41064086
is-obj "^2.0.0"
41074087

4108-
dotenv@^10.0.0:
4109-
version "10.0.0"
4110-
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"
4111-
integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==
4112-
41134088
dotenv@^9.0.2:
41144089
version "9.0.2"
41154090
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-9.0.2.tgz#dacc20160935a37dea6364aa1bef819fb9b6ab05"
@@ -5139,18 +5114,6 @@ globby@^11.0.1, globby@^11.0.3:
51395114
merge2 "^1.3.0"
51405115
slash "^3.0.0"
51415116

5142-
globby@^11.0.4:
5143-
version "11.0.4"
5144-
resolved "https://registry.yarnpkg.com/globby/-/globby-11.0.4.tgz#2cbaff77c2f2a62e71e9b2813a67b97a3a3001a5"
5145-
integrity sha512-9O4MVG9ioZJ08ffbcyVYyLOJLk5JQ688pJ4eMGLpdWLHq/Wr1D9BlriLQyL0E+jbkuePVZXYFj47QM/v093wHg==
5146-
dependencies:
5147-
array-union "^2.1.0"
5148-
dir-glob "^3.0.1"
5149-
fast-glob "^3.1.1"
5150-
ignore "^5.1.4"
5151-
merge2 "^1.3.0"
5152-
slash "^3.0.0"
5153-
51545117
graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.4:
51555118
version "4.2.6"
51565119
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
@@ -5897,11 +5860,6 @@ jest-worker@^26.5.0:
58975860
merge-stream "^2.0.0"
58985861
supports-color "^7.0.0"
58995862

5900-
jiti@^1.10.1:
5901-
version "1.11.0"
5902-
resolved "https://registry.yarnpkg.com/jiti/-/jiti-1.11.0.tgz#64120a30d97b9bf37b8b032cf4564dfadc28984c"
5903-
integrity sha512-/2c7e61hxxTIN34UeHBB0LCJ5Tq64kgJDV7GR+++e8XRxCKRIKmB8tH6ww1W+Z6Kgd6By+C3RSCu1lXjbPT68A==
5904-
59055863
jiti@^1.9.1, jiti@^1.9.2:
59065864
version "1.9.2"
59075865
resolved "https://registry.yarnpkg.com/jiti/-/jiti-1.9.2.tgz#2ee44830883dbb1b2e222adc053c3052d0bf3b61"
@@ -9675,11 +9633,6 @@ ufo@^0.7.4:
96759633
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.5.tgz#5d5b2174747c0072edd30501994c8ac4ad2e77ea"
96769634
integrity sha512-FGG+EgguC1oz5dTE1JptPWCyj6Z9mYpwvZY8PTu9Vh/Aoy+Mj9cpeQ3gg4kyEMDbMrH+lTYiw7bomG58B8X7Kg==
96779635

9678-
ufo@^0.7.5:
9679-
version "0.7.9"
9680-
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.7.9.tgz#0268e3734b413c9ed6f3510201f42372821b875c"
9681-
integrity sha512-6t9LrLk3FhqTS+GW3IqlITtfRB5JAVr5MMNjpBECfK827W+Vh5Ilw/LhTcHWrt6b3hkeBvcbjx4Ti7QVFzmcww==
9682-
96839636
uglify-js@^3.1.4, uglify-js@^3.5.1:
96849637
version "3.13.7"
96859638
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.13.7.tgz#25468a3b39b1c875df03f0937b2b7036a93f3fee"
@@ -9695,11 +9648,6 @@ unbox-primitive@^1.0.0:
96959648
has-symbols "^1.0.2"
96969649
which-boxed-primitive "^1.0.2"
96979650

9698-
unctx@^0.0.3:
9699-
version "0.0.3"
9700-
resolved "https://registry.yarnpkg.com/unctx/-/unctx-0.0.3.tgz#e06f67d5bebe2babe5a57c1dc13d77255e3458c0"
9701-
integrity sha512-x+NCoXiYn93laQNnoJGZx2UZj7vv8ViFKadUCDx9S4QoPIkGRCYT0OLUDEMlg/B+Q6bnqdSkPLmiy/kjNIwVyQ==
9702-
97039651
unfetch@^4.2.0:
97049652
version "4.2.0"
97059653
resolved "https://registry.yarnpkg.com/unfetch/-/unfetch-4.2.0.tgz#7e21b0ef7d363d8d9af0fb929a5555f6ef97a3be"
@@ -9790,11 +9738,6 @@ unset-value@^1.0.0:
97909738
has-value "^0.3.1"
97919739
isobject "^3.0.0"
97929740

9793-
untyped@^0.2.5:
9794-
version "0.2.8"
9795-
resolved "https://registry.yarnpkg.com/untyped/-/untyped-0.2.8.tgz#14a15ab7455004dc2da9972ccbadb237bbee5d7b"
9796-
integrity sha512-SCXbOqy1sT56l5y7A4LVkXTBBMdTNneAoVrqwYlpXdK85QmOPKrPUGlitejsDK0YUpWSf1KFZWAj8CH7KpuSyg==
9797-
97989741
upath@^1.1.1:
97999742
version "1.2.0"
98009743
resolved "https://registry.yarnpkg.com/upath/-/upath-1.2.0.tgz#8f66dbcd55a883acdae4408af8b035a5044c1894"

0 commit comments

Comments
 (0)