Skip to content

Commit

Permalink
feat: add esm build
Browse files Browse the repository at this point in the history
closes #319

BREAKING CHANGE: Requires node >18
  • Loading branch information
KaelWD committed Dec 12, 2023
1 parent c43dc80 commit 34a03c1
Show file tree
Hide file tree
Showing 14 changed files with 3,138 additions and 1,959 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ module.exports = {
'plugin:import/typescript',
],
rules: {
'import/no-extraneous-dependencies': ['error', { devDependencies: false }]
'import/no-extraneous-dependencies': ['error', { devDependencies: false }],
}
}
5 changes: 5 additions & 0 deletions dev/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'import/no-extraneous-dependencies': 'off',
},
}
22 changes: 11 additions & 11 deletions dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
"debug:webpack-build": "NODE_ENV=production node --inspect-brk ../node_modules/.bin/webpack --config webpack.config.js"
},
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/preset-env": "^7.16.4",
"babel-loader": "^8.2.3",
"css-loader": "^6.5.1",
"@babel/core": "^7.23.5",
"@babel/preset-env": "^7.23.5",
"babel-loader": "^9.1.3",
"css-loader": "^6.8.1",
"pug": "^3.0.2",
"pug-plain-loader": "^1.1.0",
"rollup-plugin-visualizer": "^5.8.3",
"sass-loader": "^12.6.0",
"rollup-plugin-visualizer": "^5.10.0",
"sass-loader": "^13.3.2",
"url-loader": "^4.1.1",
"vite-plugin-inspect": "^0.4.3",
"vite-plugin-vuetify": "^1.0.0-alpha.1",
"vite-plugin-inspect": "^0.8.1",
"vite-plugin-vuetify": "^1.0.2",
"vue-style-loader": "^4.1.3",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.6.0",
"webpack-plugin-vuetify": "^2.0.0-alpha.1"
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1",
"webpack-plugin-vuetify": "^2.0.1"
}
}
File renamed without changes.
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@
"packages/*"
],
"scripts": {
"lint": "eslint packages/*/src/**/*",
"dev": "lerna run dev --parallel -- --preserveWatchOutput",
"lint": "eslint packages/*/src/**/* && tsc --noEmit",
"dev": "lerna run dev",
"dev:vite": "lerna run dev:vite --stream --no-prefix",
"dev:webpack": "lerna run dev:webpack --stream --no-prefix",
"build": "lerna run build",
"publish": "lerna run build && lerna version --no-private && lerna publish from-git"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/find-cache-dir": "^3.2.1",
"@types/loader-utils": "^2.0.3",
"@types/node": "^16.11.12",
"@types/loader-utils": "^2.0.6",
"@types/mkdirp": "^1.0.2",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"@vitejs/plugin-vue": "^4.0.0",
"@vue/compiler-sfc": "^3.2.45",
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"@vitejs/plugin-vue": "^4.5.2",
"@vue/compiler-sfc": "^3.3.11",
"conventional-changelog-conventionalcommits": "^4.6.1",
"eslint": "^8.18.0",
"eslint-plugin-import": "^2.26.0",
"lerna": "^3.22.1",
"sass": "^1.52.3",
"typescript": "^4.4.2",
"vite": "^4.0.0",
"vue": "^3.2.45",
"picocolors": "^1.0.0",
"sass": "^1.69.5",
"typescript": "^5.3.3",
"unbuild": "^2.0.0",
"vite": "^5.0.6",
"vue": "^3.3.11",
"vue-loader": "^16.5.0",
"vuetify": "^3.0.4",
"webpack": "^5.65.0"
"vuetify": "^3.4.6",
"webpack": "^5.89.0"
}
}
23 changes: 13 additions & 10 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
{
"name": "@vuetify/loader-shared",
"version": "1.7.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuetifyjs/vuetify-loader.git"
},
"scripts": {
"build": "tsc --outdir dist",
"dev": "yarn run build --watch"
"build": "unbuild",
"dev": "unbuild --stub"
},
"author": "Kael Watts-Deuchar",
"license": "MIT",
Expand All @@ -18,20 +25,16 @@
},
"homepage": "https://github.com/vuetifyjs/vuetify-loader/tree/master/packages/shared",
"dependencies": {
"find-cache-dir": "^3.3.2",
"find-cache-dir": "^5.0.0",
"upath": "^2.0.1"
},
"peerDependencies": {
"vue": "^3.0.0",
"vuetify": "^3.0.0-beta.4"
"vuetify": "^3.0.0"
},
"files": [
"dist/"
],
"exports": {
".": "./dist/index.js",
"./*": "./*"
},
"publishConfig": {
"access": "public"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/src/styles/writeStyles.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import path from 'upath'
import { writeFile } from 'fs/promises'
import * as findCacheDir from 'find-cache-dir'
import findCacheDir from 'find-cache-dir'
import { normalizePath } from '../index'

export const cacheDir = findCacheDir({
name: 'vuetify',
create: true,
thunk: true
})!

export function writeStyles (files: Set<string>) {
return writeFile(
cacheDir('styles.scss'),
path.join(cacheDir, 'styles.scss'),
[
'vuetify/lib/styles/main.sass',
'vuetify/dist/_component-variables.sass',
Expand Down
21 changes: 14 additions & 7 deletions packages/vite-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
"name": "vite-plugin-vuetify",
"version": "1.0.2",
"description": "A Vite plugin for treeshaking Vuetify components and more",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuetifyjs/vuetify-loader.git"
},
"scripts": {
"build": "tsc --outdir dist",
"dev": "yarn run build --watch"
"build": "unbuild && node ../../scripts/patchCJS.mjs",
"dev": "unbuild --stub"
},
"author": "Kael Watts-Deuchar",
"license": "MIT",
Expand All @@ -24,12 +31,12 @@
"upath": "^2.0.1"
},
"peerDependencies": {
"vite": "^2.7.0 || ^3.0.0 || ^4.0.0",
"vite": ">=5",
"vue": "^3.0.0",
"vuetify": "^3.0.0-beta.4"
"vuetify": "^3.0.0"
},
"engines": {
"node": ">=12"
"node": "^18.0.0 || >=20.0.0"
},
"files": [
"dist/"
Expand Down
9 changes: 3 additions & 6 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Options, isObject, includes, transformAssetUrls } from '@vuetify/loader
import { importPlugin } from './importPlugin'
import { stylesPlugin } from './stylesPlugin'

function vuetify (_options: Options = {}): Plugin[] {
export default function vuetify (_options: Options = {}): Plugin[] {
const options: Options = {
autoImport: true,
styles: true,
Expand All @@ -21,9 +21,6 @@ function vuetify (_options: Options = {}): Plugin[] {

return plugins
}
vuetify.transformAssetUrls = transformAssetUrls

module.exports = vuetify
export default vuetify

module.exports.transformAssetUrls = transformAssetUrls
export { transformAssetUrls }
export { transformAssetUrls } from '@vuetify/loader-shared'
20 changes: 13 additions & 7 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
"name": "webpack-plugin-vuetify",
"version": "2.0.1",
"description": "A Webpack plugin for treeshaking Vuetify components and more",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuetifyjs/vuetify-loader.git"
},
"scripts": {
"build": "tsc --outdir dist",
"dev": "yarn run build --watch"
"build": "unbuild",
"dev": "unbuild --stub"
},
"author": "Kael Watts-Deuchar",
"license": "MIT",
Expand All @@ -22,7 +29,6 @@
"@vuetify/loader-shared": "^1.7.1",
"decache": "^4.6.0",
"file-loader": "^6.2.0",
"find-cache-dir": "^3.3.2",
"loader-utils": "^2.0.0",
"mkdirp": "^1.0.4",
"null-loader": "^4.0.1",
Expand All @@ -31,7 +37,7 @@
"peerDependencies": {
"@vue/compiler-sfc": "^3.2.6",
"vue": "^3.2.6",
"vuetify": "^3.0.0-beta.4",
"vuetify": "^3.0.0",
"webpack": "^5.0.0"
},
"optionalPeerDependencies": {
Expand All @@ -40,7 +46,7 @@
"sharp": "^0.21.0"
},
"engines": {
"node": ">=12"
"node": "^18.0.0 || >=20.0.0"
},
"files": [
"dist/"
Expand Down
10 changes: 6 additions & 4 deletions packages/webpack-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { URLSearchParams } from 'url'
import { writeFile } from 'fs/promises'

import * as path from 'upath'
import * as mkdirp from 'mkdirp'
import path from 'upath'
import mkdirp from 'mkdirp'

import {
resolveVuetifyBase,
Expand All @@ -21,6 +21,8 @@ function isSubdir (root: string, test: string) {
}

export class VuetifyPlugin {
static transformAssetUrls = transformAssetUrls

options: Required<Options>

constructor (options: Options) {
Expand All @@ -47,7 +49,7 @@ export class VuetifyPlugin {
}

const vueLoader = compiler.options.module.rules.find(rule => {
return typeof rule !== 'string' && rule.loader && path.toUnix(rule.loader).endsWith('vue-loader/dist/templateLoader.js')
return rule && typeof rule !== 'string' && rule.loader && path.toUnix(rule.loader).endsWith('vue-loader/dist/templateLoader.js')
})
const vueOptions = typeof vueLoader === 'object' && vueLoader?.options
if (vueOptions && typeof vueOptions === 'object') {
Expand Down Expand Up @@ -95,7 +97,7 @@ export class VuetifyPlugin {
hookResolve(async request => {
const target = request.replace(/\.css$/, '.sass')
const file = path.relative(vuetifyBase, target)
const cacheFile = cacheDir(file)
const cacheFile = path.join(cacheDir, file)

await mkdirp(path.dirname(cacheFile))
await writeFile(cacheFile, `@use "${normalizePath(configFile)}"\n@use "${normalizePath(target)}"`)
Expand Down
57 changes: 57 additions & 0 deletions scripts/patchCJS.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
It converts
```ts
exports.default = vuePlugin;
exports.parseVueRequest = parseVueRequest;
```
to
```ts
module.exports = vuePlugin;
module.exports.default = vuePlugin;
module.exports.parseVueRequest = parseVueRequest;
```
*/

import { readFileSync, writeFileSync } from 'node:fs'
import colors from 'picocolors'

const indexPath = 'dist/index.cjs'
let code = readFileSync(indexPath, 'utf-8')

const matchMixed = code.match(/\nexports.default = (\w+);/)
if (matchMixed) {
const name = matchMixed[1]

const lines = code.trimEnd().split('\n')

// search from the end to prepend `modules.` to `export[xxx]`
for (let i = lines.length - 1; i > 0; i--) {
if (lines[i].startsWith('exports')) lines[i] = 'module.' + lines[i]
else {
// at the beginning of exports, export the default function
lines[i] += `\nmodule.exports = ${name};`
break
}
}

writeFileSync(indexPath, lines.join('\n'))

console.log(colors.bold(`${indexPath} CJS patched`))
process.exit(0)
}

const matchDefault = code.match(/\nmodule.exports = (\w+);/)

if (matchDefault) {
code += `module.exports.default = ${matchDefault[1]};\n`
writeFileSync(indexPath, code)
console.log(colors.bold(`${indexPath} CJS patched`))
process.exit(0)
}

console.error(colors.red(`${indexPath} CJS patch failed`))
process.exit(1)
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"target": "es2019",
"module": "CommonJS",
"moduleResolution": "Node",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"esModuleInterop": false,
"isolatedModules": true,
"resolveJsonModule": true,
Expand Down
Loading

0 comments on commit 34a03c1

Please sign in to comment.