Skip to content

Commit

Permalink
Work around this issue: rollup-plugin-terser#88
Browse files Browse the repository at this point in the history
  • Loading branch information
undecaf committed Aug 29, 2020
1 parent 22ee005 commit bfa2824
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 182 deletions.
66 changes: 0 additions & 66 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-material-vuelidate",
"version": "0.1.10",
"version": "0.1.11",
"description": "A Vue Material adapter for Vuelidate",
"keywords": [
"vue",
Expand All @@ -25,6 +25,7 @@
"scripts": {
"prebuild": "rimraf dist/components.*",
"build": "NODE_ENV=production rollup --config rollup.config.js",
"postbuild": "node terser.js dist/components.*.js",
"serve": "vue-cli-service serve",
"lint": "eslint '{src,tests}/**/*.{js,vue}'",
"lint:fix": "npm run lint -- --fix",
Expand Down Expand Up @@ -59,7 +60,6 @@
"rollup": "^2.6.1",
"rollup-plugin-css-only": "^2.1.0",
"rollup-plugin-license": "^2.2.0",
"rollup-plugin-terser": "^7.0.0",
"rollup-plugin-vue": "^5.0.0",
"typeface-roboto": "0.0.75",
"vue": "^2.6.11",
Expand Down
174 changes: 65 additions & 109 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import vue from 'rollup-plugin-vue'
import buble from '@rollup/plugin-buble'
import commonjs from '@rollup/plugin-commonjs'
import replace from '@rollup/plugin-replace'
import { terser } from 'rollup-plugin-terser'
import resolve from '@rollup/plugin-node-resolve'
import css from 'rollup-plugin-css-only'
import license from 'rollup-plugin-license'
import minimist from 'minimist'
import moment from 'moment'
import pkg from './package.json'

Expand All @@ -20,146 +18,104 @@ function unscoped(name) {
}

const
argv = minimist(process.argv.slice(2)),
name = kebabToPascal(unscoped(pkg.name)),
src = `src/${unscoped(pkg.name)}`


const baseConfig = {
output: {
name,
exports: 'named',
const prologue = [
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
]

const epilogue = [
buble({
objectAssign: 'Object.assign',
transforms: {
dangerousForOf: true,
},
}),
resolve(),
commonjs(),
license({
sourcemap: true,
},
plugins: {
preVue: [
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
commonjs(),
],
vue: {
css: true,
template: {
isProduction: true,
},
banner: {
content: `${pkg.name} v${pkg.version}
${pkg.description}
Built ${moment().format('YYYY-MM-DD HH:mm:ss')}
(c) 2020-present Ferdinand Kasper
Released under the MIT license`,
commentStyle: 'ignored',
},
postVue: [
buble({
objectAssign: 'Object.assign',
transforms: {
dangerousForOf: true,
},
}),
resolve(),
license({
sourcemap: true,
banner: {
content: `${pkg.name} v${pkg.version}
${pkg.description}
Built ${moment().format('YYYY-MM-DD HH:mm:ss')}
(c) 2020-present Ferdinand Kasper
Released under the MIT license`,
commentStyle: 'ignored',
},
thirdParty: {
allow: 'MIT',
},
}),
],
},
}
thirdParty: {
allow: 'MIT',
},
}),
]

// Customize configs for individual targets
const buildFormats = []

if (!argv.format || argv.format === 'es') {
// ESM build to be used with webpack/rollup
const esConfig = {
...baseConfig,
export default [
{
// ESM build to be used with webpack/rollup
input: `${src}/index.js`,
output: {
...baseConfig.output,
exports: 'named',
sourcemap: true,
file: pkg.module,
format: 'esm',
},
plugins: [
...baseConfig.plugins.preVue,
css({
output: pkg.style,
}),
...prologue,
vue({
...baseConfig.plugins.vue,
css: false,
}),
...baseConfig.plugins.postVue,
terser({
output: {
ecma: 6,
},
css({
output: pkg.style,
}),
...epilogue,
],
}
},

buildFormats.push(esConfig)
}
{
// Browser build
input: `${src}/wrapper.js`,
output: {
exports: 'named',
sourcemap: true,
name,
file: pkg.unpkg,
format: 'iife',
compact: true,
},
plugins: [
...prologue,
vue(),
...epilogue,
],
},

if (!argv.format || argv.format === 'cjs') {
// SSR build
const umdConfig = {
...baseConfig,
{
// SSR build
input: `${src}/index.js`,
output: {
...baseConfig.output,
compact: true,
exports: 'named',
sourcemap: true,
file: pkg.main,
format: 'cjs',
compact: true,
},
plugins: [
...baseConfig.plugins.preVue,
...prologue,
css({
output: pkg.style,
}),
vue({
...baseConfig.plugins.vue,
css: false,
template: {
...baseConfig.plugins.vue.template,
optimizeSSR: true,
},
css: false,
}),
...baseConfig.plugins.postVue,
],
}

buildFormats.push(umdConfig)
}

if (!argv.format || argv.format === 'iife') {
// Browser build
const unpkgConfig = {
...baseConfig,
input: `${src}/wrapper.js`,
output: {
...baseConfig.output,
compact: true,
file: pkg.unpkg,
format: 'iife',
},
plugins: [
...baseConfig.plugins.preVue,
vue(baseConfig.plugins.vue),
...baseConfig.plugins.postVue,
terser({
output: {
ecma: 5,
},
}),
...epilogue,
],
}

buildFormats.push(unpkgConfig)
}

// Export config
export default buildFormats
},
]
73 changes: 73 additions & 0 deletions terser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const fs = require('fs')
const path = require('path')
const { minify } = require('terser')

const encoding = "utf-8"


function readFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, encoding, (err, content) => {
if (err) {
reject(err)
} else {
resolve(content)
}
})
})
}


function writeFile(file, content) {
return new Promise((resolve, reject) => {
fs.writeFile(file, content, encoding, err => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}


function processFile(jsFile) {
const mapFile = `${jsFile}.map`

Promise.all([readFile(jsFile), readFile(mapFile)])
.then(contents => {
const basename = path.basename(jsFile)

const options = {
compress: true,
mangle: true,
module: Boolean(basename.match(/\.esm?\.js/)),
sourceMap: {
content: contents[1],
filename: basename,
url: path.basename(mapFile),
},
}

options.toplevel = options.module && (options.compress || options.mangle)

return minify(contents[0], options)
})

.then(result => {
return Promise.all([writeFile(jsFile, result.code), writeFile(mapFile, result.map)])
console.log(result)

})

.then(() => {
console.log(`Minified ${jsFile}, updated ${mapFile}`)
})

.catch(err => {
throw `Error: ${err}`
})
}


process.argv.slice(2).forEach(processFile)

0 comments on commit bfa2824

Please sign in to comment.