Skip to content

Commit

Permalink
feat: add esm build, separate css modules in /lib
Browse files Browse the repository at this point in the history
resolves #7927
  • Loading branch information
KaelWD committed Aug 25, 2021
1 parent 325593d commit 4d4e43c
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 167 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"jest-environment-jsdom-fourteen": "^1.0.1",
"jest-serializer-html": "^7.0.0",
"lerna": "^3.16.2",
"mkdirp": "^1.0.4",
"moment": "^2.27.0",
"sass": "~1.32.13",
"sass-loader": "^11.1.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/vuetify/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ module.exports = {
lib: {
plugins: [
['babel-plugin-add-import-extension', { extension: 'mjs' }],
['./build/babel-plugin-replace-import-extension', { extMapping: {
'.sass': '.css',
'.scss': '.css',
}}],
],
},
},
Expand Down
60 changes: 60 additions & 0 deletions packages/vuetify/build/babel-plugin-replace-import-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
function transformExtension(filepath, extMapping) {
if (!filepath.startsWith('./') && !filepath.startsWith('../')) {
// Package import
return filepath;
}

const idx = filepath.lastIndexOf('.');
if (idx === -1 || filepath.includes('/', idx)) {
// No extension
const newExt = extMapping[''];
if (newExt) {
return filepath + newExt;
}
return filepath;
}

for (const [origExt, newExt] of Object.entries(extMapping).sort(
(a, b) => b[0].length - a[0].length
)) {
if (filepath.endsWith(origExt)) {
return filepath.slice(0, -origExt.length) + newExt;
}
}
return filepath;
}

function getOption(state, key) {
const opts = state.opts || {};
return opts[key];
}


module.exports = function ({types: t}) {
return {
visitor: {
ImportDeclaration(path, state) {
const extMapping = getOption(state, 'extMapping');
if (!extMapping) {
return;
}
const source = path.node.source;

source.value = transformExtension(source.value, extMapping);
},
// For re-exporting
'ExportNamedDeclaration|ExportAllDeclaration'(path, state) {
const extMapping = getOption(state, 'extMapping');
if (!extMapping) {
return;
}
const source = path.node.source;
if (source == null) {
return;
}

source.value = transformExtension(source.value, extMapping);
},
},
};
}
5 changes: 0 additions & 5 deletions packages/vuetify/build/build-lib.js

This file was deleted.

65 changes: 0 additions & 65 deletions packages/vuetify/build/config.js

This file was deleted.

Empty file removed packages/vuetify/build/dev.js
Empty file.
122 changes: 89 additions & 33 deletions packages/vuetify/build/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,95 @@
import path from 'path'
import dts from 'rollup-plugin-dts'
import mkdirp from 'mkdirp'
import { writeFile } from 'fs/promises'

import { version } from '../package.json'

import alias from '@rollup/plugin-alias'
import sass from 'rollup-plugin-sass'
import { babel } from '@rollup/plugin-babel'
import { terser } from 'rollup-plugin-terser'
import { nodeResolve } from '@rollup/plugin-node-resolve'

import autoprefixer from 'autoprefixer'
import cssnano from 'cssnano'
import postcss from 'postcss'

const externalsPlugin = () => ({
resolveId (source, importer) {
if (importer && (source.endsWith('.sass') || source.endsWith('.scss'))) {
return {
id: source,
external: true,
moduleSideEffects: false,
}
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.es6', '.es', '.mjs']
const banner = `/*!
* Vuetify v${version}
* Forged by John Leider
* Released under the MIT License.
*/\n`

export default {
input: 'src/entry-bundler.ts',
output: [
{
file: 'dist/vuetify.esm.js',
format: 'es',
sourcemap: true,
banner,
},
{
file: 'dist/vuetify.js',
name: 'Vuetify',
format: 'umd',
globals: { vue: 'Vue' },
sourcemap: true,
banner,
},
{
file: 'dist/vuetify.min.js',
name: 'Vuetify',
format: 'umd',
globals: { vue: 'Vue' },
plugins: [terser({
output: { comments: /^!/ }
})],
sourcemap: true,
banner,
}
}
})
],
external: ['vue'],
plugins: [
nodeResolve({ extensions }),
babel({
extensions,
babelHelpers: 'inline'
}),
sass({
output(styles, styleNodes) {
// Complete CSS bundle
mkdirp(path.resolve(__dirname, '../dist')).then(() => {
return Promise.all([
postcss([autoprefixer]).process(styles, { from: 'src' }),
postcss([autoprefixer, cssnano({
preset: 'default',
postcssZindex: false,
reduceIdents: false
})]).process(styles, { from: 'src' })
])
}).then(result => {
writeFile(path.resolve(__dirname, '../dist/vuetify.css'), banner + result[0].css, 'utf8')
writeFile(path.resolve(__dirname, '../dist/vuetify.min.css'), banner + result[1].css, 'utf8')
})

function createConfig (input, output) {
return {
input: 'types-temp/' + input,
output: [{ file: output, format: 'es' }],
plugins: [
dts(),
externalsPlugin(),
alias({
entries: [
{ find: /^@\/(.*)/, replacement: path.resolve(__dirname, '../types-temp/$1') },
]
})
],
}
// Individual CSS files
for (const { id, content } of styleNodes) {
const out = path.parse(id.replace(
path.resolve(__dirname, '../src'),
path.resolve(__dirname, '../lib')
))
mkdirp(out.dir).then(() => {
writeFile(path.join(out.dir, out.name + '.css'), content, 'utf8')
})
}
},
}),
alias({
entries: [
{ find: /^@\/(.*)/, replacement: path.resolve(__dirname, '../src/$1') },
]
})
],
}

export default [
createConfig('entry.d.ts', 'lib/entry.d.ts'),
createConfig('framework.d.ts', 'lib/framework.d.ts'),
createConfig('entry-bundler.d.ts', 'dist/vuetify.d.ts'),
createConfig('components/index.d.ts', 'lib/components/index.d.ts'),
createConfig('directives/index.d.ts', 'lib/directives/index.d.ts'),
]
40 changes: 40 additions & 0 deletions packages/vuetify/build/rollup.types.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import path from 'path'

import dts from 'rollup-plugin-dts'
import alias from '@rollup/plugin-alias'

const externalsPlugin = () => ({
resolveId (source, importer) {
if (importer && (source.endsWith('.sass') || source.endsWith('.scss'))) {
return {
id: source,
external: true,
moduleSideEffects: false,
}
}
}
})

function createTypesConfig (input, output) {
return {
input: 'types-temp/' + input,
output: [{ file: output, format: 'es' }],
plugins: [
dts(),
externalsPlugin(),
alias({
entries: [
{ find: /^@\/(.*)/, replacement: path.resolve(__dirname, '../types-temp/$1') },
]
})
],
}
}

export default [
createTypesConfig('entry.d.ts', 'lib/entry.d.ts'),
createTypesConfig('framework.d.ts', 'lib/framework.d.ts'),
createTypesConfig('entry-bundler.d.ts', 'dist/vuetify.d.ts'),
createTypesConfig('components/index.d.ts', 'lib/components/index.d.ts'),
createTypesConfig('directives/index.d.ts', 'lib/directives/index.d.ts'),
]
39 changes: 0 additions & 39 deletions packages/vuetify/build/webpack.prod.config.js

This file was deleted.

0 comments on commit 4d4e43c

Please sign in to comment.